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