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 AudioPathSelGetConfToJsonObj(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 #ifdef ALSA_LIB_MODE
34 char tmpfile[] = HDF_CONFIG_DIR"/alsa_paths.json";
35 #else
36 char tmpfile[] = HDF_CONFIG_DIR"/audio_paths.json";
37 #endif
38
39 int fd = mkstemp(tmpfile);
40 if (fd < 0) {
41 return 0;
42 }
43 write(fd, rawData, size);
44 close(fd);
45
46 // Call the interface with the temporary file
47 int ret = AudioPathSelGetConfToJsonObj();
48 if (ret != HDF_SUCCESS) {
49 return false;
50 }
51
52 // Remove the temporary file
53 remove(tmpfile);
54 return true;
55 }
56
LLVMFuzzerTestOneInput(const uint8_t * data,size_t size)57 extern "C" int LLVMFuzzerTestOneInput(const uint8_t *data, size_t size)
58 {
59 DoSomethingInterestingWithMyAPI(data, size);
60
61 return 0;
62 }
63