Skip to content

Commit eaa82f8

Browse files
jiangxingitster
authored andcommitted
test-pkt-line: add option parser for unpack-sideband
We can use the test helper program "test-tool pkt-line" to test pkt-line related functions. E.g.: * Use "test-tool pkt-line send-split-sideband" to generate sideband messages. * Pipe these generated sideband messages to command "test-tool pkt-line unpack-sideband" to test packet_reader_read() function. In order to make a complete test of the packet_reader_read() function, add option parser for command "test-tool pkt-line unpack-sideband". * To remove newlines in sideband messages, we can use: $ test-tool pkt-line unpack-sideband --chomp-newline * To preserve newlines in sideband messages, we can use: $ test-tool pkt-line unpack-sideband --no-chomp-newline * To parse sideband messages using "demultiplex_sideband()" inside the function "packet_reader_read()", we can use: $ test-tool pkt-line unpack-sideband --reader-use-sideband We also add new example sideband packets in send_split_sideband() and add several new test cases in t0070. Among these test cases, we pipe output of the "send-split-sideband" subcommand to the "unpack-sideband" subcommand. We found two issues: 1. The two splitted sideband messages "Hello," and " world!\n" should be concatenated together. But when we turn on use_sideband field of reader to parse sideband messages, the first part of the splitted message ("Hello,") is lost. 2. The newline characters in sideband 2 (progress info) and sideband 3 (error message) should be preserved, but they are both trimmed. Will fix the above two issues in subsequent commits. Signed-off-by: Jiang Xin <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent bcb6cae commit eaa82f8

File tree

2 files changed

+112
-5
lines changed

2 files changed

+112
-5
lines changed

t/helper/test-pkt-line.c

Lines changed: 54 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "test-tool.h"
33
#include "pkt-line.h"
44
#include "write-or-die.h"
5+
#include "parse-options.h"
56

67
static void pack_line(const char *line)
78
{
@@ -64,12 +65,33 @@ static void unpack(void)
6465
}
6566
}
6667

