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_CODECCONFIG_FUNCTIONS_H_
18 #define BT_STACK_FUZZ_A2DP_CODECCONFIG_FUNCTIONS_H_
19 
20 #include <fuzzer/FuzzedDataProvider.h>
21 
22 #include <vector>
23 
24 #include "a2dp_codec_api.h"
25 #include "fuzzers/a2dp/codec/a2dpCodecConfigFuzzHelpers.h"
26 #include "fuzzers/a2dp/codec/a2dpCodecHelperFunctions.h"
27 #include "fuzzers/a2dp/codec/a2dpCodecInfoFuzzFunctions.h"
28 #include "fuzzers/common/commonFuzzHelpers.h"
29 
30 /* This is a vector of lambda functions the fuzzer will pull from.
31  *  This is done so new functions can be added to the fuzzer easily
32  *  without requiring modifications to the main fuzzer file. This also
33  *  allows multiple fuzzers to include this file, if functionality is needed.
34  */
35 std::vector<std::function<void(FuzzedDataProvider*)>> a2dp_codec_config_operations = {
36         // createCodec
37         [](FuzzedDataProvider* fdp) -> void {
38           // Generate our arguments
39           btav_a2dp_codec_index_t codec_index = getArbitraryBtavCodecIndex(fdp);
40           btav_a2dp_codec_priority_t codec_priority = getArbitraryBtavCodecPriority(fdp);
41           // Create our new codec
42           std::shared_ptr<A2dpCodecConfig> codec_config(
43                   A2dpCodecConfig::createCodec(codec_index, codec_priority));
44           // Push it to our vector
45           if (codec_config) {
46             a2dp_codec_config_vect.push_back(codec_config);
47           }
48         },
49 
50         // A2dpCodecConfig Destructor
51         [](FuzzedDataProvider* fdp) -> void {
52           if (a2dp_codec_config_vect.empty()) {
53             return;
54           }
55           // Get random vector index
56           size_t index = fdp->ConsumeIntegralInRange<size_t>(0, a2dp_codec_config_vect.size() - 1);
57           // Remove from vector
58           a2dp_codec_config_vect.erase(a2dp_codec_config_vect.begin() + index);
59         },
60 
61         // codecIndex
62         [](FuzzedDataProvider* fdp) -> void {
63           std::shared_ptr<A2dpCodecConfig> codec_config(
64                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
65           if (codec_config == nullptr) {
66             return;
67           }
68 
69           codec_config->codecIndex();
70         },
71 
72         // name
73         [](FuzzedDataProvider* fdp) -> void {
74           std::shared_ptr<A2dpCodecConfig> codec_config(
75                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
76           if (codec_config == nullptr) {
77             return;
78           }
79 
80           codec_config->name();
81         },
82 
83         // codecPriority
84         [](FuzzedDataProvider* fdp) -> void {
85           std::shared_ptr<A2dpCodecConfig> codec_config(
86                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
87           if (codec_config == nullptr) {
88             return;
89           }
90 
91           codec_config->codecPriority();
92         },
93 
94         // getCodecSpecificConfig
95         [](FuzzedDataProvider* fdp) -> void {
96           std::shared_ptr<A2dpCodecConfig> codec_config(
97                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
98           if (codec_config == nullptr) {
99             return;
100           }
101 
102           tBT_A2DP_OFFLOAD a2dp_offload = generateArbitrarytA2dpOffload(fdp);
103           codec_config->getCodecSpecificConfig(&a2dp_offload);
104         },
105 
106         // getTrackBitRate
107         [](FuzzedDataProvider* fdp) -> void {
108           std::shared_ptr<A2dpCodecConfig> codec_config(
109                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
110           if (codec_config == nullptr) {
111             return;
112           }
113 
114           codec_config->getTrackBitRate();
115         },
116 
117         // copyOutOtaCodecConfig
118         [](FuzzedDataProvider* fdp) -> void {
119           std::shared_ptr<A2dpCodecConfig> codec_config(
120                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
121           if (codec_config == nullptr) {
122             return;
123           }
124 
125           uint8_t* codec_info = getArbitraryVectorElement(fdp, a2dp_codec_info_vect, true);
126           codec_config->copyOutOtaCodecConfig(codec_info);
127         },
128 
129         // getCodecConfig
130         [](FuzzedDataProvider* fdp) -> void {
131           std::shared_ptr<A2dpCodecConfig> codec_config(
132                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
133           if (codec_config == nullptr) {
134             return;
135           }
136 
137           codec_config->getCodecConfig();
138         },
139 
140         // getCodecCapability
141         [](FuzzedDataProvider* fdp) -> void {
142           std::shared_ptr<A2dpCodecConfig> codec_config(
143                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
144           if (codec_config == nullptr) {
145             return;
146           }
147 
148           codec_config->getCodecCapability();
149         },
150 
151         // getCodecLocalCapability
152         [](FuzzedDataProvider* fdp) -> void {
153           std::shared_ptr<A2dpCodecConfig> codec_config(
154                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
155           if (codec_config == nullptr) {
156             return;
157           }
158 
159           codec_config->getCodecLocalCapability();
160         },
161 
162         // getCodecSelectableCapability
163         [](FuzzedDataProvider* fdp) -> void {
164           std::shared_ptr<A2dpCodecConfig> codec_config(
165                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
166           if (codec_config == nullptr) {
167             return;
168           }
169 
170           codec_config->getCodecSelectableCapability();
171         },
172 
173         // getCodecUserConfig
174         [](FuzzedDataProvider* fdp) -> void {
175           std::shared_ptr<A2dpCodecConfig> codec_config(
176                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
177           if (codec_config == nullptr) {
178             return;
179           }
180 
181           codec_config->getCodecUserConfig();
182         },
183 
184         // getCodecAudioConfig
185         [](FuzzedDataProvider* fdp) -> void {
186           std::shared_ptr<A2dpCodecConfig> codec_config(
187                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
188           if (codec_config == nullptr) {
189             return;
190           }
191 
192           codec_config->getCodecAudioConfig();
193         },
194 
195         // getAudioBitsPerSample
196         [](FuzzedDataProvider* fdp) -> void {
197           std::shared_ptr<A2dpCodecConfig> codec_config(
198                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
199           if (codec_config == nullptr) {
200             return;
201           }
202 
203           codec_config->getAudioBitsPerSample();
204         },
205 
206         // getAudioBitsPerSample
207         [](FuzzedDataProvider* fdp) -> void {
208           std::shared_ptr<A2dpCodecConfig> codec_config(
209                   getArbitraryVectorElement(fdp, a2dp_codec_config_vect, false));
210           if (codec_config == nullptr) {
211             return;
212           }
213 
214           const btav_a2dp_codec_config_t btav_codec_config = getArbitraryBtavCodecConfig(fdp);
215           codec_config->isCodecConfigEmpty(btav_codec_config);
216         },
217 
218         // Dependency calling: CodecInfo
219         [](FuzzedDataProvider* fdp) -> void {
220           callArbitraryCodecInfoFunction(fdp, a2dp_codec_info_operations);
221         }};
222 
223 #endif  // BT_STACK_FUZZ_A2DP_CODECCONFIG_FUNCTIONS_H_
224