• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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         g_state = true;
83     }
84 }
85 
TestDiscoverFailed(int subscribeId,DiscoveryFailReason failReason)86 static void TestDiscoverFailed(int subscribeId, DiscoveryFailReason failReason)
87 {
88     printf("[test]TestDiscoverFailed\r\n");
89 }
90 
TestDiscoverySuccess(int subscribeId)91 static void TestDiscoverySuccess(int subscribeId)
92 {
93     printf("[test]TestDiscoverySuccess\r\n");
94 }
95 
TestPublishSuccess(int publishId)96 static void TestPublishSuccess(int publishId)
97 {
98     printf("[test]TestPublishSuccess\r\n");
99 }
100 
TestPublishFail(int publishId,PublishFailReason reason)101 static void TestPublishFail(int publishId, PublishFailReason reason)
102 {
103     printf("[test]TestPublishFail\r\n");
104 }
105 
106 static IDiscoveryCallback g_subscribeCb = {
107     .OnDeviceFound = TestDeviceFound,
108     .OnDiscoverFailed = TestDiscoverFailed,
109     .OnDiscoverySuccess = TestDiscoverySuccess
110 };
111 
112 static IPublishCallback g_publishCb = {
113     .OnPublishSuccess = TestPublishSuccess,
114     .OnPublishFail = TestPublishFail
115 };
116 
117 class DiscAccountTest : public testing::Test {
118 public:
DiscAccountTest()119     DiscAccountTest()
120     {}
~DiscAccountTest()121     ~DiscAccountTest()
122     {}
123     static void SetUpTestCase(void);
124     static void TearDownTestCase(void);
SetUp()125     void SetUp() override
126     {}
TearDown()127     void TearDown() override
128     {}
129 };
130 
SetUpTestCase(void)131 void DiscAccountTest::SetUpTestCase(void)
132 {
133     printf("********Ble Test Begin*********\r\n");
134     printf("*       0.discovery           *\r\n");
135     printf("*       1.publish             *\r\n");
136     printf("*******************************\r\n");
137     printf("input the num:");
138     if (scanf_s("%d", &g_testWay, sizeof(g_testWay)) < 0) {
139         printf("input error!\n");
140     }
141     (void)getchar();
142     if (g_testWay == PUBLISHSERVICE_WAY) {
143         return;
144     }
145     printf("***********Account*************\r\n");
146     printf("*          0.true             *\r\n");
147     printf("*          1.false            *\r\n");
148     printf("*******************************\r\n");
149     printf("input the num:");
150     if (scanf_s("%d", &g_accountWay, sizeof(g_accountWay)) < 0) {
151         printf("input error!\n");
152     }
153     (void)getchar();
154     if (g_accountWay == SAMEACCOUNT_TRUE) {
155         g_sInfo.isSameAccount = true;
156         return;
157     }
158     g_sInfo.isSameAccount = false;
159 }
160 
TearDownTestCase(void)161 void DiscAccountTest::TearDownTestCase(void)
162 {
163     if (g_testWay == STARTDISCOVERY_WAY) {
164         StopDiscovery(g_pkgName, g_subscribeId);
165         return;
166     }
167     UnPublishService(g_pkgName, g_publishId);
168 }
169 
Wait(void)170 static void Wait(void)
171 {
172     printf("[test]wait enter...\r\n");
173     do {
174         sleep(1);
175     } while (!g_state);
176     printf("[test]wait end!\r\n");
177     g_state = false;
178 }
179 
TestPublishServer()180 static int32_t TestPublishServer()
181 {
182     printf("[test]TestPublishServer enter\r\n");
183     g_pInfo.mode = DISCOVER_MODE_ACTIVE;
184     int32_t ret = PublishService(g_pkgName, &g_pInfo, &g_publishCb);
185     EXPECT_TRUE(ret == 0);
186     printf("[test]TestPublishServer end\r\n");
187     return ret;
188 }
189 
TestStartDiscovery()190 static int32_t TestStartDiscovery()
191 {
192     printf("[test]TestStartDiscovery enter\r\n");
193     g_sInfo.mode = DISCOVER_MODE_ACTIVE;
194     int32_t ret = StartDiscovery(g_pkgName, &g_sInfo, &g_subscribeCb);
195     EXPECT_TRUE(ret == 0);
196     printf("[test]TestStartDiscovery end\r\n");
197     return ret;
198 }
199 
200 /**
201  * @tc.name: StartDiscovery001
202  * @tc.desc: Verify wrong parameter
203  * @tc.type: FUNC
204  * @tc.require:
205  */
206 HWTEST_F(DiscAccountTest, StartDiscovery001, TestSize.Level0)
207 {
208     if (g_testWay != STARTDISCOVERY_WAY) {
209         printf("[test]start dsicovery test skip...\r\n");
210         EXPECT_TRUE(0 == 0);
211         return;
212     }
213     int32_t ret = TestStartDiscovery();
214     EXPECT_TRUE(ret == 0);
215     Wait();
216 };
217 
218 
219 /**
220  * @tc.name: PublishServiceTest001
221  * @tc.desc: Verify wrong parameter
222  * @tc.type: FUNC
223  * @tc.require:
224  */
225 HWTEST_F(DiscAccountTest, PublishServiceTest001, TestSize.Level0)
226 {
227     if (g_testWay != PUBLISHSERVICE_WAY) {
228         printf("[test]passive test skip...\r\n");
229         EXPECT_TRUE(0 == 0);
230         return;
231     }
232     int32_t ret = TestPublishServer();
233     EXPECT_TRUE(ret == 0);
234     Wait();
235 };
236 }
237