summaryrefslogtreecommitdiffstats
path: root/tests/aidl_test_client_nullables.cpp
blob: 4f2865ef04dd0c40ab5cead25f79255538299bc4 (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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
/*
 * Copyright (C) 2015 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_test_client_nullables.h"

#include <utils/String16.h>

#include <iostream>
#include <memory>
#include <string>
#include <vector>

// libutils:
using android::sp;
using android::String16;

// libbinder:
using android::binder::Status;

// generated
using android::aidl::tests::ITestService;
using android::aidl::tests::SimpleParcelable;

using std::string;
using std::unique_ptr;
using std::vector;
using std::cout;
using std::cerr;
using std::endl;

namespace android {
namespace aidl {
namespace tests {
namespace client {

namespace {
template<typename T>
bool ValuesEqual(const unique_ptr<T>& in, const unique_ptr<T>& out) {
  return *in == *out;
}

template<>
bool ValuesEqual<vector<unique_ptr<String16>>>(
    const unique_ptr<vector<unique_ptr<String16>>>& in,
    const unique_ptr<vector<unique_ptr<String16>>>& out) {
  if (!in) {
    return !out;
  }

  if (!out) {
    return false;
  }

  if (in->size() != out->size()) {
    return false;
  }

  for (size_t i = 0; i < in->size(); i++) {
    const unique_ptr<String16>& a = (*in)[i];
    const unique_ptr<String16>& b = (*out)[i];

    if (!(a || b)) {
      continue;
    }

    if (!(a && b)) {
      return false;
    }

    if (*a != *b) {
      return false;
    }
  }

  return true;
}

template<typename T>
bool ConfirmNullableType(const sp<ITestService>& s, string type_name,
                         unique_ptr<T> in,
                         Status(ITestService::*func)(const unique_ptr<T>&,
                                                     unique_ptr<T>*)) {
  cout << "... Confirming nullables for " << type_name << " ..." << endl;
  Status status;
  unique_ptr<T> out;

  status = (*s.*func)(in, &out);

  if (!status.isOk()) {
    cerr << "Could not repeat nullable " << type_name << "." << endl;
    return false;
  }

  if (!out) {
    cerr << "Got back null when repeating " << type_name << "." << endl;
    return false;
  }

  if (!ValuesEqual(in, out)) {
    cerr << "Got back a non-matching value when repeating " << type_name
         << "." << endl;
    return false;
  }

  in.reset();
  status = (*s.*func)(in, &out);

  if (!status.isOk()) {
    cerr << "Could not repeat null as " << type_name << "." << endl;
    return false;
  }

  if (out) {
    cerr << "Got back a value when sent null for " << type_name << "."
         << endl;
    return false;
  }

  return true;
}

}  // namespace

bool ConfirmNullables(const sp<ITestService>& s) {
  Status status;
  cout << "Confirming passing and returning nullable values works." << endl;

  if (!ConfirmNullableType(s, "integer array",
                           unique_ptr<vector<int32_t>>(
                               new vector<int32_t>({1,2,3})),
                           &ITestService::RepeatNullableIntArray)) {
    return false;
  }

  if (!ConfirmNullableType(s, "string",
                           unique_ptr<String16>(new String16("Blooob")),
                           &ITestService::RepeatNullableString)) {
    return false;
  }

  unique_ptr<vector<unique_ptr<String16>>> test_string_array(
      new vector<unique_ptr<String16>>());
  test_string_array->push_back(unique_ptr<String16>(new String16("Wat")));
  test_string_array->push_back(unique_ptr<String16>(
      new String16("Blooob")));
  test_string_array->push_back(unique_ptr<String16>(new String16("Wat")));
  test_string_array->push_back(unique_ptr<String16>(nullptr));
  test_string_array->push_back(unique_ptr<String16>(new String16("YEAH")));
  test_string_array->push_back(unique_ptr<String16>(
      new String16("OKAAAAY")));

  if (!ConfirmNullableType(s, "string array", std::move(test_string_array),
                           &ITestService::RepeatNullableStringList)) {
    return false;
  }

  if (!ConfirmNullableType(s, "parcelable",
                           unique_ptr<SimpleParcelable>(
                               new SimpleParcelable("Booya", 42)),
                           &ITestService::RepeatNullableParcelable)) {
    return false;
  }

  return true;
}

}  // namespace client
}  // namespace tests
}  // namespace aidl
}  // namespace android