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 "sessionserviceimpl_fuzzer.h"
17 
18 #include "session.h"
19 #include "session_service_impl.h"
20 
21 namespace OHOS {
SessionTest(const uint8_t * data,size_t size)22 void SessionTest(const uint8_t* data, size_t size)
23 {
24     if ((data == nullptr) || (size < sizeof(int))) {
25         return;
26     }
27     Communication::SoftBus::SessionServiceImpl sessionService;
28 
29     int flags = *(reinterpret_cast<const int*>(data));
30     std::string sessionName(data, data + size);
31     std::string peersessionName(data, data + size);
32     std::string peerNetworkId(data, data + size);
33     std::string groupId(data, data + size);
34 
35     std::shared_ptr<Communication::SoftBus::Session> session =
36         sessionService.OpenSession(sessionName, peersessionName, peerNetworkId, groupId, flags);
37     sessionService.CloseSession(session);
38 
39     int sessionId = *(reinterpret_cast<const int*>(data));
40     sessionService.OpenSessionCallback(sessionId);
41     sessionService.CloseSessionCallback(sessionId);
42 }
43 
RemovePermissionTest(const uint8_t * data,size_t size)44 void RemovePermissionTest(const uint8_t* data, size_t size)
45 {
46     if ((data == nullptr) || (size == 0)) {
47         return;
48     }
49     Communication::SoftBus::SessionServiceImpl sessionService;
50     std::string busName(data, data + size);
51     sessionService.RemovePermission(busName);
52 }
53 
ReceivedCallbackTest(const uint8_t * data,size_t size)54 void ReceivedCallbackTest(const uint8_t* data, size_t size)
55 {
56     if ((data == nullptr) || (size < sizeof(int))) {
57         return;
58     }
59     Communication::SoftBus::SessionServiceImpl sessionService;
60     int len = *(reinterpret_cast<const int*>(data));
61     sessionService.BytesReceivedCallback(size,  data, len);
62     sessionService.MessageReceivedCallback(size,  data, len);
63 }
64 
65 } // namespace OHOS
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
69 {
70     OHOS::SessionTest(data, size);
71     OHOS::RemovePermissionTest(data, size);
72     OHOS::ReceivedCallbackTest(data, size);
73     return 0;
74 }
75