-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathbuild.rs
More file actions
164 lines (141 loc) · 6.05 KB
/
Copy pathbuild.rs
File metadata and controls
164 lines (141 loc) · 6.05 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
use std::{env, fs, fs::ReadDir, path::PathBuf};
const RETIRED_TRACES: &[&str] = &[
// TODO: Remove after tickets extrinsic encoding get fixed in test vectors
"1766243315_2277",
"1766244122_5414",
"1766244251_1816",
"1767872928_1994",
];
fn is_retired(trace_name: &str) -> bool {
RETIRED_TRACES.contains(&trace_name)
}
/// Build script to generate test cases from JSON test vectors
fn main() {
// PVM test cases
generate_pvm_tests();
// Block import test cases
generate_block_import_tests();
// Fuzzer Block import test cases
generate_fuzzer_block_import_tests();
}
fn generate_pvm_tests() {
let test_vectors_dir = PathBuf::from("jamtestvectors-pvm/pvm/programs");
let full_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join(test_vectors_dir);
println!("cargo:rerun-if-changed={}", full_path.display());
let dest_path = PathBuf::from(env::var("OUT_DIR").unwrap()).join("generated_pvm_tests.rs");
let test_files = fs::read_dir(&full_path).expect("Failed to read test vectors dir");
let mut test_case_contents = String::from("use fr_test_utils::pvm_harness::run_test_case;");
for test_file in test_files {
let test_file_path = test_file.expect("Failed to get test file").path();
let test_file_name = test_file_path.file_name().unwrap().to_str().unwrap();
let test_name = test_file_name.trim_end_matches(".json");
test_case_contents.push_str(&format!(
"\
#[test]\
fn pvm_{test_name}() {{
run_test_case(\"{test_file_name}\")
}}"
));
}
fs::write(&dest_path, test_case_contents).expect("Failed to generate test cases");
}
fn write_block_import_test_cases(
test_files_dir: ReadDir,
test_group: &str,
test_case_contents: &mut String,
) {
for test_file in test_files_dir {
let test_file_path = test_file.expect("Failed to get test file").path();
let test_file_path_str = test_file_path.to_str().unwrap();
let test_file_name = test_file_path.file_name().unwrap().to_str().unwrap();
if test_file_name.ends_with(".json") {
continue;
}
if test_file_name.ends_with("genesis.bin") {
continue;
}
let test_name = test_file_name.trim_end_matches(".bin");
test_case_contents.push_str(&format!(
"\
#[tokio::test]\
async fn block_import_{test_group}_{test_name}() -> Result<(), Box<dyn std::error::Error>> {{
run_test_case(\"{test_file_path_str}\").await?;
Ok(())
}}"
));
}
}
/// Runs block import tests using simple block import harness (no forks)
fn generate_block_import_tests() {
let test_vectors_dir = PathBuf::from("jamtestvectors-polkajam/traces");
let full_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join(test_vectors_dir);
println!("cargo:rerun-if-changed={}", full_path.display());
let dest_path =
PathBuf::from(env::var("OUT_DIR").unwrap()).join("generated_block_import_tests.rs");
let mut test_case_contents =
String::from("use fr_test_utils::importer_harness::run_test_case;");
let block_import_groups = [
"fallback",
"safrole",
"preimages",
"preimages_light",
"storage",
"storage_light",
];
for group in block_import_groups {
let dir_path = full_path.join(group);
let test_files = fs::read_dir(&dir_path).unwrap_or_else(|_| {
panic!("Failed to read {group} test vectors dir");
});
write_block_import_test_cases(test_files, group, &mut test_case_contents);
}
fs::write(&dest_path, test_case_contents).expect("Failed to generate test cases");
}
/// Runs block import tests using fuzzer harness (supports forks)
fn generate_fuzzer_block_import_tests() {
let fuzz_traces_dir = PathBuf::from("jam-conformance/fuzz-reports/0.7.2/traces");
let fuzz_traces_full_path =
PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap()).join(fuzz_traces_dir);
println!("cargo:rerun-if-changed={}", fuzz_traces_full_path.display());
let dest_path =
PathBuf::from(env::var("OUT_DIR").unwrap()).join("generated_fuzzer_block_import_tests.rs");
let mut test_case_contents = String::from("use fr_fuzz::fuzzer::run_fuzz_trace_dir;");
let mut write_fuzzer_case = |test_name: &str, trace_path: &PathBuf, ignored: bool| {
let trace_path_str = trace_path.to_str().unwrap();
let ignored_attr = if ignored { "#[ignore]" } else { "" };
test_case_contents.push_str(&format!(
"\
#[tokio::test]\
{ignored_attr}\
async fn {test_name}() -> Result<(), Box<dyn std::error::Error>> {{
run_fuzz_trace_dir(\"{trace_path_str}\").await?;
Ok(())
}}"
));
};
let trace_dirs = fs::read_dir(&fuzz_traces_full_path).expect("Failed to read fuzz trace dir");
let mut trace_paths: Vec<PathBuf> = trace_dirs
.filter_map(|entry| entry.ok().map(|e| e.path()))
.filter(|path| path.is_dir())
.collect();
trace_paths.sort();
for trace_path in trace_paths {
let trace_name = match trace_path.file_name().and_then(|name| name.to_str()) {
Some(name) => name,
None => continue,
};
let test_name = format!("block_import_conformance_0_7_2_{}", trace_name);
write_fuzzer_case(&test_name, &trace_path, is_retired(trace_name));
}
let fuzzy_block_groups = ["fuzzy", "fuzzy_light"];
for group in fuzzy_block_groups {
let trace_path = PathBuf::from(env::var("CARGO_MANIFEST_DIR").unwrap())
.join(format!("jamtestvectors-polkajam/traces/{group}"));
println!("cargo:rerun-if-changed={}", trace_path.display());
if trace_path.is_dir() {
let test_name = format!("block_import_{group}_all");
write_fuzzer_case(&test_name, &trace_path, false);
}
}
fs::write(&dest_path, test_case_contents).expect("Failed to generate test cases");
}