summaryrefslogtreecommitdiff
path: root/keystore/tests/confirmationui_invocation_test.cpp
blob: 822e6a4c1ca0ca905a08f6602b3a1b0d05d18f8b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
**
** Copyright 2019, The Android Open Source Project
**
** Licensed under the Apache License, Version 2.0 (the "License");
** you may not use this file except in compliance with the License.
** You may obtain a copy of the License at
**
**     http://www.apache.org/licenses/LICENSE-2.0
**
** Unless required by applicable law or agreed to in writing, software
** distributed under the License is distributed on an "AS IS" BASIS,
** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
** See the License for the specific language governing permissions and
** limitations under the License.
*/

#include <aidl/android/security/apc/BnConfirmationCallback.h>
#include <aidl/android/security/apc/IProtectedConfirmation.h>
#include <android/binder_manager.h>
#include <android/binder_process.h>

#include <gtest/gtest.h>

#include <chrono>
#include <future>
#include <tuple>
#include <vector>

using namespace std::literals::chrono_literals;
namespace apc = ::aidl::android::security::apc;

class ConfirmationListener
    : public apc::BnConfirmationCallback,
      public std::promise<std::tuple<apc::ResponseCode, std::optional<std::vector<uint8_t>>>> {
  public:
    ConfirmationListener() {}

    virtual ::ndk::ScopedAStatus
    onCompleted(::aidl::android::security::apc::ResponseCode result,
                const std::optional<std::vector<uint8_t>>& dataConfirmed) override {
        this->set_value({result, dataConfirmed});
        return ::ndk::ScopedAStatus::ok();
    };
};

TEST(ConfirmationInvocationTest, InvokeAndCancel) {
    ABinderProcess_startThreadPool();

    ::ndk::SpAIBinder apcBinder(AServiceManager_getService("android.security.apc"));
    auto apcService = apc::IProtectedConfirmation::fromBinder(apcBinder);
    ASSERT_TRUE(apcService);

    std::string promptText("Just a little test!");
    std::string locale("en");
    std::vector<uint8_t> extraData{0xaa, 0xff, 0x00, 0x55};

    auto listener = ndk::SharedRefBase::make<ConfirmationListener>();

    auto future = listener->get_future();

    auto rc = apcService->presentPrompt(listener, promptText, extraData, locale, 0);

    ASSERT_TRUE(rc.isOk());

    auto fstatus = future.wait_for(2s);
    EXPECT_EQ(fstatus, std::future_status::timeout);

    rc = apcService->cancelPrompt(listener);
    ASSERT_TRUE(rc.isOk());

    future.wait();
    auto [responseCode, dataThatWasConfirmed] = future.get();

    ASSERT_EQ(responseCode, apc::ResponseCode::ABORTED);
}