• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021-2022 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 "key_agree_sdk_test.h"
17 
18 #include <gtest/gtest.h>
19 #include "key_agree_sdk.h"
20 #include "hc_types.h"
21 
22 using namespace std;
23 using namespace testing::ext;
24 
25 static const int32_t g_sessionIdClient = 1001;
26 static const int32_t g_sessionIdServer = 1002;
27 static const int32_t g_messageLength = 2048;
28 static const int32_t g_sharedSecretLength = 7;
29 static const int32_t g_deviceIdLength = 17;
30 static const int32_t g_sessionKeyLength = 64;
31 
32 static KeyAgreeSession *g_session;
33 static KeyAgreeSession *g_session_s;
34 static KeyAgreeBlob g_sharedSecret = { NULL, 0 };
35 static KeyAgreeBlob g_deviceId = { NULL, 0 };
36 static KeyAgreeBlob g_deviceId_s = { NULL, 0 };
37 static KeyAgreeBlob g_messageToTransmit1 = { NULL, 0 };
38 static KeyAgreeBlob g_messageToTransmit2 = { NULL, 0 };
39 static KeyAgreeBlob g_messageToTransmit3 = { NULL, 0 };
40 static KeyAgreeBlob g_messageToTransmit4 = { NULL, 0 };
41 static KeyAgreeBlob g_messageToTransmit5 = { NULL, 0 };
42 static KeyAgreeBlob g_sessionKey_s = { NULL, 0 };
43 static KeyAgreeBlob g_sessionKey = { NULL, 0 };
44 
45 class KeyAgreeInitSessionTest : public testing::Test {
46 public:
47     static void SetUpTestCase();
48     static void TearDownTestCase();
49     void SetUp();
50     void TearDown();
51 };
52 
SetUpTestCase()53 void KeyAgreeInitSessionTest::SetUpTestCase()
54 {
55     g_session = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
56     g_session_s = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
57 }
TearDownTestCase()58 void KeyAgreeInitSessionTest::TearDownTestCase()
59 {
60     HcFree(g_session);
61     HcFree(g_session_s);
62 }
SetUp()63 void KeyAgreeInitSessionTest::SetUp() {}
TearDown()64 void KeyAgreeInitSessionTest::TearDown()
65 {
66     KeyAgreeFreeSession(g_session);
67     KeyAgreeFreeSession(g_session_s);
68 }
69 
70 HWTEST_F(KeyAgreeInitSessionTest, KeyAgreeInitSessionTest001, TestSize.Level0)
71 {
72     g_session->sessionId = g_sessionIdClient;
73     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
74     EXPECT_EQ(res, KEYAGREE_SUCCESS);
75 }
76 
77 HWTEST_F(KeyAgreeInitSessionTest, KeyAgreeInitSessionTest002, TestSize.Level0)
78 {
79     g_session_s->sessionId = g_sessionIdServer;
80     KeyAgreeResult res = KeyAgreeInitSession(g_session_s, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_SERVER);
81     EXPECT_EQ(res, KEYAGREE_SUCCESS);
82 }
83 
84 HWTEST_F(KeyAgreeInitSessionTest, KeyAgreeInitSessionTest003, TestSize.Level0)
85 {
86     g_session->sessionId = g_sessionIdClient;
87     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_DL_SPEKE_256, KEYAGREE_TYPE_CLIENT);
88     EXPECT_EQ(res, KEYAGREE_SUCCESS);
89 }
90 
91 HWTEST_F(KeyAgreeInitSessionTest, KeyAgreeInitSessionTest004, TestSize.Level0)
92 {
93     g_session->sessionId = g_sessionIdClient;
94     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_DL_SPEKE_384, KEYAGREE_TYPE_CLIENT);
95     EXPECT_EQ(res, KEYAGREE_SUCCESS);
96 }
97 
98 HWTEST_F(KeyAgreeInitSessionTest, KeyAgreeInitSessionTest005, TestSize.Level0)
99 {
100     g_session->sessionId = g_sessionIdClient;
101     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_EC_SPEKE_P256, KEYAGREE_TYPE_CLIENT);
102     EXPECT_EQ(res, KEYAGREE_SUCCESS);
103 }
104 
105 HWTEST_F(KeyAgreeInitSessionTest, KeyAgreeInitSessionTest006, TestSize.Level0)
106 {
107     g_session->sessionId = g_sessionIdClient;
108     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_EC_SPEKE_X25519, KEYAGREE_TYPE_CLIENT);
109     EXPECT_EQ(res, KEYAGREE_SUCCESS);
110 }
111 
112 HWTEST_F(KeyAgreeInitSessionTest, KeyAgreeInitSessionTest007, TestSize.Level0)
113 {
114     g_session->sessionId = g_sessionIdClient;
115     KeyAgreeResult res = KeyAgreeInitSession(NULL, KEYAGREE_PROTOCOL_EC_SPEKE_X25519, KEYAGREE_TYPE_CLIENT);
116     EXPECT_NE(res, KEYAGREE_SUCCESS);
117 }
118 
119 class KeyAgreeStartSessionTest : public testing::Test {
120 public:
121     static void SetUpTestCase();
122     static void TearDownTestCase();
123     void SetUp();
124     void TearDown();
125 };
126 
SetUpTestCase()127 void KeyAgreeStartSessionTest::SetUpTestCase()
128 {
129     g_session = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
130     g_session_s = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
131 
132     g_sharedSecret.length = g_sharedSecretLength;
133     g_sharedSecret.data = (uint8_t *)HcMalloc(g_sharedSecret.length, 0);
134     (void)memcpy_s(g_sharedSecret.data, g_sharedSecret.length, (uint8_t *)"123456", g_sharedSecretLength);
135 
136     g_deviceId.length = g_deviceIdLength;
137     g_deviceId.data = (uint8_t *)HcMalloc(g_deviceId.length, 0);
138     (void)memcpy_s(g_deviceId.data, g_deviceId.length, (uint8_t *)"clientclientabcd", g_deviceIdLength);
139 
140     g_deviceId_s.length = g_deviceIdLength;
141     g_deviceId_s.data = (uint8_t *)HcMalloc(g_deviceId_s.length, 0);
142     (void)memcpy_s(g_deviceId_s.data, g_deviceId_s.length, (uint8_t *)"serverserverabcd", g_deviceIdLength);
143 }
144 
TearDownTestCase()145 void KeyAgreeStartSessionTest::TearDownTestCase()
146 {
147     HcFree(g_session);
148     HcFree(g_session_s);
149     HcFree(g_sharedSecret.data);
150     HcFree(g_deviceId.data);
151     HcFree(g_deviceId_s.data);
152 }
SetUp()153 void KeyAgreeStartSessionTest::SetUp() {}
TearDown()154 void KeyAgreeStartSessionTest::TearDown()
155 {
156     KeyAgreeFreeSession(g_session);
157     KeyAgreeFreeSession(g_session_s);
158 }
159 
160 HWTEST_F(KeyAgreeStartSessionTest, KeyAgreeStartSessionTest001, TestSize.Level0)
161 {
162     g_session->sessionId = g_sessionIdClient;
163     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
164     EXPECT_EQ(res, KEYAGREE_SUCCESS);
165 
166     res = KeyAgreeStartSession(g_session, &g_sharedSecret, &g_deviceId, NULL);
167     EXPECT_EQ(res, KEYAGREE_SUCCESS);
168 }
169 
170 HWTEST_F(KeyAgreeStartSessionTest, KeyAgreeStartSessionTest002, TestSize.Level0)
171 {
172     g_session->sessionId = g_sessionIdClient;
173     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
174     EXPECT_EQ(res, KEYAGREE_SUCCESS);
175 
176     res = KeyAgreeStartSession(g_session, &g_sharedSecret, NULL, NULL);
177     EXPECT_NE(res, KEYAGREE_SUCCESS);
178 }
179 
180 HWTEST_F(KeyAgreeStartSessionTest, KeyAgreeStartSessionTest003, TestSize.Level0)
181 {
182     g_session->sessionId = g_sessionIdClient;
183     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
184     EXPECT_EQ(res, KEYAGREE_SUCCESS);
185 
186     res = KeyAgreeStartSession(g_session, NULL, &g_deviceId, NULL);
187     EXPECT_NE(res, KEYAGREE_SUCCESS);
188 }
189 
190 HWTEST_F(KeyAgreeStartSessionTest, KeyAgreeStartSessionTest004, TestSize.Level0)
191 {
192     g_session->sessionId = g_sessionIdClient;
193     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
194     EXPECT_EQ(res, KEYAGREE_SUCCESS);
195 
196     res = KeyAgreeStartSession(NULL, &g_sharedSecret, &g_deviceId, NULL);
197     EXPECT_NE(res, KEYAGREE_SUCCESS);
198 }
199 
200 class KeyAgreeGenerateNextMessageTest : public testing::Test {
201 public:
202     static void SetUpTestCase();
203     static void TearDownTestCase();
204     void SetUp();
205     void TearDown();
206 };
207 
SetUpTestCase()208 void KeyAgreeGenerateNextMessageTest::SetUpTestCase()
209 {
210     g_session = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
211     g_session_s = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
212 
213     g_sharedSecret.length = g_sharedSecretLength;
214     g_sharedSecret.data = (uint8_t *)HcMalloc(g_sharedSecret.length, g_sharedSecretLength);
215     (void)memcpy_s(g_sharedSecret.data, g_sharedSecret.length, (uint8_t *)"123456", g_sharedSecretLength);
216 
217     g_deviceId.length = g_deviceIdLength;
218     g_deviceId.data = (uint8_t *)HcMalloc(g_deviceId.length, 0);
219     (void)memcpy_s(g_deviceId.data, g_deviceId.length, (uint8_t *)"clientclientabcd", g_deviceIdLength);
220 
221     g_deviceId_s.length = g_deviceIdLength;
222     g_deviceId_s.data = (uint8_t *)HcMalloc(g_deviceId_s.length, 0);
223     (void)memcpy_s(g_deviceId_s.data, g_deviceId_s.length, (uint8_t *)"serverserverabcd", g_deviceIdLength);
224 }
TearDownTestCase()225 void KeyAgreeGenerateNextMessageTest::TearDownTestCase()
226 {
227     HcFree(g_session);
228     HcFree(g_session_s);
229     HcFree(g_sharedSecret.data);
230     HcFree(g_deviceId.data);
231     HcFree(g_deviceId_s.data);
232 }
SetUp()233 void KeyAgreeGenerateNextMessageTest::SetUp()
234 {
235     g_messageToTransmit1.length = g_messageLength;
236     g_messageToTransmit1.data = (uint8_t *)HcMalloc(g_messageToTransmit1.length, 0);
237 
238     g_messageToTransmit2.length = g_messageLength;
239     g_messageToTransmit2.data = (uint8_t *)HcMalloc(g_messageToTransmit2.length, 0);
240 
241     g_messageToTransmit3.length = g_messageLength;
242     g_messageToTransmit3.data = (uint8_t *)HcMalloc(g_messageToTransmit3.length, 0);
243 
244     g_messageToTransmit4.length = g_messageLength;
245     g_messageToTransmit4.data = (uint8_t *)HcMalloc(g_messageToTransmit4.length, 0);
246 
247     g_messageToTransmit5.length = g_messageLength;
248     g_messageToTransmit5.data = (uint8_t *)HcMalloc(g_messageToTransmit5.length, 0);
249 
250     g_sessionKey_s.length = g_sessionKeyLength;
251     g_sessionKey_s.data = (uint8_t *)HcMalloc(g_sessionKey_s.length, 0);
252 
253     g_sessionKey.length = g_sessionKeyLength;
254     g_sessionKey.data = (uint8_t *)HcMalloc(g_sessionKey.length, 0);
255 }
TearDown()256 void KeyAgreeGenerateNextMessageTest::TearDown()
257 {
258     HcFree(g_messageToTransmit1.data);
259     HcFree(g_messageToTransmit2.data);
260     HcFree(g_messageToTransmit3.data);
261     HcFree(g_messageToTransmit4.data);
262     HcFree(g_messageToTransmit5.data);
263     HcFree(g_sessionKey_s.data);
264     HcFree(g_sessionKey.data);
265     KeyAgreeFreeSession(g_session);
266     KeyAgreeFreeSession(g_session_s);
267 }
268 
269 HWTEST_F(KeyAgreeGenerateNextMessageTest, KeyAgreeGenerateNextMessageTest001, TestSize.Level0)
270 {
271     g_session->sessionId = g_sessionIdClient;
272     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
273     EXPECT_EQ(res, KEYAGREE_SUCCESS);
274 
275     g_session_s->sessionId = g_sessionIdServer;
276     res = KeyAgreeInitSession(g_session_s, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_SERVER);
277     EXPECT_EQ(res, KEYAGREE_SUCCESS);
278 
279     res = KeyAgreeStartSession(g_session, &g_sharedSecret, &g_deviceId, NULL);
280     EXPECT_EQ(res, KEYAGREE_SUCCESS);
281 
282     res = KeyAgreeStartSession(g_session_s, &g_sharedSecret, &g_deviceId_s, NULL);
283     EXPECT_EQ(res, KEYAGREE_SUCCESS);
284 
285     res = KeyAgreeGenerateNextMessage(g_session, NULL, &g_messageToTransmit1);
286     EXPECT_EQ(res, KEYAGREE_SUCCESS);
287 
288     res = KeyAgreeIsFinish(g_session);
289     EXPECT_NE(res, KEYAGREE_SUCCESS);
290 
291     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit1, &g_messageToTransmit2);
292     EXPECT_EQ(res, KEYAGREE_SUCCESS);
293 
294     res = KeyAgreeIsFinish(g_session_s);
295     EXPECT_NE(res, KEYAGREE_SUCCESS);
296 
297     res = KeyAgreeGenerateNextMessage(g_session, &g_messageToTransmit2, &g_messageToTransmit3);
298     EXPECT_EQ(res, KEYAGREE_SUCCESS);
299 
300     res = KeyAgreeIsFinish(g_session);
301     EXPECT_NE(res, KEYAGREE_SUCCESS);
302 
303     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit3, &g_messageToTransmit4);
304     EXPECT_EQ(res, KEYAGREE_SUCCESS);
305 
306     res = KeyAgreeIsFinish(g_session_s);
307     EXPECT_EQ(res, KEYAGREE_SUCCESS);
308 
309     res = KeyAgreeGetResult(g_session_s, &g_sessionKey_s);
310     EXPECT_EQ(res, KEYAGREE_SUCCESS);
311 
312     res = KeyAgreeGenerateNextMessage(g_session, &g_messageToTransmit4, &g_messageToTransmit5);
313     EXPECT_EQ(res, KEYAGREE_SUCCESS);
314 
315     res = KeyAgreeIsFinish(g_session);
316     EXPECT_EQ(res, KEYAGREE_SUCCESS);
317 
318     res = KeyAgreeGetResult(g_session, &g_sessionKey);
319     EXPECT_EQ(res, KEYAGREE_SUCCESS);
320 
321     EXPECT_EQ(*(g_sessionKey.data), *(g_sessionKey_s.data));
322 }
323 
324 HWTEST_F(KeyAgreeGenerateNextMessageTest, KeyAgreeGenerateNextMessageTest002, TestSize.Level0)
325 {
326     g_session->sessionId = g_sessionIdClient;
327     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_DL_SPEKE_256, KEYAGREE_TYPE_CLIENT);
328     EXPECT_EQ(res, KEYAGREE_SUCCESS);
329 
330     g_session_s->sessionId = g_sessionIdServer;
331     res = KeyAgreeInitSession(g_session_s, KEYAGREE_PROTOCOL_DL_SPEKE_256, KEYAGREE_TYPE_SERVER);
332     EXPECT_EQ(res, KEYAGREE_SUCCESS);
333 
334     res = KeyAgreeStartSession(g_session, &g_sharedSecret, &g_deviceId, NULL);
335     EXPECT_EQ(res, KEYAGREE_SUCCESS);
336 
337     res = KeyAgreeStartSession(g_session_s, &g_sharedSecret, &g_deviceId_s, NULL);
338     EXPECT_EQ(res, KEYAGREE_SUCCESS);
339 
340     res = KeyAgreeGenerateNextMessage(g_session, NULL, &g_messageToTransmit1);
341     EXPECT_EQ(res, KEYAGREE_SUCCESS);
342 
343     res = KeyAgreeIsFinish(g_session);
344     EXPECT_NE(res, KEYAGREE_SUCCESS);
345 
346     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit1, &g_messageToTransmit2);
347     EXPECT_EQ(res, KEYAGREE_SUCCESS);
348 
349     res = KeyAgreeIsFinish(g_session_s);
350     EXPECT_NE(res, KEYAGREE_SUCCESS);
351 
352     res = KeyAgreeGenerateNextMessage(g_session, &g_messageToTransmit2, &g_messageToTransmit3);
353     EXPECT_EQ(res, KEYAGREE_SUCCESS);
354 
355     res = KeyAgreeIsFinish(g_session);
356     EXPECT_EQ(res, KEYAGREE_SUCCESS);
357 
358     res = KeyAgreeGetResult(g_session, &g_sessionKey);
359     EXPECT_EQ(res, KEYAGREE_SUCCESS);
360 
361     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit3, &g_messageToTransmit4);
362     EXPECT_EQ(res, KEYAGREE_SUCCESS);
363 
364     res = KeyAgreeIsFinish(g_session_s);
365     EXPECT_EQ(res, KEYAGREE_SUCCESS);
366 
367     res = KeyAgreeGetResult(g_session_s, &g_sessionKey_s);
368     EXPECT_EQ(res, KEYAGREE_SUCCESS);
369 
370     EXPECT_EQ(*(g_sessionKey.data), *(g_sessionKey_s.data));
371 }
372 
373 HWTEST_F(KeyAgreeGenerateNextMessageTest, KeyAgreeGenerateNextMessageTest003, TestSize.Level0)
374 {
375     g_session->sessionId = g_sessionIdClient;
376     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_DL_SPEKE_384, KEYAGREE_TYPE_CLIENT);
377     EXPECT_EQ(res, KEYAGREE_SUCCESS);
378 
379     g_session_s->sessionId = g_sessionIdServer;
380     res = KeyAgreeInitSession(g_session_s, KEYAGREE_PROTOCOL_DL_SPEKE_384, KEYAGREE_TYPE_SERVER);
381     EXPECT_EQ(res, KEYAGREE_SUCCESS);
382 
383     res = KeyAgreeStartSession(g_session, &g_sharedSecret, &g_deviceId, NULL);
384     EXPECT_EQ(res, KEYAGREE_SUCCESS);
385 
386     res = KeyAgreeStartSession(g_session_s, &g_sharedSecret, &g_deviceId_s, NULL);
387     EXPECT_EQ(res, KEYAGREE_SUCCESS);
388 
389     res = KeyAgreeGenerateNextMessage(g_session, NULL, &g_messageToTransmit1);
390     EXPECT_EQ(res, KEYAGREE_SUCCESS);
391 
392     res = KeyAgreeIsFinish(g_session);
393     EXPECT_NE(res, KEYAGREE_SUCCESS);
394 
395     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit1, &g_messageToTransmit2);
396     EXPECT_EQ(res, KEYAGREE_SUCCESS);
397 
398     res = KeyAgreeIsFinish(g_session_s);
399     EXPECT_NE(res, KEYAGREE_SUCCESS);
400 
401     res = KeyAgreeGenerateNextMessage(g_session, &g_messageToTransmit2, &g_messageToTransmit3);
402     EXPECT_EQ(res, KEYAGREE_SUCCESS);
403 
404     res = KeyAgreeIsFinish(g_session);
405     EXPECT_EQ(res, KEYAGREE_SUCCESS);
406 
407     res = KeyAgreeGetResult(g_session, &g_sessionKey);
408     EXPECT_EQ(res, KEYAGREE_SUCCESS);
409 
410     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit3, &g_messageToTransmit4);
411     EXPECT_EQ(res, KEYAGREE_SUCCESS);
412 
413     res = KeyAgreeIsFinish(g_session_s);
414     EXPECT_EQ(res, KEYAGREE_SUCCESS);
415 
416     res = KeyAgreeGetResult(g_session_s, &g_sessionKey_s);
417     EXPECT_EQ(res, KEYAGREE_SUCCESS);
418 
419     EXPECT_EQ(*(g_sessionKey.data), *(g_sessionKey_s.data));
420 }
421 
422 HWTEST_F(KeyAgreeGenerateNextMessageTest, KeyAgreeGenerateNextMessageTest004, TestSize.Level0)
423 {
424     g_session->sessionId = g_sessionIdClient;
425     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_EC_SPEKE_P256, KEYAGREE_TYPE_CLIENT);
426     EXPECT_EQ(res, KEYAGREE_SUCCESS);
427 
428     g_session_s->sessionId = g_sessionIdServer;
429     res = KeyAgreeInitSession(g_session_s, KEYAGREE_PROTOCOL_EC_SPEKE_P256, KEYAGREE_TYPE_SERVER);
430     EXPECT_EQ(res, KEYAGREE_SUCCESS);
431 
432     res = KeyAgreeStartSession(g_session, &g_sharedSecret, &g_deviceId, NULL);
433     EXPECT_EQ(res, KEYAGREE_SUCCESS);
434 
435     res = KeyAgreeStartSession(g_session_s, &g_sharedSecret, &g_deviceId_s, NULL);
436     EXPECT_EQ(res, KEYAGREE_SUCCESS);
437 
438     res = KeyAgreeGenerateNextMessage(g_session, NULL, &g_messageToTransmit1);
439     EXPECT_EQ(res, KEYAGREE_SUCCESS);
440 
441     res = KeyAgreeIsFinish(g_session);
442     EXPECT_NE(res, KEYAGREE_SUCCESS);
443 
444     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit1, &g_messageToTransmit2);
445     EXPECT_EQ(res, KEYAGREE_SUCCESS);
446 
447     res = KeyAgreeIsFinish(g_session_s);
448     EXPECT_NE(res, KEYAGREE_SUCCESS);
449 
450     res = KeyAgreeGenerateNextMessage(g_session, &g_messageToTransmit2, &g_messageToTransmit3);
451     EXPECT_EQ(res, KEYAGREE_SUCCESS);
452 
453     res = KeyAgreeIsFinish(g_session);
454     EXPECT_EQ(res, KEYAGREE_SUCCESS);
455 
456     res = KeyAgreeGetResult(g_session, &g_sessionKey);
457     EXPECT_EQ(res, KEYAGREE_SUCCESS);
458 
459     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit3, &g_messageToTransmit4);
460     EXPECT_EQ(res, KEYAGREE_SUCCESS);
461 
462     res = KeyAgreeIsFinish(g_session_s);
463     EXPECT_EQ(res, KEYAGREE_SUCCESS);
464 
465     res = KeyAgreeGetResult(g_session_s, &g_sessionKey_s);
466     EXPECT_EQ(res, KEYAGREE_SUCCESS);
467 
468     EXPECT_EQ(*(g_sessionKey.data), *(g_sessionKey_s.data));
469 }
470 
471 HWTEST_F(KeyAgreeGenerateNextMessageTest, KeyAgreeGenerateNextMessageTest005, TestSize.Level0)
472 {
473     g_session->sessionId = g_sessionIdClient;
474     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_EC_SPEKE_X25519, KEYAGREE_TYPE_CLIENT);
475     EXPECT_EQ(res, KEYAGREE_SUCCESS);
476 
477     g_session_s->sessionId = g_sessionIdServer;
478     res = KeyAgreeInitSession(g_session_s, KEYAGREE_PROTOCOL_EC_SPEKE_X25519, KEYAGREE_TYPE_SERVER);
479     EXPECT_EQ(res, KEYAGREE_SUCCESS);
480 
481     res = KeyAgreeStartSession(g_session, &g_sharedSecret, &g_deviceId, NULL);
482     EXPECT_EQ(res, KEYAGREE_SUCCESS);
483 
484     res = KeyAgreeStartSession(g_session_s, &g_sharedSecret, &g_deviceId_s, NULL);
485     EXPECT_EQ(res, KEYAGREE_SUCCESS);
486 
487     res = KeyAgreeGenerateNextMessage(g_session, NULL, &g_messageToTransmit1);
488     EXPECT_EQ(res, KEYAGREE_SUCCESS);
489 
490     res = KeyAgreeIsFinish(g_session);
491     EXPECT_NE(res, KEYAGREE_SUCCESS);
492 
493     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit1, &g_messageToTransmit2);
494     EXPECT_EQ(res, KEYAGREE_SUCCESS);
495 
496     res = KeyAgreeIsFinish(g_session_s);
497     EXPECT_NE(res, KEYAGREE_SUCCESS);
498 
499     res = KeyAgreeGenerateNextMessage(g_session, &g_messageToTransmit2, &g_messageToTransmit3);
500     EXPECT_EQ(res, KEYAGREE_SUCCESS);
501 
502     res = KeyAgreeIsFinish(g_session);
503     EXPECT_EQ(res, KEYAGREE_SUCCESS);
504 
505     res = KeyAgreeGetResult(g_session, &g_sessionKey);
506     EXPECT_EQ(res, KEYAGREE_SUCCESS);
507 
508     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit3, &g_messageToTransmit4);
509     EXPECT_EQ(res, KEYAGREE_SUCCESS);
510 
511     res = KeyAgreeIsFinish(g_session_s);
512     EXPECT_EQ(res, KEYAGREE_SUCCESS);
513 
514     res = KeyAgreeGetResult(g_session_s, &g_sessionKey_s);
515     EXPECT_EQ(res, KEYAGREE_SUCCESS);
516 
517     EXPECT_EQ(*(g_sessionKey.data), *(g_sessionKey_s.data));
518 }
519 
520 HWTEST_F(KeyAgreeGenerateNextMessageTest, KeyAgreeGenerateNextMessageTest006, TestSize.Level0)
521 {
522     g_session->sessionId = g_sessionIdClient;
523     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_EC_SPEKE_X25519, KEYAGREE_TYPE_CLIENT);
524     EXPECT_EQ(res, KEYAGREE_SUCCESS);
525 
526     g_session_s->sessionId = g_sessionIdServer;
527     res = KeyAgreeInitSession(g_session_s, KEYAGREE_PROTOCOL_EC_SPEKE_P256, KEYAGREE_TYPE_SERVER);
528     EXPECT_EQ(res, KEYAGREE_SUCCESS);
529 
530     res = KeyAgreeStartSession(g_session, &g_sharedSecret, &g_deviceId, NULL);
531     EXPECT_EQ(res, KEYAGREE_SUCCESS);
532 
533     res = KeyAgreeStartSession(g_session_s, &g_sharedSecret, &g_deviceId_s, NULL);
534     EXPECT_EQ(res, KEYAGREE_SUCCESS);
535 
536     res = KeyAgreeGenerateNextMessage(g_session, NULL, &g_messageToTransmit1);
537     EXPECT_EQ(res, KEYAGREE_SUCCESS);
538 
539     res = KeyAgreeIsFinish(g_session);
540     EXPECT_NE(res, KEYAGREE_SUCCESS);
541 
542     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit1, &g_messageToTransmit2);
543     EXPECT_NE(res, KEYAGREE_SUCCESS);
544 
545     res = KeyAgreeIsFinish(g_session_s);
546     EXPECT_NE(res, KEYAGREE_SUCCESS);
547 
548     res = KeyAgreeGenerateNextMessage(g_session, &g_messageToTransmit2, &g_messageToTransmit3);
549     EXPECT_NE(res, KEYAGREE_SUCCESS);
550 
551     res = KeyAgreeIsFinish(g_session);
552     EXPECT_NE(res, KEYAGREE_SUCCESS);
553 }
554 
555 class KeyAgreeIsFinishTest : public testing::Test {
556 public:
557     static void SetUpTestCase();
558     static void TearDownTestCase();
559     void SetUp();
560     void TearDown();
561 };
562 
SetUpTestCase()563 void KeyAgreeIsFinishTest::SetUpTestCase()
564 {
565     g_session = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
566     g_session_s = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
567 }
TearDownTestCase()568 void KeyAgreeIsFinishTest::TearDownTestCase()
569 {
570     HcFree(g_session);
571     HcFree(g_session_s);
572 }
SetUp()573 void KeyAgreeIsFinishTest::SetUp() {}
TearDown()574 void KeyAgreeIsFinishTest::TearDown()
575 {
576     KeyAgreeFreeSession(g_session);
577     KeyAgreeFreeSession(g_session_s);
578 }
579 
580 HWTEST_F(KeyAgreeIsFinishTest, KeyAgreeIsFinishTest001, TestSize.Level0)
581 {
582     g_session->sessionId = g_sessionIdClient;
583     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
584     EXPECT_EQ(res, KEYAGREE_SUCCESS);
585 
586     res = KeyAgreeIsFinish(g_session);
587     EXPECT_NE(res, KEYAGREE_SUCCESS);
588 }
589 
590 HWTEST_F(KeyAgreeIsFinishTest, KeyAgreeIsFinishTest002, TestSize.Level0)
591 {
592     g_session->sessionId = g_sessionIdClient;
593     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
594     EXPECT_EQ(res, KEYAGREE_SUCCESS);
595 
596     res = KeyAgreeIsFinish(NULL);
597     EXPECT_NE(res, KEYAGREE_SUCCESS);
598 }
599 
600 class KeyAgreeGetResultTest : public testing::Test {
601 public:
602     static void SetUpTestCase();
603     static void TearDownTestCase();
604     void SetUp();
605     void TearDown();
606 };
607 
SetUpTestCase()608 void KeyAgreeGetResultTest::SetUpTestCase()
609 {
610     g_session = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
611     g_session_s = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
612 
613     g_sessionKey_s.length = g_sessionKeyLength;
614     g_sessionKey_s.data = (uint8_t *)HcMalloc(g_sessionKey_s.length, 0);
615 
616     g_sessionKey.length = g_sessionKeyLength;
617     g_sessionKey.data = (uint8_t *)HcMalloc(g_sessionKey.length, 0);
618 }
TearDownTestCase()619 void KeyAgreeGetResultTest::TearDownTestCase()
620 {
621     HcFree(g_session);
622     HcFree(g_session_s);
623     HcFree(g_sessionKey_s.data);
624     HcFree(g_sessionKey.data);
625 }
SetUp()626 void KeyAgreeGetResultTest::SetUp() {}
TearDown()627 void KeyAgreeGetResultTest::TearDown()
628 {
629     KeyAgreeFreeSession(g_session);
630     KeyAgreeFreeSession(g_session_s);
631 }
632 
633 HWTEST_F(KeyAgreeGetResultTest, KeyAgreeGetResultTest001, TestSize.Level0)
634 {
635     g_session->sessionId = g_sessionIdClient;
636     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
637     EXPECT_EQ(res, KEYAGREE_SUCCESS);
638 
639     res = KeyAgreeGetResult(g_session, NULL);
640     EXPECT_NE(res, KEYAGREE_SUCCESS);
641 }
642 
643 HWTEST_F(KeyAgreeGetResultTest, KeyAgreeGetResultTest002, TestSize.Level0)
644 {
645     g_session->sessionId = g_sessionIdClient;
646     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
647     EXPECT_EQ(res, KEYAGREE_SUCCESS);
648 
649     res = KeyAgreeGetResult(g_session, &g_sessionKey);
650     EXPECT_NE(res, KEYAGREE_SUCCESS);
651 }
652 
653 class KeyAgreeFreeSessionTest : public testing::Test {
654 public:
655     static void SetUpTestCase();
656     static void TearDownTestCase();
657     void SetUp();
658     void TearDown();
659 };
660 
SetUpTestCase()661 void KeyAgreeFreeSessionTest::SetUpTestCase()
662 {
663     g_session = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
664     g_session_s = (KeyAgreeSession *)HcMalloc(sizeof(KeyAgreeSession), 0);
665 
666     g_sharedSecret.length = g_sharedSecretLength;
667     g_sharedSecret.data = (uint8_t *)HcMalloc(g_sharedSecret.length, 0);
668     (void)memcpy_s(g_sharedSecret.data, g_sharedSecret.length, (uint8_t *)"123456", g_sharedSecretLength);
669 
670     g_deviceId.length = g_deviceIdLength;
671     g_deviceId.data = (uint8_t *)HcMalloc(g_deviceId.length, 0);
672     (void)memcpy_s(g_deviceId.data, g_deviceId.length, (uint8_t *)"clientclientabcd", g_deviceIdLength);
673 
674     g_deviceId_s.length = g_deviceIdLength;
675     g_deviceId_s.data = (uint8_t *)HcMalloc(g_deviceId_s.length, 0);
676     (void)memcpy_s(g_deviceId_s.data, g_deviceId_s.length, (uint8_t *)"serverserverabcd", g_deviceIdLength);
677 
678     g_messageToTransmit1.length = g_messageLength;
679     g_messageToTransmit1.data = (uint8_t *)HcMalloc(g_messageToTransmit1.length, 0);
680 
681     g_messageToTransmit2.length = g_messageLength;
682     g_messageToTransmit2.data = (uint8_t *)HcMalloc(g_messageToTransmit2.length, 0);
683 
684     g_messageToTransmit3.length = g_messageLength;
685     g_messageToTransmit3.data = (uint8_t *)HcMalloc(g_messageToTransmit3.length, 0);
686 
687     g_messageToTransmit4.length = g_messageLength;
688     g_messageToTransmit4.data = (uint8_t *)HcMalloc(g_messageToTransmit4.length, 0);
689 
690     g_messageToTransmit5.length = g_messageLength;
691     g_messageToTransmit5.data = (uint8_t *)HcMalloc(g_messageToTransmit5.length, 0);
692 
693     g_sessionKey_s.length = g_sessionKeyLength;
694     g_sessionKey_s.data = (uint8_t *)HcMalloc(g_sessionKey_s.length, 0);
695 
696     g_sessionKey.length = g_sessionKeyLength;
697     g_sessionKey.data = (uint8_t *)HcMalloc(g_sessionKey.length, 0);
698 }
TearDownTestCase()699 void KeyAgreeFreeSessionTest::TearDownTestCase()
700 {
701     HcFree(g_session);
702     HcFree(g_session_s);
703     HcFree(g_sharedSecret.data);
704     HcFree(g_deviceId.data);
705     HcFree(g_deviceId_s.data);
706     HcFree(g_messageToTransmit1.data);
707     HcFree(g_messageToTransmit2.data);
708     HcFree(g_messageToTransmit3.data);
709     HcFree(g_messageToTransmit4.data);
710     HcFree(g_messageToTransmit5.data);
711     HcFree(g_sessionKey_s.data);
712     HcFree(g_sessionKey.data);
713 }
SetUp()714 void KeyAgreeFreeSessionTest::SetUp() {}
TearDown()715 void KeyAgreeFreeSessionTest::TearDown()
716 {
717     KeyAgreeFreeSession(g_session);
718     KeyAgreeFreeSession(g_session_s);
719 }
720 
721 HWTEST_F(KeyAgreeFreeSessionTest, KeyAgreeFreeSessionTest001, TestSize.Level0)
722 {
723     g_session->sessionId = g_sessionIdClient;
724     KeyAgreeResult res = KeyAgreeInitSession(g_session, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_CLIENT);
725     EXPECT_EQ(res, KEYAGREE_SUCCESS);
726 
727     g_session_s->sessionId = g_sessionIdServer;
728     res = KeyAgreeInitSession(g_session_s, KEYAGREE_PROTOCOL_ANY, KEYAGREE_TYPE_SERVER);
729     EXPECT_EQ(res, KEYAGREE_SUCCESS);
730 
731     res = KeyAgreeStartSession(g_session, &g_sharedSecret, &g_deviceId, NULL);
732     EXPECT_EQ(res, KEYAGREE_SUCCESS);
733 
734     res = KeyAgreeStartSession(g_session_s, &g_sharedSecret, &g_deviceId_s, NULL);
735     EXPECT_EQ(res, KEYAGREE_SUCCESS);
736 
737     res = KeyAgreeGenerateNextMessage(g_session, NULL, &g_messageToTransmit1);
738     EXPECT_EQ(res, KEYAGREE_SUCCESS);
739 
740     res = KeyAgreeIsFinish(g_session);
741     EXPECT_NE(res, KEYAGREE_SUCCESS);
742 
743     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit1, &g_messageToTransmit2);
744     EXPECT_EQ(res, KEYAGREE_SUCCESS);
745 
746     res = KeyAgreeIsFinish(g_session_s);
747     EXPECT_NE(res, KEYAGREE_SUCCESS);
748 
749     res = KeyAgreeGenerateNextMessage(g_session, &g_messageToTransmit2, &g_messageToTransmit3);
750     EXPECT_EQ(res, KEYAGREE_SUCCESS);
751 
752     res = KeyAgreeIsFinish(g_session);
753     EXPECT_NE(res, KEYAGREE_SUCCESS);
754 
755     res = KeyAgreeGenerateNextMessage(g_session_s, &g_messageToTransmit3, &g_messageToTransmit4);
756     EXPECT_EQ(res, KEYAGREE_SUCCESS);
757 
758     res = KeyAgreeIsFinish(g_session_s);
759     EXPECT_EQ(res, KEYAGREE_SUCCESS);
760 
761     res = KeyAgreeGetResult(g_session_s, &g_sessionKey_s);
762     EXPECT_EQ(res, KEYAGREE_SUCCESS);
763 
764     res = KeyAgreeGenerateNextMessage(g_session, &g_messageToTransmit4, &g_messageToTransmit5);
765     EXPECT_EQ(res, KEYAGREE_SUCCESS);
766 
767     res = KeyAgreeIsFinish(g_session);
768     EXPECT_EQ(res, KEYAGREE_SUCCESS);
769 
770     res = KeyAgreeGetResult(g_session, &g_sessionKey);
771     EXPECT_EQ(res, KEYAGREE_SUCCESS);
772 
773     EXPECT_EQ(*(g_sessionKey.data), *(g_sessionKey_s.data));
774 
775     KeyAgreeFreeSession(g_session);
776     KeyAgreeFreeSession(g_session_s);
777 
778     res = KeyAgreeGetResult(g_session, &g_sessionKey);
779     EXPECT_NE(res, KEYAGREE_SUCCESS);
780 
781     res = KeyAgreeGetResult(g_session_s, &g_sessionKey_s);
782     EXPECT_NE(res, KEYAGREE_SUCCESS);
783 }