1 /*
2 * Copyright (c) 2023-2024 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
16 #include <gtest/gtest.h>
17
18 #ifdef GTEST_API_
19 #define private public
20 #endif
21 #include "iservice_registry.h"
22 #include "system_ability_definition.h"
23 #include "bundle_mgr_proxy.h"
24 #include "net_mgr_log_wrapper.h"
25 #include "net_manager_constants.h"
26 #include "network_security_config.h"
27
28 namespace OHOS {
29 namespace NetManagerStandard {
30 namespace {
31 using namespace testing::ext;
32
33 const std::string TEST_TRUST_ANCHORS(R"([{"certificates": "@resource/raw/ca"}])");
34
35 const std::string TEST_DOMAINS(R"([{"include-subdomains": false, "name": "baidu.com"},
36 {"include-subdomains": true, "name": "taobao.com"}])");
37
38 const std::string TEST_PINSET(R"({
39 "expiration": "2024-8-6",
40 "pin": [
41 {"digest-algorithm": "sha256", "digest": "Q9TCQAWqP4t+eq41xnKaUgJdrPWqyG5L+Ni2YzMhqdY="},
42 {"digest-algorithm": "sha256", "digest": "Q6TCQAWqP4t+eq41xnKaUgJdrPWqyG5L+Ni2YzMhqdY="}
43 ]})");
44 } // namespace
45
46 std::shared_ptr<NetworkSecurityConfig> g_networkSecurityConfig;
47
48 class NetworkSecurityConfigTest : public testing::Test {
49 public:
50 static void SetUpTestCase();
51 static void TearDownTestCase();
52 void SetUp();
53 void TearDown();
54 };
55
SetUpTestCase()56 void NetworkSecurityConfigTest::SetUpTestCase() {}
57
TearDownTestCase()58 void NetworkSecurityConfigTest::TearDownTestCase() {}
59
SetUp()60 void NetworkSecurityConfigTest::SetUp() {}
61
TearDown()62 void NetworkSecurityConfigTest::TearDown() {}
63
BuildTestJsonObject(std::string & content,cJSON * & json)64 void BuildTestJsonObject(std::string &content, cJSON* &json)
65 {
66 json = cJSON_Parse(content.c_str());
67 }
68
69 /**
70 * @tc.name: IsCACertFileNameTest001
71 * @tc.desc: Test NetworkSecurityConfig::IsCACertFileName
72 * @tc.type: FUNC
73 */
74 HWTEST_F(NetworkSecurityConfigTest, IsCACertFileNameTest001, TestSize.Level1)
75 {
76 std::string fileName("cafile.Pem");
77 std::cout << "IsCACertFileNameTest001 In" << std::endl;
78 auto ret = NetworkSecurityConfig::GetInstance().IsCACertFileName(fileName.c_str());
79 EXPECT_EQ(ret, true);
80 }
81
82 /**
83 * @tc.name: GetCAFilesFromPathTest001
84 * @tc.desc: Test NetworkSecurityConfig::GetCAFilesFromPath
85 * @tc.type: FUNC
86 */
87 HWTEST_F(NetworkSecurityConfigTest, GetCAFilesFromPathTest001, TestSize.Level1)
88 {
89 std::string caPath("/etc/security/certificates/test");
90 std::vector<std::string> caFiles;
91 std::cout << "GetCAFilesFromPathTest001 In" << std::endl;
92 NetworkSecurityConfig::GetInstance().GetCAFilesFromPath(caPath, caFiles);
93 EXPECT_EQ(caFiles.size(), 0);
94 }
95
96 /**
97 * @tc.name: AddSurfixToCACertFileNameTest001
98 * @tc.desc: Test NetworkSecurityConfig::AddSurfixToCACertFileName
99 * @tc.type: FUNC
100 */
101 HWTEST_F(NetworkSecurityConfigTest, AddSurfixToCACertFileNameTest001, TestSize.Level1)
102 {
103 std::string caPath("/etc/security/certificates/test");
104 std::set<std::string> allFileNames;
105 std::string caFile("cacert.pem");
106 std::cout << "AddSurfixToCACertFileNameTest001 In" << std::endl;
107 NetworkSecurityConfig::GetInstance().AddSurfixToCACertFileName(caPath, allFileNames, caFile);
108 EXPECT_EQ(allFileNames.size(), 1);
109 }
110
111 /**
112 * @tc.name: ReadCertFileTest001
113 * @tc.desc: Test NetworkSecurityConfig::ReadCertFile
114 * @tc.type: FUNC
115 */
116 HWTEST_F(NetworkSecurityConfigTest, ReadCertFileTest001, TestSize.Level1)
117 {
118 std::string caFile("cacert.pem");
119 std::cout << "ReadCertFileTest001 In" << std::endl;
120 auto ret = NetworkSecurityConfig::GetInstance().ReadCertFile(caFile);
121 EXPECT_EQ(ret, nullptr);
122 }
123
124 /**
125 * @tc.name: GetRehashedCADirName001
126 * @tc.desc: Test NetworkSecurityConfig::GetRehashedCADirName
127 * @tc.type: FUNC
128 */
129 HWTEST_F(NetworkSecurityConfigTest, GetRehashedCADirName001, TestSize.Level1)
130 {
131 std::string caPath("/etc/security/certificates/test");
132 std::string caPathHashValue = "eb6bf998a72433bdeb4eeb32cfa4bc15";
133 std::cout << "GetRehashedCADirName001 In" << std::endl;
134 auto ret = NetworkSecurityConfig::GetInstance().GetRehashedCADirName(caPath);
135 EXPECT_EQ(ret, caPathHashValue);
136 }
137
138 /**
139 * @tc.name: BuildRehasedCAPath001
140 * @tc.desc: Test NetworkSecurityConfig::BuildRehasedCAPath
141 * @tc.type: FUNC
142 */
143 HWTEST_F(NetworkSecurityConfigTest, BuildRehasedCAPath001, TestSize.Level1)
144 {
145 std::string caPath("/etc/security/certificates/test");
146 std::cout << "BuildRehasedCAPath001 In" << std::endl;
147 auto ret = NetworkSecurityConfig::GetInstance().BuildRehasedCAPath(caPath);
148 EXPECT_EQ(ret, "");
149 }
150
151 /**
152 * @tc.name: GetRehasedCAPath001
153 * @tc.desc: Test NetworkSecurityConfig::GetRehasedCAPath
154 * @tc.type: FUNC
155 */
156 HWTEST_F(NetworkSecurityConfigTest, GetRehasedCAPath001, TestSize.Level1)
157 {
158 std::string caPath("/etc/security/certificates/test");
159 std::cout << "GetRehasedCAPath001 In" << std::endl;
160 auto ret = NetworkSecurityConfig::GetInstance().GetRehasedCAPath(caPath);
161 EXPECT_EQ(ret, "");
162 }
163
164 /**
165 * @tc.name: ReHashCAPathForX509001
166 * @tc.desc: Test NetworkSecurityConfig::ReHashCAPathForX509
167 * @tc.type: FUNC
168 */
169 HWTEST_F(NetworkSecurityConfigTest, ReHashCAPathForX509001, TestSize.Level1)
170 {
171 std::string caPath("/etc/security/certificates/test");
172 std::cout << "ReHashCAPathForX509001 In" << std::endl;
173 auto ret = NetworkSecurityConfig::GetInstance().ReHashCAPathForX509(caPath);
174 EXPECT_EQ(ret, "");
175 }
176
177 /**
178 * @tc.name: ParseJsonTrustAnchorsTest001
179 * @tc.desc: Test NetworkSecurityConfig::ParseJsonTrustAnchors, not applying for
180 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
181 * @tc.type: FUNC
182 */
183 HWTEST_F(NetworkSecurityConfigTest, ParseJsonTrustAnchorsTest001, TestSize.Level1)
184 {
185 cJSON *root = nullptr;
186 TrustAnchors trustAnchors;
187
188 std::string jsonTxt(TEST_TRUST_ANCHORS);
189 BuildTestJsonObject(jsonTxt, root);
190
191 std::cout << "ParseJsonTrustAnchorsTest001 In" << std::endl;
192 NetworkSecurityConfig::GetInstance().ParseJsonTrustAnchors(root, trustAnchors);
193 EXPECT_EQ(trustAnchors.certs_[0], "@resource/raw/ca");
194 }
195
196 /**
197 * @tc.name: ParseJsonDomainsTest001
198 * @tc.desc: Test NetworkSecurityConfig::ParseJsonDomains, not applying for
199 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
200 * @tc.type: FUNC
201 */
202 HWTEST_F(NetworkSecurityConfigTest, ParseJsonDomainsTest001, TestSize.Level1)
203 {
204 cJSON *root = nullptr;
205 std::vector<Domain> domains;
206
207 std::string jsonTxt(TEST_DOMAINS);
208 BuildTestJsonObject(jsonTxt, root);
209
210 std::cout << "ParseJsonDomainsTest001 In" << std::endl;
211 NetworkSecurityConfig::GetInstance().ParseJsonDomains(root, domains);
212 ASSERT_EQ(domains[0].domainName_, "baidu.com");
213 ASSERT_EQ(domains[0].includeSubDomains_, false);
214 ASSERT_EQ(domains[1].domainName_, "taobao.com");
215 EXPECT_EQ(domains[1].includeSubDomains_, true);
216 }
217
218 /**
219 * @tc.name: ParseJsonPinSet001
220 * @tc.desc: Test NetworkSecurityConfig::ParseJsonPinSet, not applying for
221 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
222 * @tc.type: FUNC
223 */
224 HWTEST_F(NetworkSecurityConfigTest, ParseJsonPinSet001, TestSize.Level1)
225 {
226 cJSON *root = nullptr;
227 PinSet pinSet;
228
229 std::string jsonTxt(TEST_PINSET);
230 BuildTestJsonObject(jsonTxt, root);
231
232 std::cout << "ParseJsonPinSet001 In" << std::endl;
233 NetworkSecurityConfig::GetInstance().ParseJsonPinSet(root, pinSet);
234 ASSERT_EQ(pinSet.expiration_, "2024-8-6");
235 ASSERT_EQ(pinSet.pins_[0].digestAlgorithm_, "sha256");
236 ASSERT_EQ(pinSet.pins_[0].digest_, "Q9TCQAWqP4t+eq41xnKaUgJdrPWqyG5L+Ni2YzMhqdY=");
237 ASSERT_EQ(pinSet.pins_[1].digestAlgorithm_, "sha256");
238 EXPECT_EQ(pinSet.pins_[1].digest_, "Q6TCQAWqP4t+eq41xnKaUgJdrPWqyG5L+Ni2YzMhqdY=");
239 }
240
241 /**
242 * @tc.name: HWTEST_F(NetworkSecurityConfigTest, GetPinSetForHostName001, TestSize.Level1)
243 * @tc.desc: Test NetworkSecurityConfig::GetPinSetForHostName, not applying for
244 * permission,return NETMANAGER_ERR_PERMISSION_DENIED
245 * @tc.type: FUNC
246 */
247 HWTEST_F(NetworkSecurityConfigTest, GetPinSetForHostName001, TestSize.Level1)
248 {
249 PinSet pinSet;
250
251 std::string hostname("www.example.com");
252 std::string pins;
253
254 std::cout << "GetPinSetForHostName001 In" << std::endl;
255 auto ret = NetworkSecurityConfig::GetInstance().GetPinSetForHostName(hostname, pins);
256 EXPECT_EQ(ret, NETMANAGER_SUCCESS);
257 }
258
259 }
260 }
261