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