1 /*
2 * Copyright (c) 2023 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 <cstdio>
17 #include <cstdlib>
18 #include <unistd.h>
19 #include "hdf_base.h"
20
21 #ifdef __cplusplus
22 extern "C" {
23 int32_t CfgSaveAdapterFromFile(void);
24 }
25 #endif
26
DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)27 static bool DoSomethingInterestingWithMyAPI(const uint8_t *rawData, size_t size)
28 {
29 if (rawData == NULL) {
30 return false;
31 }
32
33 char tmpfile[] = HDF_CONFIG_DIR"/alsa_adapter.json";
34
35 int fd = mkstemp(tmpfile);
36 if (fd < 0) {
37 return 0;
38 }
39 write(fd, rawData, size);
40 close(fd);
41
42 // Call the interface with the temporary file
43 int32_t ret = CfgSaveAdapterFromFile();
44 if (ret != HDF_SUCCESS) {
45 return false;
46 }
47
48 // Remove the temporary file
49 remove(tmpfile);
50 return true;
51 }
52
LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)53 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
54 {
55 DoSomethingInterestingWithMyAPI(data, size);
56
57 return 0;
58 }
59