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 <cstdio>
17 #include <ctime>
18 #include <gtest/gtest.h>
19 #include <sys/time.h>
20 #include <unistd.h>
21
22 #include "softbus_access_token_test.h"
23 #include "discovery_service.h"
24
25 using namespace testing::ext;
26
27 #define TEST_ERRO_MOUDULE ((MODULE_LNN) + 3)
28
29 namespace OHOS {
30 static int g_subscribeId = 0;
31 static int g_publishId = 0;
32 static const char *g_pkgName = "Softbus_Kits";
33
34 class DiscSdkOnlyL2Test : public testing::Test {
35 public:
DiscSdkOnlyL2Test()36 DiscSdkOnlyL2Test()
37 {}
~DiscSdkOnlyL2Test()38 ~DiscSdkOnlyL2Test()
39 {}
40 static void SetUpTestCase(void);
41 static void TearDownTestCase(void);
SetUp()42 void SetUp() override
43 {}
TearDown()44 void TearDown() override
45 {}
46 };
47
SetUpTestCase(void)48 void DiscSdkOnlyL2Test::SetUpTestCase(void)
49 {
50 SetAceessTokenPermission("discSdkOnlyL2Test");
51 }
52
TearDownTestCase(void)53 void DiscSdkOnlyL2Test::TearDownTestCase(void)
54 {}
55
GetSubscribeId(void)56 static int GetSubscribeId(void)
57 {
58 g_subscribeId++;
59 return g_subscribeId;
60 }
61
GetPublishId(void)62 static int GetPublishId(void)
63 {
64 g_publishId++;
65 return g_publishId;
66 }
67
68 static SubscribeInfo g_sInfo = {
69 .subscribeId = 1,
70 .mode = DISCOVER_MODE_ACTIVE,
71 .medium = COAP,
72 .freq = MID,
73 .isSameAccount = true,
74 .isWakeRemote = false,
75 .capability = "dvKit",
76 .capabilityData = (unsigned char *)"capdata3",
77 .dataLen = sizeof("capdata3")
78 };
79
80 static PublishInfo g_pInfo = {
81 .publishId = 1,
82 .mode = DISCOVER_MODE_ACTIVE,
83 .medium = COAP,
84 .freq = MID,
85 .capability = "dvKit",
86 .capabilityData = (unsigned char *)"capdata4",
87 .dataLen = sizeof("capdata4")
88 };
89
TestDeviceFound(const DeviceInfo * device)90 static void TestDeviceFound(const DeviceInfo *device)
91 {
92 printf("[client]TestDeviceFound\n");
93 }
94
TestDiscoverFailed(int subscribeId,DiscoveryFailReason failReason)95 static void TestDiscoverFailed(int subscribeId, DiscoveryFailReason failReason)
96 {
97 printf("[client]TestDiscoverFailed\n");
98 }
99
TestDiscoverySuccess(int subscribeId)100 static void TestDiscoverySuccess(int subscribeId)
101 {
102 printf("[client]TestDiscoverySuccess\n");
103 }
104
TestPublishSuccess(int publishId)105 static void TestPublishSuccess(int publishId)
106 {
107 printf("[client]TestPublishSuccess\n");
108 }
109
TestPublishFail(int publishId,PublishFailReason reason)110 static void TestPublishFail(int publishId, PublishFailReason reason)
111 {
112 printf("[client]TestPublishFail\n");
113 }
114
115 static IDiscoveryCallback g_subscribeCb = {
116 .OnDeviceFound = TestDeviceFound,
117 .OnDiscoverFailed = TestDiscoverFailed,
118 .OnDiscoverySuccess = TestDiscoverySuccess
119 };
120
121 static IPublishCallback g_publishCb = {
122 .OnPublishSuccess = TestPublishSuccess,
123 .OnPublishFail = TestPublishFail
124 };
125
126 /**
127 * @tc.name: UnPublishServiceTest004
128 * @tc.desc: not start publish.
129 * @tc.in: Test Moudle, Test Number, Test Levels.
130 * @tc.out: NonZero
131 * @tc.type: FUNC
132 * @tc.require: The UnPublishService operates normally.
133 */
134 HWTEST_F(DiscSdkOnlyL2Test, UnPublishServiceTest001, TestSize.Level2)
135 {
136 int ret;
137 int tmpId = GetPublishId();
138
139 ret = UnPublishService(g_pkgName, tmpId);
140 EXPECT_TRUE(ret != 0);
141 }
142
143 /**
144 * @tc.name: UnPublishServiceTest005
145 * @tc.desc: Verify UnPublishService again.
146 * @tc.in: Test Moudle, Test Number, Test Levels.
147 * @tc.out: NonZero
148 * @tc.type: FUNC
149 * @tc.require: The UnPublishService operates normally.
150 */
151 HWTEST_F(DiscSdkOnlyL2Test, UnPublishServiceTest002, TestSize.Level2)
152 {
153 int ret;
154 int tmpId = GetPublishId();
155
156 g_pInfo.publishId = tmpId;
157 PublishService(g_pkgName, &g_pInfo, &g_publishCb);
158 ret = UnPublishService(g_pkgName, tmpId);
159 EXPECT_TRUE(ret == 0);
160 ret = UnPublishService(g_pkgName, tmpId);
161 EXPECT_TRUE(ret != 0);
162 }
163
164 /**
165 * @tc.name: StopDiscoveryTest004
166 * @tc.desc: not start discover.
167 * @tc.in: Test Moudle, Test Number, Test Levels.
168 * @tc.out: NonZero
169 * @tc.type: FUNC
170 * @tc.require: The StopDiscovery operates normally.
171 */
172 HWTEST_F(DiscSdkOnlyL2Test, StopDiscoveryTest001, TestSize.Level2)
173 {
174 int ret;
175 int tmpId = GetSubscribeId();
176
177 ret = StopDiscovery(g_pkgName, tmpId);
178 EXPECT_TRUE(ret != 0);
179 }
180
181 /**
182 * @tc.name: StopDiscoveryTest005
183 * @tc.desc: Verify StopDiscovery again.
184 * @tc.in: Test Moudle, Test Number, Test Levels.
185 * @tc.out: NonZero
186 * @tc.type: FUNC
187 * @tc.require: The StopDiscovery operates normally.
188 */
189 HWTEST_F(DiscSdkOnlyL2Test, StopDiscoveryTest002, TestSize.Level2)
190 {
191 int ret;
192 int tmpId = GetSubscribeId();
193
194 g_sInfo.subscribeId = tmpId;
195 StartDiscovery(g_pkgName, &g_sInfo, &g_subscribeCb);
196 ret = StopDiscovery(g_pkgName, tmpId);
197 EXPECT_TRUE(ret == 0);
198 ret = StopDiscovery(g_pkgName, tmpId);
199 EXPECT_TRUE(ret != 0);
200 }
201 } // namespace OHOS
202