blob: 7f8a373817ddc559962b22caa8788ee85420fb62 [file] [log] [blame]
Janis Danisevskis5c6af752019-03-21 13:45:02 -07001/*
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
Janis Danisevskis2437fde2021-03-08 11:30:11 -080018#include <aidl/android/security/apc/BnConfirmationCallback.h>
19#include <aidl/android/security/apc/IProtectedConfirmation.h>
20#include <android/binder_manager.h>
21#include <android/binder_process.h>
Janis Danisevskis5c6af752019-03-21 13:45:02 -070022
23#include <gtest/gtest.h>
24
25#include <chrono>
26#include <future>
27#include <tuple>
28#include <vector>
29
Janis Danisevskis5c6af752019-03-21 13:45:02 -070030using namespace std::literals::chrono_literals;
Janis Danisevskis2437fde2021-03-08 11:30:11 -080031namespace apc = ::aidl::android::security::apc;
Janis Danisevskis5c6af752019-03-21 13:45:02 -070032
33class ConfirmationListener
Janis Danisevskis2437fde2021-03-08 11:30:11 -080034 : public apc::BnConfirmationCallback,
35 public std::promise<std::tuple<apc::ResponseCode, std::optional<std::vector<uint8_t>>>> {
Janis Danisevskis5c6af752019-03-21 13:45:02 -070036 public:
37 ConfirmationListener() {}
38
Janis Danisevskis2437fde2021-03-08 11:30:11 -080039 virtual ::ndk::ScopedAStatus
40 onCompleted(::aidl::android::security::apc::ResponseCode result,
41 const std::optional<std::vector<uint8_t>>& dataConfirmed) override {
42 this->set_value({result, dataConfirmed});
43 return ::ndk::ScopedAStatus::ok();
44 };
Janis Danisevskis5c6af752019-03-21 13:45:02 -070045};
46
47TEST(ConfirmationInvocationTest, InvokeAndCancel) {
Janis Danisevskis2437fde2021-03-08 11:30:11 -080048 ABinderProcess_startThreadPool();
Janis Danisevskis5c6af752019-03-21 13:45:02 -070049
Janis Danisevskis2437fde2021-03-08 11:30:11 -080050 ::ndk::SpAIBinder apcBinder(AServiceManager_getService("android.security.apc"));
51 auto apcService = apc::IProtectedConfirmation::fromBinder(apcBinder);
52 ASSERT_TRUE(apcService);
Janis Danisevskis5c6af752019-03-21 13:45:02 -070053
Janis Danisevskis2437fde2021-03-08 11:30:11 -080054 std::string promptText("Just a little test!");
55 std::string locale("en");
Janis Danisevskis5c6af752019-03-21 13:45:02 -070056 std::vector<uint8_t> extraData{0xaa, 0xff, 0x00, 0x55};
57
Janis Danisevskis2437fde2021-03-08 11:30:11 -080058 auto listener = std::make_shared<ConfirmationListener>();
Janis Danisevskis5c6af752019-03-21 13:45:02 -070059
60 auto future = listener->get_future();
Janis Danisevskis5c6af752019-03-21 13:45:02 -070061
Janis Danisevskis2437fde2021-03-08 11:30:11 -080062 auto rc = apcService->presentPrompt(listener, promptText, extraData, locale, 0);
63
64 ASSERT_TRUE(rc.isOk());
Janis Danisevskis5c6af752019-03-21 13:45:02 -070065
66 auto fstatus = future.wait_for(2s);
67 EXPECT_EQ(fstatus, std::future_status::timeout);
68
Janis Danisevskis2437fde2021-03-08 11:30:11 -080069 rc = apcService->cancelPrompt(listener);
70 ASSERT_TRUE(rc.isOk());
Janis Danisevskis5c6af752019-03-21 13:45:02 -070071
72 future.wait();
Janis Danisevskis2437fde2021-03-08 11:30:11 -080073 auto [responseCode, dataThatWasConfirmed] = future.get();
Janis Danisevskis5c6af752019-03-21 13:45:02 -070074
Janis Danisevskis2437fde2021-03-08 11:30:11 -080075 ASSERT_EQ(responseCode, apc::ResponseCode::ABORTED);
Janis Danisevskis5c6af752019-03-21 13:45:02 -070076}