xref: /aosp_15_r20/system/security/provisioner/support/test.cpp (revision e1997b9af69e3155ead6e072d106a0077849ffba)
1*e1997b9aSAndroid Build Coastguard Worker /*
2*e1997b9aSAndroid Build Coastguard Worker  * Copyright (C) 2023 The Android Open Source Project
3*e1997b9aSAndroid Build Coastguard Worker  *
4*e1997b9aSAndroid Build Coastguard Worker  * Licensed under the Apache License, Version 2.0 (the "License");
5*e1997b9aSAndroid Build Coastguard Worker  * you may not use this file except in compliance with the License.
6*e1997b9aSAndroid Build Coastguard Worker  * You may obtain a copy of the License at
7*e1997b9aSAndroid Build Coastguard Worker  *
8*e1997b9aSAndroid Build Coastguard Worker  *      http://www.apache.org/licenses/LICENSE-2.0
9*e1997b9aSAndroid Build Coastguard Worker  *
10*e1997b9aSAndroid Build Coastguard Worker  * Unless required by applicable law or agreed to in writing, software
11*e1997b9aSAndroid Build Coastguard Worker  * distributed under the License is distributed on an "AS IS" BASIS,
12*e1997b9aSAndroid Build Coastguard Worker  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13*e1997b9aSAndroid Build Coastguard Worker  * See the License for the specific language governing permissions and
14*e1997b9aSAndroid Build Coastguard Worker  * limitations under the License.
15*e1997b9aSAndroid Build Coastguard Worker  */
16*e1997b9aSAndroid Build Coastguard Worker 
17*e1997b9aSAndroid Build Coastguard Worker #include <aidl/Gtest.h>
18*e1997b9aSAndroid Build Coastguard Worker #include <aidl/Vintf.h>
19*e1997b9aSAndroid Build Coastguard Worker #include <android/hardware/security/keymint/IRemotelyProvisionedComponent.h>
20*e1997b9aSAndroid Build Coastguard Worker #include <binder/IServiceManager.h>
21*e1997b9aSAndroid Build Coastguard Worker #include <binder/ProcessState.h>
22*e1997b9aSAndroid Build Coastguard Worker #include <gtest/gtest.h>
23*e1997b9aSAndroid Build Coastguard Worker #include <rkp/support/rkpd_client.h>
24*e1997b9aSAndroid Build Coastguard Worker 
25*e1997b9aSAndroid Build Coastguard Worker using ::android::getAidlHalInstanceNames;
26*e1997b9aSAndroid Build Coastguard Worker using ::android::sp;
27*e1997b9aSAndroid Build Coastguard Worker using ::android::String16;
28*e1997b9aSAndroid Build Coastguard Worker using ::android::hardware::security::keymint::IRemotelyProvisionedComponent;
29*e1997b9aSAndroid Build Coastguard Worker using ::android::security::rkp::RemotelyProvisionedKey;
30*e1997b9aSAndroid Build Coastguard Worker using ::android::security::rkp::support::getRpcKey;
31*e1997b9aSAndroid Build Coastguard Worker 
32*e1997b9aSAndroid Build Coastguard Worker // TODO(b/272600606): Add tests for error cases
33*e1997b9aSAndroid Build Coastguard Worker class RkpdClientTest : public testing::TestWithParam<std::string> {
34*e1997b9aSAndroid Build Coastguard Worker   public:
SetUp()35*e1997b9aSAndroid Build Coastguard Worker     virtual void SetUp() override {
36*e1997b9aSAndroid Build Coastguard Worker         auto rpcName = String16(GetParam().c_str());
37*e1997b9aSAndroid Build Coastguard Worker         String16 avfName = String16(IRemotelyProvisionedComponent::descriptor) + String16("/avf");
38*e1997b9aSAndroid Build Coastguard Worker         if (avfName == rpcName) {
39*e1997b9aSAndroid Build Coastguard Worker             GTEST_SKIP() << "Skipping test for avf";
40*e1997b9aSAndroid Build Coastguard Worker         }
41*e1997b9aSAndroid Build Coastguard Worker         rpc_ = android::waitForService<IRemotelyProvisionedComponent>(rpcName);
42*e1997b9aSAndroid Build Coastguard Worker         ASSERT_NE(rpc_, nullptr);
43*e1997b9aSAndroid Build Coastguard Worker     }
44*e1997b9aSAndroid Build Coastguard Worker 
45*e1997b9aSAndroid Build Coastguard Worker     sp<IRemotelyProvisionedComponent> rpc_;
46*e1997b9aSAndroid Build Coastguard Worker };
47*e1997b9aSAndroid Build Coastguard Worker 
TEST_P(RkpdClientTest,getRpcKey)48*e1997b9aSAndroid Build Coastguard Worker TEST_P(RkpdClientTest, getRpcKey) {
49*e1997b9aSAndroid Build Coastguard Worker     std::optional<RemotelyProvisionedKey> key = getRpcKey(rpc_, /*keyId=*/0);
50*e1997b9aSAndroid Build Coastguard Worker 
51*e1997b9aSAndroid Build Coastguard Worker     ASSERT_TRUE(key.has_value()) << "Failed to get remotely provisioned attestation key";
52*e1997b9aSAndroid Build Coastguard Worker     ASSERT_FALSE(key->keyBlob.empty()) << "Key blob is empty";
53*e1997b9aSAndroid Build Coastguard Worker     ASSERT_FALSE(key->encodedCertChain.empty()) << "Certificate is empty";
54*e1997b9aSAndroid Build Coastguard Worker }
55*e1997b9aSAndroid Build Coastguard Worker 
56*e1997b9aSAndroid Build Coastguard Worker INSTANTIATE_TEST_SUITE_P(
57*e1997b9aSAndroid Build Coastguard Worker     PerInstance, RkpdClientTest,
58*e1997b9aSAndroid Build Coastguard Worker     testing::ValuesIn(getAidlHalInstanceNames(IRemotelyProvisionedComponent::descriptor)),
59*e1997b9aSAndroid Build Coastguard Worker     ::android::PrintInstanceNameToString);
60*e1997b9aSAndroid Build Coastguard Worker 
main(int argc,char ** argv)61*e1997b9aSAndroid Build Coastguard Worker int main(int argc, char** argv) {
62*e1997b9aSAndroid Build Coastguard Worker     testing::InitGoogleTest(&argc, argv);
63*e1997b9aSAndroid Build Coastguard Worker 
64*e1997b9aSAndroid Build Coastguard Worker     // We need one thread to issue requests to RKPD and one to handle
65*e1997b9aSAndroid Build Coastguard Worker     // asynchronous responses from RKPD.
66*e1997b9aSAndroid Build Coastguard Worker     android::ProcessState::self()->setThreadPoolMaxThreadCount(2);
67*e1997b9aSAndroid Build Coastguard Worker     android::ProcessState::self()->startThreadPool();
68*e1997b9aSAndroid Build Coastguard Worker     return RUN_ALL_TESTS();
69*e1997b9aSAndroid Build Coastguard Worker }
70