1 /*
2 * Copyright (C) 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 <cinttypes>
17 #include <unistd.h>
18 #include <gtest/gtest.h>
19 #include "alg_loader.h"
20 #include "device_auth_defines.h"
21 #include "securec.h"
22 #include "identity_defines.h"
23
24 extern "C"{
25 #include "common_standard_bind_exchange.h"
26 }
27
28 using namespace std;
29 using namespace testing::ext;
30
31 namespace {
32 static const std::string TEST_APP_ID = "TestAppId";
33 static const std::string TEST_GROUP_ID = "TestGroupId";
34 static const std::string TEST_AUTH_ID = "TestAuthId";
35 static const std::string TEST_SALT = "2f7562744654535564586e665467546b";
36
37 static const int AUTH_ID_LENGTH = 10;
38 static const int PUB_KEY_LENGTH = 128;
39 static const int NONCE_LENGTH = 64;
40 static const int INVALID_CIPHER_LENGTH = 16;
41 static const int VALID_CIPHER_LENGTH = 32;
42
43 class StandardExchangeTaskTest : public testing::Test {
44 public:
45 static void SetUpTestCase();
46 static void TearDownTestCase();
47 void SetUp();
48 void TearDown();
49 };
50
SetUpTestCase()51 void StandardExchangeTaskTest::SetUpTestCase() {}
TearDownTestCase()52 void StandardExchangeTaskTest::TearDownTestCase() {}
53
SetUp()54 void StandardExchangeTaskTest::SetUp() {}
55
TearDown()56 void StandardExchangeTaskTest::TearDown() {}
57
58 HWTEST_F(StandardExchangeTaskTest, StandardExchangeTaskTest001, TestSize.Level0)
59 {
60 int32_t ret = InitStandardBindExchangeParams(nullptr);
61 EXPECT_EQ(ret, HC_ERR_INVALID_PARAMS);
62
63 DestroyStandardBindExchangeParams(nullptr);
64
65 StandardBindExchangeParams params;
66 DestroyStandardBindExchangeParams(¶ms);
67 }
68
69 HWTEST_F(StandardExchangeTaskTest, StandardExchangeTaskTest002, TestSize.Level0)
70 {
71 PakeParams pakeParams;
72 pakeParams.userType = KEY_ALIAS_PSK;
73 pakeParams.packageName = const_cast<char *>(TEST_APP_ID.c_str());
74 pakeParams.serviceType = const_cast<char *>(TEST_GROUP_ID.c_str());
75 uint8_t authId[AUTH_ID_LENGTH] = { 0 };
76 (void)memcpy_s(authId, AUTH_ID_LENGTH, TEST_AUTH_ID.c_str(), AUTH_ID_LENGTH);
77 pakeParams.baseParams.idSelf.val = authId;
78 pakeParams.baseParams.idSelf.length = AUTH_ID_LENGTH;
79 pakeParams.baseParams.loader = GetLoaderInstance();
80
81 // pubKeySelf is null, exportPubKey failed
82 int32_t ret = ClientRequestStandardBindExchange(&pakeParams, nullptr);
83 EXPECT_NE(ret, HC_SUCCESS);
84
85 uint8_t publicKey[PUB_KEY_LENGTH] = { 0 };
86 StandardBindExchangeParams exchangeParams;
87 exchangeParams.pubKeySelf.val = publicKey;
88 exchangeParams.pubKeySelf.length = PUB_KEY_LENGTH;
89
90 ret = ClientRequestStandardBindExchange(&pakeParams, &exchangeParams);
91 EXPECT_NE(ret, HC_SUCCESS);
92
93 uint32_t challengeLen = HcStrlen(TEST_SALT.c_str());
94 uint8_t *challengeVal = static_cast<uint8_t *>(HcMalloc(challengeLen, 0));
95 EXPECT_NE(challengeVal, nullptr);
96 (void)memcpy_s(challengeVal, challengeLen, TEST_SALT.c_str(), challengeLen);
97
98 pakeParams.baseParams.challengeSelf.val = challengeVal;
99 pakeParams.baseParams.challengeSelf.length = challengeLen;
100
101 ret = ClientRequestStandardBindExchange(&pakeParams, &exchangeParams);
102 EXPECT_NE(ret, HC_SUCCESS);
103
104 pakeParams.baseParams.challengePeer.val = challengeVal;
105 pakeParams.baseParams.challengePeer.length = challengeLen;
106
107 ret = ClientRequestStandardBindExchange(&pakeParams, &exchangeParams);
108 EXPECT_NE(ret, HC_SUCCESS);
109
110 uint8_t nonceVal[NONCE_LENGTH] = { 0 };
111 exchangeParams.nonce.val = nonceVal;
112 exchangeParams.nonce.length = NONCE_LENGTH;
113
114 // aesGcmEncrypt failed, no sessionkey
115 ret = ClientRequestStandardBindExchange(&pakeParams, &exchangeParams);
116 EXPECT_NE(ret, HC_SUCCESS);
117
118 HcFree(challengeVal);
119 }
120
121 HWTEST_F(StandardExchangeTaskTest, StandardExchangeTaskTest003, TestSize.Level0)
122 {
123 PakeParams pakeParams;
124 pakeParams.userType = KEY_ALIAS_PSK;
125 pakeParams.packageName = const_cast<char *>(TEST_APP_ID.c_str());
126 pakeParams.serviceType = const_cast<char *>(TEST_GROUP_ID.c_str());
127 uint8_t authId[AUTH_ID_LENGTH] = { 0 };
128 (void)memcpy_s(authId, AUTH_ID_LENGTH, TEST_AUTH_ID.c_str(), AUTH_ID_LENGTH);
129 pakeParams.baseParams.idSelf.val = authId;
130 pakeParams.baseParams.idSelf.length = AUTH_ID_LENGTH;
131 pakeParams.baseParams.loader = GetLoaderInstance();
132
133 StandardBindExchangeParams exchangeParams;
134 exchangeParams.exInfoCipher.length = INVALID_CIPHER_LENGTH;
135
136 int32_t ret = ServerResponseStandardBindExchange(&pakeParams, &exchangeParams);
137 EXPECT_EQ(ret, HC_ERR_ALLOC_MEMORY);
138
139 exchangeParams.exInfoCipher.length = VALID_CIPHER_LENGTH;
140 ret = ServerResponseStandardBindExchange(&pakeParams, &exchangeParams);
141 EXPECT_NE(ret, HC_SUCCESS);
142 }
143
144 HWTEST_F(StandardExchangeTaskTest, StandardExchangeTaskTest004, TestSize.Level0)
145 {
146 PakeParams pakeParams;
147 pakeParams.packageName = const_cast<char *>(TEST_APP_ID.c_str());
148 pakeParams.serviceType = const_cast<char *>(TEST_GROUP_ID.c_str());
149 int32_t ret = ClientConfirmStandardBindExchange(&pakeParams, nullptr);
150 ASSERT_NE(ret, HC_SUCCESS);
151 }
152 }