1 /*
2  * Copyright (C) 2019 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 
20 #include <android/hardware/rebootescrow/BnRebootEscrow.h>
21 
22 #include <binder/IServiceManager.h>
23 #include <binder/ProcessState.h>
24 
25 using android::sp;
26 using android::String16;
27 using android::hardware::rebootescrow::IRebootEscrow;
28 
29 #define SKIP_UNSUPPORTED \
30     if (rebootescrow == nullptr) GTEST_SKIP() << "Not supported on this device"
31 
32 /**
33  * This tests that the key can be written, read, and removed. It does not test
34  * that the key survives a reboot. That needs a host-based test.
35  *
36  * atest VtsHalRebootEscrowV1_0TargetTest
37  */
38 class RebootEscrowAidlTest : public testing::TestWithParam<std::string> {
39   public:
SetUp()40     virtual void SetUp() override {
41         rebootescrow = android::waitForDeclaredService<IRebootEscrow>(String16(GetParam().c_str()));
42     }
43 
44     sp<IRebootEscrow> rebootescrow;
45 
46     std::vector<uint8_t> KEY_1{
47             0xA5, 0x00, 0xFF, 0x01, 0xA5, 0x5a, 0xAA, 0x55, 0x00, 0xD3, 0x2A,
48             0x8C, 0x2E, 0x83, 0x0E, 0x65, 0x9E, 0x8D, 0xC6, 0xAC, 0x1E, 0x83,
49             0x21, 0xB3, 0x95, 0x02, 0x89, 0x64, 0x64, 0x92, 0x12, 0x1F,
50     };
51     std::vector<uint8_t> KEY_2{
52             0xFF, 0x00, 0x00, 0xAA, 0x5A, 0x19, 0x20, 0x71, 0x9F, 0xFB, 0xDA,
53             0xB6, 0x2D, 0x06, 0xD5, 0x49, 0x7E, 0xEF, 0x63, 0xAC, 0x18, 0xFF,
54             0x5A, 0xA3, 0x40, 0xBB, 0x64, 0xFA, 0x67, 0xC1, 0x10, 0x18,
55     };
56     std::vector<uint8_t> EMPTY_KEY{
57             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59             0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60     };
61 };
62 
63 // This test assumes that it can retrieve keys immediately, but some
64 // implementations use the TEE's EARLY_BOOT_ONLY keys. This means that the
65 // earlyBootEnded() calls will need to be disabled to test this correctly.
TEST_P(RebootEscrowAidlTest,DISABLED_StoreAndRetrieve_Success)66 TEST_P(RebootEscrowAidlTest, DISABLED_StoreAndRetrieve_Success) {
67     SKIP_UNSUPPORTED;
68 
69     ASSERT_TRUE(rebootescrow->storeKey(KEY_1).isOk());
70 
71     std::vector<uint8_t> actualKey;
72     ASSERT_TRUE(rebootescrow->retrieveKey(&actualKey).isOk());
73     EXPECT_EQ(actualKey, KEY_1);
74 }
75 
76 // This test assumes that it can retrieve keys immediately, but some
77 // implementations use the TEE's EARLY_BOOT_ONLY keys. This means that the
78 // earlyBootEnded() calls will need to be disabled to test this correctly.
TEST_P(RebootEscrowAidlTest,DISABLED_StoreAndRetrieve_SecondRetrieveSucceeds)79 TEST_P(RebootEscrowAidlTest, DISABLED_StoreAndRetrieve_SecondRetrieveSucceeds) {
80     SKIP_UNSUPPORTED;
81 
82     ASSERT_TRUE(rebootescrow->storeKey(KEY_1).isOk());
83 
84     std::vector<uint8_t> actualKey;
85     ASSERT_TRUE(rebootescrow->retrieveKey(&actualKey).isOk());
86     EXPECT_EQ(actualKey, KEY_1);
87 
88     ASSERT_TRUE(rebootescrow->retrieveKey(&actualKey).isOk());
89     EXPECT_EQ(actualKey, KEY_1);
90 }
91 
92 // This test assumes that it can retrieve keys immediately, but some
93 // implementations use the TEE's EARLY_BOOT_ONLY keys. This means that the
94 // earlyBootEnded() calls will need to be disabled to test this correctly.
TEST_P(RebootEscrowAidlTest,DISABLED_StoreTwiceOverwrites_Success)95 TEST_P(RebootEscrowAidlTest, DISABLED_StoreTwiceOverwrites_Success) {
96     SKIP_UNSUPPORTED;
97 
98     ASSERT_TRUE(rebootescrow->storeKey(KEY_1).isOk());
99     ASSERT_TRUE(rebootescrow->storeKey(KEY_2).isOk());
100 
101     std::vector<uint8_t> actualKey;
102     ASSERT_TRUE(rebootescrow->retrieveKey(&actualKey).isOk());
103     EXPECT_EQ(actualKey, KEY_2);
104 }
105 
106 // This test assumes that it can retrieve keys immediately, but some
107 // implementations use the TEE's EARLY_BOOT_ONLY keys. This means that the
108 // earlyBootEnded() calls will need to be disabled to test this correctly.
TEST_P(RebootEscrowAidlTest,DISABLED_StoreEmpty_AfterGetEmptyKey_Success)109 TEST_P(RebootEscrowAidlTest, DISABLED_StoreEmpty_AfterGetEmptyKey_Success) {
110     SKIP_UNSUPPORTED;
111 
112     rebootescrow->storeKey(KEY_1);
113     rebootescrow->storeKey(EMPTY_KEY);
114 
115     std::vector<uint8_t> actualKey;
116     ASSERT_TRUE(rebootescrow->retrieveKey(&actualKey).isOk());
117     EXPECT_EQ(actualKey, EMPTY_KEY);
118 }
119 
TEST_P(RebootEscrowAidlTest,Store_Success)120 TEST_P(RebootEscrowAidlTest, Store_Success) {
121     SKIP_UNSUPPORTED;
122 
123     rebootescrow->storeKey(KEY_1);
124 }
125 
126 GTEST_ALLOW_UNINSTANTIATED_PARAMETERIZED_TEST(RebootEscrowAidlTest);
127 INSTANTIATE_TEST_SUITE_P(
128         RebootEscrow, RebootEscrowAidlTest,
129         testing::ValuesIn(android::getAidlHalInstanceNames(IRebootEscrow::descriptor)),
130         android::PrintInstanceNameToString);
131