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