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 
21 #include "avsession_errors.h"
22 #include "avsession_item.h"
23 #include "avsession_service.h"
24 #include "migrate_avsession_server.h"
25 #include "migrate_avsession_manager.h"
26 #include "system_ability_definition.h"
27 
28 namespace OHOS::AVSession {
29 static const int32_t MAX_CODE_LEN = 512;
30 static const int32_t MIN_SIZE_NUM = 4;
31 
MigrateAVSessionFuzzerTest(uint8_t * data,size_t size)32 void MigrateAVSessionFuzzerTest(uint8_t* data, size_t size)
33 {
34     if ((data == nullptr) || (size > MAX_CODE_LEN) || (size < MIN_SIZE_NUM)) {
35         return;
36     }
37     int32_t sessionId = *reinterpret_cast<const int32_t*>(data);
38     std::string scene(reinterpret_cast<const char*>(data), size);
39     std::string deviceId(reinterpret_cast<const char*>(data), size);
40 
41     std::shared_ptr<MigrateAVSessionServer> migrateServer_ = std::make_shared<MigrateAVSessionServer>();
42     sptr<AVSessionService> avservice_ = new AVSessionService(OHOS::AVSESSION_SERVICE_ID);
43     if (!avservice_) {
44         SLOGI("service is null");
45         return;
46     }
47     migrateServer_->Init(avservice_);
48     MigrateAVSessionManager::GetInstance().CreateLocalSessionStub(scene, migrateServer_);
49 
50     migrateServer_->ConnectProxy(sessionId);
51     migrateServer_->OnConnectSession(sessionId);
52     migrateServer_->OnDisconnectProxy(deviceId);
53     MigrateAVSessionManager::GetInstance().ReleaseLocalSessionStub(scene);
54 }
55 
56 
57 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)58 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
59 {
60     /* Run your code on data */
61     MigrateAVSessionFuzzerTest(const_cast<uint8_t*>(data), size);
62     return 0;
63 }
64 } // namespace OHOS::AVSession
65