summaryrefslogtreecommitdiffstats
path: root/vendor/gix-protocol/src/fetch/tests.rs
blob: 93cf6e8db78bbf6b112d458d73311ded98a63c17 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
#[cfg(any(feature = "async-client", feature = "blocking-client"))]
mod arguments {
    use bstr::ByteSlice;
    use gix_transport::Protocol;

    use crate::fetch;

    fn arguments_v1(features: impl IntoIterator<Item = &'static str>) -> fetch::Arguments {
        fetch::Arguments::new(Protocol::V1, features.into_iter().map(|n| (n, None)).collect())
    }

    fn arguments_v2(features: impl IntoIterator<Item = &'static str>) -> fetch::Arguments {
        fetch::Arguments::new(Protocol::V2, features.into_iter().map(|n| (n, None)).collect())
    }

    struct Transport<T> {
        inner: T,
        stateful: bool,
    }

    #[cfg(feature = "blocking-client")]
    mod impls {
        use std::borrow::Cow;

        use bstr::BStr;
        use gix_transport::{
            client,
            client::{Error, MessageKind, RequestWriter, SetServiceResponse, WriteMode},
            Protocol, Service,
        };

        use crate::fetch::tests::arguments::Transport;

        impl<T: client::TransportWithoutIO> client::TransportWithoutIO for Transport<T> {
            fn set_identity(&mut self, identity: client::Account) -> Result<(), Error> {
                self.inner.set_identity(identity)
            }

            fn request(
                &mut self,
                write_mode: WriteMode,
                on_into_read: MessageKind,
            ) -> Result<RequestWriter<'_>, Error> {
                self.inner.request(write_mode, on_into_read)
            }

            fn to_url(&self) -> Cow<'_, BStr> {
                self.inner.to_url()
            }

            fn supported_protocol_versions(&self) -> &[Protocol] {
                self.inner.supported_protocol_versions()
            }

            fn connection_persists_across_multiple_requests(&self) -> bool {
                self.stateful
            }

