• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "inner_session.h"
24 #include "session.h"
25 #include "softbus_utils.h"
26 
27 using namespace testing::ext;
28 
29 namespace OHOS {
30 enum TEST_PROCESS {
31     TEST_INICIAL = 0,
32     TEST_BEGIN,
33     TEST_DEVICEFOUND,
34     TEST_SESSIONOPEN,
35     TEST_DATARECEIVE,
36     TEST_SESSIONCLOSE,
37 };
38 const char *g_pkgName = "com.plrdtest";
39 const char *g_sessionName = "com.plrdtest.dsoftbus";
40 const char *g_testData = "{\"data\":\"open auth session test!!!\"}";
41 bool g_state = false;
42 int32_t g_sessionId = -1;
43 int32_t g_testCount = 0;
44 int32_t g_testTimes = 0;
45 static void Wait(void);
46 static void Start(void);
47 
48 ConnectionAddr g_addr;
49 ConnectionAddr g_addr1;
50 class BleAuthChannelPhoneTest : public testing::Test {
51 public:
BleAuthChannelPhoneTest()52     BleAuthChannelPhoneTest()
53     {}
~BleAuthChannelPhoneTest()54     ~BleAuthChannelPhoneTest()
55     {}
56     static void SetUpTestCase(void);
57     static void TearDownTestCase(void);
SetUp()58     void SetUp() override
59     {}
TearDown()60     void TearDown() override
61     {}
62 };
63 
SetUpTestCase(void)64 void BleAuthChannelPhoneTest::SetUpTestCase(void)
65 {
66     printf("input test times:");
67     if (scanf_s("%d", &g_testTimes, sizeof(g_testTimes)) < 0) {
68         printf("input error!\n");
69     }
70     getchar();
71 }
72 
TearDownTestCase(void)73 void BleAuthChannelPhoneTest::TearDownTestCase(void)
74 {}
75 
76 static SubscribeInfo g_sInfo = {
77     .subscribeId = 1,
78     .medium = BLE,
79     .mode = DISCOVER_MODE_ACTIVE,
80     .freq = MID,
81     .capability = "dvKit",
82     .capabilityData = (unsigned char *)"capdata3",
83     .dataLen = sizeof("capdata3"),
84     .isSameAccount = false,
85     .isWakeRemote = false
86 };
87 
OnSessionOpened(int32_t sessionId,int32_t result)88 static int32_t OnSessionOpened(int32_t sessionId, int32_t result)
89 {
90     printf("[test]session opened,sesison id = %d\r\n", sessionId);
91     EXPECT_TRUE(g_sessionId == sessionId);
92     EXPECT_TRUE(g_testCount == TEST_DEVICEFOUND);
93     g_testCount++;
94     Start();
95     return 0;
96 }
97 
OnSessionClosed(int32_t sessionId)98 static void OnSessionClosed(int32_t sessionId)
99 {
100     printf("[test]session closed, session id = %d\r\n", sessionId);
101 }
102 
OnBytesReceived(int32_t sessionId,const void * data,unsigned int len)103 static void OnBytesReceived(int32_t sessionId, const void *data, unsigned int len)
104 {
105     printf("[test]session bytes received, session id = %d data =%s\r\n", sessionId, data);
106 }
107 
OnMessageReceived(int32_t sessionId,const void * data,unsigned int len)108 static void OnMessageReceived(int32_t sessionId, const void *data, unsigned int len)
109 {
110     printf("[test]session msg received, session id = %d data =%s\r\n", sessionId, data);
111 }
112 
113 static ISessionListener g_sessionlistener = {
114     .OnSessionOpened = OnSessionOpened,
115     .OnSessionClosed = OnSessionClosed,
116     .OnBytesReceived = OnBytesReceived,
117     .OnMessageReceived = OnMessageReceived,
118 };
119 
Wait(void)120 static void Wait(void)
121 {
122     printf("[test]wait enter...\r\n");
123     do {
124         sleep(1);
125     } while (!g_state);
126     printf("[test]wait end!\r\n");
127     g_state = false;
128 }
129 
Start(void)130 static void Start(void)
131 {
132     g_state = true;
133 }
134 
TestCreateSessionServer()135 static int32_t TestCreateSessionServer()
136 {
137     printf("[test]TestCreateSessionServer enter\r\n");
138     int32_t ret = CreateSessionServer(g_pkgName, g_sessionName, &g_sessionlistener);
139     EXPECT_TRUE(ret == 0);
140     printf("[test]TestCreateSessionServer end\r\n");
141     return ret;
142 }
143 
TestOpenSession()144 static int32_t TestOpenSession()
145 {
146     printf("[test]TestOpenSession enter\r\n");
147     g_addr1.type = CONNECTION_ADDR_BLE;
148     int32_t ret = OpenAuthSession(g_sessionName, &g_addr1, 1, nullptr);
149     EXPECT_TRUE(ret >= 0);
150     printf("[test]TestOpenSession end\r\n");
151     return ret;
152 }
153 
TestSendData(const char * data,int32_t len)154 static int32_t TestSendData(const char *data, int32_t len)
155 {
156     printf("[test]TestSendData enter\r\n");
157     int32_t  ret = SendBytes(g_sessionId, data, len);
158     EXPECT_TRUE(ret == 0);
159     printf("[test]TestSendData end\r\n");
160     return ret;
161 }
162 
TestCloseSeeesion()163 static void TestCloseSeeesion()
164 {
165     printf("[test]TestCloseSession enter\n");
166     if (g_sessionId > 0) {
167         CloseSession(g_sessionId);
168         g_sessionId = -1;
169     }
170     printf("[test]TestCloseSession end\n");
171 }
172 
TestRemoveSessionServer()173 static int32_t TestRemoveSessionServer()
174 {
175     printf("[test]TestRemoveSessionServer enter\r\n");
176     int32_t ret = RemoveSessionServer(g_pkgName, g_sessionName);
177     EXPECT_TRUE(ret == 0);
178     printf("[test]TestRemoveSessionServer end\r\n");
179     return ret;
180 }
181 
182 /**
183  * @tc.name: PublishServiceTest001
184  * @tc.desc: Verify wrong parameter
185  * @tc.type: FUNC
186  * @tc.require:
187  */
188 HWTEST_F(BleAuthChannelPhoneTest, ProcessPhoneActive001, TestSize.Level1)
189 {
190     int32_t ret;
191     g_testCount = TEST_BEGIN;
192     Wait();
193     ret = TestCreateSessionServer();
194     EXPECT_TRUE(ret == 0);
195     EXPECT_TRUE(g_testCount == TEST_DEVICEFOUND);
196     for (int32_t i = 0; i < g_testTimes; i++) {
197         g_testCount = TEST_DEVICEFOUND;
198         g_sessionId = TestOpenSession();
199         EXPECT_TRUE(g_sessionId >= 0);
200         Wait();
201         EXPECT_TRUE(g_testCount == TEST_SESSIONOPEN);
202         ret = TestSendData(g_testData, strlen(g_testData) + 1);
203         EXPECT_TRUE(ret == 0);
204         sleep(3);
205         TestCloseSeeesion();
206         sleep(3);
207     }
208     ret = TestRemoveSessionServer();
209     EXPECT_TRUE(ret == 0);
210 END:
211     EXPECT_TRUE(TEST_INICIAL == 0);
212 };
213 }