1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 //! Tests of individual AuthGraph role (source or sink) functionality.
18 
19 #![cfg(test)]
20 
21 use authgraph_vts_test as vts;
22 use android_hardware_security_authgraph::aidl::android::hardware::security::authgraph::{
23     IAuthGraphKeyExchange::IAuthGraphKeyExchange,
24 };
25 use binder::StatusCode;
26 
27 const AUTH_GRAPH_NONSECURE: &str =
28     "android.hardware.security.authgraph.IAuthGraphKeyExchange/nonsecure";
29 
30 /// Retrieve the /nonsecure instance of AuthGraph, which supports both sink and source roles.
get_nonsecure() -> Option<binder::Strong<dyn IAuthGraphKeyExchange>>31 fn get_nonsecure() -> Option<binder::Strong<dyn IAuthGraphKeyExchange>> {
32     match binder::get_interface(AUTH_GRAPH_NONSECURE) {
33         Ok(ag) => Some(ag),
34         Err(StatusCode::NAME_NOT_FOUND) => None,
35         Err(e) => panic!("failed to get AuthGraph/nonsecure: {e:?}"),
36     }
37 }
38 
39 /// Macro to require availability of a /nonsecure instance of AuthGraph.
40 ///
41 /// Note that this macro triggers `return` if not found.
42 macro_rules! require_nonsecure {
43     {} => {
44         match get_nonsecure() {
45             Some(v) => v,
46             None => {
47                 eprintln!("Skipping test as no /nonsecure impl found");
48                 return;
49             }
50         }
51     }
52 }
53 
54 #[test]
test_nonsecure_source_mainline()55 fn test_nonsecure_source_mainline() {
56     let mut sink = vts::test_ag_participant().expect("failed to create a local sink");
57     vts::source::test_mainline(&mut sink, require_nonsecure!());
58 }
59 #[test]
test_nonsecure_source_corrupt_sig()60 fn test_nonsecure_source_corrupt_sig() {
61     let mut sink = vts::test_ag_participant().expect("failed to create a local sink");
62     vts::source::test_corrupt_sig(&mut sink, require_nonsecure!());
63 }
64 #[test]
test_nonsecure_source_corrupt_keys()65 fn test_nonsecure_source_corrupt_keys() {
66     let mut sink = vts::test_ag_participant().expect("failed to create a local sink");
67     vts::source::test_corrupt_key(&mut sink, require_nonsecure!());
68 }
69 #[test]
test_nonsecure_sink_mainline()70 fn test_nonsecure_sink_mainline() {
71     let mut source = vts::test_ag_participant().expect("failed to create a local source");
72     vts::sink::test_mainline(&mut source, require_nonsecure!());
73 }
74 #[test]
test_nonsecure_sink_corrupt_sig()75 fn test_nonsecure_sink_corrupt_sig() {
76     let mut source = vts::test_ag_participant().expect("failed to create a local source");
77     vts::sink::test_corrupt_sig(&mut source, require_nonsecure!());
78 }
79 #[test]
test_nonsecure_sink_corrupt_keys()80 fn test_nonsecure_sink_corrupt_keys() {
81     let mut source = vts::test_ag_participant().expect("failed to create a local source");
82     vts::sink::test_corrupt_keys(&mut source, require_nonsecure!());
83 }
84