Tri Vo | a9ebcd2 | 2023-03-06 14:39:18 -0800 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2023 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 <aidl/Gtest.h> |
| 18 | #include <aidl/Vintf.h> |
| 19 | #include <android/hardware/security/keymint/IRemotelyProvisionedComponent.h> |
| 20 | #include <binder/IServiceManager.h> |
| 21 | #include <binder/ProcessState.h> |
| 22 | #include <gtest/gtest.h> |
| 23 | #include <rkp/support/rkpd_client.h> |
| 24 | |
| 25 | using ::android::getAidlHalInstanceNames; |
| 26 | using ::android::sp; |
| 27 | using ::android::String16; |
| 28 | using ::android::hardware::security::keymint::IRemotelyProvisionedComponent; |
| 29 | using ::android::security::rkp::RemotelyProvisionedKey; |
| 30 | using ::android::security::rkp::support::getRpcKey; |
| 31 | |
| 32 | // TODO(b/272600606): Add tests for error cases |
| 33 | class RkpdClientTest : public testing::TestWithParam<std::string> { |
| 34 | public: |
| 35 | virtual void SetUp() override { |
| 36 | auto rpcName = String16(GetParam().c_str()); |
| 37 | rpc_ = android::waitForService<IRemotelyProvisionedComponent>(rpcName); |
| 38 | ASSERT_NE(rpc_, nullptr); |
| 39 | } |
| 40 | |
| 41 | sp<IRemotelyProvisionedComponent> rpc_; |
| 42 | }; |
| 43 | |
| 44 | TEST_P(RkpdClientTest, getRpcKey) { |
| 45 | std::optional<RemotelyProvisionedKey> key = getRpcKey(rpc_, /*keyId=*/0); |
| 46 | |
| 47 | ASSERT_TRUE(key.has_value()) << "Failed to get remotely provisioned attestation key"; |
| 48 | ASSERT_FALSE(key->keyBlob.empty()) << "Key blob is empty"; |
| 49 | ASSERT_FALSE(key->encodedCertChain.empty()) << "Certificate is empty"; |
| 50 | } |
| 51 | |
| 52 | INSTANTIATE_TEST_SUITE_P( |
| 53 | PerInstance, RkpdClientTest, |
| 54 | testing::ValuesIn(getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor)), |
| 55 | ::android::PrintInstanceNameToString); |
| 56 | |
| 57 | int main(int argc, char** argv) { |
| 58 | testing::InitGoogleTest(&argc, argv); |
| 59 | |
| 60 | // We need one thread to issue requests to RKPD and one to handle |
| 61 | // asynchronous responses from RKPD. |
| 62 | android::ProcessState::self()->setThreadPoolMaxThreadCount(2); |
| 63 | android::ProcessState::self()->startThreadPool(); |
| 64 | return RUN_ALL_TESTS(); |
| 65 | } |