test-lib-functions: use test-tool for [de]packetize()
The shell+perl "[de]packetize()" helper functions were added in4414a15002
(t/lib-git-daemon: add network-protocol helpers, 2018-01-24), and around the same time we added the "pkt-line" helper in74e7002961
(test-pkt-line: introduce a packet-line test helper, 2018-03-14). For some reason it seems we've mostly used the shell+perl version instead of the helper since then. There were discussions around88124ab263
(test-lib-functions: make packetize() more efficient, 2020-03-27) andcacae4329f
(test-lib-functions: simplify packetize() stdin code, 2020-03-29) to improve them and make them more efficient. There was one good reason to do so, we needed an equivalent of "test-tool pkt-line pack", but that command wasn't capable of handling input with "\n" (a feature) or "\0" (just because it happens to be printf-based under the hood). Let's add a "pkt-line-raw" helper for that, and expose is at a packetize_raw() to go with the existing packetize() on the shell level, this gives us the smallest amount of change to the tests themselves. Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
ebf3c04b26
commit
64f0109f17
@ -26,6 +26,16 @@ static void pack(int argc, const char **argv)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void pack_raw_stdin(void)
|
||||||
|
{
|
||||||
|
struct strbuf sb = STRBUF_INIT;
|
||||||
|
|
||||||
|
if (strbuf_read(&sb, 0, 0) < 0)
|
||||||
|
die_errno("failed to read from stdin");
|
||||||
|
packet_write(1, sb.buf, sb.len);
|
||||||
|
strbuf_release(&sb);
|
||||||
|
}
|
||||||
|
|
||||||
static void unpack(void)
|
static void unpack(void)
|
||||||
{
|
{
|
||||||
struct packet_reader reader;
|
struct packet_reader reader;
|
||||||
@ -110,6 +120,8 @@ int cmd__pkt_line(int argc, const char **argv)
|
|||||||
|
|
||||||
if (!strcmp(argv[1], "pack"))
|
if (!strcmp(argv[1], "pack"))
|
||||||
pack(argc - 2, argv + 2);
|
pack(argc - 2, argv + 2);
|
||||||
|
else if (!strcmp(argv[1], "pack-raw-stdin"))
|
||||||
|
pack_raw_stdin();
|
||||||
else if (!strcmp(argv[1], "unpack"))
|
else if (!strcmp(argv[1], "unpack"))
|
||||||
unpack();
|
unpack();
|
||||||
else if (!strcmp(argv[1], "unpack-sideband"))
|
else if (!strcmp(argv[1], "unpack-sideband"))
|
||||||
|
@ -28,10 +28,10 @@ test_expect_success "proc-receive: report status v1" '
|
|||||||
if test -z "$GIT_DEFAULT_HASH" || test "$GIT_DEFAULT_HASH" = "sha1"
|
if test -z "$GIT_DEFAULT_HASH" || test "$GIT_DEFAULT_HASH" = "sha1"
|
||||||
then
|
then
|
||||||
printf "%s %s refs/heads/main\0report-status\n" \
|
printf "%s %s refs/heads/main\0report-status\n" \
|
||||||
$A $B | packetize
|
$A $B | packetize_raw
|
||||||
else
|
else
|
||||||
printf "%s %s refs/heads/main\0report-status object-format=$GIT_DEFAULT_HASH\n" \
|
printf "%s %s refs/heads/main\0report-status object-format=$GIT_DEFAULT_HASH\n" \
|
||||||
$A $B | packetize
|
$A $B | packetize_raw
|
||||||
fi &&
|
fi &&
|
||||||
printf "%s %s refs/for/main/topic1\n" \
|
printf "%s %s refs/for/main/topic1\n" \
|
||||||
$ZERO_OID $A | packetize &&
|
$ZERO_OID $A | packetize &&
|
||||||
|
@ -63,7 +63,7 @@ test_expect_success 'setup' '
|
|||||||
hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
|
hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
|
||||||
{
|
{
|
||||||
printf "%s %s refs/heads/newbranch\\0report-status object-format=%s\\n" \
|
printf "%s %s refs/heads/newbranch\\0report-status object-format=%s\\n" \
|
||||||
"$ZERO_OID" "$hash_next" "$(test_oid algo)" | packetize &&
|
"$ZERO_OID" "$hash_next" "$(test_oid algo)" | packetize_raw
|
||||||
printf 0000 &&
|
printf 0000 &&
|
||||||
echo "$hash_next" | git pack-objects --stdout
|
echo "$hash_next" | git pack-objects --stdout
|
||||||
} >push_body &&
|
} >push_body &&
|
||||||
|
@ -194,7 +194,7 @@ test_expect_success 'hostname cannot break out of directory' '
|
|||||||
|
|
||||||
test_expect_success FAKENC 'hostname interpolation works after LF-stripping' '
|
test_expect_success FAKENC 'hostname interpolation works after LF-stripping' '
|
||||||
{
|
{
|
||||||
printf "git-upload-pack /interp.git\n\0host=localhost" | packetize
|
printf "git-upload-pack /interp.git\n\0host=localhost" | packetize_raw
|
||||||
printf "0000"
|
printf "0000"
|
||||||
} >input &&
|
} >input &&
|
||||||
fake_nc "$GIT_DAEMON_HOST_PORT" <input >output &&
|
fake_nc "$GIT_DAEMON_HOST_PORT" <input >output &&
|
||||||
|
@ -1437,46 +1437,24 @@ nongit () {
|
|||||||
)
|
)
|
||||||
} 7>&2 2>&4
|
} 7>&2 2>&4
|
||||||
|
|
||||||
# convert function arguments or stdin (if not arguments given) to pktline
|
# These functions are historical wrappers around "test-tool pkt-line"
|
||||||
# representation. If multiple arguments are given, they are separated by
|
# for older tests. Use "test-tool pkt-line" itself in new tests.
|
||||||
# whitespace and put in a single packet. Note that data containing NULs must be
|
|
||||||
# given on stdin, and that empty input becomes an empty packet, not a flush
|
|
||||||
# packet (for that you can just print 0000 yourself).
|
|
||||||
packetize () {
|
packetize () {
|
||||||
if test $# -gt 0
|
if test $# -gt 0
|
||||||
then
|
then
|
||||||
packet="$*"
|
packet="$*"
|
||||||
printf '%04x%s' "$((4 + ${#packet}))" "$packet"
|
printf '%04x%s' "$((4 + ${#packet}))" "$packet"
|
||||||
else
|
else
|
||||||
perl -e '
|
test-tool pkt-line pack
|
||||||
my $packet = do { local $/; <STDIN> };
|
|
||||||
printf "%04x%s", 4 + length($packet), $packet;
|
|
||||||
'
|
|
||||||
fi
|
fi
|
||||||
}
|
}
|
||||||
|
|
||||||
# Parse the input as a series of pktlines, writing the result to stdout.
|
packetize_raw () {
|
||||||
# Sideband markers are removed automatically, and the output is routed to
|
test-tool pkt-line pack-raw-stdin
|
||||||
# stderr if appropriate.
|
}
|
||||||
#
|
|
||||||
# NUL bytes are converted to "\\0" for ease of parsing with text tools.
|
|
||||||
depacketize () {
|
depacketize () {
|
||||||
perl -e '
|
test-tool pkt-line unpack
|
||||||
while (read(STDIN, $len, 4) == 4) {
|
|
||||||
if ($len eq "0000") {
|
|
||||||
print "FLUSH\n";
|
|
||||||
} else {
|
|
||||||
read(STDIN, $buf, hex($len) - 4);
|
|
||||||
$buf =~ s/\0/\\0/g;
|
|
||||||
if ($buf =~ s/^[\x2\x3]//) {
|
|
||||||
print STDERR $buf;
|
|
||||||
} else {
|
|
||||||
$buf =~ s/^\x1//;
|
|
||||||
print $buf;
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
'
|
|
||||||
}
|
}
|
||||||
|
|
||||||
# Converts base-16 data into base-8. The output is given as a sequence of
|
# Converts base-16 data into base-8. The output is given as a sequence of
|
||||||
|
Reference in New Issue
Block a user