blob: 3591bc2417119c75181cc1884ea9e48a7a87646a [file] [log] [blame]
Brandon Williamsed10cb92018-03-15 10:31:19 -07001#!/bin/sh
2
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -07003test_description='test protocol v2 server commands'
Brandon Williamsed10cb92018-03-15 10:31:19 -07004
Johannes Schindelin95cf2c02020-11-18 23:44:35 +00005GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME=main
Johannes Schindelin334afbc2020-11-18 23:44:19 +00006export GIT_TEST_DEFAULT_INITIAL_BRANCH_NAME
7
Ævar Arnfjörð Bjarmasond96fb142021-10-31 00:24:13 +02008TEST_PASSES_SANITIZE_LEAK=true
Brandon Williamsed10cb92018-03-15 10:31:19 -07009. ./test-lib.sh
10
11test_expect_success 'test capability advertisement' '
brian m. carlson9de0dd32020-05-25 19:59:17 +000012 test_oid_cache <<-EOF &&
13 wrong_algo sha1:sha256
14 wrong_algo sha256:sha1
15 EOF
Ævar Arnfjörð Bjarmason8b8d9a22022-12-22 15:14:07 +000016 cat >expect.base <<-EOF &&
Brandon Williamsed10cb92018-03-15 10:31:19 -070017 version 2
18 agent=git/$(git version | cut -d" " -f3)
Jonathan Tan59e12052021-02-05 12:48:47 -080019 ls-refs=unborn
Jonathan Tan9c1e6572021-05-04 14:16:01 -070020 fetch=shallow wait-for-done
Brandon Williamsecc3e532018-04-23 15:46:22 -070021 server-option
brian m. carlson9de0dd32020-05-25 19:59:17 +000022 object-format=$(test_oid algo)
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -070023 object-info
Ævar Arnfjörð Bjarmason8b8d9a22022-12-22 15:14:07 +000024 EOF
25 cat >expect.trailer <<-EOF &&
Brandon Williamsed10cb92018-03-15 10:31:19 -070026 0000
27 EOF
Ævar Arnfjörð Bjarmason8b8d9a22022-12-22 15:14:07 +000028 cat expect.base expect.trailer >expect &&
Brandon Williamsed10cb92018-03-15 10:31:19 -070029
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -070030 GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
31 --advertise-capabilities >out &&
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +020032 test-tool pkt-line unpack <out >actual &&
Matthew DeVoredcbaa0b2018-10-05 14:54:04 -070033 test_cmp expect actual
Brandon Williamsed10cb92018-03-15 10:31:19 -070034'
35
36test_expect_success 'stateless-rpc flag does not list capabilities' '
37 # Empty request
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +020038 test-tool pkt-line pack >in <<-EOF &&
Brandon Williamsed10cb92018-03-15 10:31:19 -070039 0000
40 EOF
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -070041 test-tool serve-v2 --stateless-rpc >out <in &&
Brandon Williamsed10cb92018-03-15 10:31:19 -070042 test_must_be_empty out &&
43
44 # EOF
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -070045 test-tool serve-v2 --stateless-rpc >out &&
Brandon Williamsed10cb92018-03-15 10:31:19 -070046 test_must_be_empty out
47'
48
49test_expect_success 'request invalid capability' '
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +020050 test-tool pkt-line pack >in <<-EOF &&
Brandon Williamsed10cb92018-03-15 10:31:19 -070051 foobar
52 0000
53 EOF
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -070054 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
Junio C Hamano67892752023-10-31 14:23:30 +090055 test_grep "unknown capability" err
Brandon Williamsed10cb92018-03-15 10:31:19 -070056'
57
58test_expect_success 'request with no command' '
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +020059 test-tool pkt-line pack >in <<-EOF &&
Brandon Williamsed10cb92018-03-15 10:31:19 -070060 agent=git/test
brian m. carlson9de0dd32020-05-25 19:59:17 +000061 object-format=$(test_oid algo)
Brandon Williamsed10cb92018-03-15 10:31:19 -070062 0000
63 EOF
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -070064 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
Junio C Hamano67892752023-10-31 14:23:30 +090065 test_grep "no command requested" err
Brandon Williamsed10cb92018-03-15 10:31:19 -070066'
67
68test_expect_success 'request invalid command' '
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +020069 test-tool pkt-line pack >in <<-EOF &&
Brandon Williamsed10cb92018-03-15 10:31:19 -070070 command=foo
brian m. carlson9de0dd32020-05-25 19:59:17 +000071 object-format=$(test_oid algo)
Brandon Williamsed10cb92018-03-15 10:31:19 -070072 agent=git/test
73 0000
74 EOF
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -070075 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
Junio C Hamano67892752023-10-31 14:23:30 +090076 test_grep "invalid command" err
Brandon Williamsed10cb92018-03-15 10:31:19 -070077'
78
Jeff King0ab7eec2021-09-15 14:36:36 -040079test_expect_success 'request capability as command' '
80 test-tool pkt-line pack >in <<-EOF &&
81 command=agent
82 object-format=$(test_oid algo)
83 0000
84 EOF
85 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
86 grep invalid.command.*agent err
87'
88
89test_expect_success 'request command as capability' '
90 test-tool pkt-line pack >in <<-EOF &&
91 command=ls-refs
92 object-format=$(test_oid algo)
93 fetch
94 0000
95 EOF
96 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
97 grep unknown.capability err
98'
99
Jeff King108c2652021-09-15 14:36:33 -0400100test_expect_success 'requested command is command=value' '
101 test-tool pkt-line pack >in <<-EOF &&
102 command=ls-refs=whatever
103 object-format=$(test_oid algo)
104 0000
105 EOF
106 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
107 grep invalid.command.*ls-refs=whatever err
108'
109
brian m. carlson9de0dd32020-05-25 19:59:17 +0000110test_expect_success 'wrong object-format' '
111 test-tool pkt-line pack >in <<-EOF &&
112 command=fetch
113 agent=git/test
114 object-format=$(test_oid wrong_algo)
115 0000
116 EOF
117 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
Junio C Hamano67892752023-10-31 14:23:30 +0900118 test_grep "mismatched object format" err
brian m. carlson9de0dd32020-05-25 19:59:17 +0000119'
120
Brandon Williams72d0ea02018-03-15 10:31:20 -0700121# Test the basics of ls-refs
122#
123test_expect_success 'setup some refs and tags' '
124 test_commit one &&
Johannes Schindelin95cf2c02020-11-18 23:44:35 +0000125 git branch dev main &&
Brandon Williams72d0ea02018-03-15 10:31:20 -0700126 test_commit two &&
Johannes Schindelin95cf2c02020-11-18 23:44:35 +0000127 git symbolic-ref refs/heads/release refs/heads/main &&
Brandon Williams72d0ea02018-03-15 10:31:20 -0700128 git tag -a -m "annotated tag" annotated-tag
129'
130
131test_expect_success 'basics of ls-refs' '
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200132 test-tool pkt-line pack >in <<-EOF &&
Brandon Williams72d0ea02018-03-15 10:31:20 -0700133 command=ls-refs
brian m. carlson9de0dd32020-05-25 19:59:17 +0000134 object-format=$(test_oid algo)
Brandon Williams72d0ea02018-03-15 10:31:20 -0700135 0000
136 EOF
137
138 cat >expect <<-EOF &&
139 $(git rev-parse HEAD) HEAD
140 $(git rev-parse refs/heads/dev) refs/heads/dev
Johannes Schindelin95cf2c02020-11-18 23:44:35 +0000141 $(git rev-parse refs/heads/main) refs/heads/main
Brandon Williams72d0ea02018-03-15 10:31:20 -0700142 $(git rev-parse refs/heads/release) refs/heads/release
143 $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
144 $(git rev-parse refs/tags/one) refs/tags/one
145 $(git rev-parse refs/tags/two) refs/tags/two
146 0000
147 EOF
148
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -0700149 test-tool serve-v2 --stateless-rpc <in >out &&
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200150 test-tool pkt-line unpack <out >actual &&
Matthew DeVoredcbaa0b2018-10-05 14:54:04 -0700151 test_cmp expect actual
Brandon Williams72d0ea02018-03-15 10:31:20 -0700152'
153
Jeff Kingccf09472021-09-15 14:36:38 -0400154test_expect_success 'ls-refs complains about unknown options' '
155 test-tool pkt-line pack >in <<-EOF &&
156 command=ls-refs
157 object-format=$(test_oid algo)
158 0001
159 no-such-arg
160 0000
161 EOF
162
163 test_must_fail test-tool serve-v2 --stateless-rpc 2>err <in &&
164 grep unexpected.line.*no-such-arg err
165'
166
Brandon Williams72d0ea02018-03-15 10:31:20 -0700167test_expect_success 'basic ref-prefixes' '
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200168 test-tool pkt-line pack >in <<-EOF &&
Brandon Williams72d0ea02018-03-15 10:31:20 -0700169 command=ls-refs
brian m. carlson9de0dd32020-05-25 19:59:17 +0000170 object-format=$(test_oid algo)
Brandon Williams72d0ea02018-03-15 10:31:20 -0700171 0001
Johannes Schindelin95cf2c02020-11-18 23:44:35 +0000172 ref-prefix refs/heads/main
Brandon Williams72d0ea02018-03-15 10:31:20 -0700173 ref-prefix refs/tags/one
174 0000
175 EOF
176
177 cat >expect <<-EOF &&
Johannes Schindelin95cf2c02020-11-18 23:44:35 +0000178 $(git rev-parse refs/heads/main) refs/heads/main
Brandon Williams72d0ea02018-03-15 10:31:20 -0700179 $(git rev-parse refs/tags/one) refs/tags/one
180 0000
181 EOF
182
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -0700183 test-tool serve-v2 --stateless-rpc <in >out &&
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200184 test-tool pkt-line unpack <out >actual &&
Matthew DeVoredcbaa0b2018-10-05 14:54:04 -0700185 test_cmp expect actual
Brandon Williams72d0ea02018-03-15 10:31:20 -0700186'
187
188test_expect_success 'refs/heads prefix' '
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200189 test-tool pkt-line pack >in <<-EOF &&
Brandon Williams72d0ea02018-03-15 10:31:20 -0700190 command=ls-refs
brian m. carlson9de0dd32020-05-25 19:59:17 +0000191 object-format=$(test_oid algo)
Brandon Williams72d0ea02018-03-15 10:31:20 -0700192 0001
193 ref-prefix refs/heads/
194 0000
195 EOF
196
197 cat >expect <<-EOF &&
198 $(git rev-parse refs/heads/dev) refs/heads/dev
Johannes Schindelin95cf2c02020-11-18 23:44:35 +0000199 $(git rev-parse refs/heads/main) refs/heads/main
Brandon Williams72d0ea02018-03-15 10:31:20 -0700200 $(git rev-parse refs/heads/release) refs/heads/release
201 0000
202 EOF
203
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -0700204 test-tool serve-v2 --stateless-rpc <in >out &&
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200205 test-tool pkt-line unpack <out >actual &&
Matthew DeVoredcbaa0b2018-10-05 14:54:04 -0700206 test_cmp expect actual
Brandon Williams72d0ea02018-03-15 10:31:20 -0700207'
208
Jeff King7f0e4f62021-09-15 14:35:31 -0400209test_expect_success 'ignore very large set of prefixes' '
210 # generate a large number of ref-prefixes that we expect
211 # to match nothing; the value here exceeds TOO_MANY_PREFIXES
212 # from ls-refs.c.
213 {
214 echo command=ls-refs &&
215 echo object-format=$(test_oid algo) &&
216 echo 0001 &&
217 perl -le "print \"ref-prefix refs/heads/\$_\" for (1..65536)" &&
218 echo 0000
219 } |
220 test-tool pkt-line pack >in &&
221
222 # and then confirm that we see unmatched prefixes anyway (i.e.,
223 # that the prefix was not applied).
224 cat >expect <<-EOF &&
225 $(git rev-parse HEAD) HEAD
226 $(git rev-parse refs/heads/dev) refs/heads/dev
227 $(git rev-parse refs/heads/main) refs/heads/main
228 $(git rev-parse refs/heads/release) refs/heads/release
229 $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag
230 $(git rev-parse refs/tags/one) refs/tags/one
231 $(git rev-parse refs/tags/two) refs/tags/two
232 0000
233 EOF
234
235 test-tool serve-v2 --stateless-rpc <in >out &&
236 test-tool pkt-line unpack <out >actual &&
237 test_cmp expect actual
238'
239
Brandon Williams72d0ea02018-03-15 10:31:20 -0700240test_expect_success 'peel parameter' '
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200241 test-tool pkt-line pack >in <<-EOF &&
Brandon Williams72d0ea02018-03-15 10:31:20 -0700242 command=ls-refs
brian m. carlson9de0dd32020-05-25 19:59:17 +0000243 object-format=$(test_oid algo)
Brandon Williams72d0ea02018-03-15 10:31:20 -0700244 0001
245 peel
246 ref-prefix refs/tags/
247 0000
248 EOF
249
250 cat >expect <<-EOF &&
251 $(git rev-parse refs/tags/annotated-tag) refs/tags/annotated-tag peeled:$(git rev-parse refs/tags/annotated-tag^{})
252 $(git rev-parse refs/tags/one) refs/tags/one
253 $(git rev-parse refs/tags/two) refs/tags/two
254 0000
255 EOF
256
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -0700257 test-tool serve-v2 --stateless-rpc <in >out &&
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200258 test-tool pkt-line unpack <out >actual &&
Matthew DeVoredcbaa0b2018-10-05 14:54:04 -0700259 test_cmp expect actual
Brandon Williams72d0ea02018-03-15 10:31:20 -0700260'
261
262test_expect_success 'symrefs parameter' '
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200263 test-tool pkt-line pack >in <<-EOF &&
Brandon Williams72d0ea02018-03-15 10:31:20 -0700264 command=ls-refs
brian m. carlson9de0dd32020-05-25 19:59:17 +0000265 object-format=$(test_oid algo)
Brandon Williams72d0ea02018-03-15 10:31:20 -0700266 0001
267 symrefs
268 ref-prefix refs/heads/
269 0000
270 EOF
271
272 cat >expect <<-EOF &&
273 $(git rev-parse refs/heads/dev) refs/heads/dev
Johannes Schindelin95cf2c02020-11-18 23:44:35 +0000274 $(git rev-parse refs/heads/main) refs/heads/main
275 $(git rev-parse refs/heads/release) refs/heads/release symref-target:refs/heads/main
Brandon Williams72d0ea02018-03-15 10:31:20 -0700276 0000
277 EOF
278
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -0700279 test-tool serve-v2 --stateless-rpc <in >out &&
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200280 test-tool pkt-line unpack <out >actual &&
Matthew DeVoredcbaa0b2018-10-05 14:54:04 -0700281 test_cmp expect actual
Brandon Williams72d0ea02018-03-15 10:31:20 -0700282'
283
Brandon Williamsecc3e532018-04-23 15:46:22 -0700284test_expect_success 'sending server-options' '
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200285 test-tool pkt-line pack >in <<-EOF &&
Brandon Williamsecc3e532018-04-23 15:46:22 -0700286 command=ls-refs
brian m. carlson9de0dd32020-05-25 19:59:17 +0000287 object-format=$(test_oid algo)
Brandon Williamsecc3e532018-04-23 15:46:22 -0700288 server-option=hello
289 server-option=world
290 0001
291 ref-prefix HEAD
292 0000
293 EOF
294
295 cat >expect <<-EOF &&
296 $(git rev-parse HEAD) HEAD
297 0000
298 EOF
299
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -0700300 test-tool serve-v2 --stateless-rpc <in >out &&
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200301 test-tool pkt-line unpack <out >actual &&
Matthew DeVoredcbaa0b2018-10-05 14:54:04 -0700302 test_cmp expect actual
Brandon Williamsecc3e532018-04-23 15:46:22 -0700303'
304
Jonathan Tan7cc6ed22018-05-01 17:31:29 -0700305test_expect_success 'unexpected lines are not allowed in fetch request' '
306 git init server &&
307
Nguyễn Thái Ngọc Duy8ea40cc2018-09-09 19:36:28 +0200308 test-tool pkt-line pack >in <<-EOF &&
Jonathan Tan7cc6ed22018-05-01 17:31:29 -0700309 command=fetch
brian m. carlson9de0dd32020-05-25 19:59:17 +0000310 object-format=$(test_oid algo)
Jonathan Tan7cc6ed22018-05-01 17:31:29 -0700311 0001
312 this-is-not-a-command
313 0000
314 EOF
315
Johannes Schindelinb7ce24d2019-04-18 06:16:51 -0700316 (
317 cd server &&
318 test_must_fail test-tool serve-v2 --stateless-rpc
319 ) <in >/dev/null 2>err &&
Jonathan Tan7cc6ed22018-05-01 17:31:29 -0700320 grep "unexpected line: .this-is-not-a-command." err
321'
322
Bruno Albuquerquea2ba1622021-04-20 16:38:31 -0700323# Test the basics of object-info
324#
325test_expect_success 'basics of object-info' '
326 test-tool pkt-line pack >in <<-EOF &&
327 command=object-info
328 object-format=$(test_oid algo)
329 0001
330 size
331 oid $(git rev-parse two:two.t)
332 oid $(git rev-parse two:two.t)
333 0000
334 EOF
335
336 cat >expect <<-EOF &&
337 size
338 $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
339 $(git rev-parse two:two.t) $(wc -c <two.t | xargs)
340 0000
341 EOF
342
343 test-tool serve-v2 --stateless-rpc <in >out &&
344 test-tool pkt-line unpack <out >actual &&
345 test_cmp expect actual
346'
347
Ævar Arnfjörð Bjarmason8b8d9a22022-12-22 15:14:07 +0000348test_expect_success 'test capability advertisement with uploadpack.advertiseBundleURIs' '
349 test_config uploadpack.advertiseBundleURIs true &&
350
351 cat >expect.extra <<-EOF &&
352 bundle-uri
353 EOF
354 cat expect.base \
355 expect.extra \
356 expect.trailer >expect &&
357
358 GIT_TEST_SIDEBAND_ALL=0 test-tool serve-v2 \
359 --advertise-capabilities >out &&
360 test-tool pkt-line unpack <out >actual &&
361 test_cmp expect actual
362'
363
364test_expect_success 'basics of bundle-uri: dies if not enabled' '
365 test-tool pkt-line pack >in <<-EOF &&
366 command=bundle-uri
367 0000
368 EOF
369
370 cat >err.expect <<-\EOF &&
371 fatal: invalid command '"'"'bundle-uri'"'"'
372 EOF
373
374 cat >expect <<-\EOF &&
375 ERR serve: invalid command '"'"'bundle-uri'"'"'
376 EOF
377
378 test_must_fail test-tool serve-v2 --stateless-rpc <in >out 2>err.actual &&
379 test_cmp err.expect err.actual &&
380 test_must_be_empty out
381'
382
Brandon Williamsed10cb92018-03-15 10:31:19 -0700383test_done