summaryrefslogtreecommitdiffstats
path: root/vendor/gix-protocol/src/handshake/refs/tests.rs
blob: 3a6af8d2fbc32a1dca31b175c959640904f653fa (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
use gix_transport::{client, client::Capabilities};

/// Convert a hexadecimal hash into its corresponding `ObjectId` or _panic_.
fn oid(hex: &str) -> gix_hash::ObjectId {
    gix_hash::ObjectId::from_hex(hex.as_bytes()).expect("40 bytes hex")
}

use crate::handshake::{refs, refs::shared::InternalRef, Ref};

#[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
async fn extract_references_from_v2_refs() {
    let input = &mut Fixture(
        "808e50d724f604f69ab93c6da2919c014667bedb HEAD symref-target:refs/heads/main
808e50d724f604f69ab93c6da2919c014667bedb MISSING_NAMESPACE_TARGET symref-target:(null)
unborn HEAD symref-target:refs/heads/main
unborn refs/heads/symbolic symref-target:refs/heads/target
808e50d724f604f69ab93c6da2919c014667bedb refs/heads/main
7fe1b98b39423b71e14217aa299a03b7c937d656 refs/tags/foo peeled:808e50d724f604f69ab93c6da2919c014667bedb
7fe1b98b39423b71e14217aa299a03b7c937d6ff refs/tags/blaz
978f927e6397113757dfec6332e7d9c7e356ac25 refs/heads/symbolic symref-target:refs/tags/v1.0 peeled:4d979abcde5cea47b079c38850828956c9382a56
"
        .as_bytes(),
    );

    let out = refs::from_v2_refs(input).await.expect("no failure on valid input");

    assert_eq!(
        out,
        vec![
            Ref::Symbolic {
                full_ref_name: "HEAD".into(),
                target: "refs/heads/main".into(),
                tag: None,
                object: oid("808e50d724f604f69ab93c6da2919c014667bedb")
            },
            Ref::Direct {
                full_ref_name: "MISSING_NAMESPACE_TARGET".into(),
                object: oid("808e50d724f604f69ab93c6da2919c014667bedb")
            },
            Ref::Unborn {
                full_ref_name: "HEAD".into(),
                target: "refs/heads/main".into(),
            },
            Ref::Unborn {
                full_ref_name: "refs/heads/symbolic".into(),
                target: "refs/heads/target".into(),
            },
            Ref::Direct {
                full_ref_name: "refs/heads/main".into(),
                object: oid("808e50d724f604f69ab93c6da2919c014667bedb")
            },
            Ref::Peeled {
                full_ref_name: "refs/tags/foo".into(),
                tag: oid("7fe1b98b39423b71e14217aa299a03b7c937d656"),
                object: oid("808e50d724f604f69ab93c6da2919c014667bedb")
            },
            Ref::Direct {
                full_ref_name: "refs/tags/blaz".into(),
                object: oid("7fe1b98b39423b71e14217aa299a03b7c937d6ff")
            },
            Ref::Symbolic {
                full_ref_name: "refs/heads/symbolic".into(),
                target: "refs/tags/v1.0".into(),
                tag: Some(oid("978f927e6397113757dfec6332e7d9c7e356ac25")),
                object: oid("4d979abcde5cea47b079c38850828956c9382a56")
            },
        ]
    );
}

#[maybe_async::test(feature = "blocking-client", async(feature = "async-client", async_std::test))]
async fn extract_references_from_v1_refs() {
    let input = &mut Fixture(
        "73a6868963993a3328e7d8fe94e5a6ac5078a944 HEAD
21c9b7500cb144b3169a6537961ec2b9e865be81 MISSING_NAMESPACE_TARGET
73a6868963993a3328e7d8fe94e5a6ac5078a944 refs/heads/main
8e472f9ccc7d745927426cbb2d9d077de545aa4e refs/pull/13/head
dce0ea858eef7ff61ad345cc5cdac62203fb3c10 refs/tags/gix-commitgraph-v0.0.0
21c9b7500cb144b3169a6537961ec2b9e865be81 refs/tags/gix-commitgraph-v0.0.0^{}"
            .as_bytes(),
    );
    let out = refs::from_v1_refs_received_as_part_of_handshake_and_capabilities(
        input,
        Capabilities::from_bytes(b"\0symref=HEAD:refs/heads/main symref=MISSING_NAMESPACE_TARGET:(null)")
            .expect("valid capabilities")
            .0
            .iter(),
    )
    .await
    .expect("no failure from valid input");
    assert_eq!(
        out,
        vec![
            Ref::Symbolic {
                full_ref_name: "HEAD".into(),
                target: "refs/heads/main".into(),
                tag: None,
                object: oid("73a6868963993a3328e7d8fe94e5a6ac5078a944")
            },
            Ref::Direct {
                full_ref_name: "MISSING_NAMESPACE_TARGET".into(),
                object: oid("21c9b7500cb144b3169a6537961ec2b9e865be81")
            },
            Ref::Direct {
                full_ref_name: "refs/heads/main".into(),
                object: oid("73a6868963993a3328e7d8fe94e5a6ac5078a944")
            },
            Ref::Direct {
                full_ref_name: "refs/pull/13/head".into(),
                object: oid("8e472f9ccc7d745927426cbb2d9d077de545aa4e")
            },
            Ref::Peeled {
                full_ref_name: "refs/tags/gix-commitgraph-v0.0.0".into(),
                tag: oid("dce0ea858eef7ff61ad345cc5cdac62203fb3c10"),
                object: oid("21c9b7500cb144b3169a6537961ec2b9e865be81")
            },
        ]
    )
}

#[test]
fn extract_symbolic_references_from_capabilities() -> Result<(), client::Error> {
    let caps = client::Capabilities::from_bytes(
        b"\0unrelated symref=HEAD:refs/heads/main symref=ANOTHER:refs/heads/foo symref=MISSING_NAMESPACE_TARGET:(null) agent=git/2.28.0",
    )?
        .0;
    let out = refs::shared::from_capabilities(caps.iter()).expect("a working example");

    assert_eq!(
        out,
        vec![
            InternalRef::SymbolicForLookup {
                path: "HEAD".into(),
                target: Some("refs/heads/main".into())
            },
            InternalRef::SymbolicForLookup {
                path: "ANOTHER".into(),
                target: Some("refs/heads/foo".into())
            },
            InternalRef::SymbolicForLookup {
                path: "MISSING_NAMESPACE_TARGET".into(),
                target: None
            }
        ]
    );
    Ok(())
}

#[cfg(any(feature = "async-client", feature = "blocking-client"))]
struct Fixture<'a>(&'a [u8]);

