From 178317b2525ce150215a89e21b7d88013ee35a1d Mon Sep 17 00:00:00 2001 From: Mikhail Naganov Date: Fri, 19 Feb 2021 14:08:52 -0800 Subject: [PATCH] audio: Put stronger rules on vendor extension enums The namespace part has been made mandatory. The namespace must be at least 3 characters. Bug: 176144684 Test: atest android.hardware.audio.common@7.0-util_tests Test: atest android.hardware.audio@7.0-util_tests Test: atest android.hardware.audio.effect@7.0-util_tests Change-Id: If8578dfab80b51d9c30042e99bfbf70f40598afa --- audio/7.0/config/audio_policy_configuration.xsd | 14 +++++++++----- ...android_audio_policy_configuration_V7_0-enums.h | 12 +++++------- .../all-versions/default/tests/hidlutils_tests.cpp | 6 ++++++ 3 files changed, 20 insertions(+), 12 deletions(-) diff --git a/audio/7.0/config/audio_policy_configuration.xsd b/audio/7.0/config/audio_policy_configuration.xsd index 531572b777..ccaaf98288 100644 --- a/audio/7.0/config/audio_policy_configuration.xsd +++ b/audio/7.0/config/audio_policy_configuration.xsd @@ -310,13 +310,17 @@ - - + diff --git a/audio/common/7.0/enums/include/android_audio_policy_configuration_V7_0-enums.h b/audio/common/7.0/enums/include/android_audio_policy_configuration_V7_0-enums.h index b427f3a0d4..ada0d3eb9b 100644 --- a/audio/common/7.0/enums/include/android_audio_policy_configuration_V7_0-enums.h +++ b/audio/common/7.0/enums/include/android_audio_policy_configuration_V7_0-enums.h @@ -18,8 +18,8 @@ #define ANDROID_AUDIO_POLICY_CONFIGURATION_V7_0_ENUMS_H #include -#include -#include +#include +#include #include @@ -219,11 +219,9 @@ static inline bool maybeVendorExtension(const std::string& s) { } static inline bool isVendorExtension(const std::string& s) { - // Must match the "vendorExtension" rule from the XSD file. - static const std::string vendorPrefix = "VX_"; - return maybeVendorExtension(s) && - std::all_of(s.begin() + vendorPrefix.size(), s.end(), - [](unsigned char c) { return c == '_' || std::isalnum(c); }); + // Must be the same as the "vendorExtension" rule from the XSD file. + static const std::regex vendorExtension("VX_[A-Z0-9]{3,}_[_A-Z0-9]+"); + return std::regex_match(s.begin(), s.end(), vendorExtension); } static inline bool isUnknownAudioChannelMask(const std::string& mask) { diff --git a/audio/common/all-versions/default/tests/hidlutils_tests.cpp b/audio/common/all-versions/default/tests/hidlutils_tests.cpp index ec6bdf38ef..e154453e2b 100644 --- a/audio/common/all-versions/default/tests/hidlutils_tests.cpp +++ b/audio/common/all-versions/default/tests/hidlutils_tests.cpp @@ -432,10 +432,16 @@ TEST(HidlUtils, ConvertDeviceType) { // The enums module is too small to have unit tests on its own. TEST(HidlUtils, VendorExtension) { EXPECT_TRUE(xsd::isVendorExtension("VX_GOOGLE_VR_42")); + EXPECT_TRUE(xsd::isVendorExtension("VX_QCM_SPK")); EXPECT_FALSE(xsd::isVendorExtension("")); EXPECT_FALSE(xsd::isVendorExtension("random string")); EXPECT_FALSE(xsd::isVendorExtension("VX_")); + EXPECT_FALSE(xsd::isVendorExtension("VX_X")); + EXPECT_FALSE(xsd::isVendorExtension("VX_X_")); + EXPECT_FALSE(xsd::isVendorExtension("VX_X_X")); + EXPECT_FALSE(xsd::isVendorExtension("VX_XX_X")); EXPECT_FALSE(xsd::isVendorExtension("VX_GOOGLE_$$")); + EXPECT_FALSE(xsd::isVendorExtension("VX_$CM_SPK")); } TEST(HidlUtils, ConvertInvalidDeviceAddress) {