Debian Codegraph @

crates/wasm-metadata/tests/component.rs

raw ◀ ▶
1 Debian patch touch this file
•1 #![cfg(feature = "oci")]
2
3 use std::{str::FromStr, vec};
4
5 use auditable_serde::VersionInfo;
6 use wasm_encoder::{Component, Module};
7 use wasm_metadata::*;
8
9 #[test]
10 fn add_to_empty_component() {
11 let mut add = AddMetadata::default();
12 add.name = AddMetadataField::Set("foo".to_owned());
13 add.language = vec![("bar".to_owned(), "1.0".to_owned())];
14 add.processed_by = vec![("baz".to_owned(), "1.0".to_owned())];
15 add.sdk = vec![];
16 add.authors = AddMetadataField::Set(Authors::new("Chashu Cat"));
17 add.description = AddMetadataField::Set(Description::new("Chashu likes tuna"));
18 add.licenses = AddMetadataField::Set(
19 Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap(),
20 );
21 add.source = AddMetadataField::Set(
22 Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
23 );
24 add.homepage = AddMetadataField::Set(
25 Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
26 );
27 add.revision = AddMetadataField::Set(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad"));
28 add.version = AddMetadataField::Set(Version::new("1.0.0"));
29
30 let json_str = r#"{"packages":[{"name":"adler","version":"0.2.3","source":"registry"}]}"#;
31 let info = VersionInfo::from_str(json_str).unwrap();
32 let mut component = Component::new();
33 component.section(&Dependencies::new(info.clone()));
34 let component = component.finish();
35 let component = add.to_wasm(&component).unwrap();
36
37 match Payload::from_binary(&component).unwrap() {
38 Payload::Component {
39 children,
40 metadata:
41 Metadata {
42 name,
43 producers,
44 authors: author,
45 description,
46 licenses,
47 source,
48 range,
49 homepage,
50 revision,
51 version,
52 dependencies,
53 },
54 } => {
55 assert!(children.is_empty());
56 assert_eq!(name, Some("foo".to_owned()));
57 let producers = producers.expect("some producers");
58 assert_eq!(
59 producers.get("language").unwrap().get("bar").unwrap(),
60 "1.0"
61 );
62 assert_eq!(
63 producers.get("processed-by").unwrap().get("baz").unwrap(),
64 "1.0"
65 );
66
67 assert_eq!(author.unwrap(), Authors::new("Chashu Cat"));
68 assert_eq!(description.unwrap(), Description::new("Chashu likes tuna"));
69 assert_eq!(
70 licenses.unwrap(),
71 Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap()
72 );
73 assert_eq!(
74 source.unwrap(),
75 Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
76 );
77 assert_eq!(
78 homepage.unwrap(),
79 Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
80 );
81 assert_eq!(
82 revision.unwrap(),
83 Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad")
84 );
85 assert_eq!(version.unwrap(), Version::new("1.0.0"));
86 assert_eq!(dependencies.unwrap().version_info(), &info,);
87
88 assert_eq!(range.start, 0);
89 assert_eq!(range.end, 465);
90 }
91 _ => panic!("metadata should be component"),
92 }
93 }
94
95 #[test]
96 fn add_to_nested_component() {
97 // Create the same old module, stick some metadata into it
98 let mut add = AddMetadata::default();
99 add.name = AddMetadataField::Set("foo".to_owned());
100 add.language = vec![("bar".to_owned(), "1.0".to_owned())];
101 add.processed_by = vec![("baz".to_owned(), "1.0".to_owned())];
102 add.sdk = vec![];
103 add.authors = AddMetadataField::Set(Authors::new("Chashu Cat"));
104 add.description = AddMetadataField::Set(Description::new("Chashu likes tuna"));
105 add.licenses = AddMetadataField::Set(
106 Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap(),
107 );
108 add.source = AddMetadataField::Set(
109 Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
110 );
111 add.homepage = AddMetadataField::Set(
112 Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
113 );
114 add.revision = AddMetadataField::Set(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad"));
115 add.version = AddMetadataField::Set(Version::new("1.0.0"));
116
117 let json_str = r#"{"packages":[{"name":"adler","version":"0.2.3","source":"registry"}]}"#;
118 let info = VersionInfo::from_str(json_str).unwrap();
119 let mut component = Module::new();
120 component.section(&Dependencies::new(info.clone()));
121 let module = component.finish();
122 let module = add.to_wasm(&module).unwrap();
123
124 // Stick that module inside a component.
125 let mut component = wasm_encoder::Component::new();
126 component.section(&wasm_encoder::RawSection {
127 id: wasm_encoder::ComponentSectionId::CoreModule.into(),
128 data: &module,
129 });
130
131 let component = component.finish();
132
133 // Add some different metadata to the component.
134 let mut add = AddMetadata::default();
135 add.name = AddMetadataField::Set("gussie".to_owned());
136 add.sdk = vec![("willa".to_owned(), "sparky".to_owned())];
137 let component = add.to_wasm(&component).unwrap();
138
139 match Payload::from_binary(&component).unwrap() {
140 Payload::Component {
141 children,
142 metadata: Metadata {
143 name, producers, ..
144 },
145 } => {
146 // Check that the component metadata is in the component
147 assert_eq!(name, Some("gussie".to_owned()));
148 let producers = producers.as_ref().expect("some producers");
149 assert_eq!(
150 producers.get("sdk").unwrap().get("willa").unwrap(),
151 &"sparky".to_owned()
152 );
153 // Check that there is a single child with the metadata set for the module
154 assert_eq!(children.len(), 1);
155
156 match children.get(0).unwrap() {
157 Payload::Module(Metadata {
158 name,
159 producers,
160 authors: author,
161 licenses,
162 source,
163 range,
164 description,
165 homepage,
166 revision,
167 version,
168 dependencies,
169 }) => {
170 assert_eq!(name, &Some("foo".to_owned()));
171 let producers = producers.as_ref().expect("some producers");
172 assert_eq!(
173 producers.get("language").unwrap().get("bar").unwrap(),
174 "1.0"
175 );
176 assert_eq!(
177 producers.get("processed-by").unwrap().get("baz").unwrap(),
178 "1.0"
179 );
180
181 assert_eq!(author, &Some(Authors::new("Chashu Cat")));
182 assert_eq!(description, &Some(Description::new("Chashu likes tuna")));
183 assert_eq!(
184 licenses,
185 &Some(
186 Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT")
187 .unwrap()
188 )
189 );
190 assert_eq!(
191 source,
192 &Some(
193 Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap()
194 ),
195 );
196 assert_eq!(
197 homepage,
198 &Some(
199 Homepage::new("https://github.com/bytecodealliance/wasm-tools")
200 .unwrap()
201 ),
202 );
203 assert_eq!(
204 revision,
205 &Some(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad"))
206 );
207 assert_eq!(version, &Some(Version::new("1.0.0")));
208 assert_eq!(dependencies.as_ref().unwrap().version_info(), &info);
209 assert_eq!(range.start, 11);
210 assert_eq!(range.end, 466);
211 }
212 _ => panic!("child is a module"),
213 }
214 }
215 _ => panic!("root should be component"),
216 }
217 }
218
219 #[test]
220 fn add_then_clear_fields() {
221 let mut add1 = AddMetadata::default();
222 add1.name = AddMetadataField::Set("foo".to_owned());
223 add1.language = vec![("bar".to_owned(), "1.0".to_owned())];
224 add1.processed_by = vec![("baz".to_owned(), "1.0".to_owned())];
225 add1.sdk = vec![];
226 add1.authors = AddMetadataField::Set(Authors::new("Chashu Cat"));
227 add1.description = AddMetadataField::Set(Description::new("Chashu likes tuna"));
228 add1.licenses = AddMetadataField::Set(
229 Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap(),
230 );
231 add1.source = AddMetadataField::Set(
232 Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
233 );
234 add1.homepage = AddMetadataField::Set(
235 Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
236 );
237 add1.revision =
238 AddMetadataField::Set(Revision::new("de978e17a80c1118f606fce919ba9b7d5a04a5ad"));
239 add1.version = AddMetadataField::Set(Version::new("1.0.0"));
240
241 let json_str = r#"{"packages":[{"name":"adler","version":"0.2.3","source":"registry"}]}"#;
242 let info = VersionInfo::from_str(json_str).unwrap();
243 let mut component1 = Component::new();
244 component1.section(&Dependencies::new(info.clone()));
245 let component1 = component1.finish();
246 let component1 = add1.to_wasm(&component1).unwrap();
247
248 let mut add2 = AddMetadata::default();
249 add2.name = AddMetadataField::Clear;
250 add2.version = AddMetadataField::Clear;
251 add2.description = AddMetadataField::Set(Description::new("Chashu likes something else"));
252 add2.revision = AddMetadataField::Clear;
253
254 let component2 = add2.to_wasm(&component1).unwrap();
255
256 match Payload::from_binary(&component2).unwrap() {
257 Payload::Component {
258 children,
259 metadata:
260 Metadata {
261 name,
262 producers,
263 authors: author,
264 description,
265 licenses,
266 source,
267 range,
268 homepage,
269 revision,
270 version,
271 dependencies,
272 },
273 } => {
274 assert!(children.is_empty());
275 assert_eq!(name, None);
276 let producers = producers.expect("some producers");
277 assert_eq!(
278 producers.get("language").unwrap().get("bar").unwrap(),
279 "1.0"
280 );
281 assert_eq!(
282 producers.get("processed-by").unwrap().get("baz").unwrap(),
283 "1.0"
284 );
285
286 assert_eq!(author.unwrap(), Authors::new("Chashu Cat"));
287 assert_eq!(
288 description.unwrap(),
289 Description::new("Chashu likes something else")
290 );
291 assert_eq!(
292 licenses.unwrap(),
293 Licenses::new("Apache-2.0 WITH LLVM-exception OR Apache-2.0 OR MIT").unwrap()
294 );
295 assert_eq!(
296 source.unwrap(),
297 Source::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
298 );
299 assert_eq!(
300 homepage.unwrap(),
301 Homepage::new("https://github.com/bytecodealliance/wasm-tools").unwrap(),
302 );
303 assert_eq!(revision, None);
304 assert_eq!(version, None);
305 assert_eq!(dependencies.unwrap().version_info(), &info,);
306
307 assert_eq!(range.start, 0);
308 assert_eq!(range.end, 434);
309 }
310 _ => panic!("metadata should be component"),
311 }
312 }