#[cfg(feature = "blocking-client")]
impl<'a> std::io::Read for Fixture<'a> {
    fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
        self.0.read(buf)
    }
}

#[cfg(feature = "blocking-client")]
impl<'a> std::io::BufRead for Fixture<'a> {
    fn fill_buf(&mut self) -> std::io::Result<&[u8]> {
        self.0.fill_buf()
    }

    fn consume(&mut self, amt: usize) {
        self.0.consume(amt)
    }
}

#[cfg(feature = "blocking-client")]
impl<'a> gix_transport::client::ReadlineBufRead for Fixture<'a> {
    fn readline(
        &mut self,
    ) -> Option<std::io::Result<Result<gix_packetline::PacketLineRef<'_>, gix_packetline::decode::Error>>> {
        use bstr::{BStr, ByteSlice};
        let bytes: &BStr = self.0.into();
        let mut lines = bytes.lines();
        let res = lines.next()?;
        self.0 = lines.as_bytes();
        Some(Ok(Ok(gix_packetline::PacketLineRef::Data(res))))
    }

    fn readline_str(&mut self, line: &mut String) -> std::io::Result<usize> {
        use bstr::{BStr, ByteSlice};
        let bytes: &BStr = self.0.into();
        let mut lines = bytes.lines();
        let res = match lines.next() {
            None => return Ok(0),
            Some(line) => line,
        };
        self.0 = lines.as_bytes();
        let len = res.len();
        line.push_str(res.to_str().expect("valid UTF8 in fixture"));
        Ok(len)
    }
}

#[cfg(feature = "async-client")]
impl<'a> Fixture<'a> {
    fn project_inner(self: std::pin::Pin<&mut Self>) -> std::pin::Pin<&mut &'a [u8]> {
        #[allow(unsafe_code)]
        unsafe {
            std::pin::Pin::new(&mut self.get_unchecked_mut().0)
        }
    }
}

#[cfg(feature = "async-client")]
impl<'a> futures_io::AsyncRead for Fixture<'a> {
    fn poll_read(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
        buf: &mut [u8],
    ) -> std::task::Poll<std::io::Result<usize>> {
        self.project_inner().poll_read(cx, buf)
    }
}

#[cfg(feature = "async-client")]
impl<'a> futures_io::AsyncBufRead for Fixture<'a> {
    fn poll_fill_buf(
        self: std::pin::Pin<&mut Self>,
        cx: &mut std::task::Context<'_>,
    ) -> std::task::Poll<std::io::Result<&[u8]>> {
        self.project_inner().poll_fill_buf(cx)
    }

    fn consume(self: std::pin::Pin<&mut Self>, amt: usize) {
        self.project_inner().consume(amt)
    }
}

#[cfg(feature = "async-client")]
#[async_trait::async_trait(?Send)]
impl<'a> gix_transport::client::ReadlineBufRead for Fixture<'a> {
    async fn readline(
        &mut self,
    ) -> Option<std::io::Result<Result<gix_packetline::PacketLineRef<'_>, gix_packetline::decode::Error>>> {
        use bstr::{BStr, ByteSlice};
        let bytes: &BStr = self.0.into();
        let mut lines = bytes.lines();
        let res = lines.next()?;
        self.0 = lines.as_bytes();
        Some(Ok(Ok(gix_packetline::PacketLineRef::Data(res))))
    }
    async fn readline_str(&mut self, line: &mut String) -> std::io::Result<usize> {
        use bstr::{BStr, ByteSlice};
        let bytes: &BStr = self.0.into();
        let mut lines = bytes.lines();
        let res = match lines.next() {
            None => return Ok(0),
            Some(line) => line,
        };
        self.0 = lines.as_bytes();
        let len = res.len();
        line.push_str(res.to_str().expect("valid UTF8 in fixture"));
        Ok(len)
    }
}