summaryrefslogtreecommitdiffstats
path: root/tests/aidl_test_client_nullables.cpp
blob: 805827c931299e21b48971574414e1217e93eff4 (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
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
/*
 * 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, const 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;
}

bool CheckAppropriateIBinderHandling(const sp<ITestService>& s) {

  Status status;
  sp<IBinder> binder = new BBinder();
  sp<IBinder> null_binder = nullptr;
  unique_ptr<vector<sp<IBinder>>> list_with_nulls(
      new vector<sp<IBinder>>{binder, null_binder});
  unique_ptr<vector<sp<IBinder>>> list_without_nulls(
      new vector<sp<IBinder>>{binder, binder});

  // Methods without @nullable throw up when given null binders
  if (s->TakesAnIBinder(null_binder).exceptionCode() !=
      binder::Status::EX_NULL_POINTER) {
    cerr << "Did not receive expected null exception on line: "
         << __LINE__ << endl;
    return false;
  }
  if (s->TakesAnIBinderList(*list_with_nulls).exceptionCode() !=
      binder::Status::EX_NULL_POINTER) {
    cerr << "Did not receive expected null exception on line: "
         << __LINE__ << endl;
    return false;
  }

  // But those same methods are fine with valid binders
  if (!s->TakesAnIBinder(binder).isOk()) {
    cerr << "Received unexpected exception on line "
         << __LINE__ << endl;
    return false;
  }
  if (!s->TakesAnIBinderList(*list_without_nulls).isOk()) {
    cerr << "Received unexpected exception on line "
         << __LINE__ << endl;
    return false;
  }

  // And methods with @nullable don't care.
  if (!s->TakesANullableIBinder(null_binder).isOk()) {
    cerr << "Received unexpected exception on line "
         << __LINE__ << endl;
    return false;
  }
  if (!s->TakesANullableIBinder(binder).isOk()) {
    cerr << "Received unexpected exception on line "
         << __LINE__ << endl;
    return false;
  }
  if (!s->TakesANullableIBinderList(list_with_nulls).isOk()) {
    cerr << "Received unexpected exception on line "
         << __LINE__ << endl;
    return false;
  }
  if (!s->TakesANullableIBinderList(list_without_nulls).isOk()) {
    cerr << "Received unexpected exception on line "
         << __LINE__ << endl;
    return false;
  }

  return true;
}

bool CheckAppropriateIInterfaceHandling(const sp<ITestService>& s) {

  sp<INamedCallback> callback;
  if (!s->GetCallback(false, &callback).isOk()) {
    cerr << "Received unexpected exception on line "
         << __LINE__ << endl;
    return false;
  }
  if (callback.get() == nullptr) {
    cerr << "Expected to receive a non-null binder on line: "
         << __LINE__ << endl;
    return false;
  }
  if (!s->GetCallback(true, &callback).isOk()) {
    cerr << "Received unexpected exception on line "
         << __LINE__ << endl;
    return false;
  }
  if (callback.get() != nullptr) {
    cerr << "Expected to receive a null binder on line: "
         << __LINE__ << 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;
  }

  if (!CheckAppropriateIBinderHandling(s)) {
    cerr << "Handled null IBinders poorly." << endl;
    return false;
  }

  if (!CheckAppropriateIInterfaceHandling(s)) {
    cerr << "Handled nullable IInterface instances poorly." << endl;
    return false;
  }

  return true;
}

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