67-
static void unpack_sideband(void)
68+
static void unpack_sideband(int argc, const char **argv)
6869
{
6970
struct packet_reader reader;
70-
packet_reader_init(&reader, 0, NULL, 0,
71-
PACKET_READ_GENTLE_ON_EOF |
72-
PACKET_READ_CHOMP_NEWLINE);
71+
int options = PACKET_READ_GENTLE_ON_EOF;
72+
int chomp_newline = 1;
73+
int reader_use_sideband = 0;
74+
const char *const unpack_sideband_usage[] = {
75+
"test_tool unpack_sideband [options...]", NULL
76+
};
77+
struct option cmd_options[] = {
78+
OPT_BOOL(0, "reader-use-sideband", &reader_use_sideband,
79+
"set use_sideband bit for packet reader (Default: off)"),
80+
OPT_BOOL(0, "chomp-newline", &chomp_newline,
81+
"chomp newline in packet (Default: on)"),
82+
OPT_END()
83+
};
84+
85+
argc = parse_options(argc, argv, "", cmd_options, unpack_sideband_usage,
86+
0);
87+
if (argc > 0)
88+
usage_msg_opt(_("too many arguments"), unpack_sideband_usage,
89+
cmd_options);
90+
91+
if (chomp_newline)
92+
options |= PACKET_READ_CHOMP_NEWLINE;
93+
packet_reader_init(&reader, 0, NULL, 0, options);
94+
reader.use_sideband = reader_use_sideband;
7395

7496
while (packet_reader_read(&reader) != PACKET_READ_EOF) {
7597
int band;
@@ -79,6 +101,17 @@ static void unpack_sideband(void)
79101
case PACKET_READ_EOF:
80102
break;
81103
case PACKET_READ_NORMAL:
104+
/*
105+
* When the "use_sideband" field of the reader is turned
106+
* on, sideband packets other than the payload have been
107+
* parsed and consumed in packet_reader_read(), and only
108+
* the payload arrives here.
109+
*/
110+
if (reader.use_sideband) {
111+
write_or_die(1, reader.line, reader.pktlen - 1);
112+
break;
113+
}
114+
82115
band = reader.line[0] & 0xff;
83116
if (band < 1 || band > 2)
84117
continue; /* skip non-sideband packets */
@@ -97,15 +130,31 @@ static void unpack_sideband(void)
97130

98131
static int send_split_sideband(void)
99132
{
133+
const char *foo = "Foo.\n";
134+
const char *bar = "Bar.\n";
100135
const char *part1 = "Hello,";
101136
const char *primary = "\001primary: regular output\n";
102137
const char *part2 = " world!\n";
103138

139+
/* Each sideband message has a trailing newline character. */
140+
send_sideband(1, 2, foo, strlen(foo), LARGE_PACKET_MAX);
141+
send_sideband(1, 2, bar, strlen(bar), LARGE_PACKET_MAX);
142+
143+
/*
144+
* One sideband message is divided into part1 and part2
145+
* by the primary message.
146+
*/
104147
send_sideband(1, 2, part1, strlen(part1), LARGE_PACKET_MAX);
105148
packet_write(1, primary, strlen(primary));
106149
send_sideband(1, 2, part2, strlen(part2), LARGE_PACKET_MAX);
107150
packet_response_end(1);
108151

152+
/*
153+
* We use unpack_sideband() to consume packets. A flush packet
154+
* is required to end parsing.
155+
*/
156+
packet_flush(1);
157+
109158
return 0;
110159
}
111160

@@ -126,7 +175,7 @@ int cmd__pkt_line(int argc, const char **argv)
126175
else if (!strcmp(argv[1], "unpack"))
127176
unpack();
128177
else if (!strcmp(argv[1], "unpack-sideband"))
129-
unpack_sideband();
178+
unpack_sideband(argc - 1, argv + 1);
130179
else if (!strcmp(argv[1], "send-split-sideband"))
131180
send_split_sideband();
132181
else if (!strcmp(argv[1], "receive-sideband"))

t/t0070-fundamental.sh

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,4 +53,62 @@ test_expect_success 'missing sideband designator is reported' '
5353
test_i18ngrep "missing sideband" err
5454
'
5555

56+
test_expect_success 'unpack-sideband: --no-chomp-newline' '
57+
test_when_finished "rm -f expect-out expect-err" &&
58+
test-tool pkt-line send-split-sideband >split-sideband &&
59+
test-tool pkt-line unpack-sideband \
60+
--no-chomp-newline <split-sideband >out 2>err &&
61+
cat >expect-out <<-EOF &&
62+
primary: regular output
63+
EOF
64+
cat >expect-err <<-EOF &&
65+
Foo.
66+
Bar.
67+
Hello, world!
68+
EOF
69+
test_cmp expect-out out &&
70+
test_cmp expect-err err
71+
'
72+
73+
test_expect_success 'unpack-sideband: --chomp-newline (default)' '
74+
test_when_finished "rm -f expect-out expect-err" &&
75+
test-tool pkt-line send-split-sideband >split-sideband &&
76+
test-tool pkt-line unpack-sideband \
77+
--chomp-newline <split-sideband >out 2>err &&
78+
printf "primary: regular output" >expect-out &&
79+
printf "Foo.Bar.Hello, world!" >expect-err &&
80+
test_cmp expect-out out &&
81+
test_cmp expect-err err
82+
'
83+
84+
test_expect_failure 'unpack-sideband: packet_reader_read() consumes sideband, no chomp payload' '
85+
test_when_finished "rm -f expect-out expect-err" &&
86+
test-tool pkt-line send-split-sideband >split-sideband &&
87+
test-tool pkt-line unpack-sideband \
88+
--reader-use-sideband \
89+
--no-chomp-newline <split-sideband >out 2>err &&
90+
cat >expect-out <<-EOF &&
91+
primary: regular output
92+
EOF
93+
printf "remote: Foo. \n" >expect-err &&
94+
printf "remote: Bar. \n" >>expect-err &&
95+
printf "remote: Hello, world! \n" >>expect-err &&
96+
test_cmp expect-out out &&
97+
test_cmp expect-err err
98+
'
99+
100+
test_expect_failure 'unpack-sideband: packet_reader_read() consumes sideband, chomp payload' '
101+
test_when_finished "rm -f expect-out expect-err" &&
102+
test-tool pkt-line send-split-sideband >split-sideband &&
103+
test-tool pkt-line unpack-sideband \
104+
--reader-use-sideband \
105+
--chomp-newline <split-sideband >out 2>err &&
106+
printf "primary: regular output" >expect-out &&
107+
printf "remote: Foo. \n" >expect-err &&
108+
printf "remote: Bar. \n" >>expect-err &&
109+
printf "remote: Hello, world! \n" >>expect-err &&
110+
test_cmp expect-out out &&
111+
test_cmp expect-err err
112+
'
113+
56114
test_done

0 commit comments

Comments
 (0)