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