• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 <gtest/gtest.h>
17 #include "securec.h"
18 
19 #include "trans_client_proxy.h"
20 #include "softbus_def.h"
21 #include "softbus_errcode.h"
22 #include "iservice_registry.h"
23 #include "if_system_ability_manager.h"
24 #include "iremote_object.h"
25 #include "softbus_server_death_recipient.h"
26 #include "softbus_client_info_manager.h"
27 #include "ipc_skeleton.h"
28 #include "softbus_trans_def.h"
29 #include "session.h"
30 #include "softbus_adapter_mem.h"
31 
32 
33 using namespace std;
34 using namespace testing::ext;
35 
36 namespace OHOS {
37 #define TEST_ERR_PID (-1)
38 #define TEST_LEN 10
39 #define TEST_DATA_TYPE 0
40 #define TEST_STATE 1
41 #define TEST_ERR_CODE 1
42 #define TEST_CHANNELID 5
43 #define TEST_CHANNELTYPE 2
44 #define TEST_REMOTE_TYPE 0
45 #define TEST_EVENT_ID 2
46 #define TEST_COUNT 2
47 
48 const char *g_pkgName = "dms";
49 const char *g_sessionName = "ohos.distributedschedule.dms.test";
50 const char *g_errPkgName = "abc";
51 
52 class TransClientProxyTest : public testing::Test {
53 public:
TransClientProxyTest()54     TransClientProxyTest() {}
~TransClientProxyTest()55     ~TransClientProxyTest() {}
56     static void SetUpTestCase(void);
57     static void TearDownTestCase(void);
SetUp()58     void SetUp() override {}
TearDown()59     void TearDown() override {}
60 };
61 
SetUpTestCase(void)62 void TransClientProxyTest::SetUpTestCase(void) {}
TearDownTestCase(void)63 void TransClientProxyTest::TearDownTestCase(void) {}
64 
65 /**
66  * @tc.name: InformPermissionChangeTest001
67  * @tc.desc: infomation permission change test, use the wrong or normal parameter.
68  * @tc.type: FUNC
69  * @tc.require:
70  */
71 HWTEST_F(TransClientProxyTest, InformPermissionChangeTest001, TestSize.Level0)
72 {
73     int32_t ret;
74 
75     ret = InformPermissionChange(TEST_STATE, nullptr);
76     EXPECT_EQ(SOFTBUS_ERR, ret);
77 
78     ret = InformPermissionChange(TEST_STATE, g_pkgName);
79     EXPECT_EQ(SOFTBUS_ERR, ret);
80 
81     static const uint32_t SOFTBUS_SA_ID = 4700;
82     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
83     sptr<IRemoteObject> remoteObject = saManager->GetSystemAbility(SOFTBUS_SA_ID);
84     ASSERT_TRUE(remoteObject != nullptr);
85     sptr<IRemoteObject::DeathRecipient> abilityDeath = new (std::nothrow) SoftBusDeathRecipient();
86     ASSERT_TRUE(abilityDeath != nullptr);
87     ret = SoftbusClientInfoManager::GetInstance().SoftbusAddService(g_pkgName, remoteObject, abilityDeath);
88     ASSERT_TRUE(ret == SOFTBUS_OK);
89     ret = InformPermissionChange(TEST_STATE, g_pkgName);
90     EXPECT_EQ(SOFTBUS_OK, ret);
91 }
92 
93 /**
94  * @tc.name: ClientIpcOnChannelOpenedTest001
95  * @tc.desc: client ipc on channel opened test, use the wrong or normal parameter.
96  * @tc.type: FUNC
97  * @tc.require:
98  */
99 HWTEST_F(TransClientProxyTest, ClientIpcOnChannelOpenedTest001, TestSize.Level0)
100 {
101     int32_t ret;
102     ChannelInfo channel;
103     char strTmp[] = "ABCDEFG";
104     channel.channelId = TEST_CHANNELID;
105     channel.channelType = TEST_CHANNELTYPE;
106     channel.fd = TEST_DATA_TYPE;
107     channel.isServer = true;
108     channel.isEnabled = true;
109     channel.peerUid = TEST_CHANNELID;
110     channel.peerPid = TEST_CHANNELID;
111     channel.groupId = strTmp;
112     channel.sessionKey = strTmp;
113     channel.keyLen = sizeof(channel.sessionKey);
114     channel.peerSessionName = strTmp;
115     channel.peerDeviceId = strTmp;
116     channel.businessType = TEST_COUNT;
117     channel.myIp = strTmp;
118     channel.streamType = TEST_COUNT;
119     channel.isUdpFile = true;
120     channel.peerPort = TEST_COUNT;
121     channel.peerIp = strTmp;
122     channel.routeType = TEST_DATA_TYPE;
123     channel.encrypt = TEST_COUNT;
124     channel.algorithm = TEST_COUNT;
125     channel.crc = TEST_COUNT;
126 
127     ret = ClientIpcOnChannelOpened(g_pkgName, g_sessionName, &channel);
128     EXPECT_EQ(SOFTBUS_ERR, ret);
129 
130     static const uint32_t SOFTBUS_SA_ID = 4700;
131     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
132     sptr<IRemoteObject> remoteObject = saManager->GetSystemAbility(SOFTBUS_SA_ID);
133     ASSERT_TRUE(remoteObject != nullptr);
134     sptr<IRemoteObject::DeathRecipient> abilityDeath = new (std::nothrow) SoftBusDeathRecipient();
135     ASSERT_TRUE(abilityDeath != nullptr);
136     ret = SoftbusClientInfoManager::GetInstance().SoftbusAddService(g_pkgName, remoteObject, abilityDeath);
137     ASSERT_TRUE(ret == SOFTBUS_OK);
138     ret = ClientIpcOnChannelOpened(g_pkgName, g_sessionName, &channel);
139     EXPECT_EQ(SOFTBUS_ERR, ret);
140 
141     channel.isServer = false;
142     ret = ClientIpcOnChannelOpened(g_pkgName, g_sessionName, &channel);
143     EXPECT_EQ(SOFTBUS_ERR, ret);
144 }
145 
146 /**
147  * @tc.name: ClientIpcOnChannelOpenFailedTest001
148  * @tc.desc: client ipc on channel open failed test, use the wrong or normal parameter.
149  * @tc.type: FUNC
150  * @tc.require:
151  */
152 HWTEST_F(TransClientProxyTest, ClientIpcOnChannelOpenFailedTest001, TestSize.Level0)
153 {
154     int32_t ret;
155 
156     ret = ClientIpcOnChannelOpenFailed(g_pkgName, TEST_CHANNELID, TEST_CHANNELTYPE, TEST_ERR_CODE);
157     EXPECT_EQ(SOFTBUS_OK, ret);
158 
159     static const uint32_t SOFTBUS_SA_ID = 4700;
160     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
161     sptr<IRemoteObject> remoteObject = saManager->GetSystemAbility(SOFTBUS_SA_ID);
162     ASSERT_TRUE(remoteObject != nullptr);
163     sptr<IRemoteObject::DeathRecipient> abilityDeath = new (std::nothrow) SoftBusDeathRecipient();
164     ASSERT_TRUE(abilityDeath != nullptr);
165     ret = SoftbusClientInfoManager::GetInstance().SoftbusAddService(g_pkgName, remoteObject, abilityDeath);
166     ASSERT_TRUE(ret == SOFTBUS_OK);
167     ret = ClientIpcOnChannelOpenFailed(g_pkgName, TEST_CHANNELID, TEST_CHANNELTYPE, TEST_ERR_CODE);
168     EXPECT_EQ(SOFTBUS_OK, ret);
169 }
170 
171 /**
172  * @tc.name: ClientIpcOnChannelLinkDownTest001
173  * @tc.desc: client ipc on channel link down test, use the wrong or normal parameter.
174  * @tc.type: FUNC
175  * @tc.require:
176  */
177 HWTEST_F(TransClientProxyTest, ClientIpcOnChannelLinkDownTest001, TestSize.Level0)
178 {
179     int32_t ret;
180     char networkId[] = "ABCDEFG";
181 
182     ret = ClientIpcOnChannelLinkDown(g_pkgName, networkId, TEST_REMOTE_TYPE);
183     EXPECT_EQ(SOFTBUS_OK, ret);
184 
185     static const uint32_t SOFTBUS_SA_ID = 4700;
186     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
187     sptr<IRemoteObject> remoteObject = saManager->GetSystemAbility(SOFTBUS_SA_ID);
188     ASSERT_TRUE(remoteObject != nullptr);
189     sptr<IRemoteObject::DeathRecipient> abilityDeath = new (std::nothrow) SoftBusDeathRecipient();
190     ASSERT_TRUE(abilityDeath != nullptr);
191     ret = SoftbusClientInfoManager::GetInstance().SoftbusAddService(g_pkgName, remoteObject, abilityDeath);
192     ASSERT_TRUE(ret == SOFTBUS_OK);
193     ret = ClientIpcOnChannelLinkDown(g_pkgName, networkId, TEST_REMOTE_TYPE);
194     EXPECT_EQ(SOFTBUS_OK, ret);
195 }
196 
197 /**
198  * @tc.name: ClientIpcOnChannelClosedTest001
199  * @tc.desc: client ipc on channel closed test, use the wrong or normal parameter.
200  * @tc.type: FUNC
201  * @tc.require:
202  */
203 HWTEST_F(TransClientProxyTest, ClientIpcOnChannelClosedTest001, TestSize.Level0)
204 {
205     int32_t ret;
206 
207     ret = ClientIpcOnChannelClosed(g_pkgName, TEST_CHANNELID, TEST_CHANNELTYPE);
208     EXPECT_EQ(SOFTBUS_OK, ret);
209 
210     static const uint32_t SOFTBUS_SA_ID = 4700;
211     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
212     sptr<IRemoteObject> remoteObject = saManager->GetSystemAbility(SOFTBUS_SA_ID);
213     ASSERT_TRUE(remoteObject != nullptr);
214     sptr<IRemoteObject::DeathRecipient> abilityDeath = new (std::nothrow) SoftBusDeathRecipient();
215     ASSERT_TRUE(abilityDeath != nullptr);
216     ret = SoftbusClientInfoManager::GetInstance().SoftbusAddService(g_pkgName, remoteObject, abilityDeath);
217     ASSERT_TRUE(ret == SOFTBUS_OK);
218     ret = ClientIpcOnChannelClosed(g_pkgName, TEST_CHANNELID, TEST_CHANNELTYPE);
219     EXPECT_EQ(SOFTBUS_OK, ret);
220 }
221 
222 /**
223  * @tc.name: ClientIpcOnChannelMsgReceivedTest001
224  * @tc.desc: client ipc on channel msg received test, use the wrong or normal parameter.
225  * @tc.type: FUNC
226  * @tc.require:
227  */
228 HWTEST_F(TransClientProxyTest, ClientIpcOnChannelMsgReceivedTest001, TestSize.Level0)
229 {
230     int32_t ret;
231     char data[TEST_LEN];
232 
233     ret = ClientIpcOnChannelMsgReceived(g_pkgName, TEST_CHANNELID, TEST_CHANNELTYPE, data, TEST_LEN, TEST_DATA_TYPE);
234     EXPECT_EQ(SOFTBUS_OK, ret);
235 
236     static const uint32_t SOFTBUS_SA_ID = 4700;
237     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
238     sptr<IRemoteObject> remoteObject = saManager->GetSystemAbility(SOFTBUS_SA_ID);
239     ASSERT_TRUE(remoteObject != nullptr);
240     sptr<IRemoteObject::DeathRecipient> abilityDeath = new (std::nothrow) SoftBusDeathRecipient();
241     ASSERT_TRUE(abilityDeath != nullptr);
242     ret = SoftbusClientInfoManager::GetInstance().SoftbusAddService(g_pkgName, remoteObject, abilityDeath);
243     ASSERT_TRUE(ret == SOFTBUS_OK);
244     ret = ClientIpcOnChannelMsgReceived(g_pkgName, TEST_CHANNELID, TEST_CHANNELTYPE, data, TEST_LEN, TEST_DATA_TYPE);
245     EXPECT_EQ(SOFTBUS_OK, ret);
246 }
247 
248 /**
249  * @tc.name: ClientIpcOnChannelQosEventTest001
250  * @tc.desc: client ipc on channel qos event test, use the wrong or normal parameter.
251  * @tc.type: FUNC
252  * @tc.require:
253  */
254 HWTEST_F(TransClientProxyTest, ClientIpcOnChannelQosEventTest001, TestSize.Level0)
255 {
256     int32_t ret;
257 
258     QosParam param;
259     QosTv tvList;
260     param.channelId = TEST_CHANNELID;
261     param.channelType = CHANNEL_TYPE_UDP;
262     param.eventId = TEST_EVENT_ID;
263     param.tvCount = TEST_COUNT;
264     param.tvList = &tvList;
265 
266     ret = ClientIpcOnChannelQosEvent(g_pkgName, &param);
267     EXPECT_EQ(SOFTBUS_OK, ret);
268 
269     static const uint32_t SOFTBUS_SA_ID = 4700;
270     sptr<ISystemAbilityManager> saManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
271     sptr<IRemoteObject> remoteObject = saManager->GetSystemAbility(SOFTBUS_SA_ID);
272     ASSERT_TRUE(remoteObject != nullptr);
273     sptr<IRemoteObject::DeathRecipient> abilityDeath = new (std::nothrow) SoftBusDeathRecipient();
274     ASSERT_TRUE(abilityDeath != nullptr);
275     ret = SoftbusClientInfoManager::GetInstance().SoftbusAddService(g_pkgName, remoteObject, abilityDeath);
276     ASSERT_TRUE(ret == SOFTBUS_OK);
277     ret = ClientIpcOnChannelQosEvent(g_pkgName, &param);
278     EXPECT_EQ(SOFTBUS_OK, ret);
279 }
280 } // namespace OHOS