-
-
Notifications
You must be signed in to change notification settings - Fork 462
Expand file tree
/
Copy pathencode.rs
More file actions
204 lines (186 loc) · 7.21 KB
/
encode.rs
File metadata and controls
204 lines (186 loc) · 7.21 KB
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
/// Because the `TryFrom` implementations don't return proper errors
/// on failure
#[derive(Debug, thiserror::Error)]
enum Error {
#[error("")]
TryFromError,
}
macro_rules! round_trip {
($owned:ty, $borrowed:ty, $( $files:literal ), +) => {
#[test]
fn round_trip() -> Result<(), Box<dyn std::error::Error>> {
use std::convert::TryFrom;
use std::io::Write;
use crate::fixture_bytes;
use gix_object::{ObjectRef, Object, WriteTo};
use bstr::ByteSlice;
for input_name in &[
$( $files ),*
] {
let input = fixture_bytes(input_name);
// Test the parse->borrowed->owned->write chain for an object kind
let mut output = Vec::new();
let item = <$borrowed>::from_bytes(&input)?;
item.write_to(&mut output)?;
assert_eq!(output.as_bstr(), input.as_bstr(), "borrowed: {input_name}");
let item: $owned = item.try_into()?;
output.clear();
item.write_to(&mut output)?;
assert_eq!(output.as_bstr(), input.as_bstr());
// Test the parse->borrowed->owned->write chain for the top-level objects
let item = ObjectRef::from(<$borrowed>::from_bytes(&input)?);
output.clear();
item.write_to(&mut output)?;
assert_eq!(output.as_bstr(), input.as_bstr(), "object-ref");
let item: Object = Object::try_from(item)?;
output.clear();
item.write_to(&mut output)?;
assert_eq!(output.as_bstr(), input.as_bstr(), "owned");
// Test the loose serialisation -> parse chain for an object kind
let item = <$borrowed>::from_bytes(&input)?;
// serialise a borowed item to a tagged loose object
output.clear();
{
let w = &mut output;
w.write_all(&item.loose_header())?;
item.write_to(w)?;
let parsed = ObjectRef::from_loose(&output)?;
let item2 = <$borrowed>::try_from(parsed).or(Err(super::Error::TryFromError))?;
assert_eq!(item2, item, "object-ref loose: {input_name} {:?}\n{:?}", output.as_bstr(), input.as_bstr());
}
let item: $owned = item.try_into()?;
// serialise an owned to a tagged loose object
output.clear();
let w = &mut output;
w.write_all(&item.loose_header())?;
item.write_to(w)?;
let parsed = ObjectRef::from_loose(&output)?;
let parsed_borrowed = <$borrowed>::try_from(parsed).or(Err(super::Error::TryFromError))?;
let item2: $owned = parsed_borrowed.try_into().or(Err(super::Error::TryFromError))?;
assert_eq!(item2, item, "object-ref loose owned: {input_name} {:?}\n{:?}", output.as_bstr(), input.as_bstr());
}
Ok(())
}
};
}
mod tag {
round_trip!(
gix_object::Tag,
gix_object::TagRef,
"tag/empty_missing_nl.txt",
"tag/empty.txt",
"tag/no-tagger.txt",
"tag/whitespace.txt",
"tag/with-newlines.txt",
"tag/signed.txt"
);
}
mod commit {
round_trip!(
gix_object::Commit,
gix_object::CommitRef,
"commit/email-with-space.txt",
"commit/signed-whitespace.txt",
"commit/two-multiline-headers.txt",
"commit/mergetag.txt",
"commit/merge.txt",
"commit/signed.txt",
"commit/signed-singleline.txt",
"commit/signed-with-encoding.txt",
"commit/unsigned.txt",
"commit/whitespace.txt",
"commit/with-encoding.txt",
"commit/subtle.txt"
);
}
mod tree {
use gix_object::{tree, tree::EntryKind, WriteTo};
#[test]
fn write_to_does_not_validate() {
let mut tree = gix_object::Tree::empty();
tree.entries.push(tree::Entry {
mode: EntryKind::Blob.into(),
filename: "".into(),
oid: gix_hash::Kind::Sha1.null(),
});
tree.entries.push(tree::Entry {
mode: EntryKind::Tree.into(),
filename: "something\nwith\newlines\n".into(),
oid: gix_hash::ObjectId::empty_tree(gix_hash::Kind::Sha1),
});
tree.write_to(&mut std::io::sink())
.expect("write succeeds, no validation is performed");
}
#[test]
#[cfg(feature = "sha256")]
fn write_to_does_not_validate_sha256() {
let mut tree = gix_object::Tree::empty();
tree.entries.push(tree::Entry {
mode: EntryKind::Blob.into(),
filename: "".into(),
oid: gix_hash::Kind::Sha256.null(),
});
tree.entries.push(tree::Entry {
mode: EntryKind::Tree.into(),
filename: "something\nwith\newlines\n".into(),
oid: gix_hash::ObjectId::empty_tree(gix_hash::Kind::Sha256),
});
tree.write_to(&mut std::io::sink())
.expect("write succeeds, no validation is performed");
}
#[test]
fn write_to_does_not_allow_separator() {
let mut tree = gix_object::Tree::empty();
tree.entries.push(tree::Entry {
mode: EntryKind::Blob.into(),
filename: "hi\0ho".into(),
oid: gix_hash::Kind::Sha1.null(),
});
let err = tree.write_to(&mut std::io::sink()).unwrap_err();
assert_eq!(
err.to_string(),
r#"Nullbytes are invalid in file paths as they are separators: "hi\0ho""#
);
}
#[test]
#[cfg(feature = "sha256")]
fn write_to_does_not_allow_separator_sha256() {
let mut tree = gix_object::Tree::empty();
tree.entries.push(tree::Entry {
mode: EntryKind::Blob.into(),
filename: "hi\0ho".into(),
oid: gix_hash::Kind::Sha256.null(),
});
let err = tree.write_to(&mut std::io::sink()).unwrap_err();
assert_eq!(
err.to_string(),
r#"Nullbytes are invalid in file paths as they are separators: "hi\0ho""#
);
}
round_trip!(gix_object::Tree, gix_object::TreeRef, "tree/everything.tree");
}
mod blob {
// It doesn't matter which data we use - it's not interpreted.
round_trip!(gix_object::Blob, gix_object::BlobRef, "tree/everything.tree");
}
mod loose_header {
use bstr::ByteSlice;
use gix_object::{decode, encode, Kind};
#[test]
fn round_trip() -> Result<(), Box<dyn std::error::Error>> {
for (kind, size, expected) in &[
(Kind::Tree, 1234, "tree 1234\0".as_bytes()),
(Kind::Blob, 0, b"blob 0\0"),
(Kind::Commit, 24241, b"commit 24241\0"),
(Kind::Tag, 9999999999, b"tag 9999999999\0"),
] {
let buf = encode::loose_header(*kind, *size);
assert_eq!(buf.as_bstr(), expected.as_bstr());
let (actual_kind, actual_size, actual_read) = decode::loose_header(&buf)?;
assert_eq!(actual_kind, *kind);
assert_eq!(actual_size, *size);
assert_eq!(actual_read, buf.len());
}
Ok(())
}
}