1 /*
2 * Copyright (c) 2021-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 "clienttranssessionservice_fuzzer.h"
17 #include <cstddef>
18 #include <cstdint>
19 #include <securec.h>
20 #include "dfs_session.h"
21 #include "session.h"
22 #include "client_trans_session_service.h"
23
24 namespace OHOS {
GetSessionKeyTest(const uint8_t * data,size_t size)25 void GetSessionKeyTest(const uint8_t* data, size_t size)
26 {
27 #define SESSION_KEY_LENGTH 32
28 if ((data == nullptr) || (size < sizeof(int32_t))) {
29 return;
30 }
31 if (size > SESSION_KEY_LENGTH) {
32 return;
33 }
34 unsigned int len = SESSION_KEY_LENGTH;
35 int32_t sessionId = *(reinterpret_cast<const int32_t *>(data));
36 char tmp[SESSION_KEY_LENGTH + 1] = {0};
37 if (memcpy_s(tmp, sizeof(tmp) - 1, data, size) != EOK) {
38 return;
39 }
40 GetSessionKey(sessionId, tmp, len);
41 }
42
GetSessionHandleTest(const uint8_t * data,size_t size)43 void GetSessionHandleTest(const uint8_t* data, size_t size)
44 {
45 if ((data == nullptr) || (size < sizeof(int32_t))) {
46 return;
47 }
48 int32_t handle = 1;
49 int32_t sessionId = *(reinterpret_cast<const int32_t *>(data));
50 GetSessionHandle(sessionId, &handle);
51 }
52
DisableSessionListenerTest(const uint8_t * data,size_t size)53 void DisableSessionListenerTest(const uint8_t* data, size_t size)
54 {
55 if ((data == nullptr) || (size < sizeof(int32_t))) {
56 return;
57 }
58 int32_t sessionId = *(reinterpret_cast<const int32_t *>(data));
59 DisableSessionListener(sessionId);
60 }
61
OpenSessionSyncTest(const uint8_t * data,size_t size)62 void OpenSessionSyncTest(const uint8_t* data, size_t size)
63 {
64 #define SESSION_NAME_SIZE_MAX 256
65 #define DEVICE_ID_SIZE_MAX 65
66 #define GROUP_ID_SIZE_MAX 65
67 if (data == nullptr || size >= GROUP_ID_SIZE_MAX) {
68 return;
69 }
70 char mySessionName[SESSION_NAME_SIZE_MAX] = {0};
71 char peerSessionName[SESSION_NAME_SIZE_MAX] = {0};
72 char peerNetworkId[DEVICE_ID_SIZE_MAX] = {0};
73 char groupId[GROUP_ID_SIZE_MAX] = {0};
74 SessionAttribute attr = {
75 .dataType = TYPE_BYTES,
76 };
77 if (memcpy_s(mySessionName, SESSION_NAME_SIZE_MAX, data, size) != EOK) {
78 return;
79 }
80 if (memcpy_s(peerSessionName, SESSION_NAME_SIZE_MAX, data, size) != EOK) {
81 return;
82 }
83 if (memcpy_s(peerNetworkId, DEVICE_ID_SIZE_MAX, data, size) != EOK) {
84 return;
85 }
86 if (memcpy_s(groupId, GROUP_ID_SIZE_MAX, data, size) != EOK) {
87 return;
88 }
89 OpenSessionSync(mySessionName, peerSessionName, peerNetworkId, groupId, &attr);
90 }
91 } // namespace OHOS
92
93 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)94 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
95 {
96 /* Run your code on data */
97 OHOS::GetSessionKeyTest(data, size);
98 OHOS::GetSessionHandleTest(data, size);
99 OHOS::DisableSessionListenerTest(data, size);
100 OHOS::OpenSessionSyncTest(data, size);
101 return 0;
102 }
103