t0021/rot13-filter: refactor packet reading functions

To make it possible in a following commit to move packet reading and writing functions into a Packet.pm module, let's refactor these functions, so they don't handle printing debug output and exiting. While at it let's create packet_required_key_val_read() to still handle erroring out in a common case. Signed-off-by: Christian Couder <chriscool@tuxfamily.org> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Christian Couder committed Nov 5, 2017 at 22:38 UTC 2c9ea595a7b855f1005e16dd6272b16d60a8b255
1 file changed +28 -10
t/t0021/rot13-filter.pl
+28 -10
@@ -74,8 +74,7 @@ sub packet_bin_read {
74 my $bytes_read = read STDIN, $buffer, 4;
75 if ( $bytes_read == 0 ) {
76 # EOF - Git stopped talking to us!
77 - print $debug "STOP\n";
78 - exit();
77 + return ( -1, "" );
78 }
79 elsif ( $bytes_read != 4 ) {
80 die "invalid packet: '$buffer'";
@@ -99,12 +98,21 @@ sub packet_bin_read {
98
99 sub packet_txt_read {
100 my ( $res, $buf ) = packet_bin_read();
102 - unless ( $buf eq '' or $buf =~ s/\n$// ) {
101 + unless ( $res == -1 or $buf eq '' or $buf =~ s/\n$// ) {
102 die "A non-binary line MUST be terminated by an LF.";
103 }
104 return ( $res, $buf );
105 }
106
107 +sub packet_required_key_val_read {
108 + my ( $key ) = @_;
109 + my ( $res, $buf ) = packet_txt_read();
110 + unless ( $res == -1 or ( $buf =~ s/^$key=// and $buf ne '' ) ) {
111 + die "bad $key: '$buf'";
112 + }
113 + return ( $res, $buf );
114 +}
115 +
116 sub packet_bin_write {
117 my $buf = shift;
118 print STDOUT sprintf( "%04x", length($buf) + 4 );
@@ -152,13 +160,18 @@ print $debug "init handshake complete\n";
160 $debug->flush();
161
162 while (1) {
155 - my ( $command ) = packet_txt_read() =~ /^command=(.+)$/;
163 + my ( $res, $command ) = packet_required_key_val_read("command");
164 + if ( $res == -1 ) {
165 + print $debug "STOP\n";
166 + exit();
167 + }
168 print $debug "IN: $command";
169 $debug->flush();
170
171 if ( $command eq "list_available_blobs" ) {
172 # Flush
161 - packet_bin_read();
173 + packet_compare_lists([1, ""], packet_bin_read()) ||
174 + die "bad list_available_blobs end";
175
176 foreach my $pathname ( sort keys %DELAY ) {
177 if ( $DELAY{$pathname}{"requested"} >= 1 ) {
@@ -184,14 +197,13 @@ while (1) {
197 packet_flush();
198 }
199 else {
187 - my ( $pathname ) = packet_txt_read() =~ /^pathname=(.+)$/;
200 + my ( $res, $pathname ) = packet_required_key_val_read("pathname");
201 + if ( $res == -1 ) {
202 + die "unexpected EOF while expecting pathname";
203 + }
204 print $debug " $pathname";
205 $debug->flush();
206
191 - if ( $pathname eq "" ) {
192 - die "bad pathname '$pathname'";
193 - }
194 -
207 # Read until flush
208 my ( $done, $buffer ) = packet_txt_read();
209 while ( $buffer ne '' ) {
@@ -205,6 +217,9 @@ while (1) {
217
218 ( $done, $buffer ) = packet_txt_read();
219 }
220 + if ( $done == -1 ) {
221 + die "unexpected EOF after pathname '$pathname'";
222 + }
223
224 my $input = "";
225 {
@@ -215,6 +230,9 @@ while (1) {
230 ( $done, $buffer ) = packet_bin_read();
231 $input .= $buffer;
232 }
233 + if ( $done == -1 ) {
234 + die "unexpected EOF while reading input for '$pathname'";
235 + }
236 print $debug " " . length($input) . " [OK] -- ";
237 $debug->flush();
238 }