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 "sessionmock_fuzzer.h"
17
18 #include <cstddef>
19 #include <cstdint>
20 #include "session_mock.h"
21 #include "session.h"
22
23 namespace OHOS {
GetPkgNameInnerTest(const uint8_t * data,size_t size)24 void GetPkgNameInnerTest(const uint8_t* data, size_t size)
25 {
26 if ((data == nullptr) || (size < sizeof(int32_t))) {
27 return;
28 }
29 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
30 char *tmp = const_cast<char*>(reinterpret_cast<const char*>(data));
31 GetPkgNameInner(sessionId, tmp, size);
32 }
33
GetPeerDeviceIdInnerTest(const uint8_t * data,size_t size)34 void GetPeerDeviceIdInnerTest(const uint8_t* data, size_t size)
35 {
36 if ((data == nullptr) || (size < sizeof(int32_t))) {
37 return;
38 }
39 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
40 char *tmp = const_cast<char*>(reinterpret_cast<const char*>(data));
41 GetPeerDeviceIdInner(sessionId, tmp, size);
42 }
43
GetPeerSessionNameInnerTest(const uint8_t * data,size_t size)44 void GetPeerSessionNameInnerTest(const uint8_t* data, size_t size)
45 {
46 if ((data == nullptr) || (size < sizeof(int32_t))) {
47 return;
48 }
49
50 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
51 char *tmp = const_cast<char*>(reinterpret_cast<const char*>(data));
52 GetPeerSessionNameInner(sessionId, tmp, size);
53 }
54
GetMySessionNameInnerTest(const uint8_t * data,size_t size)55 void GetMySessionNameInnerTest(const uint8_t* data, size_t size)
56 {
57 if ((data == nullptr) || (size < sizeof(int32_t))) {
58 return;
59 }
60 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
61 char *tmp = const_cast<char*>(reinterpret_cast<const char*>(data));
62 GetMySessionNameInner(sessionId, tmp, size);
63 }
64
IsServerSideInnerTest(const uint8_t * data,size_t size)65 void IsServerSideInnerTest(const uint8_t* data, size_t size)
66 {
67 if ((data == nullptr) || (size < sizeof(int32_t))) {
68 return;
69 }
70 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
71 int *tmp = const_cast<int*>(reinterpret_cast<const int*>(data));
72 IsServerSideInner(sessionId, tmp);
73 }
74
GetPeerPidInnerTest(const uint8_t * data,size_t size)75 void GetPeerPidInnerTest(const uint8_t* data, size_t size)
76 {
77 if ((data == nullptr) || (size < sizeof(int32_t))) {
78 return;
79 }
80 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
81 int *tmp = const_cast<int*>(reinterpret_cast<const int*>(data));
82 GetPeerPidInner(sessionId, tmp);
83 }
84
GetPeerUidInnerTest(const uint8_t * data,size_t size)85 void GetPeerUidInnerTest(const uint8_t* data, size_t size)
86 {
87 if ((data == nullptr) || (size < sizeof(int32_t))) {
88 return;
89 }
90 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
91 int *tmp = const_cast<int*>(reinterpret_cast<const int*>(data));
92 GetPeerUidInner(sessionId, tmp);
93 }
94
SendBytesInnerTest(const uint8_t * data,size_t size)95 void SendBytesInnerTest(const uint8_t* data, size_t size)
96 {
97 if ((data == nullptr) || (size < sizeof(int32_t))) {
98 return;
99 }
100 int32_t sessionId = *(reinterpret_cast<const int32_t*>(data));
101 char *tmp = const_cast<char*>(reinterpret_cast<const char*>(data));
102 SendBytesInner(sessionId, tmp, size);
103 }
104
RemovePermissionInnerTest(const uint8_t * data,size_t size)105 void RemovePermissionInnerTest(const uint8_t* data, size_t size)
106 {
107 if ((data == nullptr) || (size == 0)) {
108 return;
109 }
110 RemovePermissionInner(nullptr);
111 }
112
CloseSessionInnerTest(const uint8_t * data,size_t size)113 void CloseSessionInnerTest(const uint8_t* data, size_t size)
114 {
115 if ((data == nullptr) || (size < sizeof(int32_t))) {
116 return;
117 }
118 int sessionId = *(reinterpret_cast<const int32_t*>(data));
119 CloseSessionInner(sessionId);
120 }
121
OpenSessionInnerTest(const uint8_t * data,size_t size)122 void OpenSessionInnerTest(const uint8_t* data, size_t size)
123 {
124 if ((data == nullptr) || (size == 0)) {
125 return;
126 }
127 #define SESSION_NAME_SIZE_MAX 256
128 #define DEVICE_ID_SIZE_MAX 65
129 #define GROUP_ID_SIZE_MAX 65
130 char mySessionName[SESSION_NAME_SIZE_MAX] = "ohos.fuzz.dms.test";
131 char peerSessionName[SESSION_NAME_SIZE_MAX] = "ohos.fuzz.dms.test";
132 char peerNetworkId[DEVICE_ID_SIZE_MAX] = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
133 char groupId[GROUP_ID_SIZE_MAX] = "TEST_GROUP_ID";
134 OpenSessionInner(mySessionName, peerSessionName, peerNetworkId, groupId, size);
135 }
136
RemoveSessionServerInnerTest(const uint8_t * data,size_t size)137 void RemoveSessionServerInnerTest(const uint8_t* data, size_t size)
138 {
139 if ((data == nullptr) || (size == 0)) {
140 return;
141 }
142 #define SESSION_NAME_SIZE_MAX 256
143 char mySessionName[SESSION_NAME_SIZE_MAX] = "ohos.fuzz.dms.test";
144 RemoveSessionServerInner(nullptr, mySessionName);
145 }
146 } // namespace OHOS
147
148 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)149 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
150 {
151 /* Run your code on data */
152 OHOS::GetPkgNameInnerTest(data, size);
153 OHOS::GetPeerDeviceIdInnerTest(data, size);
154 OHOS::GetPeerSessionNameInnerTest(data, size);
155 OHOS::GetMySessionNameInnerTest(data, size);
156 OHOS::IsServerSideInnerTest(data, size);
157 OHOS::GetPeerPidInnerTest(data, size);
158 OHOS::GetPeerUidInnerTest(data, size);
159 OHOS::SendBytesInnerTest(data, size);
160 OHOS::RemovePermissionInnerTest(data, size);
161 OHOS::CloseSessionInnerTest(data, size);
162 OHOS::OpenSessionInnerTest(data, size);
163 OHOS::RemoveSessionServerInnerTest(data, size);
164 return 0;
165 }
166