-
Notifications
You must be signed in to change notification settings - Fork 104
Expand file tree
/
Copy pathtests.rs
More file actions
62 lines (54 loc) · 1.96 KB
/
tests.rs
File metadata and controls
62 lines (54 loc) · 1.96 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
use crate::read;
use std::fs;
use stellar_xdr::curr::{Limits, ScMetaEntry, ScMetaV0, StringM, WriteXdr};
#[test]
fn test_from_wasm() {
let wasm = fs::read("../target/wasm32v1-none/release/test_zero.wasm").unwrap();
let meta = read::from_wasm(&wasm).unwrap();
let keys = meta
.iter()
.map(|e| match e {
ScMetaEntry::ScMetaV0(v0) => v0.key.to_string(),
})
.collect::<Vec<_>>();
assert_eq!(keys, ["rsver"]);
}
#[test]
fn test_from_wasm_no_metadata() {
// Create a simple Wasm file without meta
let wasm = vec![0x00, 0x61, 0x73, 0x6d, 0x01, 0x00, 0x00, 0x00]; // minimal Wasm header
let result = read::from_wasm(&wasm).unwrap();
assert_eq!(result.len(), 0);
}
#[test]
fn test_multiple_metadata_sections() {
// Read the original test_zero.wasm
let mut wasm = fs::read("../target/wasm32v1-none/release/test_zero.wasm").unwrap();
// Add on an additional contract meta section
let section_name = b"contractmetav0";
let section_content = ScMetaEntry::ScMetaV0(ScMetaV0 {
key: StringM::try_from("mykey").unwrap(),
val: StringM::try_from("myval").unwrap(),
})
.to_xdr(Limits::none())
.unwrap();
// Encode custom section
let mut custom_section = Vec::new();
custom_section.push(0); // 0 = custom section
custom_section.push((1 + section_name.len() + section_content.len()) as u8);
custom_section.push(section_name.len() as u8);
custom_section.extend_from_slice(section_name);
custom_section.extend_from_slice(§ion_content);
// Append the custom section to the WASM file
wasm.extend_from_slice(&custom_section);
// Parse the new wasm
let meta = read::from_wasm(&wasm).unwrap();
// Should have the original entries plus the new one
let keys = meta
.iter()
.map(|e| match e {
ScMetaEntry::ScMetaV0(v0) => v0.key.to_string(),
})
.collect::<Vec<_>>();
assert_eq!(keys, ["rsver", "mykey"]);
}