Raw
1 #!/bin/sh
2
3 test_description='test protocol v2 server commands'
4
5 GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
6 export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
8 . ./test-lib.sh
9
10 unknown_oid=$(printf "test" | git hash-object --stdin)
11
12 test_expect_success 'setup to generate files with expected content' '
13 printf "agent=git/%s" "$(git version | cut -d" " -f3)" >agent_capability &&
14
15 test_oid_cache <<-EOF &&
16 wrong_algo sha1:sha256
17 wrong_algo sha256:sha1
18 EOF
19
20 if test_have_prereq WINDOWS
21 then
22 printf "agent=FAKE\n" >agent_capability
23 else
24 printf -- "-%s\n" $(uname -s | test_redact_non_printables) >>agent_capability
25 fi &&
26 cat >expect.base <<-EOF &&
27 version 2
28 $(cat agent_capability)
29 ls-refs=unborn
30 fetch=shallow wait-for-done
31 server-option
32 object-format=$(test_oid algo)
33 EOF
34 cat >expect.trailer <<-EOF
35 0000
36 EOF
37 '
38
39 test_expect_success 'test capability advertisement' '
40 cat expect.base expect.trailer >expect &&
41
42 if test_have_prereq WINDOWS
43 then
44 GIT_USER_AGENT=FAKE && export GIT_USER_AGENT
45 fi &&
46 GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
47 --advertise-capabilities >out &&
48 test-tool pkt-line unpack <out >actual &&
49 test_cmp expect actual
50 '
51
52 test_expect_success 'stateless-rpc flag does not list capabilities' '
53 # Empty request
54 test-tool pkt-line pack >in <<-EOF &&
55 0000
56 EOF
57 test-tool serve-v2 --stateless-rpc >out <in &&
58 test_must_be_empty out &&
59
60 # EOF
61 test-tool serve-v2 --stateless-rpc >out &&
62 test_must_be_empty out
63 '
64
65 test_expect_success 'request invalid capability' '
66 test-tool pkt-line pack >in <<-EOF &&
67 foobar
68 0000
69 EOF
70 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
71 test_grep "unknown capability" err
72 '
73
74 test_expect_success 'request with no command' '
75 test-tool pkt-line pack >in <<-EOF &&
76 agent=git/test
77 object-format=$(test_oid algo)
78 0000
79 EOF
80 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
81 test_grep "no command requested" err
82 '
83
84 test_expect_success 'request invalid command' '
85 test-tool pkt-line pack >in <<-EOF &&
86 command=foo
87 object-format=$(test_oid algo)
88 agent=git/test
89 0000
90 EOF
91 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
92 test_grep "invalid command" err
93 '
94
95 test_expect_success 'request capability as command' '
96 test-tool pkt-line pack >in <<-EOF &&
97 command=agent
98 object-format=$(test_oid algo)
99 0000
100 EOF
101 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
102 test_grep invalid.command.*agent err
103 '
104
105 test_expect_success 'request command as capability' '
106 test-tool pkt-line pack >in <<-EOF &&
107 command=ls-refs
108 object-format=$(test_oid algo)
109 fetch
110 0000
111 EOF
112 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
113 test_grep unknown.capability err
114 '
115
116 test_expect_success 'requested command is command=value' '
117 test-tool pkt-line pack >in <<-EOF &&
118 command=ls-refs=whatever
119 object-format=$(test_oid algo)
120 0000
121 EOF
122 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
123 test_grep invalid.command.*ls-refs=whatever err
124 '
125
126 test_expect_success 'wrong object-format' '
127 test-tool pkt-line pack >in <<-EOF &&
128 command=fetch
129 agent=git/test
130 object-format=$(test_oid wrong_algo)
131 0000
132 EOF
133 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
134 test_grep "mismatched object format" err
135 '
136
137 # Test the basics of ls-refs
138 #
139 test_expect_success 'setup some refs and tags' '
140 test_commit one &&
141 git branch dev main &&
142 test_commit two &&
143 git symbolic-ref refs/heads/release refs/heads/main &&
144 git tag -a -m "annotated tag" annotated-tag
145 '
146
147 test_expect_success 'basics of ls-refs' '
148 test-tool pkt-line pack >in <<-EOF &&
149 command=ls-refs
150 object-format=$(test_oid algo)
151 0000
152 EOF
153
154 cat >expect <<-EOF &&
155 $(git rev-parse HEAD) HEAD
156 $(git rev-parse refs/heads/dev) refs/heads/dev
157 $(git rev-parse refs/heads/main) refs/heads/main
158 $(git rev-parse refs/heads/release) refs/heads/release
159 $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
160 $(git rev-parse refs/tags/one) refs/tags/one
161 $(git rev-parse refs/tags/two) refs/tags/two
162 0000
163 EOF
164
165 test-tool serve-v2 --stateless-rpc <in >out &&
166 test-tool pkt-line unpack <out >actual &&
167 test_cmp expect actual
168 '
169
170 test_expect_success 'ls-refs complains about unknown options' '
171 test-tool pkt-line pack >in <<-EOF &&
172 command=ls-refs
173 object-format=$(test_oid algo)
174 0001
175 no-such-arg
176 0000
177 EOF
178
179 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
180 test_grep unexpected.line.*no-such-arg err
181 '
182
183 test_expect_success 'basic ref-prefixes' '
184 test-tool pkt-line pack >in <<-EOF &&
185 command=ls-refs
186 object-format=$(test_oid algo)
187 0001
188 ref-prefix refs/heads/main
189 ref-prefix refs/tags/one
190 0000
191 EOF
192
193 cat >expect <<-EOF &&
194 $(git rev-parse refs/heads/main) refs/heads/main
195 $(git rev-parse refs/tags/one) refs/tags/one
196 0000
197 EOF
198
199 test-tool serve-v2 --stateless-rpc <in >out &&
200 test-tool pkt-line unpack <out >actual &&
201 test_cmp expect actual
202 '
203
204 test_expect_success 'refs/heads prefix' '
205 test-tool pkt-line pack >in <<-EOF &&
206 command=ls-refs
207 object-format=$(test_oid algo)
208 0001
209 ref-prefix refs/heads/
210 0000
211 EOF
212
213 cat >expect <<-EOF &&
214 $(git rev-parse refs/heads/dev) refs/heads/dev
215 $(git rev-parse refs/heads/main) refs/heads/main
216 $(git rev-parse refs/heads/release) refs/heads/release
217 0000
218 EOF
219
220 test-tool serve-v2 --stateless-rpc <in >out &&
221 test-tool pkt-line unpack <out >actual &&
222 test_cmp expect actual
223 '
224
225 test_expect_success 'ignore very large set of prefixes' '
226 # generate a large number of ref-prefixes that we expect
227 # to match nothing; the value here exceeds TOO_MANY_PREFIXES
228 # from ls-refs.c.
229 {
230 echo command=ls-refs &&
231 echo object-format=$(test_oid algo) &&
232 echo 0001 &&
233 awk "{
234 for (i = 1; i <= 65536; i++)
235 print \"ref-prefix refs/heads/\", \$i
236 }" &&
237 echo 0000
238 } |
239 test-tool pkt-line pack >in &&
240
241 # and then confirm that we see unmatched prefixes anyway (i.e.,
242 # that the prefix was not applied).
243 cat >expect <<-EOF &&
244 $(git rev-parse HEAD) HEAD
245 $(git rev-parse refs/heads/dev) refs/heads/dev
246 $(git rev-parse refs/heads/main) refs/heads/main
247 $(git rev-parse refs/heads/release) refs/heads/release
248 $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
249 $(git rev-parse refs/tags/one) refs/tags/one
250 $(git rev-parse refs/tags/two) refs/tags/two
251 0000
252 EOF
253
254 test-tool serve-v2 --stateless-rpc <in >out &&
255 test-tool pkt-line unpack <out >actual &&
256 test_cmp expect actual
257 '
258
259 test_expect_success 'peel parameter' '
260 test-tool pkt-line pack >in <<-EOF &&
261 command=ls-refs
262 object-format=$(test_oid algo)
263 0001
264 peel
265 ref-prefix refs/tags/
266 0000
267 EOF
268
269 cat >expect <<-EOF &&
270 $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag peeled:$(git rev-parse refs/tags/annotated-tag^{})
271 $(git rev-parse refs/tags/one) refs/tags/one
272 $(git rev-parse refs/tags/two) refs/tags/two
273 0000
274 EOF
275
276 test-tool serve-v2 --stateless-rpc <in >out &&
277 test-tool pkt-line unpack <out >actual &&
278 test_cmp expect actual
279 '
280
281 test_expect_success 'symrefs parameter' '
282 test-tool pkt-line pack >in <<-EOF &&
283 command=ls-refs
284 object-format=$(test_oid algo)
285 0001
286 symrefs
287 ref-prefix refs/heads/
288 0000
289 EOF
290
291 cat >expect <<-EOF &&
292 $(git rev-parse refs/heads/dev) refs/heads/dev
293 $(git rev-parse refs/heads/main) refs/heads/main
294 $(git rev-parse refs/heads/release) refs/heads/release symref-target:refs/heads/main
295 0000
296 EOF
297
298 test-tool serve-v2 --stateless-rpc <in >out &&
299 test-tool pkt-line unpack <out >actual &&
300 test_cmp expect actual
301 '
302
303 test_expect_success 'sending server-options' '
304 test-tool pkt-line pack >in <<-EOF &&
305 command=ls-refs
306 object-format=$(test_oid algo)
307 server-option=hello
308 server-option=world
309 0001
310 ref-prefix HEAD
311 0000
312 EOF
313
314 cat >expect <<-EOF &&
315 $(git rev-parse HEAD) HEAD
316 0000
317 EOF
318
319 test-tool serve-v2 --stateless-rpc <in >out &&
320 test-tool pkt-line unpack <out >actual &&
321 test_cmp expect actual
322 '
323
324 test_expect_success 'unexpected lines are not allowed in fetch request' '
325 git init server &&
326
327 test-tool pkt-line pack >in <<-EOF &&
328 command=fetch
329 object-format=$(test_oid algo)
330 0001
331 this-is-not-a-command
332 0000
333 EOF
334
335 (
336 cd server &&
337 test_must_fail test-tool serve-v2 --stateless-rpc
338 ) <in >/dev/null 2>err &&
339 test_grep "unexpected line: .this-is-not-a-command." err
340 '
341
342 # Test the basics of object-info
343 #
344 test_expect_success 'basics of object-info' '
345 test_config transfer.advertiseObjectInfo true &&
346
347 two_oid=$(git rev-parse two:two.t) &&
348 two_size=$(test_file_size two.t) &&
349
350 test-tool pkt-line pack >in <<-EOF &&
351 command=object-info
352 object-format=$(test_oid algo)
353 0001
354 size
355 oid $two_oid
356 oid $two_oid
357 0000
358 EOF
359
360 cat >expect <<-EOF &&
361 size
362 $two_oid $two_size
363 $two_oid $two_size
364 0000
365 EOF
366
367 test-tool serve-v2 --stateless-rpc <in >out &&
368 test-tool pkt-line unpack <out >actual &&
369 test_cmp expect actual
370 '
371
372 test_expect_success 'object-info supports type' '
373 test_config transfer.advertiseObjectInfo true &&
374
375 two_oid=$(git rev-parse two:two.t) &&
376 two_size=$(test_file_size two.t) &&
377
378 test-tool pkt-line pack >in <<-EOF &&
379 command=object-info
380 object-format=$(test_oid algo)
381 0001
382 size
383 type
384 oid $two_oid
385 oid $two_oid
386 0000
387 EOF
388
389 cat >expect <<-EOF &&
390 size
391 type
392 $two_oid $two_size blob
393 $two_oid $two_size blob
394 0000
395 EOF
396
397 test-tool serve-v2 --stateless-rpc <in >out &&
398 test-tool pkt-line unpack <out >actual &&
399 test_cmp expect actual
400 '
401
402 test_expect_success 'bare OID request' '
403 test_config transfer.advertiseObjectInfo true &&
404
405 test-tool pkt-line pack >in <<-EOF &&
406 command=object-info
407 object-format=$(test_oid algo)
408 0001
409 oid $(git rev-parse two:two.t)
410 0000
411 EOF
412
413 cat >expect <<-EOF &&
414 $(git rev-parse two:two.t)
415 0000
416 EOF
417
418 test-tool serve-v2 --stateless-rpc <in >out &&
419 test-tool pkt-line unpack <out >actual &&
420 test_cmp expect actual
421 '
422
423 test_expect_success 'object-info with bare unrecognized OID' '
424 test_config transfer.advertiseObjectInfo true &&
425
426 test-tool pkt-line pack >in <<-EOF &&
427 command=object-info
428 object-format=$(test_oid algo)
429 0001
430 oid $unknown_oid
431 0000
432 EOF
433
434 printf "%s \n" "$unknown_oid" >expect &&
435 printf "0000\n" >>expect &&
436
437 test-tool serve-v2 --stateless-rpc <in >out &&
438 test-tool pkt-line unpack <out >actual &&
439 test_cmp expect actual
440 '
441
442 test_expect_success 'object-info with size for unrecognized OID' '
443 test_config transfer.advertiseObjectInfo true &&
444
445 test-tool pkt-line pack >in <<-EOF &&
446 command=object-info
447 object-format=$(test_oid algo)
448 0001
449 size
450 oid $unknown_oid
451 0000
452 EOF
453
454 printf "size\n" >expect &&
455 printf "%s \n" "$unknown_oid" >>expect &&
456 printf "0000\n" >>expect &&
457
458 test-tool serve-v2 --stateless-rpc <in >out &&
459 test-tool pkt-line unpack <out >actual &&
460 test_cmp expect actual
461 '
462
463 test_expect_success 'test capability advertisement with uploadpack.advertiseBundleURIs' '
464 test_config uploadpack.advertiseBundleURIs true &&
465
466 cat >expect.extra <<-EOF &&
467 bundle-uri
468 EOF
469 cat expect.base \
470 expect.extra \
471 expect.trailer >expect &&
472
473 if test_have_prereq WINDOWS
474 then
475 GIT_USER_AGENT=FAKE && export GIT_USER_AGENT
476 fi &&
477 GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
478 --advertise-capabilities >out &&
479 test-tool pkt-line unpack <out >actual &&
480 test_cmp expect actual
481 '
482
483 test_expect_success 'basics of bundle-uri: dies if not enabled' '
484 test-tool pkt-line pack >in <<-EOF &&
485 command=bundle-uri
486 0000
487 EOF
488
489 cat >err.expect <<-\EOF &&
490 fatal: invalid command '"'"'bundle-uri'"'"'
491 EOF
492
493 cat >expect <<-\EOF &&
494 ERR serve: invalid command '"'"'bundle-uri'"'"'
495 EOF
496
497 test_must_fail test-tool serve-v2 --stateless-rpc <in >out 2>err.actual &&
498 test_cmp err.expect err.actual &&
499 test_must_be_empty out
500 '
501
502 test_expect_success 'object-info missing from capabilities when disabled' '
503 test_config transfer.advertiseObjectInfo false &&
504
505 GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
506 --advertise-capabilities >out &&
507 test-tool pkt-line unpack <out >actual &&
508
509 test_grep ! object.info actual
510 '
511
512 test_expect_success 'object-info commands rejected when disabled' '
513 test_config transfer.advertiseObjectInfo false &&
514
515 test-tool pkt-line pack >in <<-EOF &&
516 command=object-info
517 EOF
518
519 test_must_fail test-tool serve-v2 --stateless-rpc <in 2>err &&
520 test_grep invalid.command err
521 '
522
523 test_done