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
|
use crate::proto::{NarInfo, PathInfo, ValidatePathInfoError};
use crate::tests::fixtures::*;
use bytes::Bytes;
use nix_compat::store_path::{self, StorePath};
use std::str::FromStr;
use test_case::test_case;
use tvix_castore::proto as castorepb;
#[test_case(
None,
Err(ValidatePathInfoError::NoNodePresent()) ;
"No node"
)]
#[test_case(
Some(castorepb::Node { node: None }),
Err(ValidatePathInfoError::NoNodePresent());
"No node 2"
)]
fn validate_no_node(
t_node: Option<castorepb::Node>,
t_result: Result<StorePath, ValidatePathInfoError>,
) {
// construct the PathInfo object
let p = PathInfo {
node: t_node,
..Default::default()
};
assert_eq!(t_result, p.validate());
}
#[test_case(
castorepb::DirectoryNode {
name: DUMMY_NAME.into(),
digest: DUMMY_DIGEST.clone().into(),
size: 0,
},
Ok(StorePath::from_str(DUMMY_NAME).expect("must succeed"));
"ok"
)]
#[test_case(
castorepb::DirectoryNode {
name: DUMMY_NAME.into(),
digest: Bytes::new(),
size: 0,
},
Err(ValidatePathInfoError::InvalidNodeDigestLen(0));
"invalid digest length"
)]
#[test_case(
castorepb::DirectoryNode {
name: "invalid".into(),
digest: DUMMY_DIGEST.clone().into(),
size: 0,
},
Err(ValidatePathInfoError::InvalidNodeName(
"invalid".into(),
store_path::Error::InvalidLength()
));
"invalid node name"
)]
fn validate_directory(
t_directory_node: castorepb::DirectoryNode,
t_result: Result<StorePath, ValidatePathInfoError>,
) {
// construct the PathInfo object
let p = PathInfo {
node: Some(castorepb::Node {
node: Some(castorepb::node::Node::Directory(t_directory_node)),
}),
..Default::default()
};
assert_eq!(t_result, p.validate());
}
#[test_case(
castorepb::FileNode {
name: DUMMY_NAME.into(),
digest: DUMMY_DIGEST.clone().into(),
size: 0,
executable: false,
},
Ok(StorePath::from_str(DUMMY_NAME).expect("must succeed"));
"ok"
)]
#[test_case(
castorepb::FileNode {
name: DUMMY_NAME.into(),
digest: Bytes::new(),
..Default::default()
},
Err(ValidatePathInfoError::InvalidNodeDigestLen(0));
"invalid digest length"
)]
#[test_case(
castorepb::FileNode {
name: "invalid".into(),
digest: DUMMY_DIGEST.clone().into(),
..Default::default()
},
Err(ValidatePathInfoError::InvalidNodeName(
"invalid".into(),
store_path::Error::InvalidLength()
));
"invalid node name"
)]
fn validate_file(
t_file_node: castorepb::FileNode,
t_result: Result<StorePath, ValidatePathInfoError>,
) {
// construct the PathInfo object
let p = PathInfo {
node: Some(castorepb::Node {
node: Some(castorepb::node::Node::File(t_file_node)),
}),
..Default::default()
};
assert_eq!(t_result, p.validate());
}
#[test_case(
castorepb::SymlinkNode {
name: DUMMY_NAME.into(),
..Default::default()
},
Ok(StorePath::from_str(DUMMY_NAME).expect("must succeed"));
"ok"
)]
#[test_case(
castorepb::SymlinkNode {
name: "invalid".into(),
..Default::default()
},
Err(ValidatePathInfoError::InvalidNodeName(
"invalid".into(),
store_path::Error::InvalidLength()
));
"invalid node name"
)]
fn validate_symlink(
t_symlink_node: castorepb::SymlinkNode,
t_result: Result<StorePath, ValidatePathInfoError>,
) {
// construct the PathInfo object
let p = PathInfo {
node: Some(castorepb::Node {
node: Some(castorepb::node::Node::Symlink(t_symlink_node)),
}),
..Default::default()
};
assert_eq!(t_result, p.validate());
}
#[test]
fn validate_references() {
// create a PathInfo without narinfo field.
let path_info = PathInfo {
node: Some(castorepb::Node {
node: Some(castorepb::node::Node::Directory(castorepb::DirectoryNode {
name: DUMMY_NAME.into(),
digest: DUMMY_DIGEST.clone().into(),
size: 0,
})),
}),
references: vec![DUMMY_DIGEST_2.clone().into()],
narinfo: None,
};
assert!(path_info.validate().is_ok());
// create a PathInfo with a narinfo field, but an inconsistent set of references
let path_info_with_narinfo_missing_refs = PathInfo {
narinfo: Some(NarInfo {
nar_size: 0,
nar_sha256: DUMMY_DIGEST.clone().into(),
signatures: vec![],
reference_names: vec![],
}),
..path_info.clone()
};
match path_info_with_narinfo_missing_refs
.validate()
.expect_err("must_fail")
{
ValidatePathInfoError::InconsistentNumberOfReferences(_, _) => {}
_ => panic!("unexpected error"),
};
// create a pathinfo with the correct number of references, should suceed
let path_info_with_narinfo = PathInfo {
narinfo: Some(NarInfo {
nar_size: 0,
nar_sha256: DUMMY_DIGEST.clone().into(),
signatures: vec![],
reference_names: vec![format!("/nix/store/{}", DUMMY_NAME)],
}),
..path_info
};
assert!(path_info_with_narinfo.validate().is_ok());
}
|