1 /*
2 * Copyright (C) 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 #include <array>
18 #include <string>
19
20 #include <gtest/gtest.h>
21
22 #define LOG_TAG "HidlUtils_Test"
23 #include <log/log.h>
24
25 #include <HidlUtils.h>
26 #include PATH(APM_XSD_ENUMS_H_FILENAME)
27 #include <system/audio.h>
28 #include <xsdc/XsdcSupport.h>
29
30 using namespace android;
31 using ::android::hardware::hidl_vec;
32 using namespace ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION;
33 using ::android::hardware::audio::common::COMMON_TYPES_CPP_VERSION::implementation::HidlUtils;
34 namespace xsd {
35 using namespace ::android::audio::policy::configuration::CPP_VERSION;
36 }
37
38 static constexpr audio_channel_mask_t kInvalidHalChannelMask = AUDIO_CHANNEL_INVALID;
39 static constexpr audio_content_type_t kInvalidHalContentType =
40 static_cast<audio_content_type_t>(0xFFFFFFFFU);
41 static constexpr audio_devices_t kInvalidHalDevice = static_cast<audio_devices_t>(0xFFFFFFFFU);
42 static constexpr audio_format_t kInvalidHalFormat = AUDIO_FORMAT_INVALID;
43 static constexpr audio_gain_mode_t kInvalidHalGainMode =
44 static_cast<audio_gain_mode_t>(0xFFFFFFFFU);
45 // AUDIO_SOURCE_INVALID is framework-only.
46 static constexpr audio_source_t kInvalidHalSource = static_cast<audio_source_t>(-1);
47 // AUDIO_STREAM_DEFAULT is framework-only
48 static constexpr audio_stream_type_t kInvalidHalStreamType = static_cast<audio_stream_type_t>(-2);
49 static constexpr audio_usage_t kInvalidHalUsage = static_cast<audio_usage_t>(0xFFFFFFFFU);
50 static constexpr audio_encapsulation_type_t kInvalidEncapsulationType =
51 static_cast<audio_encapsulation_type_t>(0xFFFFFFFFU);
52 static constexpr audio_standard_t kInvalidAudioStandard =
53 static_cast<audio_standard_t>(0xFFFFFFFFU);
54
TEST(HidlUtils,ConvertInvalidChannelMask)55 TEST(HidlUtils, ConvertInvalidChannelMask) {
56 AudioChannelMask invalid;
57 EXPECT_EQ(BAD_VALUE, HidlUtils::audioChannelMaskFromHal(kInvalidHalChannelMask,
58 false /*isInput*/, &invalid));
59 EXPECT_EQ(BAD_VALUE, HidlUtils::audioChannelMaskFromHal(kInvalidHalChannelMask,
60 true /*isInput*/, &invalid));
61 audio_channel_mask_t halInvalid;
62 EXPECT_EQ(BAD_VALUE, HidlUtils::audioChannelMaskToHal("", &halInvalid));
63 // INVALID channel mask is not in XSD thus it's not allowed for transfer over HIDL.
64 EXPECT_EQ(BAD_VALUE, HidlUtils::audioChannelMaskToHal("AUDIO_CHANNEL_INVALID", &halInvalid));
65 EXPECT_EQ(BAD_VALUE, HidlUtils::audioChannelMaskToHal("random string", &halInvalid));
66 }
67
68 // Might move these to the audio_policy_configuration_V7_0-enums library
69 // if there would be usages in the default wrapper code. In that case,
70 // it would be better to reimplement these methods using a proper switch statement
71 // over all known enum values.
isInputChannelMask(xsd::AudioChannelMask channelMask)72 static bool isInputChannelMask(xsd::AudioChannelMask channelMask) {
73 return toString(channelMask).find("_CHANNEL_IN_") != std::string::npos;
74 }
75
isOutputChannelMask(xsd::AudioChannelMask channelMask)76 static bool isOutputChannelMask(xsd::AudioChannelMask channelMask) {
77 return toString(channelMask).find("_CHANNEL_OUT_") != std::string::npos;
78 }
79
isIndexChannelMask(xsd::AudioChannelMask channelMask)80 static bool isIndexChannelMask(xsd::AudioChannelMask channelMask) {
81 return toString(channelMask).find("_CHANNEL_INDEX_") != std::string::npos;
82 }
83
TEST(HidlUtils,ConvertChannelMask)84 TEST(HidlUtils, ConvertChannelMask) {
85 for (const auto enumVal : xsdc_enum_range<xsd::AudioChannelMask>{}) {
86 const AudioChannelMask channelMask = toString(enumVal);
87 audio_channel_mask_t halChannelMask, halChannelMaskBack;
88 AudioChannelMask channelMaskBack;
89 EXPECT_EQ(NO_ERROR, HidlUtils::audioChannelMaskToHal(channelMask, &halChannelMask))
90 << "Conversion of \"" << channelMask << "\" failed";
91 EXPECT_EQ(enumVal != xsd::AudioChannelMask::AUDIO_CHANNEL_NONE,
92 audio_channel_mask_is_valid(halChannelMask))
93 << "Validity of \"" << channelMask << "\" is not as expected";
94 if (bool isInput = isInputChannelMask(enumVal); isInput || isOutputChannelMask(enumVal)) {
95 EXPECT_EQ(NO_ERROR,
96 HidlUtils::audioChannelMaskFromHal(halChannelMask, isInput, &channelMaskBack))
97 << "Conversion of " << (isInput ? "input" : "output") << " channel mask "
98 << halChannelMask << " failed";
99 // Due to aliased values, the result of 'fromHal' might not be the same
100 // as 'channelMask', thus we need to compare the results of 'toHal' conversion instead.
101 EXPECT_EQ(NO_ERROR,
102 HidlUtils::audioChannelMaskToHal(channelMaskBack, &halChannelMaskBack))
103 << "Conversion of \"" << channelMaskBack << "\" failed";
104 EXPECT_EQ(halChannelMask, halChannelMaskBack);
105 } else if (isIndexChannelMask(enumVal) ||
106 enumVal == xsd::AudioChannelMask::AUDIO_CHANNEL_NONE) {
107 // Conversions for indexed masks and "none" must not depend on the provided direction.
108 EXPECT_EQ(NO_ERROR, HidlUtils::audioChannelMaskFromHal(halChannelMask, true /*isInput*/,
109 &channelMaskBack))
110 << "Conversion of indexed / none channel mask " << halChannelMask
111 << " failed (as input channel mask)";
112 EXPECT_EQ(channelMask, channelMaskBack);
113 EXPECT_EQ(NO_ERROR, HidlUtils::audioChannelMaskFromHal(
114 halChannelMask, false /*isInput*/, &channelMaskBack))
115 << "Conversion of indexed / none channel mask " << halChannelMask
116 << " failed (as output channel mask)";
117 EXPECT_EQ(channelMask, channelMaskBack);
118 } else {
119 FAIL() << "Unrecognized channel mask \"" << channelMask << "\"";
120 }
121 }
122 }
123
TEST(HidlUtils,ConvertInvalidChannelMasksFromHal)124 TEST(HidlUtils, ConvertInvalidChannelMasksFromHal) {
125 std::vector<std::string> validAndInvalidChannelMasks = {
126 toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO), "random string", ""};
127 hidl_vec<AudioChannelMask> validChannelMask;
128 EXPECT_EQ(BAD_VALUE,
129 HidlUtils::audioChannelMasksFromHal(validAndInvalidChannelMasks, &validChannelMask));
130 EXPECT_EQ(1, validChannelMask.size());
131 EXPECT_EQ(validAndInvalidChannelMasks[0], validChannelMask[0]);
132
133 std::vector<std::string> invalidChannelMasks = {"random string", ""};
134 hidl_vec<AudioChannelMask> empty;
135 EXPECT_EQ(BAD_VALUE, HidlUtils::audioChannelMasksFromHal(invalidChannelMasks, &empty));
136 EXPECT_EQ(0, empty.size());
137 }
138
TEST(HidlUtils,ConvertChannelMasksFromHal)139 TEST(HidlUtils, ConvertChannelMasksFromHal) {
140 std::vector<std::string> allHalChannelMasks;
141 for (const auto enumVal : xsdc_enum_range<xsd::AudioChannelMask>{}) {
142 allHalChannelMasks.push_back(toString(enumVal));
143 }
144 hidl_vec<AudioChannelMask> allChannelMasks;
145 EXPECT_EQ(NO_ERROR, HidlUtils::audioChannelMasksFromHal(allHalChannelMasks, &allChannelMasks));
146 EXPECT_EQ(allHalChannelMasks.size(), allChannelMasks.size());
147 for (size_t i = 0; i < allHalChannelMasks.size(); ++i) {
148 EXPECT_EQ(allHalChannelMasks[i], allChannelMasks[i]);
149 }
150 }
151
generateValidConfigBase(bool isInput)152 static AudioConfigBase generateValidConfigBase(bool isInput) {
153 AudioConfigBase configBase;
154 configBase.sampleRateHz = 44100;
155 configBase.format = toString(xsd::AudioFormat::AUDIO_FORMAT_PCM_16_BIT);
156 configBase.channelMask = isInput ? toString(xsd::AudioChannelMask::AUDIO_CHANNEL_IN_STEREO)
157 : toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO);
158 return configBase;
159 }
160
TEST(HidlUtils,ConvertInvalidConfigBase)161 TEST(HidlUtils, ConvertInvalidConfigBase) {
162 AudioConfigBase invalid;
163 audio_config_base_t halInvalidChannelMask = AUDIO_CONFIG_BASE_INITIALIZER;
164 halInvalidChannelMask.channel_mask = kInvalidHalChannelMask;
165 EXPECT_EQ(BAD_VALUE, HidlUtils::audioConfigBaseFromHal(halInvalidChannelMask, false /*isInput*/,
166 &invalid));
167 EXPECT_EQ(BAD_VALUE,
168 HidlUtils::audioConfigBaseFromHal(halInvalidChannelMask, true /*isInput*/, &invalid));
169 audio_config_base_t halInvalidFormat = AUDIO_CONFIG_BASE_INITIALIZER;
170 halInvalidFormat.format = kInvalidHalFormat;
171 EXPECT_EQ(BAD_VALUE,
172 HidlUtils::audioConfigBaseFromHal(halInvalidFormat, false /*isInput*/, &invalid));
173 EXPECT_EQ(BAD_VALUE,
174 HidlUtils::audioConfigBaseFromHal(halInvalidFormat, true /*isInput*/, &invalid));
175
176 audio_config_base_t halInvalid;
177 AudioConfigBase invalidChannelMask = generateValidConfigBase(false /*isInput*/);
178 invalidChannelMask.channelMask = "random string";
179 EXPECT_EQ(BAD_VALUE, HidlUtils::audioConfigBaseToHal(invalidChannelMask, &halInvalid));
180 AudioConfigBase invalidFormat = generateValidConfigBase(false /*isInput*/);
181 invalidFormat.format = "random string";
182 EXPECT_EQ(BAD_VALUE, HidlUtils::audioConfigBaseToHal(invalidFormat, &halInvalid));
183 }
184
TEST(HidlUtils,ConvertConfigBaseDefault)185 TEST(HidlUtils, ConvertConfigBaseDefault) {
186 audio_config_base_t halBaseDefault = AUDIO_CONFIG_BASE_INITIALIZER;
187 AudioConfigBase baseDefaultOut, baseDefaultIn;
188 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseFromHal(halBaseDefault, false /*isInput*/,
189 &baseDefaultOut));
190 EXPECT_EQ(NO_ERROR,
191 HidlUtils::audioConfigBaseFromHal(halBaseDefault, true /*isInput*/, &baseDefaultIn));
192 EXPECT_EQ(baseDefaultOut, baseDefaultIn);
193 audio_config_base_t halBaseDefaultBack;
194 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseToHal(baseDefaultOut, &halBaseDefaultBack));
195 EXPECT_EQ(halBaseDefault.sample_rate, halBaseDefaultBack.sample_rate);
196 EXPECT_EQ(halBaseDefault.channel_mask, halBaseDefaultBack.channel_mask);
197 EXPECT_EQ(halBaseDefault.format, halBaseDefaultBack.format);
198 }
199
TEST(HidlUtils,ConvertConfigBase)200 TEST(HidlUtils, ConvertConfigBase) {
201 AudioConfigBase configBaseOut = generateValidConfigBase(false /*isInput*/);
202 audio_config_base_t halConfigBaseOut;
203 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseToHal(configBaseOut, &halConfigBaseOut));
204 AudioConfigBase configBaseOutBack;
205 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseFromHal(halConfigBaseOut, false /*isInput*/,
206 &configBaseOutBack));
207 EXPECT_EQ(configBaseOut, configBaseOutBack);
208
209 AudioConfigBase configBaseIn = generateValidConfigBase(true /*isInput*/);
210 audio_config_base_t halConfigBaseIn;
211 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseToHal(configBaseIn, &halConfigBaseIn));
212 AudioConfigBase configBaseInBack;
213 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseFromHal(halConfigBaseIn, true /*isInput*/,
214 &configBaseInBack));
215 EXPECT_EQ(configBaseIn, configBaseInBack);
216 }
217
TEST(HidlUtils,ConvertInvalidConfigBaseOptional)218 TEST(HidlUtils, ConvertInvalidConfigBaseOptional) {
219 AudioConfigBaseOptional invalid;
220 audio_config_base_t halInvalidChannelMask = AUDIO_CONFIG_BASE_INITIALIZER;
221 halInvalidChannelMask.channel_mask = kInvalidHalChannelMask;
222 EXPECT_EQ(BAD_VALUE,
223 HidlUtils::audioConfigBaseOptionalFromHal(
224 halInvalidChannelMask, false /*isInput*/, false /*formatSpecified*/,
225 false /*sampleRateSpecified*/, true /*channelMaskSpecified*/, &invalid));
226 EXPECT_EQ(BAD_VALUE,
227 HidlUtils::audioConfigBaseOptionalFromHal(
228 halInvalidChannelMask, true /*isInput*/, false /*formatSpecified*/,
229 false /*sampleRateSpecified*/, true /*channelMaskSpecified*/, &invalid));
230 // Unspecified invalid values are ignored
231 AudioConfigBaseOptional unspecified;
232 EXPECT_EQ(NO_ERROR,
233 HidlUtils::audioConfigBaseOptionalFromHal(
234 halInvalidChannelMask, false /*isInput*/, false /*formatSpecified*/,
235 false /*sampleRateSpecified*/, false /*channelMaskSpecified*/, &unspecified));
236 EXPECT_EQ(NO_ERROR,
237 HidlUtils::audioConfigBaseOptionalFromHal(
238 halInvalidChannelMask, true /*isInput*/, false /*formatSpecified*/,
239 false /*sampleRateSpecified*/, false /*channelMaskSpecified*/, &unspecified));
240 audio_config_base_t halInvalidFormat = AUDIO_CONFIG_BASE_INITIALIZER;
241 halInvalidFormat.format = kInvalidHalFormat;
242 EXPECT_EQ(BAD_VALUE,
243 HidlUtils::audioConfigBaseOptionalFromHal(
244 halInvalidFormat, false /*isInput*/, true /*formatSpecified*/,
245 false /*sampleRateSpecified*/, false /*channelMaskSpecified*/, &invalid));
246 EXPECT_EQ(BAD_VALUE,
247 HidlUtils::audioConfigBaseOptionalFromHal(
248 halInvalidFormat, true /*isInput*/, true /*formatSpecified*/,
249 false /*sampleRateSpecified*/, false /*channelMaskSpecified*/, &invalid));
250 EXPECT_EQ(NO_ERROR,
251 HidlUtils::audioConfigBaseOptionalFromHal(
252 halInvalidFormat, false /*isInput*/, false /*formatSpecified*/,
253 false /*sampleRateSpecified*/, false /*channelMaskSpecified*/, &unspecified));
254 EXPECT_EQ(NO_ERROR,
255 HidlUtils::audioConfigBaseOptionalFromHal(
256 halInvalidFormat, true /*isInput*/, false /*formatSpecified*/,
257 false /*sampleRateSpecified*/, false /*channelMaskSpecified*/, &unspecified));
258
259 audio_config_base_t halInvalid;
260 AudioConfigBaseOptional invalidChannelMask;
261 bool formatSpecified, sampleRateSpecified, channelMaskSpecified;
262 invalidChannelMask.channelMask.value("random string");
263 EXPECT_EQ(BAD_VALUE, HidlUtils::audioConfigBaseOptionalToHal(
264 invalidChannelMask, &halInvalid, &formatSpecified,
265 &sampleRateSpecified, &channelMaskSpecified));
266 AudioConfigBaseOptional invalidFormat;
267 invalidFormat.format.value("random string");
268 EXPECT_EQ(BAD_VALUE,
269 HidlUtils::audioConfigBaseOptionalToHal(invalidFormat, &halInvalid, &formatSpecified,
270 &sampleRateSpecified, &channelMaskSpecified));
271 }
272
TEST(HidlUtils,ConvertConfigBaseOptionalDefault)273 TEST(HidlUtils, ConvertConfigBaseOptionalDefault) {
274 audio_config_base_t halBaseDefault = AUDIO_CONFIG_BASE_INITIALIZER;
275 AudioConfigBaseOptional baseDefaultUnspecOut, baseDefaultUnspecIn;
276 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalFromHal(
277 halBaseDefault, false /*isInput*/, false /*formatSpecified*/,
278 false /*sampleRateSpecified*/, false /*channelMaskSpecified*/,
279 &baseDefaultUnspecOut));
280 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalFromHal(
281 halBaseDefault, true /*isInput*/, false /*formatSpecified*/,
282 false /*sampleRateSpecified*/, false /*channelMaskSpecified*/,
283 &baseDefaultUnspecIn));
284 EXPECT_EQ(baseDefaultUnspecOut, baseDefaultUnspecIn);
285 audio_config_base_t halBaseDefaultUnspecBack = AUDIO_CONFIG_BASE_INITIALIZER;
286 bool formatSpecified, sampleRateSpecified, channelMaskSpecified;
287 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalToHal(
288 baseDefaultUnspecOut, &halBaseDefaultUnspecBack, &formatSpecified,
289 &sampleRateSpecified, &channelMaskSpecified));
290 EXPECT_FALSE(formatSpecified);
291 EXPECT_FALSE(sampleRateSpecified);
292 EXPECT_FALSE(channelMaskSpecified);
293 EXPECT_EQ(halBaseDefault.sample_rate, halBaseDefaultUnspecBack.sample_rate);
294 EXPECT_EQ(halBaseDefault.channel_mask, halBaseDefaultUnspecBack.channel_mask);
295 EXPECT_EQ(halBaseDefault.format, halBaseDefaultUnspecBack.format);
296
297 AudioConfigBaseOptional baseDefaultSpecOut, baseDefaultSpecIn;
298 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalFromHal(
299 halBaseDefault, false /*isInput*/, true /*formatSpecified*/,
300 true /*sampleRateSpecified*/, true /*channelMaskSpecified*/,
301 &baseDefaultSpecOut));
302 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalFromHal(
303 halBaseDefault, true /*isInput*/, true /*formatSpecified*/,
304 true /*sampleRateSpecified*/, true /*channelMaskSpecified*/,
305 &baseDefaultSpecIn));
306 EXPECT_EQ(baseDefaultSpecOut, baseDefaultSpecIn);
307 audio_config_base_t halBaseDefaultSpecBack;
308 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalToHal(
309 baseDefaultSpecOut, &halBaseDefaultSpecBack, &formatSpecified,
310 &sampleRateSpecified, &channelMaskSpecified));
311 EXPECT_TRUE(formatSpecified);
312 EXPECT_TRUE(sampleRateSpecified);
313 EXPECT_TRUE(channelMaskSpecified);
314 EXPECT_EQ(halBaseDefault.sample_rate, halBaseDefaultSpecBack.sample_rate);
315 EXPECT_EQ(halBaseDefault.channel_mask, halBaseDefaultSpecBack.channel_mask);
316 EXPECT_EQ(halBaseDefault.format, halBaseDefaultSpecBack.format);
317 }
318
TEST(HidlUtils,ConvertConfigBaseOptionalEmpty)319 TEST(HidlUtils, ConvertConfigBaseOptionalEmpty) {
320 AudioConfigBaseOptional empty;
321 bool formatSpecified, sampleRateSpecified, channelMaskSpecified;
322 audio_config_base_t halEmpty = AUDIO_CONFIG_BASE_INITIALIZER;
323 EXPECT_EQ(NO_ERROR,
324 HidlUtils::audioConfigBaseOptionalToHal(empty, &halEmpty, &formatSpecified,
325 &sampleRateSpecified, &channelMaskSpecified));
326 EXPECT_FALSE(formatSpecified);
327 EXPECT_FALSE(sampleRateSpecified);
328 EXPECT_FALSE(channelMaskSpecified);
329 AudioConfigBaseOptional emptyOutBack, emptyInBack;
330 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalFromHal(
331 halEmpty, false /*isInput*/, formatSpecified, sampleRateSpecified,
332 channelMaskSpecified, &emptyOutBack));
333 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalFromHal(
334 halEmpty, true /*isInput*/, formatSpecified, sampleRateSpecified,
335 channelMaskSpecified, &emptyInBack));
336 EXPECT_EQ(emptyOutBack, emptyInBack);
337 EXPECT_EQ(empty, emptyOutBack);
338 }
339
TEST(HidlUtils,ConvertConfigBaseOptional)340 TEST(HidlUtils, ConvertConfigBaseOptional) {
341 AudioConfigBase validBaseOut = generateValidConfigBase(false /*isInput*/);
342 AudioConfigBaseOptional configBaseOut;
343 configBaseOut.format.value(validBaseOut.format);
344 configBaseOut.sampleRateHz.value(validBaseOut.sampleRateHz);
345 configBaseOut.channelMask.value(validBaseOut.channelMask);
346 audio_config_base_t halConfigBaseOut;
347 bool formatSpecified, sampleRateSpecified, channelMaskSpecified;
348 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalToHal(
349 configBaseOut, &halConfigBaseOut, &formatSpecified,
350 &sampleRateSpecified, &channelMaskSpecified));
351 EXPECT_TRUE(formatSpecified);
352 EXPECT_TRUE(sampleRateSpecified);
353 EXPECT_TRUE(channelMaskSpecified);
354 AudioConfigBaseOptional configBaseOutBack;
355 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalFromHal(
356 halConfigBaseOut, false /*isInput*/, formatSpecified,
357 sampleRateSpecified, channelMaskSpecified, &configBaseOutBack));
358 EXPECT_EQ(configBaseOut, configBaseOutBack);
359
360 AudioConfigBase validBaseIn = generateValidConfigBase(true /*isInput*/);
361 AudioConfigBaseOptional configBaseIn;
362 configBaseIn.format.value(validBaseIn.format);
363 configBaseIn.sampleRateHz.value(validBaseIn.sampleRateHz);
364 configBaseIn.channelMask.value(validBaseIn.channelMask);
365 audio_config_base_t halConfigBaseIn;
366 formatSpecified = false;
367 sampleRateSpecified = false;
368 channelMaskSpecified = false;
369 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalToHal(
370 configBaseIn, &halConfigBaseIn, &formatSpecified,
371 &sampleRateSpecified, &channelMaskSpecified));
372 EXPECT_TRUE(formatSpecified);
373 EXPECT_TRUE(sampleRateSpecified);
374 EXPECT_TRUE(channelMaskSpecified);
375 AudioConfigBaseOptional configBaseInBack;
376 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigBaseOptionalFromHal(
377 halConfigBaseIn, true /*isInput*/, formatSpecified,
378 sampleRateSpecified, channelMaskSpecified, &configBaseInBack));
379 EXPECT_EQ(configBaseIn, configBaseInBack);
380 }
381
TEST(HidlUtils,ConvertInvalidContentType)382 TEST(HidlUtils, ConvertInvalidContentType) {
383 AudioContentType invalid;
384 EXPECT_EQ(BAD_VALUE, HidlUtils::audioContentTypeFromHal(kInvalidHalContentType, &invalid));
385 audio_content_type_t halInvalid;
386 EXPECT_EQ(BAD_VALUE, HidlUtils::audioContentTypeToHal("", &halInvalid));
387 EXPECT_EQ(BAD_VALUE, HidlUtils::audioContentTypeToHal("random string", &halInvalid));
388 }
389
TEST(HidlUtils,ConvertContentType)390 TEST(HidlUtils, ConvertContentType) {
391 for (const auto enumVal : xsdc_enum_range<xsd::AudioContentType>{}) {
392 const AudioContentType contentType = toString(enumVal);
393 audio_content_type_t halContentType;
394 AudioContentType contentTypeBack;
395 EXPECT_EQ(NO_ERROR, HidlUtils::audioContentTypeToHal(contentType, &halContentType))
396 << "Conversion of \"" << contentType << "\" failed";
397 EXPECT_EQ(NO_ERROR, HidlUtils::audioContentTypeFromHal(halContentType, &contentTypeBack))
398 << "Conversion of content type " << halContentType << " failed";
399 EXPECT_EQ(contentType, contentTypeBack);
400 }
401 }
402
TEST(HidlUtils,ConvertInvalidDeviceType)403 TEST(HidlUtils, ConvertInvalidDeviceType) {
404 AudioDevice invalid;
405 EXPECT_EQ(BAD_VALUE, HidlUtils::audioDeviceTypeFromHal(kInvalidHalDevice, &invalid));
406 audio_devices_t halInvalid;
407 EXPECT_EQ(BAD_VALUE, HidlUtils::audioDeviceTypeToHal("", &halInvalid));
408 EXPECT_EQ(BAD_VALUE, HidlUtils::audioDeviceTypeToHal("random string", &halInvalid));
409 }
410
TEST(HidlUtils,ConvertDeviceType)411 TEST(HidlUtils, ConvertDeviceType) {
412 for (const auto enumVal : xsdc_enum_range<xsd::AudioDevice>{}) {
413 const AudioDevice deviceType = toString(enumVal);
414 audio_devices_t halDeviceType, halDeviceTypeBack;
415 AudioDevice deviceTypeBack;
416 EXPECT_EQ(NO_ERROR, HidlUtils::audioDeviceTypeToHal(deviceType, &halDeviceType))
417 << "Conversion of \"" << deviceType << "\" failed";
418 if (enumVal != xsd::AudioDevice::AUDIO_DEVICE_NONE) {
419 EXPECT_TRUE(audio_is_input_device(halDeviceType) ||
420 audio_is_output_device(halDeviceType))
421 << "Device \"" << deviceType << "\" is neither input, nor output device";
422 } else {
423 EXPECT_FALSE(audio_is_input_device(halDeviceType));
424 EXPECT_FALSE(audio_is_output_device(halDeviceType));
425 }
426 EXPECT_EQ(NO_ERROR, HidlUtils::audioDeviceTypeFromHal(halDeviceType, &deviceTypeBack))
427 << "Conversion of device type " << halDeviceType << " failed";
428 // Due to aliased values, the result of 'fromHal' might not be the same
429 // as 'deviceType', thus we need to compare the results of 'toHal' conversion instead.
430 EXPECT_EQ(NO_ERROR, HidlUtils::audioDeviceTypeToHal(deviceTypeBack, &halDeviceTypeBack))
431 << "Conversion of \"" << deviceTypeBack << "\" failed";
432 EXPECT_EQ(halDeviceType, halDeviceTypeBack);
433 }
434 }
435
436 // The enums module is too small to have unit tests on its own.
TEST(HidlUtils,VendorExtension)437 TEST(HidlUtils, VendorExtension) {
438 EXPECT_TRUE(xsd::isVendorExtension("VX_GOOGLE_VR_42"));
439 EXPECT_TRUE(xsd::isVendorExtension("VX_QCM_SPK"));
440 EXPECT_FALSE(xsd::isVendorExtension(""));
441 EXPECT_FALSE(xsd::isVendorExtension("random string"));
442 EXPECT_FALSE(xsd::isVendorExtension("VX_"));
443 EXPECT_FALSE(xsd::isVendorExtension("VX_X"));
444 EXPECT_FALSE(xsd::isVendorExtension("VX_X_"));
445 EXPECT_FALSE(xsd::isVendorExtension("VX_X_X"));
446 EXPECT_FALSE(xsd::isVendorExtension("VX_XX_X"));
447 EXPECT_FALSE(xsd::isVendorExtension("VX_GOOGLE_$$"));
448 EXPECT_FALSE(xsd::isVendorExtension("VX_$CM_SPK"));
449 }
450
TEST(HidlUtils,ConvertInvalidDeviceAddress)451 TEST(HidlUtils, ConvertInvalidDeviceAddress) {
452 DeviceAddress invalid;
453 EXPECT_EQ(BAD_VALUE, HidlUtils::deviceAddressFromHal(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,
454 nullptr, &invalid));
455 EXPECT_EQ(BAD_VALUE, HidlUtils::deviceAddressFromHal(AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER,
456 "", &invalid));
457 EXPECT_EQ(BAD_VALUE, HidlUtils::deviceAddressFromHal(AUDIO_DEVICE_OUT_IP, nullptr, &invalid));
458 EXPECT_EQ(BAD_VALUE, HidlUtils::deviceAddressFromHal(AUDIO_DEVICE_OUT_IP, "", &invalid));
459 EXPECT_EQ(BAD_VALUE,
460 HidlUtils::deviceAddressFromHal(AUDIO_DEVICE_OUT_USB_HEADSET, nullptr, &invalid));
461 EXPECT_EQ(BAD_VALUE,
462 HidlUtils::deviceAddressFromHal(AUDIO_DEVICE_OUT_USB_HEADSET, "", &invalid));
463
464 audio_devices_t halInvalid;
465 char halAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN] = {};
466 invalid = {};
467 invalid.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
468 EXPECT_EQ(BAD_VALUE, HidlUtils::deviceAddressToHal(invalid, &halInvalid, halAddress));
469 invalid.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_IP);
470 EXPECT_EQ(BAD_VALUE, HidlUtils::deviceAddressToHal(invalid, &halInvalid, halAddress));
471 invalid.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_USB_HEADSET);
472 EXPECT_EQ(BAD_VALUE, HidlUtils::deviceAddressToHal(invalid, &halInvalid, halAddress));
473 }
474
ConvertDeviceAddress(const DeviceAddress & device)475 static void ConvertDeviceAddress(const DeviceAddress& device) {
476 audio_devices_t halDeviceType;
477 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN] = {};
478 EXPECT_EQ(NO_ERROR, HidlUtils::deviceAddressToHal(device, &halDeviceType, halDeviceAddress));
479 DeviceAddress deviceBack;
480 EXPECT_EQ(NO_ERROR,
481 HidlUtils::deviceAddressFromHal(halDeviceType, halDeviceAddress, &deviceBack));
482 EXPECT_EQ(device, deviceBack);
483 }
484
TEST(HidlUtils,ConvertUniqueDeviceAddress)485 TEST(HidlUtils, ConvertUniqueDeviceAddress) {
486 DeviceAddress speaker;
487 speaker.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_SPEAKER);
488 ConvertDeviceAddress(speaker);
489
490 DeviceAddress micWithAddress;
491 micWithAddress.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_IN_BUILTIN_MIC);
492 micWithAddress.address.id("bottom");
493 ConvertDeviceAddress(micWithAddress);
494 }
495
TEST(HidlUtils,ConvertA2dpDeviceAddress)496 TEST(HidlUtils, ConvertA2dpDeviceAddress) {
497 DeviceAddress a2dpSpeaker;
498 a2dpSpeaker.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_BLUETOOTH_A2DP_SPEAKER);
499 a2dpSpeaker.address.mac(std::array<uint8_t, 6>{1, 2, 3, 4, 5, 6});
500 ConvertDeviceAddress(a2dpSpeaker);
501 }
502
TEST(HidlUtils,ConvertIpv4DeviceAddress)503 TEST(HidlUtils, ConvertIpv4DeviceAddress) {
504 DeviceAddress ipv4;
505 ipv4.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_IP);
506 ipv4.address.ipv4(std::array<uint8_t, 4>{1, 2, 3, 4});
507 ConvertDeviceAddress(ipv4);
508 }
509
TEST(HidlUtils,ConvertUsbDeviceAddress)510 TEST(HidlUtils, ConvertUsbDeviceAddress) {
511 DeviceAddress usbHeadset;
512 usbHeadset.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_USB_HEADSET);
513 usbHeadset.address.alsa({1, 2});
514 ConvertDeviceAddress(usbHeadset);
515 }
516
TEST(HidlUtils,ConvertBusDeviceAddress)517 TEST(HidlUtils, ConvertBusDeviceAddress) {
518 DeviceAddress bus;
519 bus.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_BUS);
520 bus.address.id("bus_device");
521 ConvertDeviceAddress(bus);
522 }
523
TEST(HidlUtils,ConvertRSubmixDeviceAddress)524 TEST(HidlUtils, ConvertRSubmixDeviceAddress) {
525 DeviceAddress rSubmix;
526 rSubmix.deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_REMOTE_SUBMIX);
527 rSubmix.address.id(AUDIO_REMOTE_SUBMIX_DEVICE_ADDRESS);
528 ConvertDeviceAddress(rSubmix);
529 }
530
TEST(HidlUtils,ConvertVendorDeviceAddress)531 TEST(HidlUtils, ConvertVendorDeviceAddress) {
532 // The address part is not mandatory, both cases must work.
533 {
534 DeviceAddress vendor;
535 vendor.deviceType = "VX_GOOGLE_VR";
536 audio_devices_t halDeviceType;
537 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN] = {};
538 // Ignore the result. Vendors will also add the extended device into
539 // the list of devices in audio-hal-enums.h. Without that, the conversion
540 // officially fails, but it still maps the device type to NONE.
541 (void)HidlUtils::deviceAddressToHal(vendor, &halDeviceType, halDeviceAddress);
542 EXPECT_EQ(AUDIO_DEVICE_NONE, halDeviceType);
543 EXPECT_EQ(0, strnlen(halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN));
544 }
545 {
546 DeviceAddress vendor;
547 vendor.deviceType = "VX_GOOGLE_VR";
548 vendor.address.id("vr1");
549 audio_devices_t halDeviceType;
550 char halDeviceAddress[AUDIO_DEVICE_MAX_ADDRESS_LEN] = {};
551 // Ignore the result. Vendors will also add the extended device into
552 // the list of devices in audio-hal-enums.h. Without that, the conversion
553 // officially fails, but it still maps the device type to NONE and converts
554 // the address.
555 (void)HidlUtils::deviceAddressToHal(vendor, &halDeviceType, halDeviceAddress);
556 EXPECT_EQ(AUDIO_DEVICE_NONE, halDeviceType);
557 EXPECT_EQ(0, strncmp("vr1", halDeviceAddress, AUDIO_DEVICE_MAX_ADDRESS_LEN));
558 }
559 }
560
TEST(HidlUtils,ConvertInvalidFormat)561 TEST(HidlUtils, ConvertInvalidFormat) {
562 AudioFormat invalid;
563 EXPECT_EQ(BAD_VALUE, HidlUtils::audioFormatFromHal(kInvalidHalFormat, &invalid));
564 audio_format_t halInvalid;
565 EXPECT_EQ(BAD_VALUE, HidlUtils::audioFormatToHal("", &halInvalid));
566 // INVALID format is not in XSD thus it's not allowed for transfer over HIDL.
567 EXPECT_EQ(BAD_VALUE, HidlUtils::audioFormatToHal("AUDIO_FORMAT_INVALID", &halInvalid));
568 EXPECT_EQ(BAD_VALUE, HidlUtils::audioFormatToHal("random string", &halInvalid));
569 }
570
TEST(HidlUtils,ConvertFormat)571 TEST(HidlUtils, ConvertFormat) {
572 for (const auto enumVal : xsdc_enum_range<xsd::AudioFormat>{}) {
573 const AudioFormat format = toString(enumVal);
574 audio_format_t halFormat;
575 AudioFormat formatBack;
576 EXPECT_EQ(NO_ERROR, HidlUtils::audioFormatToHal(format, &halFormat))
577 << "Conversion of \"" << format << "\" failed";
578 EXPECT_EQ(enumVal != xsd::AudioFormat::AUDIO_FORMAT_DEFAULT,
579 audio_is_valid_format(halFormat))
580 << "Validity of \"" << format << "\" is not as expected";
581 EXPECT_EQ(NO_ERROR, HidlUtils::audioFormatFromHal(halFormat, &formatBack))
582 << "Conversion of format " << halFormat << " failed";
583 EXPECT_EQ(format, formatBack);
584 }
585 }
586
TEST(HidlUtils,ConvertInvalidFormatsFromHal)587 TEST(HidlUtils, ConvertInvalidFormatsFromHal) {
588 std::vector<std::string> validAndInvalidFormats = {
589 toString(xsd::AudioFormat::AUDIO_FORMAT_PCM_16_BIT), "random string", ""};
590 hidl_vec<AudioFormat> validFormat;
591 EXPECT_EQ(BAD_VALUE, HidlUtils::audioFormatsFromHal(validAndInvalidFormats, &validFormat));
592 EXPECT_EQ(1, validFormat.size());
593 EXPECT_EQ(validAndInvalidFormats[0], validFormat[0]);
594
595 std::vector<std::string> invalidFormats = {"random string", ""};
596 hidl_vec<AudioFormat> empty;
597 EXPECT_EQ(BAD_VALUE, HidlUtils::audioFormatsFromHal(invalidFormats, &empty));
598 EXPECT_EQ(0, empty.size());
599 }
600
TEST(HidlUtils,ConvertFormatsFromHal)601 TEST(HidlUtils, ConvertFormatsFromHal) {
602 std::vector<std::string> allHalFormats;
603 for (const auto enumVal : xsdc_enum_range<xsd::AudioFormat>{}) {
604 allHalFormats.push_back(toString(enumVal));
605 }
606 hidl_vec<AudioFormat> allFormats;
607 EXPECT_EQ(NO_ERROR, HidlUtils::audioFormatsFromHal(allHalFormats, &allFormats));
608 EXPECT_EQ(allHalFormats.size(), allFormats.size());
609 for (size_t i = 0; i < allHalFormats.size(); ++i) {
610 EXPECT_EQ(allHalFormats[i], allFormats[i]);
611 }
612 }
613
TEST(HidlUtils,ConvertInvalidGainModeMask)614 TEST(HidlUtils, ConvertInvalidGainModeMask) {
615 hidl_vec<AudioGainMode> invalid;
616 EXPECT_EQ(BAD_VALUE, HidlUtils::audioGainModeMaskFromHal(kInvalidHalGainMode, &invalid));
617 audio_gain_mode_t halInvalid;
618 invalid.resize(1);
619 invalid[0] = "random string";
620 EXPECT_EQ(BAD_VALUE, HidlUtils::audioGainModeMaskToHal(invalid, &halInvalid));
621 }
622
TEST(HidlUtils,ConvertGainModeMask)623 TEST(HidlUtils, ConvertGainModeMask) {
624 hidl_vec<AudioGainMode> emptyGainModes;
625 audio_gain_mode_t halEmptyGainModes;
626 EXPECT_EQ(NO_ERROR, HidlUtils::audioGainModeMaskToHal(emptyGainModes, &halEmptyGainModes));
627 hidl_vec<AudioGainMode> emptyGainModesBack;
628 EXPECT_EQ(NO_ERROR,
629 HidlUtils::audioGainModeMaskFromHal(halEmptyGainModes, &emptyGainModesBack));
630 EXPECT_EQ(emptyGainModes, emptyGainModesBack);
631
632 std::vector<std::string> allEnumValues;
633 for (const auto enumVal : xsdc_enum_range<xsd::AudioGainMode>{}) {
634 allEnumValues.push_back(toString(enumVal));
635 }
636 hidl_vec<AudioGainMode> allGainModes;
637 allGainModes.resize(allEnumValues.size());
638 for (size_t i = 0; i < allEnumValues.size(); ++i) {
639 allGainModes[i] = allEnumValues[i];
640 }
641 audio_gain_mode_t halAllGainModes;
642 EXPECT_EQ(NO_ERROR, HidlUtils::audioGainModeMaskToHal(allGainModes, &halAllGainModes));
643 hidl_vec<AudioGainMode> allGainModesBack;
644 EXPECT_EQ(NO_ERROR, HidlUtils::audioGainModeMaskFromHal(halAllGainModes, &allGainModesBack));
645 EXPECT_EQ(allGainModes, allGainModesBack);
646 }
647
TEST(HidlUtils,ConvertInvalidSource)648 TEST(HidlUtils, ConvertInvalidSource) {
649 AudioSource invalid;
650 EXPECT_EQ(BAD_VALUE, HidlUtils::audioSourceFromHal(kInvalidHalSource, &invalid));
651 audio_source_t halInvalid;
652 EXPECT_EQ(BAD_VALUE, HidlUtils::audioSourceToHal("", &halInvalid));
653 // INVALID source is not in XSD thus it's not allowed for transfer over HIDL.
654 EXPECT_EQ(BAD_VALUE, HidlUtils::audioSourceToHal("AUDIO_SOURCE_INVALID", &halInvalid));
655 EXPECT_EQ(BAD_VALUE, HidlUtils::audioSourceToHal("random string", &halInvalid));
656 }
657
TEST(HidlUtils,ConvertSource)658 TEST(HidlUtils, ConvertSource) {
659 for (const auto enumVal : xsdc_enum_range<xsd::AudioSource>{}) {
660 const AudioSource source = toString(enumVal);
661 audio_source_t halSource;
662 AudioSource sourceBack;
663 EXPECT_EQ(NO_ERROR, HidlUtils::audioSourceToHal(source, &halSource))
664 << "Conversion of \"" << source << "\" failed";
665 EXPECT_EQ(enumVal != xsd::AudioSource::AUDIO_SOURCE_DEFAULT,
666 audio_is_valid_audio_source(halSource))
667 << "Validity of \"" << source << "\" is not as expected";
668 EXPECT_EQ(NO_ERROR, HidlUtils::audioSourceFromHal(halSource, &sourceBack))
669 << "Conversion of source " << halSource << " failed";
670 EXPECT_EQ(source, sourceBack);
671 }
672 }
673
TEST(HidlUtils,ConvertInvalidStreamType)674 TEST(HidlUtils, ConvertInvalidStreamType) {
675 AudioStreamType invalid;
676 EXPECT_EQ(BAD_VALUE, HidlUtils::audioStreamTypeFromHal(kInvalidHalStreamType, &invalid));
677 audio_stream_type_t halInvalid;
678 EXPECT_EQ(BAD_VALUE, HidlUtils::audioStreamTypeToHal("random string", &halInvalid));
679 }
680
TEST(HidlUtils,ConvertDefaultStreamType)681 TEST(HidlUtils, ConvertDefaultStreamType) {
682 AudioStreamType streamDefault = "";
683 audio_stream_type_t halStreamDefault;
684 EXPECT_EQ(NO_ERROR, HidlUtils::audioStreamTypeToHal(streamDefault, &halStreamDefault));
685 AudioStreamType streamDefaultBack;
686 EXPECT_EQ(NO_ERROR, HidlUtils::audioStreamTypeFromHal(halStreamDefault, &streamDefaultBack));
687 EXPECT_EQ(streamDefault, streamDefaultBack);
688 }
689
TEST(HidlUtils,ConvertStreamType)690 TEST(HidlUtils, ConvertStreamType) {
691 for (const auto enumVal : xsdc_enum_range<xsd::AudioStreamType>{}) {
692 const AudioStreamType streamType = toString(enumVal);
693 audio_stream_type_t halStreamType;
694 AudioStreamType streamTypeBack;
695 EXPECT_EQ(NO_ERROR, HidlUtils::audioStreamTypeToHal(streamType, &halStreamType))
696 << "Conversion of \"" << streamType << "\" failed";
697 EXPECT_EQ(NO_ERROR, HidlUtils::audioStreamTypeFromHal(halStreamType, &streamTypeBack))
698 << "Conversion of stream type " << halStreamType << " failed";
699 EXPECT_EQ(streamType, streamTypeBack);
700 }
701 }
702
TEST(HidlUtils,ConvertInvalidGain)703 TEST(HidlUtils, ConvertInvalidGain) {
704 AudioGain invalid;
705 EXPECT_EQ(BAD_VALUE, HidlUtils::audioGainFromHal({.mode = kInvalidHalGainMode},
706 false /*isInput*/, &invalid));
707 EXPECT_EQ(BAD_VALUE, HidlUtils::audioGainFromHal({.mode = kInvalidHalGainMode},
708 true /*isInput*/, &invalid));
709 struct audio_gain halInvalid;
710 invalid.mode.resize(1);
711 invalid.mode[0] = "random string";
712 EXPECT_EQ(BAD_VALUE, HidlUtils::audioGainToHal(invalid, &halInvalid));
713 }
714
TEST(HidlUtils,ConvertGain)715 TEST(HidlUtils, ConvertGain) {
716 AudioGain gain = {};
717 gain.channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO);
718 struct audio_gain halGain;
719 EXPECT_EQ(NO_ERROR, HidlUtils::audioGainToHal(gain, &halGain));
720 AudioGain gainBack;
721 EXPECT_EQ(NO_ERROR, HidlUtils::audioGainFromHal(halGain, false /*isInput*/, &gainBack));
722 EXPECT_EQ(gain, gainBack);
723 struct audio_gain halGainBack;
724 EXPECT_EQ(NO_ERROR, HidlUtils::audioGainToHal(gainBack, &halGainBack));
725 EXPECT_TRUE(audio_gains_are_equal(&halGain, &halGainBack));
726 }
727
TEST(HidlUtils,ConvertInvalidGainConfig)728 TEST(HidlUtils, ConvertInvalidGainConfig) {
729 AudioGainConfig invalid;
730 EXPECT_EQ(BAD_VALUE, HidlUtils::audioGainConfigFromHal({.mode = kInvalidHalGainMode},
731 false /*isInput*/, &invalid));
732 EXPECT_EQ(BAD_VALUE, HidlUtils::audioGainConfigFromHal({.mode = kInvalidHalGainMode},
733 true /*isInput*/, &invalid));
734 struct audio_gain_config halInvalid;
735 invalid.mode.resize(1);
736 invalid.mode[0] = "random string";
737 EXPECT_EQ(BAD_VALUE, HidlUtils::audioGainConfigToHal(invalid, &halInvalid));
738 }
739
TEST(HidlUtils,ConvertGainConfig)740 TEST(HidlUtils, ConvertGainConfig) {
741 AudioGainConfig gainConfig = {};
742 gainConfig.channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO);
743 struct audio_gain_config halGainConfig;
744 EXPECT_EQ(NO_ERROR, HidlUtils::audioGainConfigToHal(gainConfig, &halGainConfig));
745 AudioGainConfig gainConfigBack;
746 EXPECT_EQ(NO_ERROR,
747 HidlUtils::audioGainConfigFromHal(halGainConfig, false /*isInput*/, &gainConfigBack));
748 EXPECT_EQ(gainConfig, gainConfigBack);
749 struct audio_gain_config halGainConfigBack;
750 EXPECT_EQ(NO_ERROR, HidlUtils::audioGainConfigToHal(gainConfigBack, &halGainConfigBack));
751 EXPECT_TRUE(audio_gain_config_are_equal(&halGainConfig, &halGainConfigBack));
752 }
753
TEST(HidlUtils,ConvertInvalidUsage)754 TEST(HidlUtils, ConvertInvalidUsage) {
755 AudioUsage invalid;
756 EXPECT_EQ(BAD_VALUE, HidlUtils::audioUsageFromHal(kInvalidHalUsage, &invalid));
757 audio_usage_t halInvalid;
758 EXPECT_EQ(BAD_VALUE, HidlUtils::audioUsageToHal("", &halInvalid));
759 EXPECT_EQ(BAD_VALUE, HidlUtils::audioUsageToHal("random string", &halInvalid));
760 }
761
TEST(HidlUtils,ConvertUsage)762 TEST(HidlUtils, ConvertUsage) {
763 for (const auto enumVal : xsdc_enum_range<xsd::AudioUsage>{}) {
764 const AudioUsage usage = toString(enumVal);
765 audio_usage_t halUsage;
766 AudioUsage usageBack;
767 EXPECT_EQ(NO_ERROR, HidlUtils::audioUsageToHal(usage, &halUsage))
768 << "Conversion of \"" << usage << "\" failed";
769 EXPECT_EQ(NO_ERROR, HidlUtils::audioUsageFromHal(halUsage, &usageBack))
770 << "Conversion of usage " << halUsage << " failed";
771 EXPECT_EQ(usage, usageBack);
772 }
773 }
774
TEST(HidlUtils,ConvertInvalidOffloadInfo)775 TEST(HidlUtils, ConvertInvalidOffloadInfo) {
776 AudioOffloadInfo invalid;
777 audio_offload_info_t halInvalid = AUDIO_INFO_INITIALIZER;
778 halInvalid.channel_mask = kInvalidHalChannelMask;
779 halInvalid.format = kInvalidHalFormat;
780 EXPECT_EQ(BAD_VALUE, HidlUtils::audioOffloadInfoFromHal(halInvalid, &invalid));
781 invalid.base.channelMask = "random string";
782 invalid.base.format = "random string";
783 EXPECT_EQ(BAD_VALUE, HidlUtils::audioOffloadInfoToHal(invalid, &halInvalid));
784 }
785
TEST(HidlUtils,ConvertOffloadInfo)786 TEST(HidlUtils, ConvertOffloadInfo) {
787 AudioOffloadInfo offloadInfo = {};
788 offloadInfo.base = generateValidConfigBase(false /*isInput*/);
789 offloadInfo.streamType = toString(xsd::AudioStreamType::AUDIO_STREAM_MUSIC);
790 offloadInfo.bitRatePerSecond = 320;
791 offloadInfo.durationMicroseconds = -1;
792 offloadInfo.bitWidth = 16;
793 offloadInfo.bufferSize = 1024;
794 offloadInfo.usage = toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA);
795 offloadInfo.encapsulationMode = AudioEncapsulationMode::ELEMENTARY_STREAM;
796 offloadInfo.contentId = 42;
797 offloadInfo.syncId = 13;
798 audio_offload_info_t halOffloadInfo;
799 EXPECT_EQ(NO_ERROR, HidlUtils::audioOffloadInfoToHal(offloadInfo, &halOffloadInfo));
800 AudioOffloadInfo offloadInfoBack;
801 EXPECT_EQ(NO_ERROR, HidlUtils::audioOffloadInfoFromHal(halOffloadInfo, &offloadInfoBack));
802 EXPECT_EQ(offloadInfo, offloadInfoBack);
803 }
804
TEST(HidlUtils,ConvertInvalidConfig)805 TEST(HidlUtils, ConvertInvalidConfig) {
806 AudioConfig invalid;
807 audio_config_t halInvalidChannelMask = AUDIO_CONFIG_INITIALIZER;
808 halInvalidChannelMask.channel_mask = kInvalidHalChannelMask;
809 EXPECT_EQ(BAD_VALUE,
810 HidlUtils::audioConfigFromHal(halInvalidChannelMask, false /*isInput*/, &invalid));
811 EXPECT_EQ(BAD_VALUE,
812 HidlUtils::audioConfigFromHal(halInvalidChannelMask, true /*isInput*/, &invalid));
813 audio_config_t halInvalidFormat = AUDIO_CONFIG_INITIALIZER;
814 halInvalidFormat.format = kInvalidHalFormat;
815 EXPECT_EQ(BAD_VALUE,
816 HidlUtils::audioConfigFromHal(halInvalidFormat, false /*isInput*/, &invalid));
817 EXPECT_EQ(BAD_VALUE,
818 HidlUtils::audioConfigFromHal(halInvalidFormat, true /*isInput*/, &invalid));
819
820 AudioConfig invalidChannelMask;
821 audio_config_t halInvalid;
822 invalidChannelMask.base.channelMask = "random string";
823 invalidChannelMask.base.format = toString(xsd::AudioFormat::AUDIO_FORMAT_DEFAULT);
824 EXPECT_EQ(BAD_VALUE, HidlUtils::audioConfigToHal(invalidChannelMask, &halInvalid));
825 AudioConfig invalidFormat;
826 invalidFormat.base.format = "random string";
827 invalidFormat.base.channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
828 EXPECT_EQ(BAD_VALUE, HidlUtils::audioConfigToHal(invalidFormat, &halInvalid));
829 }
830
TEST(HidlUtils,ConvertConfigDefault)831 TEST(HidlUtils, ConvertConfigDefault) {
832 audio_config_t halDefault = AUDIO_CONFIG_INITIALIZER;
833 AudioConfig defaultOut, defaultIn;
834 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigFromHal(halDefault, false /*isInput*/, &defaultOut));
835 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigFromHal(halDefault, true /*isInput*/, &defaultIn));
836 EXPECT_EQ(defaultOut, defaultIn);
837 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigToHal(defaultOut, &halDefault));
838
839 // Note: empty channel mask and config are not valid values.
840 AudioConfig defaultCfg{};
841 defaultCfg.base.channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_NONE);
842 defaultCfg.base.format = toString(xsd::AudioFormat::AUDIO_FORMAT_DEFAULT);
843 audio_config_t halDefaultCfg;
844 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigToHal(defaultCfg, &halDefaultCfg));
845 AudioConfig defaultCfgBackOut, defaultCfgBackIn;
846 EXPECT_EQ(NO_ERROR,
847 HidlUtils::audioConfigFromHal(halDefaultCfg, false /*isInput*/, &defaultCfgBackOut));
848 EXPECT_EQ(NO_ERROR,
849 HidlUtils::audioConfigFromHal(halDefaultCfg, true /*isInput*/, &defaultCfgBackIn));
850 EXPECT_EQ(defaultCfgBackOut, defaultCfgBackIn);
851 EXPECT_EQ(defaultCfg, defaultCfgBackOut);
852 }
853
TEST(HidlUtils,ConvertConfig)854 TEST(HidlUtils, ConvertConfig) {
855 AudioConfig configOut{};
856 configOut.base = generateValidConfigBase(false /*isInput*/);
857 audio_config_t halConfigOut;
858 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigToHal(configOut, &halConfigOut));
859 AudioConfig configOutBack;
860 EXPECT_EQ(NO_ERROR,
861 HidlUtils::audioConfigFromHal(halConfigOut, false /*isInput*/, &configOutBack));
862 EXPECT_EQ(configOut, configOutBack);
863
864 AudioConfig configIn{};
865 configIn.base = generateValidConfigBase(true /*isInput*/);
866 audio_config_t halConfigIn;
867 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigToHal(configIn, &halConfigIn));
868 AudioConfig configInBack;
869 EXPECT_EQ(NO_ERROR,
870 HidlUtils::audioConfigFromHal(halConfigIn, true /*isInput*/, &configInBack));
871 EXPECT_EQ(configIn, configInBack);
872 }
873
TEST(HidlUtils,ConvertConfigWithOffloadInfo)874 TEST(HidlUtils, ConvertConfigWithOffloadInfo) {
875 AudioConfig config = {};
876 config.base = generateValidConfigBase(false /*isInput*/);
877 config.offloadInfo.info(
878 AudioOffloadInfo{.base = config.base,
879 .streamType = toString(xsd::AudioStreamType::AUDIO_STREAM_MUSIC),
880 .bitRatePerSecond = 320,
881 .durationMicroseconds = -1,
882 .bitWidth = 16,
883 .bufferSize = 1024,
884 .usage = toString(xsd::AudioUsage::AUDIO_USAGE_MEDIA),
885 .encapsulationMode = AudioEncapsulationMode::ELEMENTARY_STREAM,
886 .contentId = 42,
887 .syncId = 13});
888 audio_config_t halConfig;
889 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigToHal(config, &halConfig));
890 AudioConfig configBack;
891 EXPECT_EQ(NO_ERROR, HidlUtils::audioConfigFromHal(halConfig, false /*isInput*/, &configBack));
892 EXPECT_EQ(config, configBack);
893 }
894
TEST(HidlUtils,ConvertInvalidAudioProfile)895 TEST(HidlUtils, ConvertInvalidAudioProfile) {
896 AudioProfile invalid;
897 struct audio_profile halInvalid = {};
898 halInvalid.format = kInvalidHalFormat;
899 halInvalid.num_sample_rates = 0;
900 halInvalid.num_channel_masks = 1;
901 halInvalid.channel_masks[0] = kInvalidHalChannelMask;
902 EXPECT_EQ(BAD_VALUE, HidlUtils::audioProfileFromHal(halInvalid, false /*isInput*/, &invalid));
903 EXPECT_EQ(BAD_VALUE, HidlUtils::audioProfileFromHal(halInvalid, true /*isInput*/, &invalid));
904 invalid.format = "random string";
905 EXPECT_EQ(BAD_VALUE, HidlUtils::audioProfileToHal(invalid, &halInvalid));
906 }
907
TEST(HidlUtils,ConvertAudioProfile)908 TEST(HidlUtils, ConvertAudioProfile) {
909 AudioProfile profile = {};
910 profile.format = toString(xsd::AudioFormat::AUDIO_FORMAT_PCM_16_BIT);
911 profile.sampleRates.resize(2);
912 profile.sampleRates[0] = 44100;
913 profile.sampleRates[1] = 48000;
914 profile.channelMasks.resize(2);
915 profile.channelMasks[0] = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_MONO);
916 profile.channelMasks[1] = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO);
917 struct audio_profile halProfile;
918 EXPECT_EQ(NO_ERROR, HidlUtils::audioProfileToHal(profile, &halProfile));
919 AudioProfile profileBack;
920 EXPECT_EQ(NO_ERROR,
921 HidlUtils::audioProfileFromHal(halProfile, false /*isInput*/, &profileBack));
922 EXPECT_EQ(profile, profileBack);
923 }
924
TEST(HidlUtils,ConvertInvalidAudioPortConfig)925 TEST(HidlUtils, ConvertInvalidAudioPortConfig) {
926 AudioPortConfig invalid;
927 struct audio_port_config halInvalid = {};
928 halInvalid.type = AUDIO_PORT_TYPE_MIX;
929 halInvalid.role = AUDIO_PORT_ROLE_NONE; // note: this is valid.
930 halInvalid.config_mask = AUDIO_PORT_CONFIG_CHANNEL_MASK;
931 halInvalid.channel_mask = kInvalidHalChannelMask;
932 EXPECT_EQ(BAD_VALUE, HidlUtils::audioPortConfigFromHal(halInvalid, &invalid));
933 invalid.base.channelMask.value("random string");
934 EXPECT_EQ(BAD_VALUE, HidlUtils::audioPortConfigToHal(invalid, &halInvalid));
935 }
936
TEST(HidlUtils,ConvertAudioPortConfig)937 TEST(HidlUtils, ConvertAudioPortConfig) {
938 AudioPortConfig config = {};
939 config.id = 42;
940 config.base.sampleRateHz.value(44100);
941 config.base.channelMask.value(toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO));
942 config.base.format.value(toString(xsd::AudioFormat::AUDIO_FORMAT_PCM_16_BIT));
943 config.gain.config({});
944 config.gain.config().channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO);
945 config.ext.device({});
946 config.ext.device().deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_SPEAKER);
947 struct audio_port_config halConfig;
948 EXPECT_EQ(NO_ERROR, HidlUtils::audioPortConfigToHal(config, &halConfig));
949 AudioPortConfig configBack;
950 EXPECT_EQ(NO_ERROR, HidlUtils::audioPortConfigFromHal(halConfig, &configBack));
951 EXPECT_EQ(config, configBack);
952 struct audio_port_config halConfigBack;
953 EXPECT_EQ(NO_ERROR, HidlUtils::audioPortConfigToHal(configBack, &halConfigBack));
954 EXPECT_TRUE(audio_port_configs_are_equal(&halConfig, &halConfigBack));
955 }
956
generateValidAudioProfile()957 static AudioProfile generateValidAudioProfile() {
958 AudioProfile profile;
959 profile.format = toString(xsd::AudioFormat::AUDIO_FORMAT_PCM_16_BIT);
960 profile.sampleRates.resize(2);
961 profile.sampleRates[0] = 44100;
962 profile.sampleRates[1] = 48000;
963 profile.channelMasks.resize(2);
964 profile.channelMasks[0] = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_MONO);
965 profile.channelMasks[1] = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO);
966 return profile;
967 }
968
TEST(HidlUtils,ConvertInvalidAudioTransports)969 TEST(HidlUtils, ConvertInvalidAudioTransports) {
970 hidl_vec<AudioTransport> invalid;
971 struct audio_port_v7 halInvalid = {};
972 halInvalid.num_audio_profiles = 1;
973 halInvalid.audio_profiles[0].format = kInvalidHalFormat;
974 halInvalid.audio_profiles[0].encapsulation_type = kInvalidEncapsulationType;
975 halInvalid.num_extra_audio_descriptors = 1;
976 halInvalid.extra_audio_descriptors[0].standard = kInvalidAudioStandard;
977 halInvalid.extra_audio_descriptors[0].descriptor_length = EXTRA_AUDIO_DESCRIPTOR_SIZE + 1;
978 EXPECT_EQ(BAD_VALUE,
979 HidlUtils::audioTransportsFromHal(halInvalid, false /*isInput*/, &invalid));
980 invalid.resize(2);
981 AudioProfile invalidProfile;
982 invalidProfile.format = "random string";
983 invalid[0].audioCapability.profile(invalidProfile);
984 invalid[0].encapsulationType = "random string";
985 invalid[0].audioCapability.edid(hidl_vec<uint8_t>(EXTRA_AUDIO_DESCRIPTOR_SIZE + 1));
986 invalid[1].encapsulationType = "random string";
987 EXPECT_EQ(BAD_VALUE, HidlUtils::audioTransportsToHal(invalid, &halInvalid));
988
989 // The size of audio profile must not be greater than the maximum value.
990 invalid.resize(0);
991 invalid.resize(AUDIO_PORT_MAX_AUDIO_PROFILES + 1);
992 for (size_t i = 0; i < invalid.size(); ++i) {
993 invalid[i].audioCapability.profile(generateValidAudioProfile());
994 invalid[i].encapsulationType =
995 toString(xsd::AudioEncapsulationType::AUDIO_ENCAPSULATION_TYPE_NONE);
996 }
997 EXPECT_EQ(BAD_VALUE, HidlUtils::audioTransportsToHal(invalid, &halInvalid));
998
999 // The size of extra audio descriptors must not be greater than the maximum value.
1000 invalid.resize(0);
1001 invalid.resize(AUDIO_PORT_MAX_EXTRA_AUDIO_DESCRIPTORS + 1);
1002 for (size_t i = 0; i < invalid.size(); ++i) {
1003 invalid[i].audioCapability.edid({0x11, 0x06, 0x01});
1004 invalid[i].encapsulationType =
1005 toString(xsd::AudioEncapsulationType::AUDIO_ENCAPSULATION_TYPE_IEC61937);
1006 }
1007 EXPECT_EQ(BAD_VALUE, HidlUtils::audioTransportsToHal(invalid, &halInvalid));
1008 }
1009
TEST(HidlUtils,ConvertAudioTransports)1010 TEST(HidlUtils, ConvertAudioTransports) {
1011 hidl_vec<AudioTransport> transports;
1012 transports.resize(2);
1013 transports[0].audioCapability.profile(generateValidAudioProfile());
1014 hidl_vec<uint8_t> shortAudioDescriptor({0x11, 0x06, 0x01});
1015 transports[0].encapsulationType =
1016 toString(xsd::AudioEncapsulationType::AUDIO_ENCAPSULATION_TYPE_NONE);
1017 transports[1].audioCapability.edid(std::move(shortAudioDescriptor));
1018 transports[1].encapsulationType =
1019 toString(xsd::AudioEncapsulationType::AUDIO_ENCAPSULATION_TYPE_IEC61937);
1020 struct audio_port_v7 halPort;
1021 EXPECT_EQ(NO_ERROR, HidlUtils::audioTransportsToHal(transports, &halPort));
1022 hidl_vec<AudioTransport> transportsBack;
1023 EXPECT_EQ(NO_ERROR,
1024 HidlUtils::audioTransportsFromHal(halPort, false /*isInput*/, &transportsBack));
1025 EXPECT_EQ(transports, transportsBack);
1026 }
1027
TEST(HidlUtils,ConvertInvalidAudioPort)1028 TEST(HidlUtils, ConvertInvalidAudioPort) {
1029 AudioPort invalid;
1030 struct audio_port_v7 halInvalid = {};
1031 halInvalid.type = AUDIO_PORT_TYPE_MIX;
1032 halInvalid.role = AUDIO_PORT_ROLE_NONE; // note: this is valid.
1033 halInvalid.num_audio_profiles = 1;
1034 halInvalid.audio_profiles[0].format = kInvalidHalFormat;
1035 EXPECT_EQ(BAD_VALUE, HidlUtils::audioPortFromHal(halInvalid, &invalid));
1036 invalid.transports.resize(1);
1037 AudioProfile invalidProfile;
1038 invalidProfile.format = "random string";
1039 invalid.transports[0].audioCapability.profile(invalidProfile);
1040 EXPECT_EQ(BAD_VALUE, HidlUtils::audioPortToHal(invalid, &halInvalid));
1041 }
1042
TEST(HidlUtils,ConvertAudioPort)1043 TEST(HidlUtils, ConvertAudioPort) {
1044 AudioPort port = {};
1045 port.id = 42;
1046 port.name = "test";
1047 port.transports.resize(2);
1048 AudioProfile profile;
1049 profile.format = toString(xsd::AudioFormat::AUDIO_FORMAT_PCM_16_BIT);
1050 profile.sampleRates.resize(2);
1051 profile.sampleRates[0] = 44100;
1052 profile.sampleRates[1] = 48000;
1053 profile.channelMasks.resize(2);
1054 profile.channelMasks[0] = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_MONO);
1055 profile.channelMasks[1] = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO);
1056 port.transports[0].audioCapability.profile(profile);
1057 port.transports[0].encapsulationType =
1058 toString(xsd::AudioEncapsulationType::AUDIO_ENCAPSULATION_TYPE_NONE);
1059 hidl_vec<uint8_t> shortAudioDescriptor({0x11, 0x06, 0x01});
1060 port.transports[1].audioCapability.edid(std::move(shortAudioDescriptor));
1061 port.transports[1].encapsulationType =
1062 toString(xsd::AudioEncapsulationType::AUDIO_ENCAPSULATION_TYPE_IEC61937);
1063 port.gains.resize(1);
1064 port.gains[0].channelMask = toString(xsd::AudioChannelMask::AUDIO_CHANNEL_OUT_STEREO);
1065 port.ext.device({});
1066 port.ext.device().deviceType = toString(xsd::AudioDevice::AUDIO_DEVICE_OUT_SPEAKER);
1067 // active config left unspecified.
1068 struct audio_port_v7 halPort;
1069 EXPECT_EQ(NO_ERROR, HidlUtils::audioPortToHal(port, &halPort));
1070 AudioPort portBack;
1071 EXPECT_EQ(NO_ERROR, HidlUtils::audioPortFromHal(halPort, &portBack));
1072 EXPECT_EQ(port, portBack);
1073 struct audio_port_v7 halPortBack;
1074 EXPECT_EQ(NO_ERROR, HidlUtils::audioPortToHal(portBack, &halPortBack));
1075 EXPECT_TRUE(audio_ports_v7_are_equal(&halPort, &halPortBack));
1076 }
1077
TEST(HidlUtils,ConvertInvalidAudioTags)1078 TEST(HidlUtils, ConvertInvalidAudioTags) {
1079 char halTag[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE] = {};
1080
1081 hidl_vec<AudioTag> emptyTag = {{""}};
1082 EXPECT_EQ(BAD_VALUE, HidlUtils::audioTagsToHal(emptyTag, halTag));
1083
1084 hidl_vec<AudioTag> longTag = {{std::string(AUDIO_ATTRIBUTES_TAGS_MAX_SIZE + 1, 'A')}};
1085 EXPECT_EQ(BAD_VALUE, HidlUtils::audioTagsToHal(longTag, halTag));
1086
1087 hidl_vec<AudioTag> tagSeparator = {
1088 {std::string(AUDIO_ATTRIBUTES_TAGS_MAX_SIZE - 1, HidlUtils::sAudioTagSeparator)}};
1089 EXPECT_EQ(BAD_VALUE, HidlUtils::audioTagsToHal(tagSeparator, halTag));
1090
1091 hidl_vec<AudioTag> notExtensions = {{"", "random string", "VX_", "VX_GOOGLE_$$"}};
1092 EXPECT_EQ(BAD_VALUE, HidlUtils::audioTagsToHal(notExtensions, halTag));
1093 }
1094
TEST(HidlUtils,ConvertAudioTags)1095 TEST(HidlUtils, ConvertAudioTags) {
1096 hidl_vec<AudioTag> emptyTags;
1097 char halEmptyTags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE] = {};
1098 EXPECT_EQ(NO_ERROR, HidlUtils::audioTagsToHal(emptyTags, halEmptyTags));
1099 hidl_vec<AudioTag> emptyTagsBack;
1100 EXPECT_EQ(NO_ERROR,
1101 HidlUtils::audioTagsFromHal(HidlUtils::splitAudioTags(halEmptyTags), &emptyTagsBack));
1102 EXPECT_EQ(emptyTags, emptyTagsBack);
1103
1104 hidl_vec<AudioTag> oneTag = {{"VX_GOOGLE_VR"}};
1105 char halOneTag[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE] = {};
1106 EXPECT_EQ(NO_ERROR, HidlUtils::audioTagsToHal(oneTag, halOneTag));
1107 hidl_vec<AudioTag> oneTagBack;
1108 EXPECT_EQ(NO_ERROR,
1109 HidlUtils::audioTagsFromHal(HidlUtils::splitAudioTags(halOneTag), &oneTagBack));
1110 EXPECT_EQ(oneTag, oneTagBack);
1111
1112 hidl_vec<AudioTag> twoTags = {{"VX_GOOGLE_VR_42", "VX_GOOGLE_1E100"}};
1113 char halTwoTags[AUDIO_ATTRIBUTES_TAGS_MAX_SIZE] = {};
1114 EXPECT_EQ(NO_ERROR, HidlUtils::audioTagsToHal(twoTags, halTwoTags));
1115 hidl_vec<AudioTag> twoTagsBack;
1116 EXPECT_EQ(NO_ERROR,
1117 HidlUtils::audioTagsFromHal(HidlUtils::splitAudioTags(halTwoTags), &twoTagsBack));
1118 EXPECT_EQ(twoTags, twoTagsBack);
1119 }
1120
1121 template <typename T>
1122 class FilterTest : public ::testing::Test {};
1123 using FilterTestTypeParams = ::testing::Types<hidl_vec<AudioTag>, std::vector<std::string>>;
1124 TYPED_TEST_SUITE(FilterTest, FilterTestTypeParams);
1125
TYPED_TEST(FilterTest,FilterOutNonVendorTags)1126 TYPED_TEST(FilterTest, FilterOutNonVendorTags) {
1127 TypeParam emptyTags;
1128 EXPECT_EQ(emptyTags, HidlUtils::filterOutNonVendorTags(emptyTags));
1129
1130 // b/248421569, allocate two vendor tags at a time can run out of memory
1131 // TypeParam allVendorTags = {{"VX_GOOGLE_VR_42", "VX_GOOGLE_1E100"}};
1132 TypeParam allVendorTags1 = {{"VX_GOOGLE_VR_42"}};
1133 EXPECT_EQ(allVendorTags1, HidlUtils::filterOutNonVendorTags(allVendorTags1));
1134 TypeParam allVendorTags2 = {{"VX_GOOGLE_1E100"}};
1135 EXPECT_EQ(allVendorTags2, HidlUtils::filterOutNonVendorTags(allVendorTags2));
1136
1137 TypeParam oneVendorTag = {{"", "VX_GOOGLE_VR", "random_string"}};
1138 TypeParam oneVendorTagOnly = HidlUtils::filterOutNonVendorTags(oneVendorTag);
1139 EXPECT_EQ(1, oneVendorTagOnly.size());
1140 EXPECT_EQ(oneVendorTag[1], oneVendorTagOnly[0]);
1141
1142 // The vendor extension isn't valid, however it must not be filtered out
1143 // so the converter can detect the issue.
1144 TypeParam oneMaybeVendorTag = {{"", "random string", "VX_GOOGLE_$$"}};
1145 TypeParam oneMaybeVendorTagOnly = HidlUtils::filterOutNonVendorTags(oneMaybeVendorTag);
1146 EXPECT_EQ(1, oneMaybeVendorTagOnly.size());
1147 EXPECT_EQ(oneMaybeVendorTag[2], oneMaybeVendorTagOnly[0]);
1148
1149 TypeParam noVendorTags = {{"", "random string", "V_"}};
1150 EXPECT_EQ(emptyTags, HidlUtils::filterOutNonVendorTags(noVendorTags));
1151 }
1152