• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 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 <cinttypes>
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include "device_auth_defines.h"
20 #include "hc_dev_info_mock.h"
21 #include "mk_agree_task.h"
22 #include "device_auth.h"
23 #include "key_manager.h"
24 #include "string_util.h"
25 
26 using namespace std;
27 using namespace testing::ext;
28 
29 namespace {
30 #define PAKE_X25519_KEY_PAIR_LEN 32
31 #define TEST_DEVICE_ID "TestAuthId"
32 #define TEST_DEVICE_ID2 "TestAuthId2"
33 #define TEST_REAL_INFO "37364761534f454d33567a73424e794f33573330507069434b31676f7254706b"
34 #define TEST_INDEX_KEY "DCBA4321"
35 #define TEST_DEV_PK "87364761534f454d33567a73424e794f"
36 #define BYTE_TO_HEX_OPER_LENGTH 2
37 
38 static const std::string TEST_HKS_DATA_PATH = "/data/service/el1/public/huks_service/tmp/+0+0+0+0";
39 static const std::string TEST_GROUP_DATA_PATH = "/data/service/el1/public/deviceauthMock";
40 static const int TEST_DEV_AUTH_BUFFER_SIZE = 128;
41 
42 class MKAgreeTest : public testing::Test {
43 public:
44     static void SetUpTestCase();
45     static void TearDownTestCase();
46     void SetUp();
47     void TearDown();
48 };
49 
SetUpTestCase()50 void MKAgreeTest::SetUpTestCase() {}
TearDownTestCase()51 void MKAgreeTest::TearDownTestCase() {}
52 
RemoveDir(const char * path)53 static void RemoveDir(const char *path)
54 {
55     char strBuf[TEST_DEV_AUTH_BUFFER_SIZE] = { 0 };
56     if (path == nullptr) {
57         return;
58     }
59     if (sprintf_s(strBuf, sizeof(strBuf) - 1, "rm -rf %s", path) < 0) {
60         return;
61     }
62     system(strBuf);
63 }
64 
DeleteDatabase()65 static void DeleteDatabase()
66 {
67     RemoveDir(TEST_GROUP_DATA_PATH.c_str());
68     RemoveDir(TEST_HKS_DATA_PATH.c_str());
69 }
70 
SetUp()71 void MKAgreeTest::SetUp()
72 {
73     DeleteDatabase();
74     int32_t ret = InitDeviceAuthService();
75     ASSERT_EQ(ret, HC_SUCCESS);
76 }
77 
TearDown()78 void MKAgreeTest::TearDown()
79 {
80     DestroyDeviceAuthService();
81 }
82 
83 HWTEST_F(MKAgreeTest, GenerateDeviceKeyPairTest001, TestSize.Level0)
84 {
85     int32_t ret = GenerateDeviceKeyPair();
86     EXPECT_EQ(ret, HC_SUCCESS);
87 }
88 
89 HWTEST_F(MKAgreeTest, GenerateMkTest001, TestSize.Level0)
90 {
91     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
92     uint8_t *peerDevPkVal = static_cast<uint8_t *>(HcMalloc(peerDevPkLen, 0));
93     ASSERT_NE(peerDevPkVal, nullptr);
94     int32_t ret = HexStringToByte(TEST_DEV_PK, peerDevPkVal, peerDevPkLen);
95     EXPECT_EQ(ret, HC_SUCCESS);
96     Uint8Buff peerPkBuff = { peerDevPkVal, peerDevPkLen };
97     ret = GenerateMk(TEST_DEVICE_ID, &peerPkBuff);
98     HcFree(peerDevPkVal);
99     EXPECT_NE(ret, HC_SUCCESS);
100     ret = DeleteMk(TEST_DEVICE_ID);
101     EXPECT_EQ(ret, HC_SUCCESS);
102 }
103 
104 HWTEST_F(MKAgreeTest, GenerateMkTest002, TestSize.Level0)
105 {
106     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
107     Uint8Buff peerPkBuff = { nullptr, peerDevPkLen };
108     int32_t ret = GenerateMk(TEST_DEVICE_ID, &peerPkBuff);
109     EXPECT_NE(ret, HC_SUCCESS);
110 }
111 
112 HWTEST_F(MKAgreeTest, GenerateMkTest003, TestSize.Level0)
113 {
114     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
115     uint8_t *peerDevPkVal = static_cast<uint8_t *>(HcMalloc(peerDevPkLen, 0));
116     ASSERT_NE(peerDevPkVal, nullptr);
117     int32_t ret = HexStringToByte(TEST_DEV_PK, peerDevPkVal, peerDevPkLen);
118     EXPECT_EQ(ret, HC_SUCCESS);
119     Uint8Buff peerPkBuff = { peerDevPkVal, 0 };
120     ret = GenerateMk(TEST_DEVICE_ID, &peerPkBuff);
121     HcFree(peerDevPkVal);
122     EXPECT_NE(ret, HC_SUCCESS);
123 }
124 
125 HWTEST_F(MKAgreeTest, GenerateMkTest004, TestSize.Level0)
126 {
127     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
128     uint8_t *peerDevPkVal = static_cast<uint8_t *>(HcMalloc(peerDevPkLen, 0));
129     ASSERT_NE(peerDevPkVal, nullptr);
130     int32_t ret = HexStringToByte(TEST_DEV_PK, peerDevPkVal, peerDevPkLen);
131     EXPECT_EQ(ret, HC_SUCCESS);
132     Uint8Buff peerPkBuff = { peerDevPkVal, peerDevPkLen };
133     ret = GenerateMk(nullptr, &peerPkBuff);
134     HcFree(peerDevPkVal);
135     EXPECT_NE(ret, HC_SUCCESS);
136 }
137 
138 HWTEST_F(MKAgreeTest, DeleteMkTest001, TestSize.Level0)
139 {
140     int32_t ret = DeleteMk(TEST_DEVICE_ID2);
141     EXPECT_EQ(ret, HC_SUCCESS);
142     ret = DeleteMk(nullptr);
143     EXPECT_NE(ret, HC_SUCCESS);
144     ret = DeleteMk("");
145     EXPECT_NE(ret, HC_SUCCESS);
146 }
147 
148 HWTEST_F(MKAgreeTest, GeneratePseudonymPskTest001, TestSize.Level0)
149 {
150     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
151     uint8_t *peerDevPkVal = static_cast<uint8_t *>(HcMalloc(peerDevPkLen, 0));
152     ASSERT_NE(peerDevPkVal, nullptr);
153     int32_t ret = HexStringToByte(TEST_DEV_PK, peerDevPkVal, peerDevPkLen);
154     EXPECT_EQ(ret, HC_SUCCESS);
155     Uint8Buff salt = { peerDevPkVal, peerDevPkLen };
156     ret = GeneratePseudonymPsk(TEST_DEVICE_ID, &salt);
157     HcFree(peerDevPkVal);
158     EXPECT_NE(ret, HC_SUCCESS);
159 
160     ret = DeletePseudonymPsk(TEST_DEVICE_ID);
161     EXPECT_EQ(ret, HC_SUCCESS);
162 }
163 
164 HWTEST_F(MKAgreeTest, GeneratePseudonymPskTest002, TestSize.Level0)
165 {
166     Uint8Buff salt = { nullptr, 0 };
167     int32_t ret = GeneratePseudonymPsk(TEST_DEVICE_ID, &salt);
168     EXPECT_NE(ret, HC_SUCCESS);
169 }
170 
171 HWTEST_F(MKAgreeTest, GeneratePseudonymPskTest003, TestSize.Level0)
172 {
173     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
174     uint8_t *peerDevPkVal = static_cast<uint8_t *>(HcMalloc(peerDevPkLen, 0));
175     ASSERT_NE(peerDevPkVal, nullptr);
176     int32_t ret = HexStringToByte(TEST_DEV_PK, peerDevPkVal, peerDevPkLen);
177     EXPECT_EQ(ret, HC_SUCCESS);
178     Uint8Buff salt = { peerDevPkVal, peerDevPkLen };
179     ret = GeneratePseudonymPsk(nullptr, &salt);
180     HcFree(peerDevPkVal);
181     EXPECT_NE(ret, HC_SUCCESS);
182 }
183 
184 HWTEST_F(MKAgreeTest, GenerateAndSavePseudonymIdTest001, TestSize.Level0)
185 {
186     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
187     uint8_t *peerDevPkVal = static_cast<uint8_t *>(HcMalloc(peerDevPkLen, 0));
188     ASSERT_NE(peerDevPkVal, nullptr);
189     int32_t ret = HexStringToByte(TEST_DEV_PK, peerDevPkVal, peerDevPkLen);
190     EXPECT_EQ(ret, HC_SUCCESS);
191     Uint8Buff salt = { peerDevPkVal, peerDevPkLen };
192     Uint8Buff tmpKcfData = { NULL, 0 };
193     PseudonymKeyInfo info = { TEST_REAL_INFO, TEST_INDEX_KEY };
194     ret = GenerateAndSavePseudonymId(DEFAULT_OS_ACCOUNT, TEST_DEVICE_ID,
195         &info, &salt, &tmpKcfData);
196     HcFree(peerDevPkVal);
197     EXPECT_NE(ret, HC_SUCCESS);
198 }
199 
200 HWTEST_F(MKAgreeTest, GenerateAndSavePseudonymIdTest002, TestSize.Level0)
201 {
202     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
203     uint8_t *peerDevPkVal = static_cast<uint8_t *>(HcMalloc(peerDevPkLen, 0));
204     ASSERT_NE(peerDevPkVal, nullptr);
205     int32_t ret = HexStringToByte(TEST_DEV_PK, peerDevPkVal, peerDevPkLen);
206     EXPECT_EQ(ret, HC_SUCCESS);
207     Uint8Buff salt = { peerDevPkVal, peerDevPkLen };
208     Uint8Buff tmpKcfData = { NULL, 0 };
209     PseudonymKeyInfo info = { TEST_REAL_INFO, TEST_INDEX_KEY };
210     ret = GenerateAndSavePseudonymId(DEFAULT_OS_ACCOUNT, nullptr,
211         &info, &salt, &tmpKcfData);
212     HcFree(peerDevPkVal);
213     EXPECT_NE(ret, HC_SUCCESS);
214 }
215 
216 HWTEST_F(MKAgreeTest, GenerateAndSavePseudonymIdTest003, TestSize.Level0)
217 {
218     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
219     uint8_t *peerDevPkVal = static_cast<uint8_t *>(HcMalloc(peerDevPkLen, 0));
220     ASSERT_NE(peerDevPkVal, nullptr);
221     int32_t ret = HexStringToByte(TEST_DEV_PK, peerDevPkVal, peerDevPkLen);
222     EXPECT_EQ(ret, HC_SUCCESS);
223     Uint8Buff salt = { peerDevPkVal, peerDevPkLen };
224     Uint8Buff tmpKcfData = { NULL, 0 };
225     PseudonymKeyInfo info = { nullptr, nullptr };
226     ret = GenerateAndSavePseudonymId(DEFAULT_OS_ACCOUNT, TEST_DEVICE_ID,
227         &info, &salt, &tmpKcfData);
228     HcFree(peerDevPkVal);
229     EXPECT_NE(ret, HC_SUCCESS);
230 }
231 
232 HWTEST_F(MKAgreeTest, GenerateAndSavePseudonymIdTest004, TestSize.Level0)
233 {
234     uint32_t peerDevPkLen = HcStrlen(TEST_DEV_PK) / BYTE_TO_HEX_OPER_LENGTH;
235     uint8_t *peerDevPkVal = static_cast<uint8_t *>(HcMalloc(peerDevPkLen, 0));
236     ASSERT_NE(peerDevPkVal, nullptr);
237     int32_t ret = HexStringToByte(TEST_DEV_PK, peerDevPkVal, peerDevPkLen);
238     EXPECT_EQ(ret, HC_SUCCESS);
239     Uint8Buff salt = { peerDevPkVal, 0 };
240     Uint8Buff tmpKcfData = { NULL, 0 };
241     PseudonymKeyInfo info = { TEST_REAL_INFO, TEST_INDEX_KEY };
242     ret = GenerateAndSavePseudonymId(DEFAULT_OS_ACCOUNT, TEST_DEVICE_ID,
243         &info, &salt, &tmpKcfData);
244     HcFree(peerDevPkVal);
245     EXPECT_NE(ret, HC_SUCCESS);
246 }
247 
248 HWTEST_F(MKAgreeTest, GenerateAndSavePseudonymIdTest005, TestSize.Level0)
249 {
250     Uint8Buff salt = { nullptr, 0 };
251     Uint8Buff tmpKcfData = { NULL, 0 };
252     PseudonymKeyInfo info = { TEST_REAL_INFO, TEST_INDEX_KEY };
253     int32_t ret = GenerateAndSavePseudonymId(DEFAULT_OS_ACCOUNT, TEST_DEVICE_ID,
254         &info, &salt, &tmpKcfData);
255     EXPECT_NE(ret, HC_SUCCESS);
256 }
257 
258 HWTEST_F(MKAgreeTest, GetDevicePubKeyTest001, TestSize.Level0)
259 {
260     int32_t ret = GenerateDeviceKeyPair();
261     EXPECT_EQ(ret, HC_SUCCESS);
262     Uint8Buff devicePk = { NULL, 0 };
263     ret = InitUint8Buff(&devicePk, PAKE_X25519_KEY_PAIR_LEN);
264     EXPECT_EQ(ret, HC_SUCCESS);
265     ret = GetDevicePubKey(&devicePk);
266     EXPECT_EQ(ret, HC_SUCCESS);
267     FreeUint8Buff(&devicePk);
268 }
269 
270 HWTEST_F(MKAgreeTest, GetDevicePubKeyTest002, TestSize.Level0)
271 {
272     Uint8Buff devicePk = { NULL, 0 };
273     int32_t ret = GetDevicePubKey(&devicePk);
274     EXPECT_NE(ret, HC_SUCCESS);
275 }
276 }