Convert VtsHalKeymasterV*_0TargetTest to be parameterized test

Bug: 142397658
Test: atest VtsHalKeymasterV3_0TargetTest \
  VtsHalKeymasterV4_0TargetTest

Change-Id: I0fbda4d9ab810a4ef616f8741919c522d5d58cc0
This commit is contained in:
Dan Shi 2019-12-10 15:41:18 -08:00
parent 56cda83f8c
commit 3bacd7f634
10 changed files with 544 additions and 463 deletions

View file

@ -29,5 +29,5 @@ cc_test {
"libcrypto_static",
"libsoftkeymasterdevice",
],
test_suites: ["general-tests"],
test_suites: ["general-tests", "vts-core"],
}

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2019 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<configuration description="Runs VtsHalKeymasterV3_0TargetTest.">
<option name="test-suite-tag" value="apct" />
<option name="test-suite-tag" value="apct-native" />
<target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer">
</target_preparer>
<target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
<option name="cleanup" value="true" />
<option name="push" value="VtsHalKeymasterV3_0TargetTest->/data/local/tmp/VtsHalKeymasterV3_0TargetTest" />
</target_preparer>
<test class="com.android.tradefed.testtype.GTest" >
<option name="native-test-device-path" value="/data/local/tmp" />
<option name="module-name" value="VtsHalKeymasterV3_0TargetTest" />
<option name="native-test-timeout" value="900000"/>
</test>
</configuration>

File diff suppressed because it is too large Load diff

View file

@ -29,5 +29,5 @@ cc_test {
"libkeymaster4support",
"libsoftkeymasterdevice",
],
test_suites: ["general-tests"],
test_suites: ["general-tests", "vts-core"],
}

View file

@ -0,0 +1,33 @@
<?xml version="1.0" encoding="utf-8"?>
<!-- Copyright (C) 2019 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<configuration description="Runs VtsHalKeymasterV4_0TargetTest.">
<option name="test-suite-tag" value="apct" />
<option name="test-suite-tag" value="apct-native" />
<target_preparer class="com.android.tradefed.targetprep.RootTargetPreparer">
</target_preparer>
<target_preparer class="com.android.tradefed.targetprep.PushFilePreparer">
<option name="cleanup" value="true" />
<option name="push" value="VtsHalKeymasterV4_0TargetTest->/data/local/tmp/VtsHalKeymasterV4_0TargetTest" />
</target_preparer>
<test class="com.android.tradefed.testtype.GTest" >
<option name="native-test-device-path" value="/data/local/tmp" />
<option name="module-name" value="VtsHalKeymasterV4_0TargetTest" />
<option name="native-test-timeout" value="900000"/>
</test>
</configuration>

View file

