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 <cstdio>
17 #include <ctime>
18 #include <gtest/gtest.h>
19 #include <securec.h>
20 #include <sys/time.h>
21 #include <unistd.h>
22
23 #include "discovery_service.h"
24 #include "inner_session.h"
25 #include "session.h"
26 #include "softbus_utils.h"
27
28 using namespace testing::ext;
29
30 namespace OHOS {
31 int32_t g_testWay = -1;
32 int32_t g_accountWay = -1;
33 int32_t g_publishId = 1;
34 int32_t g_subscribeId = 1;
35 enum TEST_WAY {
36 STARTDISCOVERY_WAY = 0,
37 PUBLISHSERVICE_WAY
38 };
39
40 enum ACCOUNT_MODE {
41 SAMEACCOUNT_TRUE = 0,
42 SAMEACCOUNT_FALSE
43 };
44
45 const char *g_pkgName = "com.objectstore.foundation";
46 bool g_state = false;
47 static void Wait(void);
48
49 ConnectionAddr g_addr;
50 ConnectionAddr g_addr1;
51
52 static SubscribeInfo g_sInfo = {
53 .subscribeId = g_subscribeId,
54 .medium = BLE,
55 .mode = DISCOVER_MODE_ACTIVE,
56 .freq = MID,
57 .capability = "dvKit",
58 .capabilityData = (unsigned char *)"capdata3",
59 .dataLen = sizeof("capdata3"),
60 .isSameAccount = false,
61 .isWakeRemote = false
62 };
63
64 static PublishInfo g_pInfo = {
65 .publishId = g_publishId,
66 .medium = BLE,
67 .mode = DISCOVER_MODE_PASSIVE,
68 .freq = MID,
69 .capability = "dvKit",
70 .capabilityData = (unsigned char *)"capdata4",
71 .dataLen = sizeof("capdata4")
72 };
73
TestDeviceFound(const DeviceInfo * device)74 static void TestDeviceFound(const DeviceInfo *device)
75 {
76 if (ConvertBtMacToStr(g_addr.info.ble.bleMac, 18, (const uint8_t *)&(device->addr[0].info.ble.bleMac[0]), 6) != 0) {
77 return;
78 }
79 if (strcmp(g_addr1.info.ble.bleMac, g_addr.info.ble.bleMac) != 0) {
80 strcpy_s(g_addr1.info.ble.bleMac, BT_MAC_LEN, g_addr.info.ble.bleMac);
81 printf("[client]TestDeviceFound\r\n");
82 printf("account = %s\r\n", device->hwAccountHash);
83 g_state = true;
84 }
85 }
86
TestDiscoverFailed(int subscribeId,DiscoveryFailReason failReason)87 static void TestDiscoverFailed(int subscribeId, DiscoveryFailReason failReason)
88 {
89 printf("[test]TestDiscoverFailed\r\n");
90 }
91
TestDiscoverySuccess(int subscribeId)92 static void TestDiscoverySuccess(int subscribeId)
93 {
94 printf("[test]TestDiscoverySuccess\r\n");
95 }
96
TestPublishSuccess(int publishId)97 static void TestPublishSuccess(int publishId)
98 {
99 printf("[test]TestPublishSuccess\r\n");
100 }
101
TestPublishFail(int publishId,PublishFailReason reason)102 static void TestPublishFail(int publishId, PublishFailReason reason)
103 {
104 printf("[test]TestPublishFail\r\n");
105 }
106
107 static IDiscoveryCallback g_subscribeCb = {
108 .OnDeviceFound = TestDeviceFound,
109 .OnDiscoverFailed = TestDiscoverFailed,
110 .OnDiscoverySuccess = TestDiscoverySuccess
111 };
112
113 static IPublishCallback g_publishCb = {
114 .OnPublishSuccess = TestPublishSuccess,
115 .OnPublishFail = TestPublishFail
116 };
117
118 class DiscAccountTest : public testing::Test {
119 public:
DiscAccountTest()120 DiscAccountTest()
121 {}
~DiscAccountTest()122 ~DiscAccountTest()
123 {}
124 static void SetUpTestCase(void);
125 static void TearDownTestCase(void);
SetUp()126 void SetUp() override
127 {}
TearDown()128 void TearDown() override
129 {}
130 };
131
SetUpTestCase(void)132 void DiscAccountTest::SetUpTestCase(void)
133 {
134 printf("********Ble Test Begin*********\r\n");
135 printf("* 0.discovery *\r\n");
136 printf("* 1.publish *\r\n");
137 printf("*******************************\r\n");
138 printf("input the num:");
139 if (scanf_s("%d", &g_testWay, sizeof(g_testWay)) < 0) {
140 printf("input error!\n");
141 }
142 getchar();
143 if (g_testWay == PUBLISHSERVICE_WAY) {
144 return;
145 }
146 printf("***********Account*************\r\n");
147 printf("* 0.true *\r\n");
148 printf("* 1.false *\r\n");
149 printf("*******************************\r\n");
150 printf("input the num:");
151 if (scanf_s("%d", &g_accountWay, sizeof(g_accountWay)) < 0) {
152 printf("input error!\n");
153 }
154 getchar();
155 if (g_accountWay == SAMEACCOUNT_TRUE) {
156 g_sInfo.isSameAccount = true;
157 return;
158 }
159 g_sInfo.isSameAccount = false;
160 }
161
TearDownTestCase(void)162 void DiscAccountTest::TearDownTestCase(void)
163 {
164 if (g_testWay == STARTDISCOVERY_WAY) {
165 StopDiscovery(g_pkgName, g_subscribeId);
166 return;
167 }
168 UnPublishService(g_pkgName, g_publishId);
169 }
170
Wait(void)171 static void Wait(void)
172 {
173 printf("[test]wait enter...\r\n");
174 do {
175 sleep(1);
176 } while (!g_state);
177 printf("[test]wait end!\r\n");
178 g_state = false;
179 }
180
TestPublishServer()181 static int32_t TestPublishServer()
182 {
183 printf("[test]TestPublishServer enter\r\n");
184 g_pInfo.mode = DISCOVER_MODE_ACTIVE;
185 int32_t ret = PublishService(g_pkgName, &g_pInfo, &g_publishCb);
186 EXPECT_TRUE(ret == 0);
187 printf("[test]TestPublishServer end\r\n");
188 return ret;
189 }
190
TestStartDiscovery()191 static int32_t TestStartDiscovery()
192 {
193 printf("[test]TestStartDiscovery enter\r\n");
194 g_sInfo.mode = DISCOVER_MODE_ACTIVE;
195 int32_t ret = StartDiscovery(g_pkgName, &g_sInfo, &g_subscribeCb);
196 EXPECT_TRUE(ret == 0);
197 printf("[test]TestStartDiscovery end\r\n");
198 return ret;
199 }
200
201 /**
202 * @tc.name: StartDiscovery001
203 * @tc.desc: Verify wrong parameter
204 * @tc.type: FUNC
205 * @tc.require:
206 */
207 HWTEST_F(DiscAccountTest, StartDiscovery001, TestSize.Level0)
208 {
209 if (g_testWay != STARTDISCOVERY_WAY) {
210 printf("[test]start dsicovery test skip...\r\n");
211 EXPECT_TRUE(0 == 0);
212 return;
213 }
214 int32_t ret = TestStartDiscovery();
215 EXPECT_TRUE(ret == 0);
216 Wait();
217 };
218
219
220 /**
221 * @tc.name: PublishServiceTest001
222 * @tc.desc: Verify wrong parameter
223 * @tc.type: FUNC
224 * @tc.require:
225 */
226 HWTEST_F(DiscAccountTest, PublishServiceTest001, TestSize.Level0)
227 {
228 if (g_testWay != PUBLISHSERVICE_WAY) {
229 printf("[test]passive test skip...\r\n");
230 EXPECT_TRUE(0 == 0);
231 return;
232 }
233 int32_t ret = TestPublishServer();
234 EXPECT_TRUE(ret == 0);
235 Wait();
236 };
237 }