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