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