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
This commit is contained in:
Mikhail Naganov 2021-02-19 14:08:52 -08:00
parent c3325707a5
commit 178317b252
3 changed files with 20 additions and 12 deletions

View file

@ -310,13 +310,17 @@
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="vendorExtension">
<!-- Vendor extension names must be prefixed by "VX_" to distinguish them from AOSP values.
Vendor are encouraged to namespace their module names to avoid conflicts.
Example for an hypothetical Google virtual reality device:
<devicePort tagName="VR" type="VX_GOOGLE_VR" role="sink">
<!-- Vendor extension names must be prefixed by "VX_" to distinguish them from
AOSP values. Vendors must namespace their names to avoid conflicts. The
namespace part must only use capital latin characters and decimal digits and
consist of at least 3 characters. The part of the extension name after the
namespace may in addition include underscores. Example for a hypothetical
Google virtual reality device:
<devicePort tagName="VR" type="VX_GOOGLE_VR" role="sink" />
-->
<xs:restriction base="xs:string">
<xs:pattern value="VX_[_a-zA-Z0-9]+"/>
<xs:pattern value="VX_[A-Z0-9]{3,}_[_A-Z0-9]+"/>
</xs:restriction>
</xs:simpleType>
<xs:simpleType name="extendableAudioDevice">

View file

@ -18,8 +18,8 @@
#define ANDROID_AUDIO_POLICY_CONFIGURATION_V7_0_ENUMS_H
#include <sys/types.h>
#include <algorithm>
#include <cctype>
#include <regex>
#include <string>
#include <android_audio_policy_configuration_V7_0.h>
@ -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) {

View file

@ -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) {