• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 #include <gtest/gtest.h>
16 #include <securec.h>
17 
18 #include "accesstoken_kit.h"
19 #include "general_client_connection.h"
20 #include "general_connection_server_proxy.h"
21 #include "nativetoken_kit.h"
22 #include "softbus_connection.h"
23 #include "softbus_error_code.h"
24 #include "token_setproc.h"
25 
26 using namespace testing::ext;
27 namespace OHOS {
28 namespace {
29 class ClientConnectionTest : public testing::Test {
30 public:
31     static void SetUpTestCase();
32     static void TearDownTestCase();
33     void SetUp();
34     void TearDown();
35 };
36 
AddPermission(void)37 void AddPermission(void)
38 {
39     const char *perms[2];
40     perms[0] = OHOS_PERMISSION_DISTRIBUTED_DATASYNC;
41     perms[1] = OHOS_PERMISSION_DISTRIBUTED_SOFTBUS_CENTER;
42     NativeTokenInfoParams infoInstance = {
43         .dcapsNum = 0,
44         .permsNum = 2,
45         .aclsNum = 0,
46         .dcaps = nullptr,
47         .perms = perms,
48         .acls = nullptr,
49         .processName = "dsoftbus_server",
50         .aplStr = "system_core",
51     };
52     uint64_t tokenId = GetAccessTokenId(&infoInstance);
53     SetSelfTokenID(tokenId);
54     OHOS::Security::AccessToken::AccessTokenKit::ReloadNativeTokenInfo();
55 }
56 
SetUpTestCase()57 void ClientConnectionTest::SetUpTestCase()
58 {
59     AddPermission();
60 }
61 
TearDownTestCase()62 void ClientConnectionTest::TearDownTestCase() { }
63 
SetUp()64 void ClientConnectionTest::SetUp() { }
65 
TearDown()66 void ClientConnectionTest::TearDown() { }
67 
OnAcceptConnect(const char * name,uint32_t handle)68 static int32_t OnAcceptConnect(const char *name, uint32_t handle)
69 {
70     printf("OnAcceptConnect called, name: %s, handle: %d\n", name, handle);
71     return 0;
72 }
73 
OnConnectionStateChange(uint32_t handle,int32_t state,int32_t reason)74 static int32_t OnConnectionStateChange(uint32_t handle, int32_t state, int32_t reason)
75 {
76     printf("OnConnectionStateChange called, handle: %d, state: %d, reason: %d\n", handle, state, reason);
77     return 0;
78 }
79 
OnDataRecevied(uint32_t handle,const uint8_t * data,uint32_t len)80 static void OnDataRecevied(uint32_t handle, const uint8_t *data, uint32_t len)
81 {
82     printf("OnDataRecevied called, handle: %d, data: %s, len: %d\n", handle, data, len);
83 }
84 
OnServiceDied(void)85 static void OnServiceDied(void)
86 {
87     printf("OnServiceDied called\n");
88 }
89 
90 static IGeneralListener g_listener = {
91     .OnAcceptConnect = OnAcceptConnect,
92     .OnConnectionStateChange = OnConnectionStateChange,
93     .OnDataReceived = OnDataRecevied,
94     .OnServiceDied = OnServiceDied,
95 };
96 
97 /*
98  * @tc.name: RegisterListenerTest
99  * @tc.desc: register listener test
100  * @tc.type: FUNC
101  * @tc.require:
102  */
103 HWTEST_F(ClientConnectionTest, RegisterListenerTest, TestSize.Level0)
104 {
105     int32_t ret = GeneralRegisterListener(nullptr);
106     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
107 
108     IGeneralListener listener = {
109         .OnAcceptConnect = nullptr,
110         .OnConnectionStateChange = nullptr,
111         .OnDataReceived = nullptr,
112         .OnServiceDied = nullptr,
113     };
114     ret = GeneralRegisterListener(&listener);
115     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
116 
117     listener.OnAcceptConnect = OnAcceptConnect;
118     ret = GeneralRegisterListener(&listener);
119     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
120 
121     listener.OnConnectionStateChange = OnConnectionStateChange;
122     ret = GeneralRegisterListener(&listener);
123     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
124 
125     listener.OnDataReceived = OnDataRecevied;
126     ret = GeneralRegisterListener(&listener);
127     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
128 
129     listener.OnServiceDied = OnServiceDied;
130     ret = GeneralRegisterListener(&listener);
131     ASSERT_EQ(ret, SOFTBUS_OK);
132 
133     ret = GeneralUnregisterListener();
134     ASSERT_EQ(ret, SOFTBUS_OK);
135 }
136 
137 /*
138  * @tc.name: ClientConnectionTest
139  * @tc.desc: register listener test
140  * @tc.type: FUNC
141  * @tc.require:
142  */
143 HWTEST_F(ClientConnectionTest, ClientConnectionTest, TestSize.Level0)
144 {
145     int32_t ret = GeneralRegisterListener(&g_listener);
146     ASSERT_EQ(ret, SOFTBUS_OK);
147     const char *pkgName = "ohos.distributedschedule.dms";
148     const char *name = "hanglvzongheng";
149     ret = GeneralCreateServer(pkgName, name);
150     ASSERT_EQ(ret, SOFTBUS_TRANS_GET_BUNDLENAME_FAILED);
151     ret = GeneralRemoveServer(pkgName, name);
152     ASSERT_EQ(ret, SOFTBUS_TRANS_GET_BUNDLENAME_FAILED);
153     Address address;
154     address.addrType = CONNECTION_ADDR_BLE;
155     char mac[BT_MAC_LEN] = "12:32:43:54:65:76";
156     ret = memcpy_s(address.addr.ble.mac, BT_MAC_LEN, mac, BT_MAC_LEN);
157     ASSERT_EQ(ret, EOK);
158     ret = GeneralConnect(pkgName, name, &address);
159     ASSERT_EQ(ret, SOFTBUS_TRANS_GET_BUNDLENAME_FAILED);
160     GeneralDisconnect(0);
161     const uint8_t *data = (const uint8_t *)"hello world";
162     ret = GeneralSend(1, data, strlen((const char *)data));
163     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
164     ret = GeneralGetPeerDeviceId(0, nullptr, 0);
165     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
166     ret = GeneralGetPeerDeviceId(1, nullptr, 0);
167     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
168     uint32_t invalidLen = 20;
169     const uint32_t len = 10;
170     char udid[len] = { 0 };
171     ret = GeneralGetPeerDeviceId(1, udid, invalidLen);
172     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
173     ret = GeneralGetPeerDeviceId(1, udid, 0);
174     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
175     ret = GeneralGetPeerDeviceId(1, udid, len);
176     ASSERT_EQ(ret, SOFTBUS_INVALID_PARAM);
177     ret = GeneralUnregisterListener();
178     ASSERT_EQ(ret, SOFTBUS_OK);
179 }
180 
181 /*
182  * @tc.name: DataReceivedTest
183  * @tc.desc: data received test
184  * @tc.type: FUNC
185  * @tc.require:
186  */
187 HWTEST_F(ClientConnectionTest, DataReceivedTest, TestSize.Level0)
188 {
189     const uint8_t *data = reinterpret_cast<const uint8_t *>("test");
190     uint32_t len = strlen(reinterpret_cast<const char *>(data));
191     DataReceived(0, data, len);
192     IGeneralListener listener = {
193         .OnAcceptConnect = OnAcceptConnect,
194         .OnConnectionStateChange = OnConnectionStateChange,
195         .OnDataReceived = OnDataRecevied,
196         .OnServiceDied = OnServiceDied,
197     };
198     int32_t ret = GeneralRegisterListener(&listener);
199     ASSERT_EQ(ret, SOFTBUS_OK);
200     DataReceived(0, data, len);
201     listener.OnDataReceived = nullptr;
202     DataReceived(0, data, len);
203     ret = GeneralUnregisterListener();
204     ASSERT_EQ(ret, SOFTBUS_OK);
205 }
206 
207 /*
208  * @tc.name: ConnectionDeathNotifyTest
209  * @tc.desc: connection death notify test
210  * @tc.type: FUNC
211  * @tc.require:
212  */
213 HWTEST_F(ClientConnectionTest, ConnectionDeathNotifyTest, TestSize.Level0)
214 {
215     ConnectionDeathNotify();
216     IGeneralListener listener = {
217         .OnAcceptConnect = OnAcceptConnect,
218         .OnConnectionStateChange = OnConnectionStateChange,
219         .OnDataReceived = OnDataRecevied,
220         .OnServiceDied = OnServiceDied,
221     };
222     int32_t ret = GeneralRegisterListener(&listener);
223     ASSERT_EQ(ret, SOFTBUS_OK);
224     ConnectionDeathNotify();
225     listener.OnServiceDied = nullptr;
226     ConnectionDeathNotify();
227     listener.OnConnectionStateChange = nullptr;
228     ConnectionDeathNotify();
229     ret = GeneralUnregisterListener();
230     ASSERT_EQ(ret, SOFTBUS_OK);
231 }
232 } // namespace
233 } // namespace OHOS
234