1 /*
2  * Copyright 2020 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #ifndef BT_STACK_FUZZ_A2DP_CODECINFO_HELPERS_H_
18 #define BT_STACK_FUZZ_A2DP_CODECINFO_HELPERS_H_
19 
20 // NOTE: This file should not be included directly.
21 //       It is included by the corresponding "...Functions.h" file.
22 
23 #include <fuzzer/FuzzedDataProvider.h>
24 
25 #include <vector>
26 
27 #include "a2dp_codec_api.h"
28 #include "stack/test/fuzzers/common/commonFuzzHelpers.h"
29 
30 // Keep a vector of any allocated codec_info objects.
31 // It will be up to the caller to free this array at the end of a fuzz loop
32 std::vector<uint8_t*> a2dp_codec_info_vect;
33 
34 // Calls a function from the ops_vector
callArbitraryCodecInfoFunction(FuzzedDataProvider * fdp,std::vector<std::function<void (FuzzedDataProvider *,uint8_t *)>> ops_vector)35 void callArbitraryCodecInfoFunction(
36     FuzzedDataProvider* fdp,
37     std::vector<std::function<void(FuzzedDataProvider*, uint8_t*)>>
38         ops_vector) {
39   // Choose which function we'll be calling
40   uint8_t function_id =
41       fdp->ConsumeIntegralInRange<uint8_t>(0, ops_vector.size() - 1);
42 
43   // Get a info object
44   uint8_t* codec_info =
45       getArbitraryVectorElement(fdp, a2dp_codec_info_vect, false);
46 
47   // Most functions require a valid codec_info
48   if (codec_info || function_id == 0 || function_id == 25 ||
49       function_id == 26) {
50     // Call the function we've chosen
51     ops_vector[function_id](fdp, codec_info);
52   }
53 }
54 
55 // Function to clean up and clear our allocated objects
cleanupA2dpCodecInfoFuzz()56 void cleanupA2dpCodecInfoFuzz() {
57   for (auto it : a2dp_codec_info_vect) {
58     if (it != nullptr) {
59       delete it;
60     }
61   }
62   a2dp_codec_info_vect.clear();
63 }
64 
65 #endif  // BT_STACK_FUZZ_A2DP_CODECINFO_HELPERS_H_
66