@ -101,7 +101,7 @@ class HmacKeySharingTest : public KeymasterHidlTest {
}
};
TEST_F(HmacKeySharingTest, GetParameters) {
TEST_P(HmacKeySharingTest, GetParameters) {
auto result1 = getHmacSharingParameters(keymaster());
EXPECT_EQ(ErrorCode::OK, result1.error);
@ -114,7 +114,7 @@ TEST_F(HmacKeySharingTest, GetParameters) {
<< "A given keymaster should always return the same nonce until restart.";
}
TEST_F(HmacKeySharingTest, ComputeSharedHmac) {
TEST_P(HmacKeySharingTest, ComputeSharedHmac) {
auto params = getHmacSharingParameters(all_keymasters());
ASSERT_EQ(all_keymasters().size(), params.size())
<< "One or more keymasters failed to provide parameters.";
@ -143,8 +143,8 @@ TEST_F(HmacKeySharingTest, ComputeSharedHmac) {
template <class F>
class final_action {
public:
explicit final_action(F f) : f_(move(f)) {}
~final_action() { f_(); }
explicit final_action(F f) : f_(std::move(f)) {}
~final_action() { f_(); }
private:
F f_;
@ -155,7 +155,7 @@ inline final_action<F> finally(const F& f) {
return final_action<F>(f);
}
TEST_F(HmacKeySharingTest, ComputeSharedHmacCorruptNonce) {
TEST_P(HmacKeySharingTest, ComputeSharedHmacCorruptNonce) {
// Important: The execution of this test gets the keymaster implementations on the device out of
// sync with respect to the HMAC key. Granted that VTS tests aren't run on in-use production
// devices, this still has the potential to cause confusion. To mitigate that, we always
@ -194,7 +194,7 @@ TEST_F(HmacKeySharingTest, ComputeSharedHmacCorruptNonce) {
}
}
TEST_F(HmacKeySharingTest, ComputeSharedHmacCorruptSeed) {
TEST_P(HmacKeySharingTest, ComputeSharedHmacCorruptSeed) {
// Important: The execution of this test gets the keymaster implementations on the device out of
// sync with respect to the HMAC key. Granted that VTS tests aren't run on in-use production
// devices, this still has the potential to cause confusion. To mitigate that, we always
@ -236,6 +236,11 @@ TEST_F(HmacKeySharingTest, ComputeSharedHmacCorruptSeed) {
}
}
INSTANTIATE_TEST_SUITE_P(
PerInstance, HmacKeySharingTest,
testing::ValuesIn(android::hardware::getAllHalInstanceNames(IKeymasterDevice::descriptor)),
android::hardware::PrintInstanceNameToString);
} // namespace test
} // namespace V4_0
} // namespace keymaster

View file

@ -41,18 +41,9 @@ namespace V4_0 {
namespace test {
sp<IKeymasterDevice> KeymasterHidlTest::keymaster_;
std::vector<sp<IKeymasterDevice>> KeymasterHidlTest::all_keymasters_;
uint32_t KeymasterHidlTest::os_version_;
uint32_t KeymasterHidlTest::os_patch_level_;
SecurityLevel KeymasterHidlTest::securityLevel_;
hidl_string KeymasterHidlTest::name_;
hidl_string KeymasterHidlTest::author_;
string KeymasterHidlTest::service_name_;
void KeymasterHidlTest::InitializeKeymaster() {
service_name_ = KeymasterHidlEnvironment::Instance()->getServiceName<IKeymasterDevice>();
keymaster_ = ::testing::VtsHalHidlTargetTestBase::getService<IKeymasterDevice>(service_name_);
service_name_ = GetParam();
keymaster_ = IKeymasterDevice::getService(service_name_);
ASSERT_NE(keymaster_, nullptr);
ASSERT_TRUE(keymaster_
@ -65,8 +56,7 @@ void KeymasterHidlTest::InitializeKeymaster() {
.isOk());
}
void KeymasterHidlTest::SetUpTestCase() {
void KeymasterHidlTest::SetUp() {
InitializeKeymaster();
os_version_ = ::keymaster::GetOsVersion();
@ -79,8 +69,7 @@ void KeymasterHidlTest::SetUpTestCase() {
IKeymasterDevice::descriptor, [&](const hidl_vec<hidl_string>& names) {
for (auto& name : names) {
if (name == service_name_) continue;
auto keymaster =
::testing::VtsHalHidlTargetTestBase::getService<IKeymasterDevice>(name);
auto keymaster = IKeymasterDevice::getService(name);
ASSERT_NE(keymaster, nullptr);
all_keymasters_.push_back(keymaster);
}
@ -222,8 +211,8 @@ void KeymasterHidlTest::CheckCreationDateTime(
std::chrono::time_point<std::chrono::system_clock> reported_time{
std::chrono::milliseconds(sw_enforced[i].f.dateTime)};
// The test is flaky for EC keys, so a buffer time of 120 seconds will be added.
EXPECT_LE(creation - 120s, reported_time);
EXPECT_LE(reported_time, now + 1s);
EXPECT_LE(creation - std::chrono::seconds(120), reported_time);
EXPECT_LE(reported_time, now + std::chrono::seconds(1));
}
}
}

View file

@ -14,14 +14,13 @@
* limitations under the License.
*/
#ifndef HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_
#define HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_
#pragma once
#include <android/hardware/keymaster/4.0/IKeymasterDevice.h>
#include <android/hardware/keymaster/4.0/types.h>
#include <VtsHalHidlTargetTestBase.h>
#include <gtest/gtest.h>
#include <hidl/GtestPrinter.h>
#include <hidl/ServiceManagement.h>
#include <keymaster/keymaster_configuration.h>
#include <keymasterV4_0/authorization_set.h>
@ -69,43 +68,24 @@ class HidlBuf : public hidl_vec<uint8_t> {
constexpr uint64_t kOpHandleSentinel = 0xFFFFFFFFFFFFFFFF;
class KeymasterHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
public:
// get the test environment singleton
static KeymasterHidlEnvironment* Instance() {
static KeymasterHidlEnvironment* instance = new KeymasterHidlEnvironment;
return instance;
}
void registerTestServices() override { registerTestService<IKeymasterDevice>(); }
private:
KeymasterHidlEnvironment(){};
GTEST_DISALLOW_COPY_AND_ASSIGN_(KeymasterHidlEnvironment);
};
class KeymasterHidlTest : public ::testing::VtsHalHidlTargetTestBase {
public:
class KeymasterHidlTest : public ::testing::TestWithParam<std::string> {
public:
void SetUp();
void TearDown() override {
if (key_blob_.size()) {
CheckedDeleteKey();
}
AbortIfNeeded();
}
// SetUpTestCase runs only once per test case, not once per test.
static void SetUpTestCase();
static void InitializeKeymaster();
static void TearDownTestCase() {
keymaster_.clear();
all_keymasters_.clear();
}
static IKeymasterDevice& keymaster() { return *keymaster_; }
static const std::vector<sp<IKeymasterDevice>>& all_keymasters() { return all_keymasters_; }
static uint32_t os_version() { return os_version_; }
static uint32_t os_patch_level() { return os_patch_level_; }
void InitializeKeymaster();
IKeymasterDevice& keymaster() { return *keymaster_; }
const std::vector<sp<IKeymasterDevice>>& all_keymasters() { return all_keymasters_; }
uint32_t os_version() { return os_version_; }
uint32_t os_patch_level() { return os_patch_level_; }
ErrorCode GenerateKey(const AuthorizationSet& key_desc, HidlBuf* key_blob,
KeyCharacteristics* key_characteristics);
@ -216,8 +196,8 @@ class KeymasterHidlTest : public ::testing::VtsHalHidlTargetTestBase {
std::pair<ErrorCode, HidlBuf> UpgradeKey(const HidlBuf& key_blob);
static bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
static SecurityLevel SecLevel() { return securityLevel_; }
bool IsSecure() { return securityLevel_ != SecurityLevel::SOFTWARE; }
SecurityLevel SecLevel() { return securityLevel_; }
std::vector<uint32_t> ValidKeySizes(Algorithm algorithm);
std::vector<uint32_t> InvalidKeySizes(Algorithm algorithm);
@ -233,15 +213,15 @@ class KeymasterHidlTest : public ::testing::VtsHalHidlTargetTestBase {
OperationHandle op_handle_ = kOpHandleSentinel;
private:
static sp<IKeymasterDevice> keymaster_;
static std::vector<sp<IKeymasterDevice>> all_keymasters_;
static uint32_t os_version_;
static uint32_t os_patch_level_;
sp<IKeymasterDevice> keymaster_;
std::vector<sp<IKeymasterDevice>> all_keymasters_;
uint32_t os_version_;
uint32_t os_patch_level_;
static SecurityLevel securityLevel_;
static hidl_string name_;
static hidl_string author_;
static string service_name_;
SecurityLevel securityLevel_;
hidl_string name_;
hidl_string author_;
string service_name_;
};
} // namespace test
@ -249,5 +229,3 @@ class KeymasterHidlTest : public ::testing::VtsHalHidlTargetTestBase {
} // namespace keymaster
} // namespace hardware
} // namespace android
#endif // HARDWARE_INTERFACES_KEYMASTER_40_VTS_FUNCTIONAL_KEYMASTER_HIDL_TEST_H_

View file

@ -75,7 +75,7 @@ class VerificationTokenTest : public KeymasterHidlTest {
* thing we really can test is that tokens can be created by TEE keymasters, and that the
* timestamps increase as expected.
*/
TEST_F(VerificationTokenTest, TestCreation) {
TEST_P(VerificationTokenTest, TestCreation) {
auto result1 = verifyAuthorization(
1 /* operation handle */, AuthorizationSet() /* paramtersToVerify */, HardwareAuthToken());
ASSERT_TRUE(result1.callSuccessful);
@ -134,7 +134,7 @@ TEST_F(VerificationTokenTest, TestCreation) {
* stamp is included in the mac but on failure we know that it is not. Other than in the test
* case above we call verifyAuthorization with the exact same set of parameters.
*/
TEST_F(VerificationTokenTest, MacChangesOnChangingTimestamp) {
TEST_P(VerificationTokenTest, MacChangesOnChangingTimestamp) {
auto result1 =
verifyAuthorization(0 /* operation handle */,
AuthorizationSet() /* paramtersToVerify */, HardwareAuthToken());
@ -185,6 +185,11 @@ TEST_F(VerificationTokenTest, MacChangesOnChangingTimestamp) {
memcmp(result1.token.mac.data(), result2.token.mac.data(), result1.token.mac.size()));
}
INSTANTIATE_TEST_SUITE_P(
PerInstance, VerificationTokenTest,
testing::ValuesIn(android::hardware::getAllHalInstanceNames(IKeymasterDevice::descriptor)),
android::hardware::PrintInstanceNameToString);
} // namespace test
} // namespace V4_0
} // namespace keymaster

File diff suppressed because it is too large Load diff