            fn configure(
                &mut self,
                config: &dyn std::any::Any,
            ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
                self.inner.configure(config)
            }
        }

        impl<T: client::Transport> client::Transport for Transport<T> {
            fn handshake<'a>(
                &mut self,
                service: Service,
                extra_parameters: &'a [(&'a str, Option<&'a str>)],
            ) -> Result<SetServiceResponse<'_>, Error> {
                self.inner.handshake(service, extra_parameters)
            }
        }
    }

    #[cfg(feature = "async-client")]
    mod impls {
        use std::borrow::Cow;

        use async_trait::async_trait;
        use bstr::BStr;
        use gix_transport::{
            client,
            client::{Error, MessageKind, RequestWriter, SetServiceResponse, WriteMode},
            Protocol, Service,
        };

        use crate::fetch::tests::arguments::Transport;
        impl<T: client::TransportWithoutIO + Send> client::TransportWithoutIO for Transport<T> {
            fn set_identity(&mut self, identity: client::Account) -> Result<(), Error> {
                self.inner.set_identity(identity)
            }

            fn request(
                &mut self,
                write_mode: WriteMode,
                on_into_read: MessageKind,
            ) -> Result<RequestWriter<'_>, Error> {
                self.inner.request(write_mode, on_into_read)
            }

            fn to_url(&self) -> Cow<'_, BStr> {
                self.inner.to_url()
            }

            fn supported_protocol_versions(&self) -> &[Protocol] {
                self.inner.supported_protocol_versions()
            }

            fn connection_persists_across_multiple_requests(&self) -> bool {
                self.stateful
            }

            fn configure(
                &mut self,
                config: &dyn std::any::Any,
            ) -> Result<(), Box<dyn std::error::Error + Send + Sync + 'static>> {
                self.inner.configure(config)
            }
        }

        #[async_trait(?Send)]
        impl<T: client::Transport + Send> client::Transport for Transport<T> {
            async fn handshake<'a>(
                &mut self,
                service: Service,
                extra_parameters: &'a [(&'a str, Option<&'a str>)],
            ) -> Result<SetServiceResponse<'_>, Error> {
                self.inner.handshake(service, extra_parameters).await
            }
        }
    }

    fn transport(
        out: &mut Vec<u8>,
        stateful: bool,
    ) -> Transport<gix_transport::client::git::Connection<&'static [u8], &mut Vec<u8>>> {
        Transport {
            inner: gix_transport::client::git::Connection::new(
                &[],
                out,
                Protocol::V1, // does not matter
                b"does/not/matter".as_bstr().to_owned(),
                None::<(&str, _)>,
                gix_transport::client::git::ConnectMode::Process, // avoid header to be sent
            ),
            stateful,
        }
    }

    fn id(hex: &str) -> gix_hash::ObjectId {
        gix_hash::ObjectId::from_hex(hex.as_bytes()).expect("expect valid hex id")
    }

    mod v1 {
        use bstr::ByteSlice;

        use crate::fetch::tests::arguments::{arguments_v1, id, transport};

        #[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
        async fn include_tag() {
            let mut out = Vec::new();
            let mut t = transport(&mut out, true);
            let mut arguments = arguments_v1(["include-tag", "feature-b"].iter().copied());
            assert!(arguments.can_use_include_tag());

            arguments.use_include_tag();
            arguments.want(id("ff333369de1221f9bfbbe03a3a13e9a09bc1ffff"));
            arguments.send(&mut t, true).await.expect("sending to buffer to work");
            assert_eq!(
                out.as_bstr(),
                b"0048want ff333369de1221f9bfbbe03a3a13e9a09bc1ffff feature-b include-tag
00000009done
"
                .as_bstr()
            );
        }

        #[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
        async fn no_include_tag() {
            let mut out = Vec::new();
            let mut t = transport(&mut out, true);
            let mut arguments = arguments_v1(["include-tag", "feature-b"].iter().copied());
            assert!(arguments.can_use_include_tag());

            arguments.want(id("ff333369de1221f9bfbbe03a3a13e9a09bc1ffff"));
            arguments.send(&mut t, true).await.expect("sending to buffer to work");
            assert_eq!(
                out.as_bstr(),
                b"003cwant ff333369de1221f9bfbbe03a3a13e9a09bc1ffff feature-b
00000009done
"
                .as_bstr(),
                "it's possible to not have it enabled, even though it's advertised by the server"
            );
        }

        #[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
        async fn haves_and_wants_for_clone() {
            let mut out = Vec::new();
            let mut t = transport(&mut out, true);
            let mut arguments = arguments_v1(["feature-a", "feature-b"].iter().copied());
            assert!(
                !arguments.can_use_include_tag(),
                "needs to be enabled by features in V1"
            );

            arguments.want(id("7b333369de1221f9bfbbe03a3a13e9a09bc1c907"));
            arguments.want(id("ff333369de1221f9bfbbe03a3a13e9a09bc1ffff"));
            arguments.send(&mut t, true).await.expect("sending to buffer to work");
            assert_eq!(
                out.as_bstr(),
                b"0046want 7b333369de1221f9bfbbe03a3a13e9a09bc1c907 feature-a feature-b
0032want ff333369de1221f9bfbbe03a3a13e9a09bc1ffff
00000009done
"
                .as_bstr()
            );
        }

        #[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
        async fn haves_and_wants_for_fetch_stateless() {
            let mut out = Vec::new();
            let mut t = transport(&mut out, false);
            let mut arguments = arguments_v1(["feature-a", "shallow", "deepen-since", "deepen-not"].iter().copied());

            arguments.deepen(1);
            arguments.shallow(id("7b333369de1221f9bfbbe03a3a13e9a09bc1c9ff"));
            arguments.want(id("7b333369de1221f9bfbbe03a3a13e9a09bc1c907"));
            arguments.deepen_since(12345);
            arguments.deepen_not("refs/heads/main".into());
            arguments.have(id("0000000000000000000000000000000000000000"));
            arguments.send(&mut t, false).await.expect("sending to buffer to work");

            arguments.have(id("1111111111111111111111111111111111111111"));
            arguments.send(&mut t, true).await.expect("sending to buffer to work");
            assert_eq!(
                out.as_bstr(),
                b"005cwant 7b333369de1221f9bfbbe03a3a13e9a09bc1c907 feature-a shallow deepen-since deepen-not
0035shallow 7b333369de1221f9bfbbe03a3a13e9a09bc1c9ff
000ddeepen 1
0017deepen-since 12345
001fdeepen-not refs/heads/main
00000032have 0000000000000000000000000000000000000000
0000005cwant 7b333369de1221f9bfbbe03a3a13e9a09bc1c907 feature-a shallow deepen-since deepen-not
0035shallow 7b333369de1221f9bfbbe03a3a13e9a09bc1c9ff
000ddeepen 1
0017deepen-since 12345
001fdeepen-not refs/heads/main
00000032have 1111111111111111111111111111111111111111
0009done
"
                .as_bstr()
            );
        }

        #[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
        async fn haves_and_wants_for_fetch_stateful() {
            let mut out = Vec::new();
            let mut t = transport(&mut out, true);
            let mut arguments = arguments_v1(["feature-a", "shallow"].iter().copied());

            arguments.deepen(1);
            arguments.want(id("7b333369de1221f9bfbbe03a3a13e9a09bc1c907"));
            arguments.have(id("0000000000000000000000000000000000000000"));
            arguments.send(&mut t, false).await.expect("sending to buffer to work");

            arguments.have(id("1111111111111111111111111111111111111111"));
            arguments.send(&mut t, true).await.expect("sending to buffer to work");
            assert_eq!(
                out.as_bstr(),
                b"0044want 7b333369de1221f9bfbbe03a3a13e9a09bc1c907 feature-a shallow
000ddeepen 1
00000032have 0000000000000000000000000000000000000000
00000032have 1111111111111111111111111111111111111111
0009done
"
                .as_bstr()
            );
        }
    }

    mod v2 {
        use bstr::ByteSlice;

        use crate::fetch::tests::arguments::{arguments_v2, id, transport};

        #[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
        async fn include_tag() {
            let mut out = Vec::new();
            let mut t = transport(&mut out, true);
            let mut arguments = arguments_v2(["does not matter for us here"].iter().copied());
            assert!(arguments.can_use_include_tag(), "always on in V2");
            arguments.use_include_tag();

            arguments.want(id("ff333369de1221f9bfbbe03a3a13e9a09bc1ffff"));
            arguments.send(&mut t, true).await.expect("sending to buffer to work");
            assert_eq!(
                out.as_bstr(),
                b"0012command=fetch
0001000ethin-pack
000eofs-delta
0010include-tag
0032want ff333369de1221f9bfbbe03a3a13e9a09bc1ffff
0009done
0000"
                    .as_bstr(),
                "we filter features/capabilities without value as these apparently shouldn't be listed (remote dies otherwise)"
            );
        }

        #[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
        async fn haves_and_wants_for_clone_stateful() {
            let mut out = Vec::new();
            let mut t = transport(&mut out, true);
            let mut arguments = arguments_v2(["feature-a", "shallow"].iter().copied());
            assert!(arguments.is_stateless(true), "V2 is stateless…");
            assert!(arguments.is_stateless(false), "…in all cases");

            arguments.add_feature("no-progress");
            arguments.deepen(1);
            arguments.deepen_relative();
            arguments.want(id("7b333369de1221f9bfbbe03a3a13e9a09bc1c907"));
            arguments.want(id("ff333369de1221f9bfbbe03a3a13e9a09bc1ffff"));
            arguments.send(&mut t, true).await.expect("sending to buffer to work");
            assert_eq!(
                out.as_bstr(),
                b"0012command=fetch
0001000ethin-pack
000eofs-delta
0010no-progress
000ddeepen 1
0014deepen-relative
0032want 7b333369de1221f9bfbbe03a3a13e9a09bc1c907
0032want ff333369de1221f9bfbbe03a3a13e9a09bc1ffff
0009done
0000"
                    .as_bstr(),
                "we filter features/capabilities without value as these apparently shouldn't be listed (remote dies otherwise)"
            );
        }

        #[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
        async fn haves_and_wants_for_fetch_stateless_and_stateful() {
            for is_stateful in &[false, true] {
                let mut out = Vec::new();
                let mut t = transport(&mut out, *is_stateful);
                let mut arguments = arguments_v2(Some("shallow"));

                arguments.add_feature("no-progress");
                arguments.deepen(1);
                arguments.deepen_since(12345);
                arguments.shallow(id("7b333369de1221f9bfbbe03a3a13e9a09bc1c9ff"));
                arguments.want(id("7b333369de1221f9bfbbe03a3a13e9a09bc1c907"));
                arguments.deepen_not("refs/heads/main".into());
                arguments.have(id("0000000000000000000000000000000000000000"));
                arguments.send(&mut t, false).await.expect("sending to buffer to work");

                arguments.have(id("1111111111111111111111111111111111111111"));
                arguments.send(&mut t, true).await.expect("sending to buffer to work");
                assert_eq!(
                    out.as_bstr(),
                    b"0012command=fetch
0001000ethin-pack
000eofs-delta
0010no-progress
000ddeepen 1
0017deepen-since 12345
0035shallow 7b333369de1221f9bfbbe03a3a13e9a09bc1c9ff
0032want 7b333369de1221f9bfbbe03a3a13e9a09bc1c907
001fdeepen-not refs/heads/main
0032have 0000000000000000000000000000000000000000
00000012command=fetch
0001000ethin-pack
000eofs-delta
0010no-progress
000ddeepen 1
0017deepen-since 12345
0035shallow 7b333369de1221f9bfbbe03a3a13e9a09bc1c9ff
0032want 7b333369de1221f9bfbbe03a3a13e9a09bc1c907
001fdeepen-not refs/heads/main
0032have 1111111111111111111111111111111111111111
0009done
0000"
                        .as_bstr(),
                    "V2 is stateless by default, so it repeats all but 'haves' in each request"
                );
            }
        }

        #[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
        async fn ref_in_want() {
            let mut out = Vec::new();
            let mut t = transport(&mut out, false);
            let mut arguments = arguments_v2(["ref-in-want"].iter().copied());

            arguments.want_ref(b"refs/heads/main".as_bstr());
            arguments.send(&mut t, true).await.expect("sending to buffer to work");
            assert_eq!(
                out.as_bstr(),
                b"0012command=fetch
0001000ethin-pack
000eofs-delta
001dwant-ref refs/heads/main
0009done
0000"
                    .as_bstr()
            )
        }
    }
}