• 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_property_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 "mtp_packet_tools.h"
31 #include "property.h"
32 #undef private
33 
34 namespace OHOS {
35 using namespace std;
36 using namespace Media;
37 static const int32_t NUM_BYTES = 1;
38 FuzzedDataProvider *provider = nullptr;
39 
FuzzVectorInt32()40 static inline vector<int32_t> FuzzVectorInt32()
41 {
42     return {provider->ConsumeIntegral<int32_t>()};
43 }
44 
45 // PropertyTest start
PropertySetFormEnumTest()46 static void PropertySetFormEnumTest()
47 {
48     uint16_t propCode = provider->ConsumeIntegral<uint16_t>();
49     uint16_t propType = provider->ConsumeIntegral<uint16_t>();
50     bool propWriteable = provider->ConsumeBool();
51     int value = provider->ConsumeIntegral<int32_t>();
52     Property property(propCode, propType, propWriteable, value);
53 
54     property.SetFormRange(0, 0, 0);
55     vector<int> values = FuzzVectorInt32();
56     property.SetFormEnum(values);
57     property.Dump();
58 
59     property.GetPropertyCode();
60     property.GetDataType();
61     Property  propertyTest;
62     propertyTest.GetDataType();
63 
64     property.SetFormDateTime();
65     property.IsDeviceProperty();
66     property.IsArrayType();
67 }
68 
PropertyWriteTest()69 static void PropertyWriteTest()
70 {
71     uint16_t propCode = provider->ConsumeIntegral<uint16_t>();
72     Property property(propCode, MTP_TYPE_UINT8_CODE);
73     vector<uint8_t> buffer = provider->ConsumeBytes<uint8_t>(NUM_BYTES);
74     property.Write(buffer);
75     size_t offsetTest = 0;
76     property.Read(buffer, offsetTest);
77     shared_ptr<string> str = make_shared<string>(provider->ConsumeBytesAsString(NUM_BYTES));
78     property.SetDefaultValue(str);
79     property.SetCurrentValue(str);
80     property.GetCurrentValue();
81     property.IsArrayType();
82 }
83 
PropertyStringTest()84 static void PropertyStringTest()
85 {
86     uint16_t propCode = provider->ConsumeIntegral<uint16_t>();
87     Property property(propCode, MTP_TYPE_AINT8_CODE);
88     uint8_t indent = provider->ConsumeIntegral<uint8_t>();
89     shared_ptr<vector<Property::Value>> values;
90     string name = provider->ConsumeBytesAsString(NUM_BYTES);
91     property.DumpValues(indent, values, name);
92     values = make_shared<vector<Property::Value>>();
93     indent = provider->ConsumeIntegral<uint8_t>();
94     property.DumpValues(indent, values, name);
95     indent = provider->ConsumeIntegral<uint8_t>();
96     property.DumpForm(indent);
97     property.SetFormRange(0, 0, 0);
98     indent = provider->ConsumeIntegral<uint8_t>();
99     property.DumpForm(indent);
100 
101     shared_ptr<Property::Value> value = make_shared<Property::Value>();
102     uint32_t valueType = provider->ConsumeIntegral<uint32_t>();
103     value->Dump(valueType);
104     valueType = provider->ConsumeIntegral<uint32_t>();
105     string outStr = value->ToString(valueType);
106     value->BinToString(valueType, outStr);
107 }
108 
PropertyReadValueTest()109 static void PropertyReadValueTest()
110 {
111     uint16_t propCode = provider->ConsumeIntegral<uint16_t>();
112     uint16_t propType = provider->ConsumeIntegral<uint16_t>();
113     bool propWriteable = provider->ConsumeBool();
114     int values = provider->ConsumeIntegral<int32_t>();
115 
116     Property property(propCode, propType, propWriteable, values);
117 
118     vector<uint8_t> buffer = provider->ConsumeBytes<uint8_t>(NUM_BYTES);
119     size_t offsetTest = 0;
120     Property::Value value;
121     property.ReadValue(buffer, offsetTest, value);
122     property.WriteValue(buffer, value);
123     property.ReadValue(buffer, offsetTest, value);
124 
125     property.ReadValueEx(buffer, offsetTest, value);
126     property.WriteValue(buffer, value);
127     property.WriteValueEx(buffer, value);
128     property.ReadValueEx(buffer, offsetTest, value);
129 }
130 
PropertyReadArrayValuesTest()131 static void PropertyReadArrayValuesTest()
132 {
133     uint16_t propCode = provider->ConsumeIntegral<uint16_t>();
134     uint16_t propType = provider->ConsumeIntegral<uint16_t>();
135     bool propWriteable = provider->ConsumeBool();
136     int value = provider->ConsumeIntegral<int32_t>();
137     Property property(propCode, propType, propWriteable, value);
138     shared_ptr<vector<Property::Value>> values;
139 
140     vector<uint8_t> buffer = provider->ConsumeBytes<uint8_t>(NUM_BYTES);
141     value = provider->ConsumeIntegral<uint32_t>();
142     MtpPacketTool::PutUInt32(buffer, value);
143     property.WriteValueData(buffer);
144     size_t offsetTest = 0;
145     property.ReadArrayValues(buffer, offsetTest, values);
146 }
147 
PropertyDumpValueTest()148 static void PropertyDumpValueTest()
149 {
150     uint16_t propCode = provider->ConsumeIntegral<uint16_t>();
151     uint16_t propType = provider->ConsumeIntegral<uint16_t>();
152     bool propWriteable = provider->ConsumeBool();
153     int value = provider->ConsumeIntegral<int32_t>();
154     Property property(propCode, propType, propWriteable, value);
155 
156     uint8_t indent = provider->ConsumeIntegral<uint8_t>();
157     string name = provider->ConsumeBytesAsString(NUM_BYTES);
158     shared_ptr<Property::Value> valueTest;
159     property.DumpValue(indent, valueTest, name);
160     valueTest = make_shared<Property::Value>();
161     uint32_t valueType = provider->ConsumeIntegral<uint32_t>();
162     string outStr = provider->ConsumeBytesAsString(NUM_BYTES);
163     valueTest->StrToString(valueType, outStr);
164 
165     valueTest->str_ = make_shared<string>(provider->ConsumeBytesAsString(NUM_BYTES));
166     valueTest->StrToString(valueType, outStr);
167     indent = provider->ConsumeIntegral<uint8_t>();
168     property.DumpValue(indent, valueTest, name);
169 }
170 
PropertyWriteFormDataTest()171 static void PropertyWriteFormDataTest()
172 {
173     uint16_t propCode = provider->ConsumeIntegral<uint16_t>();
174     uint16_t propType = provider->ConsumeIntegral<uint16_t>();
175     bool propWriteable = provider->ConsumeBool();
176     int value = provider->ConsumeIntegral<int32_t>();
177     Property property(propCode, propType, propWriteable, value);
178 
179     property.SetFormRange(0, 0, 0);
180     vector<uint8_t> buffer = provider->ConsumeBytes<uint8_t>(NUM_BYTES);
181     size_t offsetTest = 0;
182     property.ReadFormData(buffer, offsetTest);
183 
184     property.WriteFormData(buffer);
185 
186     MtpPacketTool::PutInt8(buffer, offsetTest);
187     property.ReadFormData(buffer, offsetTest);
188 
189     vector<int> values = FuzzVectorInt32();
190     property.SetFormEnum(values);
191     property.ReadFormData(buffer, offsetTest);
192     property.WriteFormData(buffer);
193 }
194 
PropertyTest()195 static void PropertyTest()
196 {
197     PropertySetFormEnumTest();
198     PropertyWriteTest();
199     PropertyStringTest();
200     PropertyReadValueTest();
201     PropertyReadArrayValuesTest();
202     PropertyDumpValueTest();
203     PropertyWriteFormDataTest();
204 }
205 } // namespace OHOS
206 
207 /* Fuzzer entry point */
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)208 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
209 {
210     /* Run your code on data */
211     FuzzedDataProvider fdp(data, size);
212     OHOS::provider = &fdp;
213     if (data == nullptr) {
214         return 0;
215     }
216     OHOS::PropertyTest();
217     return 0;
218 }