• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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 "medialibrary_mtp_headerdata_fuzzer.h"
16 
17 #include <cstdint>
18 #include <string>
19 #include <vector>
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #include "system_ability_definition.h"
23 #include "iservice_registry.h"
24 #include "userfilemgr_uri.h"
25 #include "payload_data.h"
26 #include "close_session_data.h"
27 #include "media_log.h"
28 
29 #define private public
30 #include "header_data.h"
31 #undef private
32 
33 namespace OHOS {
34 using namespace std;
35 using namespace Media;
36 static const int32_t NUM_BYTES = 1;
37 FuzzedDataProvider *provider = nullptr;
38 
HeaderDataTest()39 static void HeaderDataTest()
40 {
41     shared_ptr<MtpOperationContext> context = make_shared<MtpOperationContext>();
42     if (context == nullptr) {
43         MEDIA_ERR_LOG("context is nullptr");
44         return;
45     }
46     shared_ptr<HeaderData> headerData = make_shared<HeaderData>(context);
47 
48     vector<uint8_t> buffer = provider->ConsumeBytes<uint8_t>(NUM_BYTES);
49     int32_t readSize = buffer.size();
50 
51     headerData->Parser(buffer, readSize);
52     headerData->Maker(buffer);
53     headerData->SetCode(provider->ConsumeIntegral<uint16_t>());
54     headerData->SetContainerLength(provider->ConsumeIntegral<uint32_t>());
55     headerData->SetContainerType(provider->ConsumeIntegral<uint16_t>());
56     headerData->SetTransactionId(provider->ConsumeIntegral<uint32_t>());
57 
58     headerData->GetCode();
59     headerData->GetContainerLength();
60     headerData->GetContainerType();
61     headerData->GetTransactionId();
62 
63     headerData->Reset();
64 }
65 } // namespace OHOS
66 
67 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)68 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
69 {
70     /* Run your code on data */
71     FuzzedDataProvider fdp(data, size);
72     OHOS::provider = &fdp;
73     if (data == nullptr) {
74         return 0;
75     }
76     OHOS::HeaderDataTest();
77     return 0;
78 }