1 /*
2 **
3 ** Copyright 2019, The Android Open Source Project
4 **
5 ** Licensed under the Apache License, Version 2.0 (the "License");
6 ** you may not use this file except in compliance with the License.
7 ** You may obtain a copy of the License at
8 **
9 ** http://www.apache.org/licenses/LICENSE-2.0
10 **
11 ** Unless required by applicable law or agreed to in writing, software
12 ** distributed under the License is distributed on an "AS IS" BASIS,
13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 ** See the License for the specific language governing permissions and
15 ** limitations under the License.
16 */
17
18 #include <android/hardware/confirmationui/1.0/types.h>
19 #include <android/security/BnConfirmationPromptCallback.h>
20 #include <android/security/keystore/IKeystoreService.h>
21 #include <binder/IPCThreadState.h>
22 #include <binder/IServiceManager.h>
23
24 #include <gtest/gtest.h>
25
26 #include <chrono>
27 #include <future>
28 #include <tuple>
29 #include <vector>
30
31 using ConfirmationResponseCode = android::hardware::confirmationui::V1_0::ResponseCode;
32 using android::IBinder;
33 using android::IServiceManager;
34 using android::sp;
35 using android::String16;
36 using android::security::keystore::IKeystoreService;
37
38 using namespace std::literals::chrono_literals;
39
40 class ConfirmationListener
41 : public android::security::BnConfirmationPromptCallback,
42 public std::promise<std::tuple<ConfirmationResponseCode, std::vector<uint8_t>>> {
43 public:
ConfirmationListener()44 ConfirmationListener() {}
45
46 virtual ::android::binder::Status
onConfirmationPromptCompleted(int32_t result,const::std::vector<uint8_t> & dataThatWasConfirmed)47 onConfirmationPromptCompleted(int32_t result,
48 const ::std::vector<uint8_t>& dataThatWasConfirmed) override {
49 this->set_value({static_cast<ConfirmationResponseCode>(result), dataThatWasConfirmed});
50 return ::android::binder::Status::ok();
51 }
52 };
53
TEST(ConfirmationInvocationTest,InvokeAndCancel)54 TEST(ConfirmationInvocationTest, InvokeAndCancel) {
55 android::ProcessState::self()->startThreadPool();
56
57 sp<IServiceManager> sm = android::defaultServiceManager();
58 sp<IBinder> binder = sm->getService(String16("android.security.keystore"));
59 sp<IKeystoreService> service = android::interface_cast<IKeystoreService>(binder);
60 ASSERT_TRUE(service);
61
62 String16 promptText16("Just a little test!");
63 String16 locale16("en");
64 std::vector<uint8_t> extraData{0xaa, 0xff, 0x00, 0x55};
65
66 sp<ConfirmationListener> listener = new ConfirmationListener();
67
68 auto future = listener->get_future();
69 int32_t aidl_return;
70
71 android::binder::Status status = service->presentConfirmationPrompt(
72 listener, promptText16, extraData, locale16, 0, &aidl_return);
73 ASSERT_TRUE(status.isOk()) << "Presenting confirmation prompt failed with binder status '"
74 << status.toString8().c_str() << "'.\n";
75 ConfirmationResponseCode responseCode = static_cast<ConfirmationResponseCode>(aidl_return);
76 ASSERT_EQ(responseCode, ConfirmationResponseCode::OK)
77 << "Presenting confirmation prompt failed with response code " << aidl_return << ".\n";
78
79 auto fstatus = future.wait_for(2s);
80 EXPECT_EQ(fstatus, std::future_status::timeout);
81
82 status = service->cancelConfirmationPrompt(listener, &aidl_return);
83 ASSERT_TRUE(status.isOk());
84
85 responseCode = static_cast<ConfirmationResponseCode>(aidl_return);
86 ASSERT_EQ(responseCode, ConfirmationResponseCode::OK);
87
88 future.wait();
89 auto [rc, dataThatWasConfirmed] = future.get();
90
91 ASSERT_EQ(rc, ConfirmationResponseCode::Aborted);
92 }
93