blob: 211d0fac6cd531336985c40a44d938d1d00a14e0 [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 Wileyb5e698c2015-10-17 09:32:22 -070038
39// generated
Christopher Wiley521bb612015-10-22 11:40:30 -070040using android::aidl::tests::ITestService;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -080041using android::aidl::tests::INamedCallback;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070042
43using std::cerr;
44using std::cout;
45using std::endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070046using std::vector;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070047
48namespace {
49
Christopher Wiley521bb612015-10-22 11:40:30 -070050const char kServiceName[] = "android.aidl.tests.ITestService";
Christopher Wileyb5e698c2015-10-17 09:32:22 -070051
Christopher Wiley521bb612015-10-22 11:40:30 -070052bool GetService(sp<ITestService>* service) {
53 cout << "Retrieving test service binder" << endl;
Christopher Wiley33ad81e2015-10-21 14:41:11 -070054 status_t status = getService(String16(kServiceName), service);
55 if (status != OK) {
56 cerr << "Failed to get service binder: '" << kServiceName
57 << "' status=" << status << endl;
58 return false;
59 }
60 return true;
61}
62
Christopher Wileyd6130f22015-10-26 10:24:35 -070063template <typename T>
64bool RepeatPrimitive(const sp<ITestService>& service,
65 status_t(ITestService::*func)(T, T*),
66 const T input) {
67 T reply;
68 status_t status = (*service.*func)(input, &reply);
69 if (status != OK || input != reply) {
70 cerr << "Failed to repeat primitive. status=" << status << "." << endl;
71 return false;
72 }
73 return true;
74}
75
76bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) {
Christopher Wiley8949f832015-10-27 15:32:03 -070077 cout << "Confirming passing and returning primitives works." << endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070078
79 if (!RepeatPrimitive(s, &ITestService::RepeatBoolean, true) ||
80 !RepeatPrimitive(s, &ITestService::RepeatByte, int8_t{-128}) ||
81 !RepeatPrimitive(s, &ITestService::RepeatChar, char16_t{'A'}) ||
82 !RepeatPrimitive(s, &ITestService::RepeatInt, int32_t{1 << 30}) ||
83 !RepeatPrimitive(s, &ITestService::RepeatLong, int64_t{1ll << 60}) ||
84 !RepeatPrimitive(s, &ITestService::RepeatFloat, float{1.0f/3.0f}) ||
85 !RepeatPrimitive(s, &ITestService::RepeatDouble, double{1.0/3.0})) {
86 return false;
87 }
88
89 vector<String16> inputs = {
90 String16("Deliver us from evil."),
91 String16(),
92 String16("\0\0", 2),
93 };
94 for (const auto& input : inputs) {
95 String16 reply;
96 status_t status = s->RepeatString(input, &reply);
97 if (status != OK || input != reply) {
98 cerr << "Failed while requesting service to repeat String16=\""
99 << String8(input).string()
100 << "\". Got status=" << status << endl;
101 return false;
102 }
103 }
104 return true;
105}
106
Christopher Wiley8949f832015-10-27 15:32:03 -0700107template <typename T>
108bool ReverseArray(const sp<ITestService>& service,
109 status_t(ITestService::*func)(const vector<T>&,
110 vector<T>*,
111 vector<T>*),
112 vector<T> input) {
113 vector<T> actual_reversed;
114 vector<T> actual_repeated;
115 status_t status = (*service.*func)(input, &actual_repeated, &actual_reversed);
116 if (status != OK) {
117 cerr << "Failed to repeat array. status=" << status << "." << endl;
118 return false;
119 }
120 if (input != actual_repeated) {
121 cerr << "Repeated version of array did not match" << endl;
122 cerr << "input.size()=" << input.size()
123 << " repeated.size()=" << actual_repeated.size() << endl;
124 return false;
125 }
126 std::reverse(input.begin(), input.end());
127 if (input != actual_reversed) {
128 cerr << "Reversed version of array did not match" << endl;
129 return false;
130 }
131 return true;
132}
133
134bool ConfirmReverseArrays(const sp<ITestService>& s) {
135 cout << "Confirming passing and returning arrays works." << endl;
136
137 if (!ReverseArray(s, &ITestService::ReverseBoolean,
138 {true, false, false}) ||
139 !ReverseArray(s, &ITestService::ReverseByte,
140 {int8_t{-128}, int8_t{0}, int8_t{127}}) ||
141 !ReverseArray(s, &ITestService::ReverseChar,
142 {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}}) ||
143 !ReverseArray(s, &ITestService::ReverseInt,
144 {1, 2, 3}) ||
145 !ReverseArray(s, &ITestService::ReverseLong,
146 {-1ll, 0ll, int64_t{1ll << 60}}) ||
147 !ReverseArray(s, &ITestService::ReverseFloat,
148 {-0.3f, -0.7f, 8.0f}) ||
149 !ReverseArray(s, &ITestService::ReverseDouble,
150 {1.0/3.0, 1.0/7.0, 42.0}) ||
151 !ReverseArray(s, &ITestService::ReverseString,
152 {String16{"f"}, String16{"a"}, String16{"b"}})) {
153 return false;
154 }
155
156 return true;
157}
158
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700159bool ConfirmReverseLists(const sp<ITestService>& s) {
160 cout << "Confirming passing and returning List<T> works." << endl;
161
162 if (!ReverseArray(s, &ITestService::ReverseStringList,
163 {String16{"f"}, String16{"a"}, String16{"b"}})) {
164 return false;
165 }
166
167 return true;
168}
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800169
170bool ConfirmReverseBinderLists(const sp<ITestService>& s) {
171 status_t status;
172 cout << "Confirming passing and returning List<T> works with binders." << endl;
173
174 vector<String16> names = {
175 String16{"Larry"},
176 String16{"Curly"},
177 String16{"Moe"}
178 };
179
180 vector<sp<IBinder>> input;
181
182 for (int i = 0; i < 3; i++) {
183 sp<INamedCallback> got;
184
185 status = s->GetOtherTestService(names[i], &got);
186 if (status != OK) {
187 cerr << "Could not retrieve service for test." << endl;
188 return false;
189 }
190
191 input.push_back(INamedCallback::asBinder(got));
192 }
193
194 vector<sp<IBinder>> output;
195 vector<sp<IBinder>> reversed;
196
197 status = s->ReverseNamedCallbackList(input, &output, &reversed);
198
199 if (output.size() != 3) {
200 cerr << "ReverseNamedCallbackList gave repetition with wrong length." << endl;
201 return false;
202 }
203
204 if (reversed.size() != 3) {
205 cerr << "ReverseNamedCallbackList gave reversal with wrong length." << endl;
206 return false;
207 }
208
209 for (int i = 0; i < 3; i++) {
210 String16 ret;
211 sp<INamedCallback> named_callback =
212 android::interface_cast<INamedCallback>(output[i]);
213 status = named_callback->GetName(&ret);
214
215 if (status != OK) {
216 cerr << "Could not query INamedCallback from output" << endl;
217 return false;
218 }
219
220 if (ret != names[i]) {
221 cerr << "Output had wrong INamedCallback" << endl;
222 return false;
223 }
224 }
225
226 for (int i = 0; i < 3; i++) {
227 String16 ret;
228 sp<INamedCallback> named_callback =
229 android::interface_cast<INamedCallback>(reversed[i]);
230 status = named_callback->GetName(&ret);
231
232 if (status != OK) {
233 cerr << "Could not query INamedCallback from reversed output" << endl;
234 return false;
235 }
236
237 if (ret != names[2 - i]) {
238 cerr << "Reversed output had wrong INamedCallback" << endl;
239 return false;
240 }
241 }
242
243 return true;
244}
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700245} // namespace
246
Brian Carlstromad3b8062015-10-21 08:54:48 -0700247int main(int /* argc */, char * /* argv */ []) {
Christopher Wiley521bb612015-10-22 11:40:30 -0700248 sp<ITestService> service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700249
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700250 if (!GetService(&service)) return 1;
251
Christopher Wileyd6130f22015-10-26 10:24:35 -0700252 if (!ConfirmPrimitiveRepeat(service)) return 1;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700253
Christopher Wiley8949f832015-10-27 15:32:03 -0700254 if (!ConfirmReverseArrays(service)) return 1;
255
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700256 if (!ConfirmReverseLists(service)) return 1;
257
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800258 if (!ConfirmReverseBinderLists(service)) return 1;
259
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700260 return 0;
261}