1 /*
2 * Copyright (c) 2023 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 "migrateavsession_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include <string>
21
22 #include "avsession_errors.h"
23 #include "avsession_item.h"
24 #include "avsession_service.h"
25 #include "migrate_avsession_server.h"
26 #include "migrate_avsession_manager.h"
27 #include "system_ability_definition.h"
28 #include "securec.h"
29
30 namespace OHOS::AVSession {
31 static const int32_t MAX_CODE_LEN = 512;
32 static const int32_t MIN_SIZE_NUM = 4;
33
34 static const uint8_t *RAW_DATA = nullptr;
35 static size_t g_dataSize = 0;
36 static size_t g_pos;
37
38 template<class T>
GetData()39 T GetData()
40 {
41 T object {};
42 size_t objectSize = sizeof(object);
43 if (RAW_DATA == nullptr || objectSize > g_dataSize - g_pos) {
44 return object;
45 }
46 errno_t ret = memcpy_s(&object, objectSize, RAW_DATA + g_pos, objectSize);
47 if (ret != EOK) {
48 return {};
49 }
50 g_pos += objectSize;
51 return object;
52 }
53
MigrateAVSessionFuzzerTest(uint8_t * data,size_t size)54 void MigrateAVSessionFuzzerTest(uint8_t* data, size_t size)
55 {
56 if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) {
57 return;
58 }
59 int32_t sessionId = GetData<uint8_t>();
60 std::string scene = std::to_string(GetData<uint8_t>());
61 std::string deviceId = std::to_string(GetData<uint8_t>());
62
63 std::shared_ptr<MigrateAVSessionServer> migrateServer_ = std::make_shared<MigrateAVSessionServer>();
64 sptr<AVSessionService> avservice_ = new AVSessionService(OHOS::AVSESSION_SERVICE_ID);
65 if (!avservice_) {
66 SLOGI("service is null");
67 return;
68 }
69 migrateServer_->Init(avservice_);
70 MigrateAVSessionManager::GetInstance().CreateLocalSessionStub(scene, migrateServer_);
71
72 migrateServer_->ConnectProxy(sessionId);
73 migrateServer_->OnConnectSession(sessionId);
74 migrateServer_->OnDisconnectProxy(deviceId);
75 MigrateAVSessionManager::GetInstance().ReleaseLocalSessionStub(scene);
76 }
77
78
79 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)80 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
81 {
82 RAW_DATA = data;
83 g_dataSize = size;
84 g_pos = 0;
85 /* Run your code on data */
86 MigrateAVSessionFuzzerTest(const_cast<uint8_t*>(data), size);
87 return 0;
88 }
89 } // namespace OHOS::AVSession
90