From f43406a5a7481f2cfac53805f548aa86b2e664f4 Mon Sep 17 00:00:00 2001 From: David Drysdale Date: Wed, 11 Jan 2023 13:27:26 +0000 Subject: [PATCH] Add check_feature helper Test: VtsAidlKeyMintTargetTest (cherry picked from commit 3d2ba0a137debcbf48a4efdd2016fe132293b6bc) (cherry picked from commit b7d44a2eefc18bdb3fab322813613b564240eff5) (cherry picked from https://android-review.googlesource.com/q/commit:352c383e0d9b6bbf98fd54afc63821e08cf6f9ee) Merged-In: I4a5b2a41172c15ae29efb5b177eb86cea5527b4e Change-Id: I4a5b2a41172c15ae29efb5b177eb86cea5527b4e --- .../keymint/aidl/vts/functional/Android.bp | 1 + .../vts/functional/KeyMintAidlTestBase.cpp | 24 +++++++++++++++++++ .../aidl/vts/functional/KeyMintAidlTestBase.h | 1 + 3 files changed, 26 insertions(+) diff --git a/security/keymint/aidl/vts/functional/Android.bp b/security/keymint/aidl/vts/functional/Android.bp index 77eea8afd6..2de19d5f32 100644 --- a/security/keymint/aidl/vts/functional/Android.bp +++ b/security/keymint/aidl/vts/functional/Android.bp @@ -30,6 +30,7 @@ cc_defaults { "VtsHalTargetTestDefaults", ], shared_libs: [ + "libbinder", "libbinder_ndk", "libcrypto", ], diff --git a/security/keymint/aidl/vts/functional/KeyMintAidlTestBase.cpp b/security/keymint/aidl/vts/functional/KeyMintAidlTestBase.cpp index 20324117b9..c81d9a02d3 100644 --- a/security/keymint/aidl/vts/functional/KeyMintAidlTestBase.cpp +++ b/security/keymint/aidl/vts/functional/KeyMintAidlTestBase.cpp @@ -22,6 +22,7 @@ #include #include +#include #include #include #include @@ -1679,6 +1680,29 @@ void p256_pub_key(const vector& coseKeyData, EVP_PKEY_Ptr* signingKey) *signingKey = std::move(pubKey); } +// Check whether the given named feature is available. +bool check_feature(const std::string& name) { + ::android::sp<::android::IServiceManager> sm(::android::defaultServiceManager()); + ::android::sp<::android::IBinder> binder(sm->getService(::android::String16("package_native"))); + if (binder == nullptr) { + GTEST_LOG_(ERROR) << "getService package_native failed"; + return false; + } + ::android::sp<::android::content::pm::IPackageManagerNative> packageMgr = + ::android::interface_cast<::android::content::pm::IPackageManagerNative>(binder); + if (packageMgr == nullptr) { + GTEST_LOG_(ERROR) << "Cannot find package manager"; + return false; + } + bool hasFeature = false; + auto status = packageMgr->hasSystemFeature(::android::String16(name.c_str()), 0, &hasFeature); + if (!status.isOk()) { + GTEST_LOG_(ERROR) << "hasSystemFeature('" << name << "') failed: " << status; + return false; + } + return hasFeature; +} + } // namespace test } // namespace aidl::android::hardware::security::keymint diff --git a/security/keymint/aidl/vts/functional/KeyMintAidlTestBase.h b/security/keymint/aidl/vts/functional/KeyMintAidlTestBase.h index ec3fcf6a3e..890bb56ff9 100644 --- a/security/keymint/aidl/vts/functional/KeyMintAidlTestBase.h +++ b/security/keymint/aidl/vts/functional/KeyMintAidlTestBase.h @@ -346,6 +346,7 @@ vector make_name_from_str(const string& name); void check_maced_pubkey(const MacedPublicKey& macedPubKey, bool testMode, vector* payload_value); void p256_pub_key(const vector& coseKeyData, EVP_PKEY_Ptr* signingKey); +bool check_feature(const std::string& name); AuthorizationSet HwEnforcedAuthorizations(const vector& key_characteristics); AuthorizationSet SwEnforcedAuthorizations(const vector& key_characteristics);