• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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  * miscservices under the License is miscservices 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 #include "distributed_stream_test.h"
16 
17 #include "gtest/gtest.h"
18 #include <cstring>
19 #include <ctime>
20 #include <iostream>
21 #include <semaphore.h>
22 #include <string>
23 #include <unistd.h>
24 #include <unordered_map>
25 #include <unordered_set>
26 #include "securec.h"
27 #include "softbus_bus_center.h"
28 #include "session.h"
29 #include "softbus_common.h"
30 #include "softbus_access_token_test.h"
31 #include "distributed_agent.h"
32 
33 using namespace std;
34 using namespace testing;
35 using namespace testing::ext;
36 using namespace OHOS::DistributeSystemTest;
37 
38 static bool g_isTerminal = false;
39 namespace OHOS {
40 
SetNumebrInStreamData(char * streamData,int32_t i)41 void SetNumebrInStreamData(char *streamData, int32_t i)
42 {
43     string strI = std::to_string(i);
44     char len = strI.length();
45     streamData[0] = len;
46     (void)memcpy_s(streamData + 1, len, strI.c_str(), len);
47 }
48 
GetNumebrInStreamData(const char * streamData)49 int32_t GetNumebrInStreamData(const char *streamData)
50 {
51     char len = streamData[0];
52     string str(streamData + 1, len);
53 
54     return std::stoi(str);
55 }
56 
57 class DistributeStreamTestAgent : public DistributedAgent {
58 public:
59     virtual bool SetUp();
60     virtual bool TearDown();
61 
62     static int32_t OnsessionOpened(int32_t sessionId, int32_t result);
63     static int32_t OnCtrlsessionOpened(int32_t sessionId, int32_t result);
64     static void OnSessionClosed(int32_t sessionId);
65     static void OnStreamReceived(int32_t sessionId, const StreamData *data,
66         const StreamData *ext, const StreamFrameInfo *param);
67     static void OnBytesReceived(int32_t sessionId, const void *data, unsigned int dataLen);
68 
69     virtual int32_t OnProcessMsg(const string &msg, int32_t len, string &strReturnValue, int32_t returnValueLen);
70     int32_t CreateTestSessionServer(string &strReturnValue);
71     int32_t RemoverTestSessionServer(string &strReturnValue);
72     int32_t TerminalServer(string &strReturnValue);
73 
74     static int32_t contrlSessionId_;
75     static char sendBytes[BYTES_SIZE];
76 
77     using MsgFunc = int32_t (DistributeStreamTestAgent::*)(string &);
78     static map<string, MsgFunc> msgFunMap;
79 };
80 
81 int32_t DistributeStreamTestAgent::contrlSessionId_ = 0;
82 char DistributeStreamTestAgent::sendBytes[BYTES_SIZE];
83 map<string, DistributeStreamTestAgent::MsgFunc> DistributeStreamTestAgent::msgFunMap;
84 
OnsessionOpened(int32_t sessionId,int32_t result)85 int32_t DistributeStreamTestAgent::OnsessionOpened(int32_t sessionId, int32_t result)
86 {
87     EXPECT_EQ(result, 0);
88 
89     return 0;
90 }
91 
OnCtrlsessionOpened(int32_t sessionId,int32_t result)92 int32_t DistributeStreamTestAgent::OnCtrlsessionOpened(int32_t sessionId, int32_t result)
93 {
94     EXPECT_EQ(result, 0);
95     if (result == 0) {
96         contrlSessionId_ = sessionId;
97     }
98 
99     return 0;
100 }
101 
OnSessionClosed(int32_t sessionId)102 void DistributeStreamTestAgent::OnSessionClosed(int32_t sessionId)
103 {
104 }
105 
OnStreamReceived(int32_t sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)106 void DistributeStreamTestAgent::OnStreamReceived(int32_t sessionId, const StreamData *data,
107     const StreamData *ext, const StreamFrameInfo *param)
108 {
109     int32_t i = GetNumebrInStreamData((const char*)data->buf);
110     if (i < 0) {
111         return;
112     }
113     SetNumebrInStreamData(sendBytes, i);
114     int32_t ret = SendBytes(contrlSessionId_, sendBytes, BYTES_SIZE);
115     EXPECT_EQ(ret, 0);
116 }
117 
OnBytesReceived(int32_t sessionId,const void * data,unsigned int dataLen)118 void DistributeStreamTestAgent::OnBytesReceived(int32_t sessionId, const void *data, unsigned int dataLen)
119 {
120 }
121 
122 static ISessionListener g_listener = {
123     .OnSessionOpened = DistributeStreamTestAgent::OnsessionOpened,
124     .OnSessionClosed = DistributeStreamTestAgent::OnSessionClosed,
125     .OnBytesReceived = DistributeStreamTestAgent::OnBytesReceived,
126     .OnStreamReceived = DistributeStreamTestAgent::OnStreamReceived
127 };
128 
129 static ISessionListener g_ctrllistener = {
130     .OnSessionOpened = DistributeStreamTestAgent::OnCtrlsessionOpened,
131     .OnSessionClosed = DistributeStreamTestAgent::OnSessionClosed,
132     .OnBytesReceived = DistributeStreamTestAgent::OnBytesReceived,
133     .OnStreamReceived = DistributeStreamTestAgent::OnStreamReceived
134 };
135 
SetUp()136 bool DistributeStreamTestAgent::SetUp()
137 {
138     msgFunMap["createSessionServer"] = &DistributeStreamTestAgent::CreateTestSessionServer;
139     msgFunMap["TerminalServer"] = &DistributeStreamTestAgent::TerminalServer;
140     msgFunMap["removeSessionServer"] = &DistributeStreamTestAgent::RemoverTestSessionServer;
141 
142     SetAccessTokenPermission("distributed_stream_test");
143 
144     cout << "agent start" <<endl;
145     return true;
146 }
147 
TearDown()148 bool DistributeStreamTestAgent::TearDown()
149 {
150     return true;
151 }
152 
CreateTestSessionServer(string & strReturnValue)153 int32_t DistributeStreamTestAgent::CreateTestSessionServer(string &strReturnValue)
154 {
155     int32_t ret = CreateSessionServer(TEST_PKG_NAME.c_str(), STREAM_SESSION_NAME.c_str(), &g_listener);
156     EXPECT_EQ(ret, 0);
157     cout << "pkgName : " << TEST_PKG_NAME << ", sessionName : " << STREAM_SESSION_NAME << endl;
158 
159     ret = CreateSessionServer(TEST_PKG_NAME.c_str(), CONTRL_SESSION_NAME.c_str(), &g_ctrllistener);
160     EXPECT_EQ(ret, 0);
161     cout << "pkgName : " << TEST_PKG_NAME << ", sessionName : " << CONTRL_SESSION_NAME << endl;
162 
163     strReturnValue = "ok";
164     return strReturnValue.length();
165 }
166 
RemoverTestSessionServer(string & strReturnValue)167 int32_t DistributeStreamTestAgent::RemoverTestSessionServer(string &strReturnValue)
168 {
169     int32_t ret = RemoveSessionServer(TEST_PKG_NAME.c_str(), STREAM_SESSION_NAME.c_str());
170     EXPECT_EQ(ret, 0);
171 
172     ret = RemoveSessionServer(TEST_PKG_NAME.c_str(), CONTRL_SESSION_NAME.c_str());
173     EXPECT_EQ(ret, 0);
174 
175     strReturnValue = "ok";
176     return strReturnValue.length();
177 }
178 
TerminalServer(string & strReturnValue)179 int32_t DistributeStreamTestAgent::TerminalServer(string &strReturnValue)
180 {
181     g_isTerminal = true;
182     strReturnValue = "ok";
183     return strReturnValue.length();
184 }
185 
OnProcessMsg(const string & msg,int32_t len,string & strReturnValue,int32_t returnValueLen)186 int32_t DistributeStreamTestAgent::OnProcessMsg(const string &msg, int32_t len,
187                                                 string &strReturnValue, int32_t returnValueLen)
188 {
189     cout << "receive message: " << msg <<endl;
190     map<string, MsgFunc>::iterator it = msgFunMap.find(msg);
191     if (it != msgFunMap.end()) {
192         MsgFunc msgFunc = msgFunMap[msg];
193         return (this->*msgFunc)(strReturnValue);
194     }
195 
196     return -1;
197 }
198 }
199 
main()200 int32_t main()
201 {
202     OHOS::DistributeStreamTestAgent obj;
203     if (obj.SetUp()) {
204         obj.Start("agent.desc");
205         obj.Join();
206     } else {
207         cout << "init environment failed" << endl;
208     }
209 
210     while (!g_isTerminal) {
211         sleep(1);
212     }
213 
214     if (obj.TearDown()) {
215         return 0;
216     } else {
217         cout << "clear environment failed" << endl;
218         return -1;
219     }
220 }
221