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 #include <string>
16 #include <memory>
17
18 #include "avsession_errors.h"
19 #include "avsession_manager_impl.h"
20 #include "avsessionmanager_fuzzer.h"
21
22 using namespace std;
23 using namespace OHOS;
24 using namespace OHOS::AVSession;
25
26 constexpr int32_t MAX_CODE_LEN = 512;
27 constexpr int32_t TIME = 1000;
28 constexpr int32_t MIN_SIZE_NUM = 4;
29 static char g_testBundleName[] = "test.ohos.avsession";
30 static char g_testAbilityName[] = "test.ability";
31
AVSessionManagerFuzzTest(const uint8_t * data,size_t size)32 bool AVSessionManagerFuzzer::AVSessionManagerFuzzTest(const uint8_t* data, size_t size)
33 {
34 if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) {
35 return false;
36 }
37
38 int32_t type = *reinterpret_cast<const int32_t *>(data);
39 std::string tag(reinterpret_cast<const char *>(data), size);
40 OHOS::AppExecFwk::ElementName elementName;
41 elementName.SetBundleName(g_testBundleName);
42 elementName.SetAbilityName(g_testAbilityName);
43
44 std::shared_ptr <AVSession> avSession = AVSessionManager::GetInstance().CreateSession(tag, type, elementName);
45 if (!avSession) {
46 SLOGI("avSession is null");
47 return false;
48 }
49 std::shared_ptr <AVSessionController> avSessionController;
50 int32_t ret = AVSessionManager::GetInstance().CreateController(avSession->GetSessionId(), avSessionController);
51 if (ret != AVSESSION_SUCCESS) {
52 SLOGI("CreateController fail");
53 return false;
54 }
55 if (!avSessionController) {
56 SLOGI("avSessionController is null");
57 return false;
58 }
59
60 if (avSession != nullptr) {
61 avSession->Destroy();
62 }
63 if (avSessionController != nullptr) {
64 avSessionController->Destroy();
65 }
66
67 bool result = SendSystemControlCommandFuzzTest(data);
68 return result == AVSESSION_SUCCESS;
69 }
70
SendSystemControlCommandFuzzTest(const uint8_t * data)71 bool AVSessionManagerFuzzer::SendSystemControlCommandFuzzTest(const uint8_t *data)
72 {
73 std::shared_ptr<TestSessionListener> listener = std::make_shared<TestSessionListener>();
74 if (!listener) {
75 SLOGI("listener is null");
76 return false;
77 }
78 auto result = AVSessionManager::GetInstance().RegisterSessionListener(listener);
79 auto keyEvent = OHOS::MMI::KeyEvent::Create();
80 if (!keyEvent) {
81 SLOGI("keyEvent is null");
82 return false;
83 }
84 int32_t keyCode = *reinterpret_cast<const int32_t*>(data);
85 keyEvent->SetKeyCode(keyCode);
86 keyEvent->SetKeyAction(*reinterpret_cast<const int32_t*>(data));
87 keyEvent->SetActionTime(TIME);
88 auto keyItem = OHOS::MMI::KeyEvent::KeyItem();
89 keyItem.SetKeyCode(*reinterpret_cast<const int32_t*>(data));
90 keyItem.SetDownTime(TIME);
91 keyItem.SetPressed(true);
92 keyEvent->AddKeyItem(keyItem);
93 result = AVSessionManager::GetInstance().SendSystemAVKeyEvent(*keyEvent);
94 AVControlCommand command;
95 command.SetCommand(*reinterpret_cast<const int32_t*>(data));
96 result = AVSessionManager::GetInstance().SendSystemControlCommand(command);
97
98 return result;
99 }
100
AVSessionManagerInterfaceTest(uint8_t * data,size_t size)101 bool OHOS::AVSession::AVSessionManagerInterfaceTest(uint8_t* data, size_t size)
102 {
103 auto avSessionManager = std::make_unique<AVSessionManagerFuzzer>();
104 if (avSessionManager == nullptr) {
105 SLOGI("avSessionManagerFuzzer is null");
106 return false;
107 }
108 return avSessionManager->AVSessionManagerFuzzTest(data, size);
109 }
110
AVSessionManagerTest(uint8_t * data,size_t size)111 void OHOS::AVSession::AVSessionManagerTest(uint8_t* data, size_t size)
112 {
113 if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) {
114 SLOGI("Invalid data");
115 return;
116 }
117
118 std::vector<AVSessionDescriptor> descriptors;
119 std::string avSessionId(reinterpret_cast<const char*>(data), size);
120 AVSessionDescriptor avSessionDescriptor;
121 avSessionDescriptor.sessionId_ = avSessionId;
122 descriptors.push_back(avSessionDescriptor);
123
124 std::string sessionId(reinterpret_cast<const char*>(data), size);
125 std::shared_ptr<AVSessionController> controller;
126
127 std::string bySessionId(reinterpret_cast<const char*>(data), size);
128 AVSessionDescriptor descriptor;
129 int32_t maxSize = 10;
130 AVControlCommand command;
131 int32_t cmd = *(reinterpret_cast<const int32_t*>(data));
132 command.SetCommand(cmd);
133 SessionToken sessionToken;
134 sessionToken.sessionId = sessionId;
135 std::vector<AudioStandard::AudioDeviceDescriptor> deviceDescriptor;
136
137 AVSessionManagerImpl avSessionManagerImpl;
138 avSessionManagerImpl.GetAllSessionDescriptors(descriptors);
139 avSessionManagerImpl.GetHistoricalSessionDescriptors(maxSize, descriptors);
140 avSessionManagerImpl.CreateController(sessionId, controller);
141 avSessionManagerImpl.GetActivatedSessionDescriptors(descriptors);
142 avSessionManagerImpl.GetSessionDescriptorsBySessionId(bySessionId, descriptor);
143 avSessionManagerImpl.SendSystemControlCommand(command);
144 avSessionManagerImpl.CastAudio(sessionToken, deviceDescriptor);
145 }
146
147 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(uint8_t * data,size_t size)148 extern "C" int LLVMFuzzerTestOneInput(uint8_t* data, size_t size)
149 {
150 /* Run your code on data */
151 OHOS::AVSession::AVSessionManagerInterfaceTest(data, size);
152 OHOS::AVSession::AVSessionManagerTest(data, size);
153 return 0;
154 }