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 <gtest/gtest.h>
17 
18 #include "avsession_log.h"
19 #include "avsession_dumper.h"
20 #include "avsession_proxy.h"
21 
22 using namespace testing::ext;
23 using namespace OHOS;
24 using namespace OHOS::AVSession;
25 
26 static const std::string ARGS_HELP = "-h";
27 static const std::string ILLEGAL_INFORMATION = "AVSession service, enter '-h' for usage.\n";
28 static const std::string ARGS_SHOW_METADATA = "-show_metadata";
29 static const std::string ARGS_SHOW_SESSION_INFO = "-show_session_info";
30 static const std::string ARGS_SHOW_CONTROLLER_INFO = "-show_controller_info";
31 static const std::string ARGS_SHOW_ERROR_INFO = "-show_error_info";
32 static const std::string ARGS_TRUSTED_DEVICES_INFO = "-show_trusted_devices_Info";
33 
34 class AVSessionDumperAnotherTest : public testing::Test {
35 public:
36     static void SetUpTestCase(void);
37     static void TearDownTestCase(void);
38     void SetUp();
39     void TearDown();
40 };
41 
SetUpTestCase()42 void AVSessionDumperAnotherTest::SetUpTestCase()
43 {
44 }
45 
TearDownTestCase()46 void AVSessionDumperAnotherTest::TearDownTestCase()
47 {
48 }
49 
SetUp()50 void AVSessionDumperAnotherTest::SetUp()
51 
52 {
53 }
54 
TearDown()55 void AVSessionDumperAnotherTest::TearDown()
56 {
57 }
58 
59 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowHelp, TestSize.Level1)
60 {
61     SLOGI("Dump__ShowHelp begin!");
62 
63     int32_t ret = AVSESSION_ERROR;
64 
65     AVSessionDumper avSessionDumper;
66     std::vector<std::string> args = {};
67     args.push_back(ARGS_HELP);
68 
69     int32_t systemAbilityId = 1;
70     bool runOnCreate = true;
71     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
72     sessionService->OnStart();
73 
74     sptr<IRemoteObject> sessionInner;
75     std::string tag = "tag";
76     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_VOICE_CALL;
77     std::string deviceId = "deviceId";
78     std::string bundleName = "bundleName";
79     std::string abilityName = "abilityName";
80     std::string moduleName = "moduleName";
81     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
82     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
83     EXPECT_TRUE(sessionInner != nullptr);
84     EXPECT_EQ(ret, AVSESSION_SUCCESS);
85 
86     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
87     std::string sessionId = avSessionProxy->GetSessionId();
88     sptr<IRemoteObject> controllerInner;
89     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
90     EXPECT_TRUE(controllerInner != nullptr);
91     EXPECT_EQ(ret, AVSESSION_SUCCESS);
92 
93     std::string result = "";
94     avSessionDumper.Dump(args, result, *sessionService);
95 
96     std::string resultExpect = "";
97     resultExpect.append("Usage:dump <command> [options]\n")
98         .append("Description:\n")
99         .append("-show_metadata               :show all avsession metadata in the system\n")
100         .append("-show_session_info           :show information of all sessions\n")
101         .append("-show_controller_info        :show information of all controllers \n")
102 
103         .append("-show_error_info             :show error information about avsession\n")
104         .append("-show_trusted_devices_Info   :show trusted devices Info\n");
105     EXPECT_EQ(result, resultExpect);
106 
107     sessionService->OnStop();
108     sessionService = nullptr;
109     avSessionProxy = nullptr;
110 
111     SLOGI("Dump__ShowHelp end!");
112 }
113 
114 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowMetaData, TestSize.Level1)
115 {
116     SLOGI("Dump__ShowMetaData begin!");
117 
118     int32_t ret = AVSESSION_ERROR;
119 
120     AVSessionDumper avSessionDumper;
121     std::vector<std::string> args = {};
122     args.push_back(ARGS_SHOW_METADATA);
123 
124     int32_t systemAbilityId = 1;
125     bool runOnCreate = true;
126     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
127     sessionService->OnStart();
128 
129     sptr<IRemoteObject> sessionInner;
130     std::string tag = "tag";
131     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_VOICE_CALL;
132     std::string deviceId = "deviceId";
133     std::string bundleName = "bundleName";
134     std::string abilityName = "abilityName";
135     std::string moduleName = "moduleName";
136     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
137     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
138     EXPECT_TRUE(sessionInner != nullptr);
139     EXPECT_EQ(ret, AVSESSION_SUCCESS);
140 
141     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
142     std::string sessionId = avSessionProxy->GetSessionId();
143     sptr<IRemoteObject> controllerInner;
144     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
145     EXPECT_TRUE(controllerInner != nullptr);
146     EXPECT_EQ(ret, AVSESSION_SUCCESS);
147 
148     std::string result = "";
149     avSessionDumper.Dump(args, result, *sessionService);
150     EXPECT_NE(result, "");
151     SLOGI("result: %{public}s", result.c_str());
152 
153     sessionService->OnStop();
154     sessionService = nullptr;
155     avSessionProxy = nullptr;
156 
157     SLOGI("Dump__ShowMetaData end!");
158 }
159 
160 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_AUDIO, TestSize.Level1)
161 {
162     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_AUDIO begin!");
163 
164     int32_t ret = AVSESSION_ERROR;
165 
166     AVSessionDumper avSessionDumper;
167     std::vector<std::string> args = {};
168     args.push_back(ARGS_SHOW_SESSION_INFO);
169 
170     int32_t systemAbilityId = 1;
171     bool runOnCreate = true;
172     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
173     sessionService->OnStart();
174 
175     sptr<IRemoteObject> sessionInner;
176     std::string tag = "tag";
177     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_AUDIO;
178     std::string deviceId = "deviceId";
179     std::string bundleName = "bundleName";
180     std::string abilityName = "abilityName";
181     std::string moduleName = "moduleName";
182     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
183     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
184     EXPECT_TRUE(sessionInner != nullptr);
185     EXPECT_EQ(ret, AVSESSION_SUCCESS);
186 
187     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
188     std::string sessionId = avSessionProxy->GetSessionId();
189     sptr<IRemoteObject> controllerInner;
190     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
191     EXPECT_TRUE(controllerInner != nullptr);
192     EXPECT_EQ(ret, AVSESSION_SUCCESS);
193 
194 
195     std::string result = "";
196     avSessionDumper.Dump(args, result, *sessionService);
197     EXPECT_NE(result, "");
198     SLOGI("result: %{public}s", result.c_str());
199 
200     sessionService->OnStop();
201     sessionService = nullptr;
202     avSessionProxy = nullptr;
203 
204 
205     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_AUDIO end!");
206 }
207 
208 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_VIDEO, TestSize.Level1)
209 {
210     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VIDEO begin!");
211 
212     int32_t ret = AVSESSION_ERROR;
213 
214     AVSessionDumper avSessionDumper;
215     std::vector<std::string> args = {};
216     args.push_back(ARGS_SHOW_SESSION_INFO);
217 
218     int32_t systemAbilityId = 1;
219     bool runOnCreate = true;
220     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
221     sessionService->OnStart();
222 
223     sptr<IRemoteObject> sessionInner;
224     std::string tag = "tag";
225     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_VIDEO;
226     std::string deviceId = "deviceId";
227     std::string bundleName = "bundleName";
228     std::string abilityName = "abilityName";
229     std::string moduleName = "moduleName";
230     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
231     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
232     EXPECT_TRUE(sessionInner != nullptr);
233     EXPECT_EQ(ret, AVSESSION_SUCCESS);
234 
235     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
236     std::string sessionId = avSessionProxy->GetSessionId();
237     sptr<IRemoteObject> controllerInner;
238     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
239     EXPECT_TRUE(controllerInner != nullptr);
240     EXPECT_EQ(ret, AVSESSION_SUCCESS);
241 
242     std::string result = "";
243     avSessionDumper.Dump(args, result, *sessionService);
244     EXPECT_NE(result, "");
245     SLOGI("result: %{public}s", result.c_str());
246 
247     sessionService->OnStop();
248     sessionService = nullptr;
249     avSessionProxy = nullptr;
250 
251     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VIDEO end!");
252 }
253 
254 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_VOICE_CALL, TestSize.Level1)
255 {
256     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VOICE_CALL begin!");
257 
258     int32_t ret = AVSESSION_ERROR;
259 
260     AVSessionDumper avSessionDumper;
261     std::vector<std::string> args = {};
262     args.push_back(ARGS_SHOW_SESSION_INFO);
263 
264     int32_t systemAbilityId = 1;
265     bool runOnCreate = true;
266     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
267     sessionService->OnStart();
268 
269     sptr<IRemoteObject> sessionInner;
270     std::string tag = "tag";
271     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_VOICE_CALL;
272     std::string deviceId = "deviceId";
273     std::string bundleName = "bundleName";
274     std::string abilityName = "abilityName";
275     std::string moduleName = "moduleName";
276     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
277     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
278     EXPECT_TRUE(sessionInner != nullptr);
279     EXPECT_EQ(ret, AVSESSION_SUCCESS);
280 
281     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
282     std::string sessionId = avSessionProxy->GetSessionId();
283     sptr<IRemoteObject> controllerInner;
284     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
285     EXPECT_TRUE(controllerInner != nullptr);
286     EXPECT_EQ(ret, AVSESSION_SUCCESS);
287 
288     std::string result = "";
289     avSessionDumper.Dump(args, result, *sessionService);
290     EXPECT_NE(result, "");
291     SLOGI("result: %{public}s", result.c_str());
292 
293     sessionService->OnStop();
294     sessionService = nullptr;
295     avSessionProxy = nullptr;
296 
297     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VOICE_CALL end!");
298 }
299 
300 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowSessionInfo__SESSION_TYPE_VIDEO_CALL, TestSize.Level1)
301 {
302     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VIDEO_CALL begin!");
303 
304     int32_t ret = AVSESSION_ERROR;
305 
306     AVSessionDumper avSessionDumper;
307     std::vector<std::string> args = {};
308     args.push_back(ARGS_SHOW_SESSION_INFO);
309 
310     int32_t systemAbilityId = 1;
311     bool runOnCreate = true;
312     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
313     sessionService->OnStart();
314 
315     sptr<IRemoteObject> sessionInner;
316     std::string tag = "tag";
317     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_VIDEO_CALL;
318     std::string deviceId = "deviceId";
319     std::string bundleName = "bundleName";
320     std::string abilityName = "abilityName";
321     std::string moduleName = "moduleName";
322     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
323     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
324     EXPECT_TRUE(sessionInner != nullptr);
325     EXPECT_EQ(ret, AVSESSION_SUCCESS);
326 
327     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
328     std::string sessionId = avSessionProxy->GetSessionId();
329     sptr<IRemoteObject> controllerInner;
330     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
331     EXPECT_TRUE(controllerInner != nullptr);
332     EXPECT_EQ(ret, AVSESSION_SUCCESS);
333 
334     std::string result = "";
335     avSessionDumper.Dump(args, result, *sessionService);
336     EXPECT_NE(result, "");
337     SLOGI("result: %{public}s", result.c_str());
338 
339     sessionService->OnStop();
340     sessionService = nullptr;
341     avSessionProxy = nullptr;
342 
343     SLOGI("Dump__ShowSessionInfo__SESSION_TYPE_VIDEO_CALL end!");
344 }
345 
346 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowControllerInfo, TestSize.Level1)
347 {
348     SLOGI("Dump__ShowControllerInfo begin!");
349 
350     int32_t ret = AVSESSION_ERROR;
351 
352     AVSessionDumper avSessionDumper;
353     std::vector<std::string> args = {};
354 
355     args.push_back(ARGS_SHOW_CONTROLLER_INFO);
356 
357     int32_t systemAbilityId = 1;
358     bool runOnCreate = true;
359     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
360     sessionService->OnStart();
361 
362     sptr<IRemoteObject> sessionInner;
363     std::string tag = "tag";
364     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_VIDEO_CALL;
365     std::string deviceId = "deviceId";
366     std::string bundleName = "bundleName";
367     std::string abilityName = "abilityName";
368     std::string moduleName = "moduleName";
369     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
370     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
371     EXPECT_TRUE(sessionInner != nullptr);
372     EXPECT_EQ(ret, AVSESSION_SUCCESS);
373 
374     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
375     std::string sessionId = avSessionProxy->GetSessionId();
376     sptr<IRemoteObject> controllerInner;
377     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
378     EXPECT_TRUE(controllerInner != nullptr);
379     EXPECT_EQ(ret, AVSESSION_SUCCESS);
380 
381     std::string result = "";
382     avSessionDumper.Dump(args, result, *sessionService);
383     EXPECT_NE(result, "");
384     SLOGI("result: %{public}s", result.c_str());
385 
386     sessionService->OnStop();
387     sessionService = nullptr;
388     avSessionProxy = nullptr;
389 
390     SLOGI("Dump__ShowControllerInfo end!");
391 }
392 
393 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowErrorInfo, TestSize.Level1)
394 {
395     SLOGI("Dump__ShowErrorInfo begin!");
396 
397     int32_t ret = AVSESSION_ERROR;
398 
399     AVSessionDumper avSessionDumper;
400     std::vector<std::string> args = {};
401     args.push_back(ARGS_SHOW_ERROR_INFO);
402 
403     int32_t systemAbilityId = 1;
404     bool runOnCreate = true;
405 
406     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
407     sessionService->OnStart();
408 
409     sptr<IRemoteObject> sessionInner;
410     std::string tag = "tag";
411     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_VIDEO_CALL;
412     std::string deviceId = "deviceId";
413     std::string bundleName = "bundleName";
414     std::string abilityName = "abilityName";
415     std::string moduleName = "moduleName";
416     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
417     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
418     EXPECT_TRUE(sessionInner != nullptr);
419     EXPECT_EQ(ret, AVSESSION_SUCCESS);
420 
421     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
422     std::string sessionId = avSessionProxy->GetSessionId();
423     sptr<IRemoteObject> controllerInner;
424     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
425     EXPECT_TRUE(controllerInner != nullptr);
426     EXPECT_EQ(ret, AVSESSION_SUCCESS);
427 
428     std::string resultExpect = "No Error Information!\n";
429     std::string result = "";
430     avSessionDumper.Dump(args, result, *sessionService);
431     EXPECT_EQ(result, resultExpect);
432 
433     std::string inErrMsg = "inErrMsg";
434     avSessionDumper.SetErrorInfo(inErrMsg);
435     result = "";
436     avSessionDumper.Dump(args, result, *sessionService);
437     EXPECT_NE(result, "");
438 
439     sessionService->OnStop();
440     sessionService = nullptr;
441     avSessionProxy = nullptr;
442 
443     SLOGI("Dump__ShowErrorInfo end!");
444 }
445 
446 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowTrustedDevicesInfo, TestSize.Level1)
447 {
448     SLOGI("Dump__ShowTrustedDevicesInfo begin!");
449 
450     int32_t ret = AVSESSION_ERROR;
451 
452     AVSessionDumper avSessionDumper;
453     std::vector<std::string> args = {};
454     args.push_back(ARGS_TRUSTED_DEVICES_INFO);
455 
456     int32_t systemAbilityId = 1;
457     bool runOnCreate = true;
458     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
459     sessionService->OnStart();
460 
461     sptr<IRemoteObject> sessionInner;
462     std::string tag = "tag";
463     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_VIDEO_CALL;
464     std::string deviceId = "deviceId";
465     std::string bundleName = "bundleName";
466     std::string abilityName = "abilityName";
467     std::string moduleName = "moduleName";
468     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
469     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
470     EXPECT_TRUE(sessionInner != nullptr);
471     EXPECT_EQ(ret, AVSESSION_SUCCESS);
472 
473     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
474     std::string sessionId = avSessionProxy->GetSessionId();
475     sptr<IRemoteObject> controllerInner;
476     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
477     EXPECT_TRUE(controllerInner != nullptr);
478     EXPECT_EQ(ret, AVSESSION_SUCCESS);
479 
480     std::string result = "";
481     avSessionDumper.Dump(args, result, *sessionService);
482     EXPECT_NE(result, "");
483     SLOGI("result: %{public}s", result.c_str());
484 
485     sessionService->OnStop();
486     sessionService = nullptr;
487     avSessionProxy = nullptr;
488 
489     SLOGI("Dump__ShowTrustedDevicesInfo end!");
490 }
491 
492 static HWTEST_F(AVSessionDumperAnotherTest, Dump__ShowIllegalInfo, TestSize.Level1)
493 {
494     SLOGI("Dump__ShowIllegalInfo begin!");
495 
496     int32_t ret = AVSESSION_ERROR;
497 
498     AVSessionDumper avSessionDumper;
499     std::vector<std::string> args = {};
500     args.push_back(ARGS_TRUSTED_DEVICES_INFO);
501     args.push_back(ARGS_TRUSTED_DEVICES_INFO);
502 
503     int32_t systemAbilityId = 1;
504     bool runOnCreate = true;
505     sptr<AVSessionService> sessionService = new AVSessionService(systemAbilityId, runOnCreate);
506 
507     sessionService->OnStart();
508 
509     sptr<IRemoteObject> sessionInner;
510     std::string tag = "tag";
511     int32_t type = OHOS::AVSession::AVSession::SESSION_TYPE_VIDEO_CALL;
512     std::string deviceId = "deviceId";
513     std::string bundleName = "bundleName";
514     std::string abilityName = "abilityName";
515     std::string moduleName = "moduleName";
516     AppExecFwk::ElementName elementName(deviceId, bundleName, abilityName, moduleName);
517     ret = sessionService->CreateSessionInner(tag, type, elementName, sessionInner);
518     EXPECT_TRUE(sessionInner != nullptr);
519     EXPECT_EQ(ret, AVSESSION_SUCCESS);
520 
521     sptr<AVSessionProxy> avSessionProxy = new AVSessionProxy(sessionInner);
522     std::string sessionId = avSessionProxy->GetSessionId();
523     sptr<IRemoteObject> controllerInner;
524     ret = sessionService->CreateControllerInner(sessionId, controllerInner);
525     EXPECT_TRUE(controllerInner != nullptr);
526     EXPECT_EQ(ret, AVSESSION_SUCCESS);
527 
528     std::string result = "";
529     avSessionDumper.Dump(args, result, *sessionService);
530     std::string resultExpect = ILLEGAL_INFORMATION;
531     EXPECT_EQ(result, resultExpect);
532 
533     sessionService->OnStop();
534     sessionService = nullptr;
535     avSessionProxy = nullptr;
536 
537     SLOGI("Dump__ShowIllegalInfo end!");
538 }