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 <cstddef>
17 #include <cstdint>
18 #include "session.h"
19 #include "softbus_utils.h"
20 #include "sessionimpl_fuzzer.h"
21 #include "session_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::SessionImpl session;
30
31 int sessionId = *(reinterpret_cast<const int*>(data));
32 session.SetSessionId(sessionId);
33 session.GetSessionId();
34
35 std::string sessionName(data, data + size);
36 session.SetMySessionName(sessionName);
37 session.SetPeerSessionName(sessionName);
38 }
39
SetOpeTest(const uint8_t * data,size_t size)40 void SetOpeTest(const uint8_t* data, size_t size)
41 {
42 if ((data == nullptr) || (size < sizeof(pid_t)) || (size < sizeof(uid_t))) {
43 return;
44 }
45 Communication::SoftBus::SessionImpl session;
46 std::string deviceId(data, data + size);
47 session.SetPeerDeviceId(deviceId);
48 session.SetDeviceId(deviceId);
49 session.SetIsServer(true);
50
51 uid_t peerUid = *reinterpret_cast<const uid_t *>(data);
52 pid_t peerPid = *reinterpret_cast<const pid_t *>(data);
53 session.SetPeerUid(peerUid);
54 session.SetPeerPid(peerPid);
55
56 session.GetChannelId();
57 }
58
SendBytesTest(const uint8_t * data,size_t size)59 void SendBytesTest(const uint8_t* data, size_t size)
60 {
61 if ((data == nullptr) || (size == 0)) {
62 return;
63 }
64 Communication::SoftBus::SessionImpl session;
65 session.SendBytes(data, size);
66 }
67
68 } // namespace OHOS
69
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73 OHOS::SessionTest(data, size);
74 OHOS::SetOpeTest(data, size);
75 OHOS::SendBytesTest(data, size);
76 return 0;
77 }
78