• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "streamadaptor_fuzzer.h"
17 
18 #include <string>
19 
20 #include "client_trans_udp_stream_interface.h"
21 #include "stream_adaptor.h"
22 
23 #define STANDARD_NUMBER 2
24 using namespace std;
25 
26 namespace OHOS {
SetAliveStateDataTest(const uint8_t * data,size_t size)27     void SetAliveStateDataTest(const uint8_t* data, size_t size)
28     {
29         if (data == nullptr || size < sizeof(int32_t)) {
30             return;
31         }
32         int32_t cnt = *(reinterpret_cast<const int32_t *>(data));
33         bool state;
34         if (cnt % STANDARD_NUMBER == 0) {
35             state = true;
36         } else {
37             state = false;
38         }
39         const std::string &pkgName = "ohos.msdp.spatialawareness";
40 
41         OHOS::StreamAdaptor streamadaptor(pkgName);
42         streamadaptor.SetAliveState(state);
43     }
44 
InitAdaptorTest(const uint8_t * data,size_t size)45     void InitAdaptorTest(const uint8_t* data, size_t size)
46     {
47         if (data == nullptr || size < sizeof(uint32_t)) {
48             return;
49         }
50         VtpStreamOpenParam param  = {
51             .pkgName = reinterpret_cast<const char *>(data),
52             .myIp = const_cast<char *>(reinterpret_cast<const char *>(data)),
53             .peerIp = const_cast<char *>(reinterpret_cast<const char *>(data)),
54             .peerPort = *(reinterpret_cast<const int32_t *>(data)),
55             .type = *(reinterpret_cast<const StreamType *>(data)),
56             .sessionKey = const_cast<uint8_t *>(data),
57             .keyLen = *(reinterpret_cast<const uint32_t *>(data)),
58             .isRawStreamEncrypt = size % 2,
59         };
60         int32_t channelId = *(reinterpret_cast<const int32_t *>(data));
61         IStreamListener *callback = nullptr;
62         const std::string &pkgName = "ohos.msdp.spatialawareness";
63         bool isServerSide = size % 2;
64 
65         OHOS::StreamAdaptor streamadaptor(pkgName);
66         streamadaptor.InitAdaptor(channelId, &param, isServerSide, callback);
67     }
68 } // namespace OHOS
69 
70 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)71 extern "C" int32_t LLVMFuzzerTestOneInput(const uint8_t* data, size_t size)
72 {
73     /* Run your code on data */
74     OHOS::SetAliveStateDataTest(data, size);
75     return 0;
76 }
77