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 crate::{crypto_ffi, CargoOptions};
16 use cmd_runner::{run_cmd_shell, run_cmd_shell_with_color, YellowStderr};
17 use std::{fs, path};
18
19 // wrapper for checking all ffi related things
check_ffi(root: &path::Path, cargo_options: &CargoOptions) -> anyhow::Result<()>20 pub fn check_ffi(root: &path::Path, cargo_options: &CargoOptions) -> anyhow::Result<()> {
21 crypto_ffi::check_boringssl(root, cargo_options)?;
22 check_np_ffi_cmake(root, cargo_options)?;
23 check_ldt_cmake(root, cargo_options)?;
24
25 Ok(())
26 }
27
check_np_ffi_cmake(root: &path::Path, cargo_options: &CargoOptions) -> anyhow::Result<()>28 pub fn check_np_ffi_cmake(root: &path::Path, cargo_options: &CargoOptions) -> anyhow::Result<()> {
29 log::info!("Checking CMake build and tests for np ffi c/c++ code");
30 let build_dir = root.join("presence/cmake-build");
31 fs::create_dir_all(&build_dir)?;
32
33 let locked_arg = if cargo_options.locked { "--locked" } else { "" };
34
35 run_cmd_shell_with_color::<YellowStderr>(
36 &build_dir,
37 "cmake -G Ninja -DENABLE_TESTS=true -DCMAKE_BUILD_TYPE=Release -DENABLE_FUZZ=false ..",
38 )?;
39
40 // verify sample and benchmarks build
41 let np_ffi_crate_dir = root.to_path_buf().join("presence/np_c_ffi");
42 run_cmd_shell(&np_ffi_crate_dir, format!("cargo build {locked_arg} --release"))?;
43 run_cmd_shell_with_color::<YellowStderr>(&build_dir, "cmake --build . --target np_cpp_sample")?;
44 run_cmd_shell_with_color::<YellowStderr>(&build_dir, "cmake --build . --target np_ffi_bench")?;
45
46 // Run tests with different crypto backends
47 let tests_dir = build_dir.join("np_cpp_ffi/tests");
48 for build_config in [
49 // test with default build settings (rustcrypto)
50 format!("build {locked_arg} --quiet --release"),
51 // test with boringssl
52 format!("build {locked_arg} --quiet --no-default-features --features=boringssl"),
53 ] {
54 let _ = run_cmd_shell_with_color::<YellowStderr>(
55 &build_dir,
56 "rm np_cpp_ffi/tests/np_ffi_tests",
57 );
58 run_cmd_shell(&np_ffi_crate_dir, format!("cargo {}", build_config))?;
59 run_cmd_shell_with_color::<YellowStderr>(
60 &build_dir,
61 "cmake --build . --target np_ffi_tests",
62 )?;
63 run_cmd_shell_with_color::<YellowStderr>(&tests_dir, "ctest")?;
64 }
65
66 Ok(())
67 }
68
check_ldt_cmake(root: &path::Path, cargo_options: &CargoOptions) -> anyhow::Result<()>69 pub fn check_ldt_cmake(root: &path::Path, cargo_options: &CargoOptions) -> anyhow::Result<()> {
70 log::info!("Checking CMake build and tests for ldt c/c++ code");
71 let build_dir = root.join("presence/cmake-build");
72 fs::create_dir_all(&build_dir)?;
73
74 let locked_arg = if cargo_options.locked { "--locked" } else { "" };
75
76 run_cmd_shell_with_color::<YellowStderr>(
77 &build_dir,
78 "cmake -G Ninja -DENABLE_TESTS=true -DCMAKE_BUILD_TYPE=Release -DENABLE_FUZZ=false ..",
79 )?;
80
81 run_cmd_shell(root, format!("cargo build {locked_arg} -p ldt_np_adv_ffi --quiet --release"))?;
82 run_cmd_shell_with_color::<YellowStderr>(&build_dir, "cmake --build . --target ldt_c_sample")?;
83 run_cmd_shell_with_color::<YellowStderr>(
84 &build_dir,
85 "cmake --build . --target ldt_benchmarks",
86 )?;
87
88 // Run the LDT ffi unit tests. These are rebuilt and tested against all of the different
89 // Cargo build configurations based on the feature flags.
90 let ldt_tests_dir = build_dir.join("ldt_np_adv_ffi/c/tests");
91 for build_config in [
92 // test with default build settings (rustcrypto, no_std)
93 format!("build {locked_arg} --quiet --release"),
94 // test with boringssl crypto feature flag
95 format!("build {locked_arg} --quiet --no-default-features --features boringssl --release"),
96 // test without defaults and std feature flag
97 format!("build {locked_arg} --quiet --no-default-features --features std --release"),
98 ] {
99 run_cmd_shell(root, format!("cargo {} -p ldt_np_adv_ffi", build_config))?;
100 // Force detection of updated `ldt_np_adv_ffi` static lib
101 let _ = run_cmd_shell_with_color::<YellowStderr>(&ldt_tests_dir, "rm ldt_ffi_tests");
102 run_cmd_shell_with_color::<YellowStderr>(
103 &build_dir,
104 "cmake --build . --target ldt_ffi_tests",
105 )?;
106 run_cmd_shell_with_color::<YellowStderr>(&ldt_tests_dir, "ctest")?;
107 }
108
109 Ok(())
110 }
111