• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 "deviceauth_test.h"
17 #include <gtest/gtest.h>
18 #include "securec.h"
19 #include "hichain.h"
20 
21 #define LOG(format, ...) (printf(format"\n", ##__VA_ARGS__))
22 
23 using namespace std;
24 using namespace testing::ext;
25 
26 namespace {
27 const int KEY_LEN = 32;
28 
29 class DeviceAuthTest : public testing::Test {
30 public:
31     static void SetUpTestCase(void);
32     static void TearDownTestCase(void);
33     void SetUp();
34     void TearDown();
35 };
36 
SetUpTestCase(void)37 void DeviceAuthTest::SetUpTestCase(void) {}
TearDownTestCase(void)38 void DeviceAuthTest::TearDownTestCase(void) {}
SetUp()39 void DeviceAuthTest::SetUp() {}
TearDown()40 void DeviceAuthTest::TearDown() {}
41 
42 static struct session_identity g_serverIdentity = {
43     0,
44     {strlen("testServer"), "testServer"},
45     {strlen("testServer"), "testServer"},
46     0
47 };
48 static struct hc_pin g_testPin = {strlen("123456789012345"), "123456789012345"};
49 static struct hc_auth_id g_testClientAuthId = {strlen("authClient"), "authClient"};
50 static struct hc_auth_id g_testServerAuthId = {strlen("authServer"), "authServer"};
51 
Transmit(const struct session_identity * identity,const void * data,uint32_t length)52 static void Transmit(const struct session_identity *identity, const void *data, uint32_t length)
53 {
54     LOG("--------Transmit--------");
55     LOG("identity session_id[%d] package_name[%s]", identity->session_id, identity->package_name.name);
56     LOG("length[%u]", length);
57     LOG("--------Transmit--------");
58 }
59 
GetProtocolParams(const struct session_identity * identity,int32_t operationCode,struct hc_pin * pin,struct operation_parameter * para)60 static void GetProtocolParams(const struct session_identity *identity, int32_t operationCode,
61     struct hc_pin *pin, struct operation_parameter *para)
62 {
63     LOG("--------GetProtocolParams--------");
64     LOG("identity session_id[%d] package_name[%s]", identity->session_id, identity->package_name.name);
65     LOG("operationCode[%d]", operationCode);
66     *pin = g_testPin;
67     para->self_auth_id = g_testServerAuthId;
68     para->peer_auth_id = g_testClientAuthId;
69     para->key_length = KEY_LEN;
70     LOG("--------GetProtocolParams--------");
71 }
72 
SetSessionKey(const struct session_identity * identity,const struct hc_session_key * sessionKey)73 static void SetSessionKey(const struct session_identity *identity, const struct hc_session_key *sessionKey)
74 {
75     LOG("--------SetSessionKey--------");
76     LOG("identity session_id[%d] package_name[%s]", identity->session_id, identity->package_name.name);
77     LOG("sessionKey[%s]", sessionKey->session_key);
78     LOG("--------SetSessionKey--------");
79 }
80 
SetServiceResult(const struct session_identity * identity,int32_t result)81 static void SetServiceResult(const struct session_identity *identity, int32_t result)
82 {
83     LOG("--------SetServiceResult--------");
84     LOG("identity session_id[%d] package_name[%s]", identity->session_id, identity->package_name.name);
85     LOG("result[%d]", result);
86     LOG("--------SetServiceResult--------");
87 }
88 
ConfirmReceiveRequest(const struct session_identity * identity,int32_t operationCode)89 static int32_t ConfirmReceiveRequest(const struct session_identity *identity, int32_t operationCode)
90 {
91     LOG("--------ConfirmReceiveRequest--------");
92     LOG("identity session_id[%d] package_name[%s]", identity->session_id, identity->package_name.name);
93     LOG("operationCode[%d]", operationCode);
94     LOG("--------ConfirmReceiveRequest--------");
95     return HC_OK;
96 }
97 
98 static HWTEST_F(DeviceAuthTest, Test001, TestSize.Level2)
99 {
100     LOG("--------DeviceAuthTest Test001--------");
101     LOG("--------get_instance--------");
102     struct hc_call_back callBack = {
103         Transmit,
104         GetProtocolParams,
105         SetSessionKey,
106         SetServiceResult,
107         ConfirmReceiveRequest
108     };
109     hc_handle server = get_instance(&g_serverIdentity, HC_ACCESSORY, &callBack);
110     ASSERT_TRUE(server != NULL);
111     destroy(&server);
112     LOG("--------DeviceAuthTest Test001--------");
113 }
114 
115 static HWTEST_F(DeviceAuthTest, Test002, TestSize.Level2)
116 {
117     LOG("--------DeviceAuthTest Test002--------");
118     LOG("--------start_pake--------");
119     struct hc_call_back callBack = {
120         Transmit,
121         GetProtocolParams,
122         SetSessionKey,
123         SetServiceResult,
124         ConfirmReceiveRequest
125     };
126     hc_handle server = get_instance(&g_serverIdentity, HC_ACCESSORY, &callBack);
127     const struct operation_parameter params = {g_testServerAuthId, g_testClientAuthId, KEY_LEN};
128     int32_t ret = start_pake(server, &params);
129     EXPECT_EQ(ret, HC_OK);
130     destroy(&server);
131     LOG("--------DeviceAuthTest Test002--------");
132 }
133 
134 static HWTEST_F(DeviceAuthTest, Test003, TestSize.Level2)
135 {
136     LOG("--------DeviceAuthTest Test003--------");
137     LOG("--------is_trust_peer--------");
138     struct hc_call_back callBack = {
139         Transmit,
140         GetProtocolParams,
141         SetSessionKey,
142         SetServiceResult,
143         ConfirmReceiveRequest
144     };
145     hc_handle server = get_instance(&g_serverIdentity, HC_ACCESSORY, &callBack);
146     struct hc_user_info userInfo = {g_testServerAuthId, 1};
147     int32_t ret = is_trust_peer(server, &userInfo);
148     EXPECT_EQ(ret, HC_OK);
149     destroy(&server);
150     LOG("--------DeviceAuthTest Test003--------");
151 }
152 }
153