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 "dslm_rpc_process_test.h"
17 
18 #include "device_security_defines.h"
19 #include "device_security_info.h"
20 #include "dslm_msg_interface_mock.h"
21 #include "dslm_rpc_process.h"
22 
23 using namespace testing;
24 using namespace testing::ext;
25 
26 extern "C" int32_t OnPeerMsgReceived(const DeviceIdentify *devId, const uint8_t *msg, uint32_t len);
27 extern "C" int32_t OnSendResultNotifier(const DeviceIdentify *devId, uint64_t transNo, uint32_t result);
28 
29 namespace OHOS {
30 namespace Security {
31 namespace DslmUnitTest {
SetUpTestCase()32 void DslmRpcProcessTest::SetUpTestCase()
33 {
34 }
35 
TearDownTestCase()36 void DslmRpcProcessTest::TearDownTestCase()
37 {
38 }
39 
SetUp()40 void DslmRpcProcessTest::SetUp()
41 {
42 }
43 
TearDown()44 void DslmRpcProcessTest::TearDown()
45 {
46 }
47 
48 /**
49  * @tc.name: OnPeerMsgReceived_InvalidInputPara_001
50  * @tc.desc: function OnPeerMsgReceived with null input
51  * @tc.type: FUNC
52  * @tc.require: issueNumber
53  */
54 HWTEST_F(DslmRpcProcessTest, OnPeerMsgReceived_InvalidInputPara_001, TestSize.Level0)
55 {
56     int32_t ret;
57 
58     ret = OnPeerMsgReceived(NULL, NULL, 0);
59     EXPECT_EQ(ret, ERR_INVALID_PARA);
60 
61     const DeviceIdentify identify = {DEVICE_ID_MAX_LEN, {0}};
62     ret = OnPeerMsgReceived(&identify, NULL, 0);
63     EXPECT_EQ(ret, ERR_INVALID_PARA);
64 
65     const uint8_t message[] = {'1', '2'};
66     ret = OnPeerMsgReceived(&identify, message, 0);
67     EXPECT_EQ(ret, ERR_INVALID_PARA);
68 }
69 
70 /**
71  * @tc.name: OnPeerMsgReceived_InvalidInputPara_002
72  * @tc.desc: function OnPeerMsgReceived with invalid inputs
73  * @tc.type: FUNC
74  * @tc.require: issueNumber
75  */
76 HWTEST_F(DslmRpcProcessTest, OnPeerMsgReceived_InvalidInputPara_002, TestSize.Level0)
77 {
78     int32_t ret;
79     const DeviceIdentify identify = {DEVICE_ID_MAX_LEN, {0}};
80     const uint8_t message1[] = {'1', '2', 3};
81     ret = OnPeerMsgReceived(&identify, message1, 3);
82     EXPECT_EQ(ret, ERR_INVALID_PARA);
83 
84     const uint8_t message2[] = {'1', 0xff, '2', '3'};
85     ret = OnPeerMsgReceived(&identify, message2, 4);
86     EXPECT_EQ(ret, ERR_INVALID_PARA);
87 }
88 
89 /**
90  * @tc.name: OnPeerMsgReceived_InvalidInputPara_003
91  * @tc.desc: function OnPeerMsgReceived with invalid inputs
92  * @tc.type: FUNC
93  * @tc.require: issueNumber
94  */
95 HWTEST_F(DslmRpcProcessTest, OnPeerMsgReceived_InvalidInputPara_003, TestSize.Level0)
96 {
97     const DeviceIdentify identify = {DEVICE_ID_MAX_LEN, {0}};
98     const char *message = R"({"message":1,"payload":{"version":131072,"challenge111":"0102030405060708"}})";
99     uint32_t messageLen = strlen(message) + 1;
100     int32_t ret = OnPeerMsgReceived(&identify, (const uint8_t *)message, messageLen);
101     EXPECT_EQ(ret, ERR_NO_CHALLENGE);
102 }
103 
104 /**
105  * @tc.name: OnPeerMsgReceived_InvalidInputPara_004
106  * @tc.desc: function OnPeerMsgReceived with no valid device
107  * @tc.type: FUNC
108  * @tc.require: issueNumber
109  */
110 HWTEST_F(DslmRpcProcessTest, OnPeerMsgReceived_InvalidInputPara_004, TestSize.Level0)
111 {
112     const DeviceIdentify identify = {DEVICE_ID_MAX_LEN, {0}};
113     const char *message = R"({"message":2,"payload":{"version":131072,"challenge111":"0102030405060708"}})";
114     uint32_t messageLen = strlen(message) + 1;
115     int32_t ret = OnPeerMsgReceived(&identify, (const uint8_t *)message, messageLen);
116     EXPECT_EQ(ret, ERR_NOEXIST_DEVICE);
117 }
118 
119 /**
120  * @tc.name: OnPeerMsgReceived_InvalidInputPara_005
121  * @tc.desc: function OnPeerMsgReceived with invalid msg type
122  * @tc.type: FUNC
123  * @tc.require: issueNumber
124  */
125 HWTEST_F(DslmRpcProcessTest, OnPeerMsgReceived_InvalidInputPara_005, TestSize.Level0)
126 {
127     const DeviceIdentify identify = {DEVICE_ID_MAX_LEN, {0}};
128     const char *message = R"({"message":3,"payload":{"version":131072,"challenge111":"0102030405060708"}})";
129     uint32_t messageLen = strlen(message) + 1;
130     int32_t ret = OnPeerMsgReceived(&identify, (const uint8_t *)message, messageLen);
131     EXPECT_EQ(ret, ERR_INVALID_PARA);
132 }
133 
134 /**
135  * @tc.name: OnSendResultNotifier_InvalidInputPara_001
136  * @tc.desc: function OnSendResultNotifier test
137  * @tc.type: FUNC
138  * @tc.require: issueNumber
139  */
140 HWTEST_F(DslmRpcProcessTest, OnSendResultNotifier_InvalidInputPara_001, TestSize.Level0)
141 {
142     const DeviceIdentify identify = {DEVICE_ID_MAX_LEN, {0}};
143     int32_t ret = OnSendResultNotifier(&identify, 0, SUCCESS);
144     EXPECT_EQ(ret, SUCCESS);
145 }
146 
147 /**
148  * @tc.name: InitService_InvalidInputPara_001
149  * @tc.desc: function InitService test with messenger not init
150  * @tc.type: FUNC
151  * @tc.require: issueNumber
152  */
153 HWTEST_F(DslmRpcProcessTest, InitService_InvalidInputPara_001, TestSize.Level0)
154 {
155     uint32_t ret = InitService();
156     EXPECT_EQ(ret, ERR_MSG_NOT_INIT);
157 }
158 
159 /**
160  * @tc.name: InitService_InvalidInputPara_002
161  * @tc.desc: function InitService test with messenger init success
162  * @tc.type: FUNC
163  * @tc.require: issueNumber
164  */
165 HWTEST_F(DslmRpcProcessTest, InitService_InvalidInputPara_002, TestSize.Level0)
166 {
167     DslmMsgInterfaceMock mock;
168 
__anon0649868d0102(DeviceIdentify *identify) 169     auto fillUpDeviceIdentify = [](DeviceIdentify *identify) { identify->length = DEVICE_ID_MAX_LEN; };
170 
171     EXPECT_CALL(mock, IsMessengerReady(_)).Times(AtLeast(1)).WillRepeatedly(Return(true));
172     EXPECT_CALL(mock, GetSelfDeviceIdentify(_, _, _))
173         .WillOnce(Return(false))
174         .WillRepeatedly(DoAll(WithArg<1>(fillUpDeviceIdentify), Return(true)));
175 
176     uint32_t ret = InitService();
177     EXPECT_EQ(ret, SUCCESS);
178 }
179 } // namespace DslmUnitTest
180 } // namespace Security
181 } // namespace OHOS
182