1 // Copyright 2023 Google LLC
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 //     http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14 
15 use cmd_runner::{run_cmd_shell, run_cmd_shell_with_color, YellowStderr};
16 use std::{fs, path};
17 
run_rust_fuzzers(root: &path::Path) -> anyhow::Result<()>18 pub(crate) fn run_rust_fuzzers(root: &path::Path) -> anyhow::Result<()> {
19     log::info!("Running rust fuzzers");
20     run_cmd_shell_with_color::<YellowStderr>(
21         &root.join("presence/xts_aes"),
22         "cargo +nightly fuzz run xts_roundtrip -- -runs=10000 -max_total_time=60",
23     )?;
24     run_cmd_shell_with_color::<YellowStderr>(
25         &root.join("presence/ldt"),
26         "cargo +nightly fuzz run ldt_roundtrip -- -runs=10000 -max_total_time=60",
27     )?;
28     run_cmd_shell_with_color::<YellowStderr>(
29         &root.join("presence/ldt_np_adv"),
30         "cargo +nightly fuzz run ldt_np_decrypt -- -runs=10000 -max_total_time=60",
31     )?;
32     run_cmd_shell_with_color::<YellowStderr>(
33         &root.join("presence/ldt_np_adv"),
34         "cargo +nightly fuzz run ldt_np_roundtrip -- -runs=10000 -max_total_time=60",
35     )?;
36     run_cmd_shell_with_color::<YellowStderr>(
37         &root.join("connections/ukey2/ukey2_connections"),
38         "cargo +nightly fuzz run fuzz_connection -- -runs=10000 -max_total_time=60",
39     )?;
40     run_cmd_shell_with_color::<YellowStderr>(
41         &root.join("connections/ukey2/ukey2_connections"),
42         "cargo +nightly fuzz run fuzz_from_saved_session -- -runs=10000 -max_total_time=60",
43     )?;
44     run_cmd_shell_with_color::<YellowStderr>(
45         &root.join("connections/ukey2/ukey2_connections"),
46         "cargo +nightly fuzz run fuzz_handshake -- -runs=10000 -max_total_time=60",
47     )?;
48     run_cmd_shell_with_color::<YellowStderr>(
49         &root.join("crypto/crypto_provider_test"),
50         "cargo +nightly fuzz run fuzz_p256 -- -runs=10000 -max_total_time=60",
51     )?;
52     run_cmd_shell_with_color::<YellowStderr>(
53         &root.join("crypto/crypto_provider_test"),
54         concat!(
55             "cargo +nightly fuzz run fuzz_p256 --features=boringssl --no-default-features ",
56             "-- -runs=10000 -max_total_time=60"
57         ),
58     )?;
59 
60     Ok(())
61 }
62 
63 // Runs the fuzztest fuzzers as short lived unit tests, compatible with gtest
build_fuzztest_unit_tests(root: &path::Path) -> anyhow::Result<()>64 pub(crate) fn build_fuzztest_unit_tests(root: &path::Path) -> anyhow::Result<()> {
65     log::info!("Checking fuzztest targets in unit test mode");
66 
67     // first build the rust static libs to link against
68     let np_ffi_crate_dir = root.join("presence/np_c_ffi");
69     run_cmd_shell(&np_ffi_crate_dir, "cargo build --release")?;
70     let build_dir = root.join("presence/cmake-build");
71     fs::create_dir_all(&build_dir)?;
72     run_cmd_shell_with_color::<YellowStderr>(&build_dir, "cmake -G Ninja -DENABLE_FUZZ=true ..")?;
73 
74     for target in ["deserialization_fuzzer", "ldt_fuzzer"] {
75         run_cmd_shell_with_color::<YellowStderr>(
76             &build_dir,
77             format!("cmake --build . --target {}", target),
78         )?;
79     }
80 
81     run_cmd_shell_with_color::<YellowStderr>(&build_dir.join("np_cpp_ffi/fuzz/"), "ctest")?;
82     run_cmd_shell_with_color::<YellowStderr>(&build_dir.join("ldt_np_adv_ffi/c/fuzz/"), "ctest")?;
83     Ok(())
84 }
85