1 /*
2  * Copyright (c) 2024-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 #undef HUKS_ENABLE_SKIP_UPGRADE_KEY_STORAGE_SECURE_LEVEL
17 #define HUKS_ENABLE_SKIP_UPGRADE_KEY_STORAGE_SECURE_LEVEL
18 
19 #undef HKS_ENABLE_LITE_HAP
20 #define HKS_ENABLE_LITE_HAP
21 
22 #undef HKS_USE_RKC_IN_STANDARD
23 #define HKS_USE_RKC_IN_STANDARD
24 
25 #undef HKS_KEY_STORE_LITE_HAP
26 #define HKS_KEY_STORE_LITE_HAP "/lite/hap"
27 
28 #undef HKS_CONFIG_RKC_STORE_PATH
29 #define HKS_CONFIG_RKC_STORE_PATH ""
30 
31 #include <gtest/gtest.h>
32 #include <cstring>
33 
34 #include "file_ex.h"
35 #include "hks_log.h"
36 #include "hks_type_inner.h"
37 #include "hks_param.h"
38 
39 #include "hks_storage_utils.h"
40 
41 #include "base/security/huks/services/huks_standard/huks_service/main/hks_storage/src/hks_storage_utils.c"
42 
43 using namespace testing::ext;
44 namespace Unittest::HksStorageUtilTest {
45 class HksStorageUtilTest : public testing::Test {
46 public:
47     static void SetUpTestCase(void);
48 
49     static void TearDownTestCase(void);
50 
51     void SetUp();
52 
53     void TearDown();
54 };
55 
SetUpTestCase(void)56 void HksStorageUtilTest::SetUpTestCase(void)
57 {
58 }
59 
TearDownTestCase(void)60 void HksStorageUtilTest::TearDownTestCase(void)
61 {
62 }
63 
SetUp()64 void HksStorageUtilTest::SetUp()
65 {
66 }
67 
TearDown()68 void HksStorageUtilTest::TearDown()
69 {
70 }
71 
72 /**
73  * @tc.name: HksStorageUtilTest.HksStorageUtilTest001
74  * @tc.desc: test HksGetFileInfo with full DE_PATH
75  * @tc.type: FUNC
76  */
77 HWTEST_F(HksStorageUtilTest, HksStorageUtilTest001, TestSize.Level0)
78 {
79     HKS_LOG_I("enter HksStorageUtilTest001");
80     struct HksStoreMaterial material = { DE_PATH, 0, 0, 0, 0 };
81     material.keyAliasPath = const_cast<char *>("alias");
82     material.storageTypePath = const_cast<char *>("key");
83     material.uidPath = const_cast<char *>("123");
84     material.userIdPath = const_cast<char *>("999");
85 #ifdef SUPPORT_STORAGE_BACKUP
86     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
87 #else
88     struct HksStoreFileInfo fileInfo = { { 0 } };
89 #endif
90     int32_t ret = HksGetFileInfo(&material, &fileInfo);
91     ASSERT_EQ(HKS_SUCCESS, ret);
92     ASSERT_EQ(strlen(fileInfo.mainPath.fileName), strlen(material.keyAliasPath));
93     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath)));
94 
95     const char *expectPath =
96         HKS_KEY_STORE_PATH "/999/123/key";
97     ASSERT_EQ(strlen(fileInfo.mainPath.path), strlen(expectPath)) << fileInfo.mainPath.path;
98     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath)));
99 }
100 
101 /**
102  * @tc.name: HksStorageUtilTest.HksStorageUtilTest002
103  * @tc.desc: test HksGetFileInfo with DE_PATH without user id
104  * @tc.type: FUNC
105  */
106 HWTEST_F(HksStorageUtilTest, HksStorageUtilTest002, TestSize.Level0)
107 {
108     HKS_LOG_I("enter HksStorageUtilTest002");
109     struct HksStoreMaterial material = { DE_PATH, 0, 0, 0, 0 };
110     material.keyAliasPath = const_cast<char *>("alias");
111     material.storageTypePath = const_cast<char *>("key");
112     material.uidPath = const_cast<char *>("222");
113     material.userIdPath = const_cast<char *>("");
114 #ifdef SUPPORT_STORAGE_BACKUP
115     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
116 #else
117     struct HksStoreFileInfo fileInfo = { { 0 } };
118 #endif
119     int32_t ret = HksGetFileInfo(&material, &fileInfo);
120     ASSERT_EQ(HKS_SUCCESS, ret);
121     ASSERT_EQ(strlen(fileInfo.mainPath.fileName), strlen(material.keyAliasPath));
122     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath)));
123 
124     const char *expectPath =
125         HKS_KEY_STORE_PATH "/222/key";
126     ASSERT_EQ(strlen(fileInfo.mainPath.path), strlen(expectPath)) << fileInfo.mainPath.path;
127     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath)));
128 }
129 
130 /**
131  * @tc.name: HksStorageUtilTest.HksStorageUtilTest003
132  * @tc.desc: test HksGetFileInfo with CE_PATH
133  * @tc.type: FUNC
134  */
135 HWTEST_F(HksStorageUtilTest, HksStorageUtilTest003, TestSize.Level0)
136 {
137     HKS_LOG_I("enter HksStorageUtilTest003");
138     struct HksStoreMaterial material = { CE_PATH, 0 };
139     material.keyAliasPath = const_cast<char *>("alias");
140     material.storageTypePath = const_cast<char *>("key");
141     material.uidPath = const_cast<char *>("333");
142     material.userIdPath = const_cast<char *>("100");
143 #ifdef SUPPORT_STORAGE_BACKUP
144     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
145 #else
146     struct HksStoreFileInfo fileInfo = { { 0 } };
147 #endif
148     int32_t ret = HksGetFileInfo(&material, &fileInfo);
149     ASSERT_EQ(HKS_SUCCESS, ret);
150     ASSERT_EQ(strlen(fileInfo.mainPath.fileName), strlen(material.keyAliasPath));
151     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath)));
152 
153     const char *expectPath =
154         HKS_CE_ROOT_PATH "/100/" HKS_STORE_SERVICE_PATH "/333/key";
155     ASSERT_EQ(strlen(fileInfo.mainPath.path), strlen(expectPath)) << fileInfo.mainPath.path;
156     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath)));
157 }
158 
159 /**
160  * @tc.name: HksStorageUtilTest.HksStorageUtilTest004
161  * @tc.desc: test HksGetFileInfo with ECE_PATH
162  * @tc.type: FUNC
163  */
164 HWTEST_F(HksStorageUtilTest, HksStorageUtilTest004, TestSize.Level0)
165 {
166     HKS_LOG_I("enter HksStorageUtilTest004");
167     struct HksStoreMaterial material = { ECE_PATH, 0 };
168     material.keyAliasPath = const_cast<char *>("alias");
169     material.storageTypePath = const_cast<char *>("key");
170     material.uidPath = const_cast<char *>("444");
171     material.userIdPath = const_cast<char *>("100");
172 #ifdef SUPPORT_STORAGE_BACKUP
173     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
174 #else
175     struct HksStoreFileInfo fileInfo = { { 0 } };
176 #endif
177     int32_t ret = HksGetFileInfo(&material, &fileInfo);
178     ASSERT_EQ(HKS_SUCCESS, ret);
179     ASSERT_EQ(strlen(fileInfo.mainPath.fileName), strlen(material.keyAliasPath));
180     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath)));
181 
182     const char *expectPath =
183         HKS_ECE_ROOT_PATH "/100/" HKS_STORE_SERVICE_PATH "/444/key";
184     ASSERT_EQ(strlen(fileInfo.mainPath.path), strlen(expectPath)) << fileInfo.mainPath.path;
185     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath)));
186 }
187 
188 #ifdef HUKS_ENABLE_SKIP_UPGRADE_KEY_STORAGE_SECURE_LEVEL
189 /**
190  * @tc.name: HksStorageUtilTest.HksStorageUtilTest005
191  * @tc.desc: test HksGetFileInfo with TMP_PATH
192  * @tc.type: FUNC
193  */
194 HWTEST_F(HksStorageUtilTest, HksStorageUtilTest005, TestSize.Level0)
195 {
196     HKS_LOG_I("enter HksStorageUtilTest005");
197     struct HksStoreMaterial material = { TMP_PATH, 0 };
198     material.keyAliasPath = const_cast<char *>("alias");
199     material.storageTypePath = const_cast<char *>("key");
200     material.uidPath = const_cast<char *>("555");
201     material.userIdPath = const_cast<char *>("555");
202 #ifdef SUPPORT_STORAGE_BACKUP
203     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
204 #else
205     struct HksStoreFileInfo fileInfo = { { 0 } };
206 #endif
207     int32_t ret = HksGetFileInfo(&material, &fileInfo);
208     ASSERT_EQ(HKS_SUCCESS, ret);
209     ASSERT_EQ(strlen(fileInfo.mainPath.fileName), strlen(material.keyAliasPath));
210     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath)));
211 
212     const char *expectPath =
213         HKS_KEY_STORE_TMP_PATH "/555/555/key";
214     ASSERT_EQ(strlen(fileInfo.mainPath.path), strlen(expectPath)) << fileInfo.mainPath.path;
215     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath)));
216 }
217 #endif
218 
219 /**
220  * @tc.name: HksStorageUtilTest.HksStorageUtilTest006
221  * @tc.desc: test HksGetFileInfo with LITE_HAP_PATH
222  * @tc.type: FUNC
223  */
224 HWTEST_F(HksStorageUtilTest, HksStorageUtilTest006, TestSize.Level0)
225 {
226     HKS_LOG_I("enter HksStorageUtilTest006");
227     struct HksStoreMaterial material = { LITE_HAP_PATH, 0 };
228     material.keyAliasPath = const_cast<char *>("alias");
229     material.storageTypePath = const_cast<char *>("key");
230     material.uidPath = const_cast<char *>("hks_client");
231     material.userIdPath = const_cast<char *>("");
232 #ifdef SUPPORT_STORAGE_BACKUP
233     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
234 #else
235     struct HksStoreFileInfo fileInfo = { { 0 } };
236 #endif
237     int32_t ret = HksGetFileInfo(&material, &fileInfo);
238     ASSERT_EQ(HKS_SUCCESS, ret);
239     ASSERT_EQ(strlen(fileInfo.mainPath.fileName), strlen(material.keyAliasPath));
240     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath)));
241 
242     const char *expectPath =
243         HKS_KEY_STORE_LITE_HAP "/hks_client/key";
244     ASSERT_EQ(strlen(fileInfo.mainPath.path), strlen(expectPath)) << fileInfo.mainPath.path;
245     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath)));
246 }
247 
248 /**
249  * @tc.name: HksStorageUtilTest.HksStorageUtilTest007
250  * @tc.desc: test HksGetFileInfo with RKC_IN_STANDARD_PATH
251  * @tc.type: FUNC
252  */
253 HWTEST_F(HksStorageUtilTest, HksStorageUtilTest007, TestSize.Level0)
254 {
255     HKS_LOG_I("enter HksStorageUtilTest007");
256     struct HksStoreMaterial material = { RKC_IN_STANDARD_PATH, 0 };
257     material.keyAliasPath = const_cast<char *>("alias");
258     material.storageTypePath = const_cast<char *>("key");
259     material.uidPath = const_cast<char *>("hks_client");
260     material.userIdPath = const_cast<char *>("0");
261 #ifdef SUPPORT_STORAGE_BACKUP
262     struct HksStoreFileInfo fileInfo = { { 0 }, { 0 } };
263 #else
264     struct HksStoreFileInfo fileInfo = { { 0 } };
265 #endif
266     int32_t ret = HksGetFileInfo(&material, &fileInfo);
267     ASSERT_EQ(HKS_SUCCESS, ret);
268     ASSERT_EQ(strlen(fileInfo.mainPath.fileName), strlen(material.keyAliasPath));
269     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.fileName, material.keyAliasPath, strlen(material.keyAliasPath)));
270 
271     const char *expectPath =
272         HKS_KEY_RKC_PATH "/hks_client/key";
273     ASSERT_EQ(strlen(fileInfo.mainPath.path), strlen(expectPath)) << fileInfo.mainPath.path;
274     ASSERT_EQ(EOK, HksMemCmp(fileInfo.mainPath.path, expectPath, strlen(expectPath)));
275 }
276 }
277