xref: /aosp_15_r20/system/security/provisioner/support/test.cpp (revision e1997b9af69e3155ead6e072d106a0077849ffba)
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:
SetUp()35     virtual void SetUp() override {
36         auto rpcName = String16(GetParam().c_str());
37         String16 avfName = String16(IRemotelyProvisionedComponent::descriptor) + String16("/avf");
38         if (avfName == rpcName) {
39             GTEST_SKIP() << "Skipping test for avf";
40         }
41         rpc_ = android::waitForService<IRemotelyProvisionedComponent>(rpcName);
42         ASSERT_NE(rpc_, nullptr);
43     }
44 
45     sp<IRemotelyProvisionedComponent> rpc_;
46 };
47 
TEST_P(RkpdClientTest,getRpcKey)48 TEST_P(RkpdClientTest, getRpcKey) {
49     std::optional<RemotelyProvisionedKey> key = getRpcKey(rpc_, /*keyId=*/0);
50 
51     ASSERT_TRUE(key.has_value()) << "Failed to get remotely provisioned attestation key";
52     ASSERT_FALSE(key->keyBlob.empty()) << "Key blob is empty";
53     ASSERT_FALSE(key->encodedCertChain.empty()) << "Certificate is empty";
54 }
55 
56 INSTANTIATE_TEST_SUITE_P(
57     PerInstance, RkpdClientTest,
58     testing::ValuesIn(getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor)),
59     ::android::PrintInstanceNameToString);
60 
main(int argc,char ** argv)61 int main(int argc, char** argv) {
62     testing::InitGoogleTest(&argc, argv);
63 
64     // We need one thread to issue requests to RKPD and one to handle
65     // asynchronous responses from RKPD.
66     android::ProcessState::self()->setThreadPoolMaxThreadCount(2);
67     android::ProcessState::self()->startThreadPool();
68     return RUN_ALL_TESTS();
69 }
70