1 /*
2 * Copyright (c) 2023 Huawei Device Co., Ltd.
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 extern crate key_enable;
16 use key_enable::cert_chain_utils::PemCollection;
17 use key_enable::cert_path_utils::TrustCertPath;
18
19 // pem_cert_file
20 const VALID_PEM_CERT: &str = "/data/test/tmp/valid_pem_cert.json";
21 const NON_EXISTEND_PEM_CERT: &str = "/data/test/tmp/non_existent_cert_path.json";
22 const INVALID_STRUCTURE_PEM_CERT: &str = "/data/test/tmp/invalid_structure_cert_path.json";
23 const EMPTY_PEM_CERT: &str = "/data/test/tmp/empty_pem_cert.json";
24 // cert_path_file
25 const VALID_CERT_PATH: &str = "/data/test/tmp/valid_cert_path.json";
26 const NON_EXISTEND_CERT_PATH: &str = "/data/test/tmp/non_existent_cert_path.json";
27 const INVALID_STRUCTURE_CERT_PATH: &str = "/data/test/tmp/invalid_structure_cert_path.json";
28 const EMPTY_CERT_PATH: &str = "/data/test/tmp/empty_cert_path.json";
29
30 const ALLOWED_ROOT_CERT_MEMBER_NAMES: &[&str] = &[
31 "C=CN, O=Huawei, OU=Huawei CBG, CN=Huawei CBG Root CA G2",
32 "C=CN, O=OpenHarmony, OU=OpenHarmony Team, CN=OpenHarmony Application Root CA",
33 "C=CN, O=Huawei, OU=Huawei CBG, CN=Huawei CBG Root CA G2 Test",
34 ];
35
36 #[test]
test_load_pem_cert_from_valid_json_file()37 fn test_load_pem_cert_from_valid_json_file() {
38 // test is_debuggable true
39 let mut root_cert = PemCollection::new();
40 root_cert.load_pem_certs_from_json_file(VALID_PEM_CERT, ALLOWED_ROOT_CERT_MEMBER_NAMES);
41 assert_eq!(root_cert.pem_data.len(), 3);
42 }
43
44 #[test]
test_invalid_pem_cert_file_path()45 fn test_invalid_pem_cert_file_path() {
46 let mut root_cert = PemCollection::new();
47 root_cert.load_pem_certs_from_json_file(NON_EXISTEND_PEM_CERT, ALLOWED_ROOT_CERT_MEMBER_NAMES);
48 assert!(root_cert.pem_data.is_empty());
49 }
50
51 #[test]
test_invalid_pem_cert_json_structure()52 fn test_invalid_pem_cert_json_structure() {
53 let mut root_cert = PemCollection::new();
54 root_cert
55 .load_pem_certs_from_json_file(INVALID_STRUCTURE_PEM_CERT, ALLOWED_ROOT_CERT_MEMBER_NAMES);
56 assert!(root_cert.pem_data.is_empty());
57 }
58
59 #[test]
test_empty_pem_cert_json_file()60 fn test_empty_pem_cert_json_file() {
61 let mut root_cert = PemCollection::new();
62 root_cert.load_pem_certs_from_json_file(EMPTY_PEM_CERT, ALLOWED_ROOT_CERT_MEMBER_NAMES);
63 assert!(root_cert.pem_data.is_empty());
64 }
65
66 #[test]
test_successful_load_cert_path()67 fn test_successful_load_cert_path() {
68 let mut cert_paths = TrustCertPath::new();
69 cert_paths.load_cert_path_from_json_file(VALID_CERT_PATH);
70 assert_eq!(cert_paths.profile_signers.len(), 4);
71 assert_eq!(cert_paths.app_sources.len(), 6);
72 }
73 #[test]
test_invalid_cert_path_file_path()74 fn test_invalid_cert_path_file_path() {
75 let mut cert_paths = TrustCertPath::new();
76 cert_paths.load_cert_path_from_json_file(NON_EXISTEND_CERT_PATH);
77 assert!(
78 cert_paths.app_sources.is_empty(),
79 "Expected cert_paths.app_sources to be empty for an empty JSON file"
80 );
81 }
82
83 #[test]
test_invalid_cert_path_json_structure()84 fn test_invalid_cert_path_json_structure() {
85 let mut cert_paths = TrustCertPath::new();
86 cert_paths.load_cert_path_from_json_file(INVALID_STRUCTURE_CERT_PATH);
87 assert!(
88 cert_paths.app_sources.is_empty(),
89 "Expected cert_paths.app_sources to be empty for an empty JSON file"
90 );
91 }
92
93 #[test]
test_empty_cert_path_json_file()94 fn test_empty_cert_path_json_file() {
95 let mut cert_paths = TrustCertPath::new();
96 cert_paths.load_cert_path_from_json_file(EMPTY_CERT_PATH);
97 assert!(
98 cert_paths.app_sources.is_empty(),
99 "Expected cert_paths.app_sources to be empty for an empty JSON file"
100 );
101 }
102