t0021/rot13-filter: add capability functions

These function help read and write capabilities. To make them more generic and make it easy to reuse them, the following changes are made: - we don't require capabilities to come in a fixed order, - we allow duplicates, - we check that the remote supports the capabilities we advertise, - we don't check if the remote declares any capability we don't know about. The reason behind the last change is that the protocol should work using only the capabilities that both ends support, and it should not stop working if one end starts to advertise a new capability. Despite those changes, we can still require a set of capabilities, and die if one of them is not supported. 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 f11c8ce1f6fe85f11d6f6e4453fa81b6b6389b06
1 file changed +45 -13
t/t0021/rot13-filter.pl
+45 -13
@@ -150,24 +150,56 @@ sub packet_initialize {
150 packet_flush();
151 }
152
153 +sub packet_read_capabilities {
154 + my @cap;
155 + while (1) {
156 + my ( $res, $buf ) = packet_bin_read();
157 + if ( $res == -1 ) {
158 + die "unexpected EOF when reading capabilities";
159 + }
160 + return ( $res, @cap ) if ( $res != 0 );
161 + $buf = remove_final_lf_or_die($buf);
162 + unless ( $buf =~ s/capability=// ) {
163 + die "bad capability buf: '$buf'";
164 + }
165 + push @cap, $buf;
166 + }
167 +}
168 +
169 +# Read remote capabilities and check them against capabilities we require
170 +sub packet_read_and_check_capabilities {
171 + my @required_caps = @_;
172 + my ($res, @remote_caps) = packet_read_capabilities();
173 + my %remote_caps = map { $_ => 1 } @remote_caps;
174 + foreach (@required_caps) {
175 + unless (exists($remote_caps{$_})) {
176 + die "required '$_' capability not available from remote" ;
177 + }
178 + }
179 + return %remote_caps;
180 +}
181 +
182 +# Check our capabilities we want to advertise against the remote ones
183 +# and then advertise our capabilities
184 +sub packet_check_and_write_capabilities {
185 + my ($remote_caps, @our_caps) = @_;
186 + foreach (@our_caps) {
187 + unless (exists($remote_caps->{$_})) {
188 + die "our capability '$_' is not available from remote"
189 + }
190 + packet_txt_write( "capability=" . $_ );
191 + }
192 + packet_flush();
193 +}
194 +
195 print $debug "START\n";
196 $debug->flush();
197
198 packet_initialize("git-filter", 2);
199
158 -packet_compare_lists([0, "capability=clean"], packet_txt_read()) ||
159 - die "bad capability";
160 -packet_compare_lists([0, "capability=smudge"], packet_txt_read()) ||
161 - die "bad capability";
162 -packet_compare_lists([0, "capability=delay"], packet_txt_read()) ||
163 - die "bad capability";
164 -packet_compare_lists([1, ""], packet_bin_read()) ||
165 - die "bad capability end";
166 -
167 -foreach (@capabilities) {
168 - packet_txt_write( "capability=" . $_ );
169 -}
170 -packet_flush();
200 +my %remote_caps = packet_read_and_check_capabilities("clean", "smudge", "delay");
201 +packet_check_and_write_capabilities(\%remote_caps, @capabilities);
202 +
203 print $debug "init handshake complete\n";
204 $debug->flush();
205