• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2017 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 #include <android/hardware/oemlock/1.0/IOemLock.h>
17 
18 #include <VtsHalHidlTargetTestBase.h>
19 #include <VtsHalHidlTargetTestEnvBase.h>
20 
21 using ::android::hardware::oemlock::V1_0::IOemLock;
22 using ::android::hardware::oemlock::V1_0::OemLockStatus;
23 using ::android::hardware::oemlock::V1_0::OemLockSecureStatus;
24 using ::android::hardware::hidl_string;
25 using ::android::hardware::hidl_vec;
26 using ::android::sp;
27 
28 // Test environment for OemLock HIDL HAL.
29 class OemLockHidlEnvironment : public ::testing::VtsHalHidlTargetTestEnvBase {
30    public:
31     // get the test environment singleton
Instance()32     static OemLockHidlEnvironment* Instance() {
33         static OemLockHidlEnvironment* instance = new OemLockHidlEnvironment;
34         return instance;
35     }
36 
registerTestServices()37     virtual void registerTestServices() override { registerTestService<IOemLock>(); }
38 };
39 
40 struct OemLockHidlTest : public ::testing::VtsHalHidlTargetTestBase {
SetUpOemLockHidlTest41     virtual void SetUp() override {
42         oemlock = ::testing::VtsHalHidlTargetTestBase::getService<IOemLock>(
43             OemLockHidlEnvironment::Instance()->getServiceName<IOemLock>());
44         ASSERT_NE(oemlock, nullptr);
45     }
46 
TearDownOemLockHidlTest47     virtual void TearDown() override {}
48 
49     sp<IOemLock> oemlock;
50 };
51 
52 /*
53  * Check the name can be retrieved
54  */
TEST_F(OemLockHidlTest,GetName)55 TEST_F(OemLockHidlTest, GetName) {
56     std::string name;
57     OemLockStatus status;
58 
59     bool callbackCalled = false;
60     const auto ret = oemlock->getName([&](OemLockStatus s, hidl_string n) {
61         callbackCalled = true;
62         status = s;
63         name = n.c_str();
64     });
65 
66     ASSERT_TRUE(ret.isOk());
67     ASSERT_TRUE(callbackCalled);
68     EXPECT_EQ(status, OemLockStatus::OK);
69     // Any value acceptable
70 };
71 
72 /*
73  * Check the unlock allowed by device state can be queried
74  */
TEST_F(OemLockHidlTest,QueryUnlockAllowedByDevice)75 TEST_F(OemLockHidlTest, QueryUnlockAllowedByDevice) {
76     bool allowed;
77     OemLockStatus status;
78 
79     bool callbackCalled = false;
80     const auto ret = oemlock->isOemUnlockAllowedByDevice([&](OemLockStatus s, bool a) {
81         callbackCalled = true;
82         status = s;
83         allowed = a;
84     });
85 
86     ASSERT_TRUE(ret.isOk());
87     ASSERT_TRUE(callbackCalled);
88     EXPECT_EQ(status, OemLockStatus::OK);
89     // Any value acceptable
90 }
91 
92 /*
93  * Check unlock allowed by device state can be toggled
94  */
TEST_F(OemLockHidlTest,AllowedByDeviceCanBeToggled)95 TEST_F(OemLockHidlTest, AllowedByDeviceCanBeToggled) {
96     bool allowed;
97     OemLockStatus status;
98 
99     auto getAllowedCallback = [&](OemLockStatus s, bool a) {
100         status = s;
101         allowed = a;
102     };
103 
104     // Get the original state so it can be restored
105     const auto get_ret = oemlock->isOemUnlockAllowedByDevice(getAllowedCallback);
106     ASSERT_TRUE(get_ret.isOk());
107     ASSERT_EQ(status, OemLockStatus::OK);
108     const bool originallyAllowed = allowed;
109 
110     // Toggle the state
111     const auto set_ret = oemlock->setOemUnlockAllowedByDevice(!originallyAllowed);
112     ASSERT_TRUE(set_ret.isOk());
113     ASSERT_EQ(set_ret, OemLockStatus::OK);
114     const auto check_set_ret = oemlock->isOemUnlockAllowedByDevice(getAllowedCallback);
115     ASSERT_TRUE(check_set_ret.isOk());
116     ASSERT_EQ(status, OemLockStatus::OK);
117     ASSERT_EQ(allowed, !originallyAllowed);
118 
119     // Restore the state
120     const auto restore_ret = oemlock->setOemUnlockAllowedByDevice(originallyAllowed);
121     ASSERT_TRUE(restore_ret.isOk());
122     ASSERT_EQ(restore_ret, OemLockStatus::OK);
123     const auto check_restore_ret = oemlock->isOemUnlockAllowedByDevice(getAllowedCallback);
124     ASSERT_TRUE(check_restore_ret.isOk());
125     ASSERT_EQ(status, OemLockStatus::OK);
126     ASSERT_EQ(allowed, originallyAllowed);
127 };
128 
129 /*
130  * Check the unlock allowed by device state can be queried
131  */
TEST_F(OemLockHidlTest,QueryUnlockAllowedByCarrier)132 TEST_F(OemLockHidlTest, QueryUnlockAllowedByCarrier) {
133     bool allowed;
134     OemLockStatus status;
135 
136     bool callbackCalled = false;
137     const auto ret = oemlock->isOemUnlockAllowedByCarrier([&](OemLockStatus s, bool a) {
138         callbackCalled = true;
139         status = s;
140         allowed = a;
141     });
142 
143     ASSERT_TRUE(ret.isOk());
144     ASSERT_TRUE(callbackCalled);
145     EXPECT_EQ(status, OemLockStatus::OK);
146     // Any value acceptable
147 }
148 
149 /*
150  * Attempt to check unlock allowed by carrier can be toggled
151  *
152  * The implementation may involve a signature which cannot be tested here. That
153  * is a valid implementation so the test will pass. If there is no signature
154  * required, the test will toggle the value.
155  */
TEST_F(OemLockHidlTest,CarrierUnlock)156 TEST_F(OemLockHidlTest, CarrierUnlock) {
157     const hidl_vec<uint8_t> noSignature = {};
158     bool allowed;
159     OemLockStatus status;
160 
161     auto getAllowedCallback = [&](OemLockStatus s, bool a) {
162         status = s;
163         allowed = a;
164     };
165 
166     // Get the original state so it can be restored
167     const auto get_ret = oemlock->isOemUnlockAllowedByCarrier(getAllowedCallback);
168     ASSERT_TRUE(get_ret.isOk());
169     ASSERT_EQ(status, OemLockStatus::OK);
170     const bool originallyAllowed = allowed;
171 
172     if (originallyAllowed) {
173         // Only applied to locked devices
174         return;
175     }
176 
177     // Toggle the state
178     const auto set_ret = oemlock->setOemUnlockAllowedByCarrier(!originallyAllowed, noSignature);
179     ASSERT_TRUE(set_ret.isOk());
180     ASSERT_NE(set_ret, OemLockSecureStatus::FAILED);
181     const auto check_set_ret = oemlock->isOemUnlockAllowedByCarrier(getAllowedCallback);
182     ASSERT_TRUE(check_set_ret.isOk());
183     ASSERT_EQ(status, OemLockStatus::OK);
184 
185     if (set_ret == OemLockSecureStatus::INVALID_SIGNATURE) {
186         // Signature is required so we cannot toggle the value in the test, but this is allowed
187         ASSERT_EQ(allowed, originallyAllowed);
188         return;
189     }
190 
191     ASSERT_EQ(set_ret, OemLockSecureStatus::OK);
192     ASSERT_EQ(allowed, !originallyAllowed);
193 
194     // Restore the state
195     const auto restore_ret = oemlock->setOemUnlockAllowedByCarrier(originallyAllowed, noSignature);
196     ASSERT_TRUE(restore_ret.isOk());
197     ASSERT_EQ(restore_ret, OemLockSecureStatus::OK);
198     const auto check_restore_ret = oemlock->isOemUnlockAllowedByCarrier(getAllowedCallback);
199     ASSERT_TRUE(check_restore_ret.isOk());
200     ASSERT_EQ(status, OemLockStatus::OK);
201     ASSERT_EQ(allowed, originallyAllowed);
202 };
203 
main(int argc,char ** argv)204 int main(int argc, char** argv) {
205     ::testing::AddGlobalTestEnvironment(OemLockHidlEnvironment::Instance());
206     ::testing::InitGoogleTest(&argc, argv);
207     OemLockHidlEnvironment::Instance()->init(&argc, argv);
208     int status = RUN_ALL_TESTS();
209     ALOGI("Test result = %d", status);
210     return status;
211 }
212