1 /*
2  * Copyright (c) 2022-2024 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 "softbus_bus_center.h"
24 
25 using namespace testing::ext;
26 
27 namespace OHOS {
28 static int g_subscribeId = 0;
29 static int g_publishId = 0;
30 static const char *g_pkgName = "Softbus_Kits";
31 
32 class DiscSdkOnlyL2Test : public testing::Test {
33 public:
DiscSdkOnlyL2Test()34     DiscSdkOnlyL2Test()
35     {}
~DiscSdkOnlyL2Test()36     ~DiscSdkOnlyL2Test()
37     {}
38     static void SetUpTestCase(void);
39     static void TearDownTestCase(void);
SetUp()40     void SetUp() override
41     {}
TearDown()42     void TearDown() override
43     {}
44 };
45 
SetUpTestCase(void)46 void DiscSdkOnlyL2Test::SetUpTestCase(void)
47 {
48     SetAceessTokenPermission("discSdkOnlyL2Test");
49 }
50 
TearDownTestCase(void)51 void DiscSdkOnlyL2Test::TearDownTestCase(void)
52 {}
53 
GetSubscribeId(void)54 static int GetSubscribeId(void)
55 {
56     g_subscribeId++;
57     return g_subscribeId;
58 }
59 
GetPublishId(void)60 static int GetPublishId(void)
61 {
62     g_publishId++;
63     return g_publishId;
64 }
65 
66 static SubscribeInfo g_sInfo = {
67     .subscribeId = 1,
68     .mode = DISCOVER_MODE_PASSIVE,
69     .medium = COAP,
70     .freq = MID,
71     .isSameAccount = true,
72     .isWakeRemote = false,
73     .capability = "dvKit",
74     .capabilityData = (unsigned char *)"capdata3",
75     .dataLen = strlen("capdata3")
76 };
77 
78 static PublishInfo g_pInfo = {
79     .publishId = 1,
80     .mode = DISCOVER_MODE_PASSIVE,
81     .medium = COAP,
82     .freq = MID,
83     .capability = "dvKit",
84     .capabilityData = (unsigned char *)"capdata4",
85     .dataLen = strlen("capdata4")
86 };
87 
TestDeviceFound(const DeviceInfo * device)88 static void TestDeviceFound(const DeviceInfo *device)
89 {
90     printf("[client]TestDeviceFound\n");
91 }
92 
TestOnDiscoverResult(int32_t refreshId,RefreshResult reason)93 static void TestOnDiscoverResult(int32_t refreshId, RefreshResult reason)
94 {
95     (void)refreshId;
96     (void)reason;
97     printf("[client]TestDiscoverResult\n");
98 }
99 
TestOnPublishResult(int publishId,PublishResult reason)100 static void TestOnPublishResult(int publishId, PublishResult reason)
101 {
102     (void)publishId;
103     (void)reason;
104     printf("[client]TestPublishResult\n");
105 }
106 
107 static IRefreshCallback g_refreshCb = {
108     .OnDeviceFound = TestDeviceFound,
109     .OnDiscoverResult = TestOnDiscoverResult
110 };
111 
112 static IPublishCb g_publishCb = {
113     .OnPublishResult = TestOnPublishResult,
114 };
115 
116 /**
117  * @tc.name: StopPublishLNNTest004
118  * @tc.desc: not start publish.
119  * @tc.in: Test Moudle, Test Number, Test Levels.
120  * @tc.out: NonZero
121  * @tc.type: FUNC
122  * @tc.require: The StopPublishLNN operates normally.
123  */
124 HWTEST_F(DiscSdkOnlyL2Test, StopPublishLNNTest001, TestSize.Level2)
125 {
126     int ret;
127     int tmpId = GetPublishId();
128 
129     ret = StopPublishLNN(g_pkgName, tmpId);
130     EXPECT_TRUE(ret != 0);
131 }
132 
133 /**
134  * @tc.name: StopPublishLNNTest005
135  * @tc.desc: Verify StopPublishLNN again.
136  * @tc.in: Test Moudle, Test Number, Test Levels.
137  * @tc.out: NonZero
138  * @tc.type: FUNC
139  * @tc.require: The StopPublishLNN operates normally.
140  */
141 HWTEST_F(DiscSdkOnlyL2Test, StopPublishLNNTest002, TestSize.Level2)
142 {
143     int ret;
144     int tmpId = GetPublishId();
145 
146     g_pInfo.publishId = tmpId;
147     PublishLNN(g_pkgName, &g_pInfo, &g_publishCb);
148     ret = StopPublishLNN(g_pkgName, tmpId);
149     EXPECT_TRUE(ret == 0);
150     ret = StopPublishLNN(g_pkgName, tmpId);
151     EXPECT_TRUE(ret != 0);
152 }
153 
154 /**
155  * @tc.name: StopRefreshLNNTest004
156  * @tc.desc: not start discover.
157  * @tc.in: Test Moudle, Test Number, Test Levels.
158  * @tc.out: NonZero
159  * @tc.type: FUNC
160  * @tc.require: The StopRefreshLNN operates normally.
161  */
162 HWTEST_F(DiscSdkOnlyL2Test, StopRefreshLNNTest001, TestSize.Level2)
163 {
164     int ret;
165     int tmpId = GetSubscribeId();
166 
167     ret = StopRefreshLNN(g_pkgName, tmpId);
168     EXPECT_TRUE(ret != 0);
169 }
170 
171 /**
172  * @tc.name: StopRefreshLNNTest005
173  * @tc.desc: Verify StopRefreshLNN again.
174  * @tc.in: Test Moudle, Test Number, Test Levels.
175  * @tc.out: NonZero
176  * @tc.type: FUNC
177  * @tc.require: The StopRefreshLNN operates normally.
178  */
179 HWTEST_F(DiscSdkOnlyL2Test, StopRefreshLNNTest002, TestSize.Level2)
180 {
181     int ret;
182     int tmpId = GetSubscribeId();
183 
184     g_sInfo.subscribeId = tmpId;
185     RefreshLNN(g_pkgName, &g_sInfo, &g_refreshCb);
186     ret = StopRefreshLNN(g_pkgName, tmpId);
187     EXPECT_TRUE(ret == 0);
188     ret = StopRefreshLNN(g_pkgName, tmpId);
189     EXPECT_TRUE(ret != 0);
190 }
191 } // namespace OHOS
192