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 <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>
22
23 #include <gtest/gtest.h>
24
25 #include <chrono>
26 #include <future>
27 #include <tuple>
28 #include <vector>
29
30 using namespace std::literals::chrono_literals;
31 namespace apc = ::aidl::android::security::apc;
32
33 class ConfirmationListener
34 : public apc::BnConfirmationCallback,
35 public std::promise<std::tuple<apc::ResponseCode, std::optional<std::vector<uint8_t>>>> {
36 public:
ConfirmationListener()37 ConfirmationListener() {}
38
39 virtual ::ndk::ScopedAStatus
onCompleted(::aidl::android::security::apc::ResponseCode result,const std::optional<std::vector<uint8_t>> & dataConfirmed)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 };
45 };
46
TEST(ConfirmationInvocationTest,InvokeAndCancel)47 TEST(ConfirmationInvocationTest, InvokeAndCancel) {
48 ABinderProcess_startThreadPool();
49
50 ::ndk::SpAIBinder apcBinder(AServiceManager_getService("android.security.apc"));
51 auto apcService = apc::IProtectedConfirmation::fromBinder(apcBinder);
52 ASSERT_TRUE(apcService);
53
54 std::string promptText("Just a little test!");
55 std::string locale("en");
56 std::vector<uint8_t> extraData{0xaa, 0xff, 0x00, 0x55};
57
58 auto listener = std::make_shared<ConfirmationListener>();
59
60 auto future = listener->get_future();
61
62 auto rc = apcService->presentPrompt(listener, promptText, extraData, locale, 0);
63
64 ASSERT_TRUE(rc.isOk());
65
66 auto fstatus = future.wait_for(2s);
67 EXPECT_EQ(fstatus, std::future_status::timeout);
68
69 rc = apcService->cancelPrompt(listener);
70 ASSERT_TRUE(rc.isOk());
71
72 future.wait();
73 auto [responseCode, dataThatWasConfirmed] = future.get();
74
75 ASSERT_EQ(responseCode, apc::ResponseCode::ABORTED);
76 }
77