blob: e9df4b524fc2adb874f71c54436465388ebaa96f [file] [log] [blame]
Christopher Wileyb5e698c2015-10-17 09:32:22 -07001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#include <iostream>
Christopher Wileyd6130f22015-10-26 10:24:35 -070018#include <vector>
Christopher Wileyb5e698c2015-10-17 09:32:22 -070019
20#include <binder/IServiceManager.h>
Christopher Wileyd6130f22015-10-26 10:24:35 -070021#include <utils/String8.h>
Christopher Wileyb5e698c2015-10-17 09:32:22 -070022#include <utils/String16.h>
23#include <utils/StrongPointer.h>
24
Christopher Wiley521bb612015-10-22 11:40:30 -070025#include "android/aidl/tests/ITestService.h"
Casey Dahlin7ecd69f2015-11-03 13:52:38 -080026#include "android/aidl/tests/INamedCallback.h"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070027
28// libutils:
29using android::OK;
30using android::sp;
31using android::status_t;
32using android::String16;
Christopher Wileyd6130f22015-10-26 10:24:35 -070033using android::String8;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070034
35// libbinder:
36using android::getService;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -080037using android::IBinder;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080038using android::binder::Status;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070039
40// generated
Christopher Wiley521bb612015-10-22 11:40:30 -070041using android::aidl::tests::ITestService;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -080042using android::aidl::tests::INamedCallback;
Christopher Wiley95d44b02015-11-19 07:11:30 -080043using android::aidl::tests::SimpleParcelable;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070044
45using std::cerr;
46using std::cout;
47using std::endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070048using std::vector;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070049
50namespace {
51
Christopher Wiley521bb612015-10-22 11:40:30 -070052const char kServiceName[] = "android.aidl.tests.ITestService";
Christopher Wileyb5e698c2015-10-17 09:32:22 -070053
Christopher Wiley521bb612015-10-22 11:40:30 -070054bool GetService(sp<ITestService>* service) {
55 cout << "Retrieving test service binder" << endl;
Christopher Wiley33ad81e2015-10-21 14:41:11 -070056 status_t status = getService(String16(kServiceName), service);
57 if (status != OK) {
58 cerr << "Failed to get service binder: '" << kServiceName
59 << "' status=" << status << endl;
60 return false;
61 }
62 return true;
63}
64
Christopher Wileyd6130f22015-10-26 10:24:35 -070065template <typename T>
66bool RepeatPrimitive(const sp<ITestService>& service,
Christopher Wiley433c8bb2015-11-12 14:20:46 -080067 Status(ITestService::*func)(T, T*),
Christopher Wileyd6130f22015-10-26 10:24:35 -070068 const T input) {
69 T reply;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080070 Status status = (*service.*func)(input, &reply);
71 if (!status.isOk() || input != reply) {
72 cerr << "Failed to repeat primitive. status=" << status.toString8()
73 << "." << endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070074 return false;
75 }
76 return true;
77}
78
79bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) {
Christopher Wiley8949f832015-10-27 15:32:03 -070080 cout << "Confirming passing and returning primitives works." << endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070081
82 if (!RepeatPrimitive(s, &ITestService::RepeatBoolean, true) ||
83 !RepeatPrimitive(s, &ITestService::RepeatByte, int8_t{-128}) ||
84 !RepeatPrimitive(s, &ITestService::RepeatChar, char16_t{'A'}) ||
85 !RepeatPrimitive(s, &ITestService::RepeatInt, int32_t{1 << 30}) ||
86 !RepeatPrimitive(s, &ITestService::RepeatLong, int64_t{1ll << 60}) ||
87 !RepeatPrimitive(s, &ITestService::RepeatFloat, float{1.0f/3.0f}) ||
88 !RepeatPrimitive(s, &ITestService::RepeatDouble, double{1.0/3.0})) {
89 return false;
90 }
91
92 vector<String16> inputs = {
93 String16("Deliver us from evil."),
94 String16(),
95 String16("\0\0", 2),
96 };
97 for (const auto& input : inputs) {
98 String16 reply;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080099 Status status = s->RepeatString(input, &reply);
100 if (!status.isOk() || input != reply) {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700101 cerr << "Failed while requesting service to repeat String16=\""
102 << String8(input).string()
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800103 << "\". Got status=" << status.toString8() << endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -0700104 return false;
105 }
106 }
107 return true;
108}
109
Christopher Wiley8949f832015-10-27 15:32:03 -0700110template <typename T>
111bool ReverseArray(const sp<ITestService>& service,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800112 Status(ITestService::*func)(const vector<T>&,
113 vector<T>*,
114 vector<T>*),
Christopher Wiley8949f832015-10-27 15:32:03 -0700115 vector<T> input) {
116 vector<T> actual_reversed;
117 vector<T> actual_repeated;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800118 Status status = (*service.*func)(input, &actual_repeated, &actual_reversed);
119 if (!status.isOk()) {
120 cerr << "Failed to repeat array. status=" << status.toString8() << "."
121 << endl;
Christopher Wiley8949f832015-10-27 15:32:03 -0700122 return false;
123 }
124 if (input != actual_repeated) {
125 cerr << "Repeated version of array did not match" << endl;
126 cerr << "input.size()=" << input.size()
127 << " repeated.size()=" << actual_repeated.size() << endl;
128 return false;
129 }
130 std::reverse(input.begin(), input.end());
131 if (input != actual_reversed) {
132 cerr << "Reversed version of array did not match" << endl;
133 return false;
134 }
135 return true;
136}
137
138bool ConfirmReverseArrays(const sp<ITestService>& s) {
139 cout << "Confirming passing and returning arrays works." << endl;
140
141 if (!ReverseArray(s, &ITestService::ReverseBoolean,
142 {true, false, false}) ||
143 !ReverseArray(s, &ITestService::ReverseByte,
144 {int8_t{-128}, int8_t{0}, int8_t{127}}) ||
145 !ReverseArray(s, &ITestService::ReverseChar,
146 {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}}) ||
147 !ReverseArray(s, &ITestService::ReverseInt,
148 {1, 2, 3}) ||
149 !ReverseArray(s, &ITestService::ReverseLong,
150 {-1ll, 0ll, int64_t{1ll << 60}}) ||
151 !ReverseArray(s, &ITestService::ReverseFloat,
152 {-0.3f, -0.7f, 8.0f}) ||
153 !ReverseArray(s, &ITestService::ReverseDouble,
154 {1.0/3.0, 1.0/7.0, 42.0}) ||
155 !ReverseArray(s, &ITestService::ReverseString,
156 {String16{"f"}, String16{"a"}, String16{"b"}})) {
157 return false;
158 }
159
160 return true;
161}
162
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700163bool ConfirmReverseLists(const sp<ITestService>& s) {
164 cout << "Confirming passing and returning List<T> works." << endl;
165
166 if (!ReverseArray(s, &ITestService::ReverseStringList,
167 {String16{"f"}, String16{"a"}, String16{"b"}})) {
168 return false;
169 }
170
171 return true;
172}
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800173
Christopher Wiley95d44b02015-11-19 07:11:30 -0800174bool ConfirmParcelables(const sp<ITestService>& s) {
175 cout << "Confirming passing and returning Parcelables works." << endl;
176
177 SimpleParcelable input("Booya", 42);
178 SimpleParcelable out_param, returned;
179 Status status = s->RepeatParcelable(input, &out_param, &returned);
180 if (!status.isOk()) {
181 cout << "Binder call failed." << endl;
182 return false;
183 }
184 if (input != out_param || input != returned) {
185 cout << "Failed to repeat parcelables." << endl;
186 return false;
187 }
188
189 cout << "Attempting to reverse an array of parcelables." << endl;
190 const vector<SimpleParcelable> original{SimpleParcelable("first", 0),
191 SimpleParcelable("second", 1),
192 SimpleParcelable("third", 2)};
193 vector<SimpleParcelable> repeated;
194 vector<SimpleParcelable> reversed;
195 status = s->ReverseParcelables(original, &repeated, &reversed);
196 if (!status.isOk()) {
197 cout << "Binder call failed." << endl;
198 return false;
199 }
200 std::reverse(reversed.begin(), reversed.end());
201 if (repeated != original || reversed != original) {
202 cout << "Failed to reverse an array of parcelables." << endl;
203 return false;
204 }
205
206 return true;
207}
208
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800209bool ConfirmReverseBinderLists(const sp<ITestService>& s) {
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800210 Status status;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800211 cout << "Confirming passing and returning List<T> works with binders." << endl;
212
213 vector<String16> names = {
214 String16{"Larry"},
215 String16{"Curly"},
216 String16{"Moe"}
217 };
218
219 vector<sp<IBinder>> input;
220
221 for (int i = 0; i < 3; i++) {
222 sp<INamedCallback> got;
223
224 status = s->GetOtherTestService(names[i], &got);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800225 if (!status.isOk()) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800226 cerr << "Could not retrieve service for test." << endl;
227 return false;
228 }
229
230 input.push_back(INamedCallback::asBinder(got));
231 }
232
233 vector<sp<IBinder>> output;
234 vector<sp<IBinder>> reversed;
235
236 status = s->ReverseNamedCallbackList(input, &output, &reversed);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800237 if (!status.isOk()) {
238 cerr << "Failed to reverse named callback list." << endl;
239 }
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800240
241 if (output.size() != 3) {
242 cerr << "ReverseNamedCallbackList gave repetition with wrong length." << endl;
243 return false;
244 }
245
246 if (reversed.size() != 3) {
247 cerr << "ReverseNamedCallbackList gave reversal with wrong length." << endl;
248 return false;
249 }
250
251 for (int i = 0; i < 3; i++) {
252 String16 ret;
253 sp<INamedCallback> named_callback =
254 android::interface_cast<INamedCallback>(output[i]);
255 status = named_callback->GetName(&ret);
256
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800257 if (!status.isOk()) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800258 cerr << "Could not query INamedCallback from output" << endl;
259 return false;
260 }
261
262 if (ret != names[i]) {
263 cerr << "Output had wrong INamedCallback" << endl;
264 return false;
265 }
266 }
267
268 for (int i = 0; i < 3; i++) {
269 String16 ret;
270 sp<INamedCallback> named_callback =
271 android::interface_cast<INamedCallback>(reversed[i]);
272 status = named_callback->GetName(&ret);
273
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800274 if (!status.isOk()) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800275 cerr << "Could not query INamedCallback from reversed output" << endl;
276 return false;
277 }
278
279 if (ret != names[2 - i]) {
280 cerr << "Reversed output had wrong INamedCallback" << endl;
281 return false;
282 }
283 }
284
285 return true;
286}
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700287} // namespace
288
Brian Carlstromad3b8062015-10-21 08:54:48 -0700289int main(int /* argc */, char * /* argv */ []) {
Christopher Wiley521bb612015-10-22 11:40:30 -0700290 sp<ITestService> service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700291
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700292 if (!GetService(&service)) return 1;
293
Christopher Wileyd6130f22015-10-26 10:24:35 -0700294 if (!ConfirmPrimitiveRepeat(service)) return 1;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700295
Christopher Wiley8949f832015-10-27 15:32:03 -0700296 if (!ConfirmReverseArrays(service)) return 1;
297
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700298 if (!ConfirmReverseLists(service)) return 1;
299
Christopher Wiley95d44b02015-11-19 07:11:30 -0800300 if (!ConfirmParcelables(service)) return 1;
301
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800302 if (!ConfirmReverseBinderLists(service)) return 1;
303
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700304 return 0;
305}