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,int i)41 void SetNumebrInStreamData(char *streamData, int 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 int 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 int OnsessionOpened(int sessionId, int result);
63     static int OnCtrlsessionOpened(int sessionId, int result);
64     static void OnSessionClosed(int sessionId);
65     static void OnStreamReceived(int sessionId, const StreamData *data,
66         const StreamData *ext, const StreamFrameInfo *param);
67     static void OnBytesReceived(int sessionId, const void *data, unsigned int dataLen);
68 
69     virtual int OnProcessMsg(const string &msg, int len, string &strReturnValue, int returnValueLen);
70     int CreateTestSessionServer(string &strReturnValue);
71     int RemoverTestSessionServer(string &strReturnValue);
72     int TerminalServer(string &strReturnValue);
73 
74     static int contrlSessionId_;
75     static char sendBytes[BYTES_SIZE];
76 
77     using MsgFunc = int (DistributeStreamTestAgent::*)(string &);
78     static map<string, MsgFunc> msgFunMap;
79 };
80 
81 int DistributeStreamTestAgent::contrlSessionId_ = 0;
82 char DistributeStreamTestAgent::sendBytes[BYTES_SIZE];
83 map<string, DistributeStreamTestAgent::MsgFunc> DistributeStreamTestAgent::msgFunMap;
84 
OnsessionOpened(int sessionId,int result)85 int DistributeStreamTestAgent::OnsessionOpened(int sessionId, int result)
86 {
87     EXPECT_EQ(result, 0);
88 
89     return 0;
90 }
91 
OnCtrlsessionOpened(int sessionId,int result)92 int DistributeStreamTestAgent::OnCtrlsessionOpened(int sessionId, int result)
93 {
94     EXPECT_EQ(result, 0);
95     if (result == 0) {
96         contrlSessionId_ = sessionId;
97     }
98 
99     return 0;
100 }
101 
OnSessionClosed(int sessionId)102 void DistributeStreamTestAgent::OnSessionClosed(int sessionId)
103 {
104 }
105 
OnStreamReceived(int sessionId,const StreamData * data,const StreamData * ext,const StreamFrameInfo * param)106 void DistributeStreamTestAgent::OnStreamReceived(int sessionId, const StreamData *data,
107     const StreamData *ext, const StreamFrameInfo *param)
108 {
109     int i = GetNumebrInStreamData((const char*)data->buf);
110     if (i < 0) {
111         return;
112     }
113     SetNumebrInStreamData(sendBytes, i);
114     int ret = SendBytes(contrlSessionId_, sendBytes, BYTES_SIZE);
115     EXPECT_EQ(ret, 0);
116 }
117 
OnBytesReceived(int sessionId,const void * data,unsigned int dataLen)118 void DistributeStreamTestAgent::OnBytesReceived(int 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     SetAceessTokenPermission("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 int DistributeStreamTestAgent::CreateTestSessionServer(string &strReturnValue)
154 {
155     int 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 int DistributeStreamTestAgent::RemoverTestSessionServer(string &strReturnValue)
168 {
169     int 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 int DistributeStreamTestAgent::TerminalServer(string &strReturnValue)
180 {
181     g_isTerminal = true;
182     strReturnValue = "ok";
183     return strReturnValue.length();
184 }
185 
OnProcessMsg(const string & msg,int len,string & strReturnValue,int returnValueLen)186 int DistributeStreamTestAgent::OnProcessMsg(const string &msg, int len, string &strReturnValue, int returnValueLen)
187 {
188     cout << "receive message: " << msg <<endl;
189     map<string, MsgFunc>::iterator it = msgFunMap.find(msg);
190     if (it != msgFunMap.end()) {
191         MsgFunc msgFunc = msgFunMap[msg];
192         return (this->*msgFunc)(strReturnValue);
193     }
194 
195     return -1;
196 }
197 }
198 
main()199 int main()
200 {
201     OHOS::DistributeStreamTestAgent obj;
202     if (obj.SetUp()) {
203         obj.Start("agent.desc");
204         obj.Join();
205     } else {
206         cout << "init environment failed" << endl;
207     }
208 
209     while (!g_isTerminal) {
210         sleep(1);
211     }
212 
213     if (obj.TearDown()) {
214         return 0;
215     } else {
216         cout << "clear environment failed" << endl;
217         return -1;
218     }
219 }