1 /*
2  * Copyright (c) 2024 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 #include "transportmsghandle_fuzzer.h"
16 
17 #include <cstddef>
18 #include <cstdint>
19 #include <memory>
20 
21 #include "cloud_fuzzer_helper.h"
22 #include "message_handler.h"
23 
24 namespace OHOS {
25 constexpr size_t U16_AT_SIZE = 2;
26 constexpr size_t U32_AT_SIZE = 4;
27 constexpr size_t U64_AT_SIZE = 8;
28 constexpr int SPLITE_SIZE = 3;
29 
30 using namespace std;
31 using namespace OHOS::FileManagement::CloudSync;
32 
DoSomethingInterestingWithMyAPI(const uint8_t * data,size_t size)33 bool DoSomethingInterestingWithMyAPI(const uint8_t *data, size_t size)
34 {
35     FuzzData fuzzData(data, size);
36     uint16_t msgType = fuzzData.GetData<uint16_t>();
37     int32_t errorCode = fuzzData.GetData<int32_t>();
38     int32_t userId = fuzzData.GetData<int32_t>();
39     uint64_t taskId = fuzzData.GetData<uint64_t>();
40     int len = static_cast<int>(fuzzData.GetRemainSize()) / SPLITE_SIZE;
41     if (len <= 0) {
42         return false;
43     }
44     std::string srcNetworkId = fuzzData.GetStringFromData(len);
45     std::string dstNetworkId = fuzzData.GetStringFromData(len);
46     std::string uri = fuzzData.GetStringFromData(len);
47     MessageInputInfo msgInputInfo = {.srcNetworkId = srcNetworkId,
48                                      .dstNetworkId = dstNetworkId,
49                                      .uri = uri,
50                                      .msgType = msgType,
51                                      .errorCode = errorCode,
52                                      .userId = userId,
53                                      .taskId = taskId};
54     MessageHandler msgHandle(msgInputInfo);
55     auto dataSubPtr = std::make_unique<uint8_t[]>(size);
56     msgHandle.PackData(dataSubPtr.get(), static_cast<uint32_t>(size));
57     MessageHandler msgHandleSub;
58     msgHandleSub.UnPackData(dataSubPtr.get(), msgHandle.GetDataSize());
59     msgHandleSub.GetTaskId();
60     msgHandleSub.GetMsgType();
61     msgHandleSub.GetUserId();
62     msgHandleSub.GetErrorCode();
63     msgHandleSub.GetUri();
64     msgHandleSub.GetSrcNetworkId();
65     msgHandleSub.GetDstNetworkId();
66     return true;
67 }
68 } // namespace OHOS
69 
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
72 {
73     /* Run your code on data */
74     if (data == nullptr || size <= (OHOS::U16_AT_SIZE + OHOS::U64_AT_SIZE + (OHOS::U32_AT_SIZE << 1))) {
75         return 0;
76     }
77 
78     OHOS::DoSomethingInterestingWithMyAPI(data, size);
79     return 0;
80 }
81