aboutsummaryrefslogtreecommitdiff
path: root/tests/aidl_test_client_ndk_primitives_test.cpp
blob: e82354fe2a8ce69fe9b19778962019eb3f3b1f05 (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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
/*
 * Copyright (C) 2021 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 <android/binder_auto_utils.h>
#include <android/binder_manager.h>
#include <gmock/gmock.h>
#include <gtest/gtest.h>

#include <aidl/android/aidl/tests/ITestService.h>

using aidl::android::aidl::tests::INamedCallback;
using aidl::android::aidl::tests::ITestService;
using testing::Eq;

struct AidlTest : testing::Test {
  template <typename T>
  std::shared_ptr<T> getService() {
    ndk::SpAIBinder binder = ndk::SpAIBinder(AServiceManager_getService(T::descriptor));
    return T::fromBinder(binder);
  }
};

TEST_F(AidlTest, InterfaceExchange) {
  auto service = getService<ITestService>();
  std::vector<std::string> names = {"Larry", "Curly", "Moe"};

  for (size_t i = 0; i < names.size(); i++) {
    std::shared_ptr<INamedCallback> got;
    ASSERT_TRUE(service->GetOtherTestService(names[i], &got).isOk());
    std::string name;
    ASSERT_TRUE(got->GetName(&name).isOk());
    ASSERT_THAT(name, Eq(names[i]));
  }
  {
    std::vector<std::shared_ptr<INamedCallback>> got;
    ASSERT_TRUE(service->GetInterfaceArray(names, &got).isOk());

    bool verified = false;
    ASSERT_TRUE(service->VerifyNamesWithInterfaceArray(got, names, &verified).isOk());
    ASSERT_TRUE(verified);

    for (size_t i = 0; i < names.size(); i++) {
      std::string name;
      ASSERT_TRUE(got[i]->GetName(&name).isOk());
      ASSERT_THAT(name, Eq(names[i]));
    }
  }
  {
    std::vector<std::optional<std::string>> names = {"Larry", std::nullopt, "Moe"};
    std::optional<std::vector<std::shared_ptr<INamedCallback>>> got;
    ASSERT_TRUE(service->GetNullableInterfaceArray(names, &got).isOk());
    bool verified = false;
    ASSERT_TRUE(service->VerifyNamesWithNullableInterfaceArray(got, names, &verified).isOk());
    ASSERT_TRUE(verified);
    ASSERT_TRUE(got.has_value());
    for (size_t i = 0; i < names.size(); i++) {
      if (names[i].has_value()) {
        ASSERT_NE(got->at(i).get(), nullptr);
        std::string name;
        ASSERT_TRUE(got->at(i)->GetName(&name).isOk());
        ASSERT_THAT(name, Eq(names[i].value()));
      } else {
        ASSERT_EQ(got->at(i).get(), nullptr);
      }
    }
  }
  {
    std::vector<std::optional<std::string>> names = {"Larry", std::nullopt, "Moe"};
    std::optional<std::vector<std::shared_ptr<INamedCallback>>> got;
    ASSERT_TRUE(service->GetInterfaceList(names, &got).isOk());
    bool verified = false;
    ASSERT_TRUE(service->VerifyNamesWithInterfaceList(got, names, &verified).isOk());
    ASSERT_TRUE(verified);
    ASSERT_TRUE(got.has_value());
    for (size_t i = 0; i < names.size(); i++) {
      if (names[i].has_value()) {
        ASSERT_NE(got->at(i).get(), nullptr);
        std::string name;
        ASSERT_TRUE(got->at(i)->GetName(&name).isOk());
        ASSERT_THAT(name, Eq(names[i].value()));
      } else {
        ASSERT_EQ(got->at(i).get(), nullptr);
      }
    }
  }
}