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         int 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         if ((data == nullptr) || (size == 0)) {
65             return;
66         }
67         #define SESSION_NAME_SIZE_MAX 256
68         #define DEVICE_ID_SIZE_MAX 65
69         #define GROUP_ID_SIZE_MAX 65
70         char peerSessionName[SESSION_NAME_SIZE_MAX] = "ohos.fuzz.dms.test";
71         char peerNetworkId[DEVICE_ID_SIZE_MAX] = "ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00ABCDEF00";
72         SessionAttribute attr = {
73             .dataType = TYPE_BYTES,
74         };
75         char groupId[GROUP_ID_SIZE_MAX] = "TEST_GROUP_ID";
76         OpenSessionSync(nullptr, peerSessionName, peerNetworkId, groupId, &attr);
77     }
78 } // namespace OHOS
79 
80 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)81 extern "C" int LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
82 {
83     /* Run your code on data */
84     OHOS::GetSessionKeyTest(data, size);
85     OHOS::GetSessionHandleTest(data, size);
86     OHOS::DisableSessionListenerTest(data, size);
87     OHOS::OpenSessionSyncTest(data, size);
88     return 0;
89 }
90