test-lib-functions: use test-tool for [de]packetize()

The shell+perl "[de]packetize()" helper functions were added in 4414a150025 (t/lib-git-daemon: add network-protocol helpers, 2018-01-24), and around the same time we added the "pkt-line" helper in 74e70029615 (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 around 88124ab2636 (test-lib-functions: make packetize() more efficient, 2020-03-27) and cacae4329fa (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>

Ævar Arnfjörð Bjarmason committed Jul 16, 2021 at 17:41 UTC 64f0109f17ecfdd504f497b67cc066b672e00dc5
5 files changed +24 -34
t/helper/test-pkt-line.c
+12
@@ -26,6 +26,16 @@ static void pack(int argc, const char **argv)
26 }
27 }
28
29 +static void pack_raw_stdin(void)
30 +{
31 + struct strbuf sb = STRBUF_INIT;
32 +
33 + if (strbuf_read(&sb, 0, 0) < 0)
34 + die_errno("failed to read from stdin");
35 + packet_write(1, sb.buf, sb.len);
36 + strbuf_release(&sb);
37 +}
38 +
39 static void unpack(void)
40 {
41 struct packet_reader reader;
@@ -110,6 +120,8 @@ int cmd__pkt_line(int argc, const char **argv)
120
121 if (!strcmp(argv[1], "pack"))
122 pack(argc - 2, argv + 2);
123 + else if (!strcmp(argv[1], "pack-raw-stdin"))
124 + pack_raw_stdin();
125 else if (!strcmp(argv[1], "unpack"))
126 unpack();
127 else if (!strcmp(argv[1], "unpack-sideband"))
t/t5411/once-0010-report-status-v1.sh
+2 -2
@@ -28,10 +28,10 @@ test_expect_success "proc-receive: report status v1" '
28 if test -z "$GIT_DEFAULT_HASH" || test "$GIT_DEFAULT_HASH" = "sha1"
29 then
30 printf "%s %s refs/heads/main\0report-status\n" \
31 - $A $B | packetize
31 + $A $B | packetize_raw
32 else
33 printf "%s %s refs/heads/main\0report-status object-format=$GIT_DEFAULT_HASH\n" \
34 - $A $B | packetize
34 + $A $B | packetize_raw
35 fi &&
36 printf "%s %s refs/for/main/topic1\n" \
37 $ZERO_OID $A | packetize &&
t/t5562-http-backend-content-length.sh
+1 -1
@@ -63,7 +63,7 @@ test_expect_success 'setup' '
63 hash_next=$(git commit-tree -p HEAD -m next HEAD^{tree}) &&
64 {
65 printf "%s %s refs/heads/newbranch\\0report-status object-format=%s\\n" \
66 - "$ZERO_OID" "$hash_next" "$(test_oid algo)" | packetize &&
66 + "$ZERO_OID" "$hash_next" "$(test_oid algo)" | packetize_raw
67 printf 0000 &&
68 echo "$hash_next" | git pack-objects --stdout
69 } >push_body &&
t/t5570-git-daemon.sh
+1 -1
@@ -194,7 +194,7 @@ test_expect_success 'hostname cannot break out of directory' '
194
195 test_expect_success FAKENC 'hostname interpolation works after LF-stripping' '
196 {
197 - printf "git-upload-pack /interp.git\n\0host=localhost" | packetize
197 + printf "git-upload-pack /interp.git\n\0host=localhost" | packetize_raw
198 printf "0000"
199 } >input &&
200 fake_nc "$GIT_DAEMON_HOST_PORT" <input >output &&
t/test-lib-functions.sh
+8 -30
@@ -1437,46 +1437,24 @@ nongit () {
1437 )
1438 } 7>&2 2>&4
1439
1440 -# convert function arguments or stdin (if not arguments given) to pktline
1441 -# representation. If multiple arguments are given, they are separated by
1442 -# whitespace and put in a single packet. Note that data containing NULs must be
1443 -# given on stdin, and that empty input becomes an empty packet, not a flush
1444 -# packet (for that you can just print 0000 yourself).
1440 +# These functions are historical wrappers around "test-tool pkt-line"
1441 +# for older tests. Use "test-tool pkt-line" itself in new tests.
1442 packetize () {
1443 if test $# -gt 0
1444 then
1445 packet="$*"
1446 printf '%04x%s' "$((4 + ${#packet}))" "$packet"
1447 else
1451 - perl -e '
1452 - my $packet = do { local $/; <STDIN> };
1453 - printf "%04x%s", 4 + length($packet), $packet;
1454 - '
1448 + test-tool pkt-line pack
1449 fi
1450 }
1451
1458 -# Parse the input as a series of pktlines, writing the result to stdout.
1459 -# Sideband markers are removed automatically, and the output is routed to
1460 -# stderr if appropriate.
1461 -#
1462 -# NUL bytes are converted to "\\0" for ease of parsing with text tools.
1452 +packetize_raw () {
1453 + test-tool pkt-line pack-raw-stdin
1454 +}
1455 +
1456 depacketize () {
1464 - perl -e '
1465 - while (read(STDIN, $len, 4) == 4) {
1466 - if ($len eq "0000") {
1467 - print "FLUSH\n";
1468 - } else {
1469 - read(STDIN, $buf, hex($len) - 4);
1470 - $buf =~ s/\0/\\0/g;
1471 - if ($buf =~ s/^[\x2\x3]//) {
1472 - print STDERR $buf;
1473 - } else {
1474 - $buf =~ s/^\x1//;
1475 - print $buf;
1476 - }
1477 - }
1478 - }
1479 - '
1457 + test-tool pkt-line unpack
1458 }
1459
1460 # Converts base-16 data into base-8. The output is given as a sequence of