blob: c3ae443315926edb55a0a200d3f5f0c54972ee3a [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 Wileyb5e698c2015-10-17 09:32:22 -070043
44using std::cerr;
45using std::cout;
46using std::endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070047using std::vector;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070048
49namespace {
50
Christopher Wiley521bb612015-10-22 11:40:30 -070051const char kServiceName[] = "android.aidl.tests.ITestService";
Christopher Wileyb5e698c2015-10-17 09:32:22 -070052
Christopher Wiley521bb612015-10-22 11:40:30 -070053bool GetService(sp<ITestService>* service) {
54 cout << "Retrieving test service binder" << endl;
Christopher Wiley33ad81e2015-10-21 14:41:11 -070055 status_t status = getService(String16(kServiceName), service);
56 if (status != OK) {
57 cerr << "Failed to get service binder: '" << kServiceName
58 << "' status=" << status << endl;
59 return false;
60 }
61 return true;
62}
63
Christopher Wileyd6130f22015-10-26 10:24:35 -070064template <typename T>
65bool RepeatPrimitive(const sp<ITestService>& service,
Christopher Wiley433c8bb2015-11-12 14:20:46 -080066 Status(ITestService::*func)(T, T*),
Christopher Wileyd6130f22015-10-26 10:24:35 -070067 const T input) {
68 T reply;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080069 Status status = (*service.*func)(input, &reply);
70 if (!status.isOk() || input != reply) {
71 cerr << "Failed to repeat primitive. status=" << status.toString8()
72 << "." << endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070073 return false;
74 }
75 return true;
76}
77
78bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) {
Christopher Wiley8949f832015-10-27 15:32:03 -070079 cout << "Confirming passing and returning primitives works." << endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070080
81 if (!RepeatPrimitive(s, &ITestService::RepeatBoolean, true) ||
82 !RepeatPrimitive(s, &ITestService::RepeatByte, int8_t{-128}) ||
83 !RepeatPrimitive(s, &ITestService::RepeatChar, char16_t{'A'}) ||
84 !RepeatPrimitive(s, &ITestService::RepeatInt, int32_t{1 << 30}) ||
85 !RepeatPrimitive(s, &ITestService::RepeatLong, int64_t{1ll << 60}) ||
86 !RepeatPrimitive(s, &ITestService::RepeatFloat, float{1.0f/3.0f}) ||
87 !RepeatPrimitive(s, &ITestService::RepeatDouble, double{1.0/3.0})) {
88 return false;
89 }
90
91 vector<String16> inputs = {
92 String16("Deliver us from evil."),
93 String16(),
94 String16("\0\0", 2),
95 };
96 for (const auto& input : inputs) {
97 String16 reply;
Christopher Wiley433c8bb2015-11-12 14:20:46 -080098 Status status = s->RepeatString(input, &reply);
99 if (!status.isOk() || input != reply) {
Christopher Wileyd6130f22015-10-26 10:24:35 -0700100 cerr << "Failed while requesting service to repeat String16=\""
101 << String8(input).string()
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800102 << "\". Got status=" << status.toString8() << endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -0700103 return false;
104 }
105 }
106 return true;
107}
108
Christopher Wiley8949f832015-10-27 15:32:03 -0700109template <typename T>
110bool ReverseArray(const sp<ITestService>& service,
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800111 Status(ITestService::*func)(const vector<T>&,
112 vector<T>*,
113 vector<T>*),
Christopher Wiley8949f832015-10-27 15:32:03 -0700114 vector<T> input) {
115 vector<T> actual_reversed;
116 vector<T> actual_repeated;
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800117 Status status = (*service.*func)(input, &actual_repeated, &actual_reversed);
118 if (!status.isOk()) {
119 cerr << "Failed to repeat array. status=" << status.toString8() << "."
120 << endl;
Christopher Wiley8949f832015-10-27 15:32:03 -0700121 return false;
122 }
123 if (input != actual_repeated) {
124 cerr << "Repeated version of array did not match" << endl;
125 cerr << "input.size()=" << input.size()
126 << " repeated.size()=" << actual_repeated.size() << endl;
127 return false;
128 }
129 std::reverse(input.begin(), input.end());
130 if (input != actual_reversed) {
131 cerr << "Reversed version of array did not match" << endl;
132 return false;
133 }
134 return true;
135}
136
137bool ConfirmReverseArrays(const sp<ITestService>& s) {
138 cout << "Confirming passing and returning arrays works." << endl;
139
140 if (!ReverseArray(s, &ITestService::ReverseBoolean,
141 {true, false, false}) ||
142 !ReverseArray(s, &ITestService::ReverseByte,
143 {int8_t{-128}, int8_t{0}, int8_t{127}}) ||
144 !ReverseArray(s, &ITestService::ReverseChar,
145 {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}}) ||
146 !ReverseArray(s, &ITestService::ReverseInt,
147 {1, 2, 3}) ||
148 !ReverseArray(s, &ITestService::ReverseLong,
149 {-1ll, 0ll, int64_t{1ll << 60}}) ||
150 !ReverseArray(s, &ITestService::ReverseFloat,
151 {-0.3f, -0.7f, 8.0f}) ||
152 !ReverseArray(s, &ITestService::ReverseDouble,
153 {1.0/3.0, 1.0/7.0, 42.0}) ||
154 !ReverseArray(s, &ITestService::ReverseString,
155 {String16{"f"}, String16{"a"}, String16{"b"}})) {
156 return false;
157 }
158
159 return true;
160}
161
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700162bool ConfirmReverseLists(const sp<ITestService>& s) {
163 cout << "Confirming passing and returning List<T> works." << endl;
164
165 if (!ReverseArray(s, &ITestService::ReverseStringList,
166 {String16{"f"}, String16{"a"}, String16{"b"}})) {
167 return false;
168 }
169
170 return true;
171}
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800172
173bool ConfirmReverseBinderLists(const sp<ITestService>& s) {
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800174 Status status;
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800175 cout << "Confirming passing and returning List<T> works with binders." << endl;
176
177 vector<String16> names = {
178 String16{"Larry"},
179 String16{"Curly"},
180 String16{"Moe"}
181 };
182
183 vector<sp<IBinder>> input;
184
185 for (int i = 0; i < 3; i++) {
186 sp<INamedCallback> got;
187
188 status = s->GetOtherTestService(names[i], &got);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800189 if (!status.isOk()) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800190 cerr << "Could not retrieve service for test." << endl;
191 return false;
192 }
193
194 input.push_back(INamedCallback::asBinder(got));
195 }
196
197 vector<sp<IBinder>> output;
198 vector<sp<IBinder>> reversed;
199
200 status = s->ReverseNamedCallbackList(input, &output, &reversed);
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800201 if (!status.isOk()) {
202 cerr << "Failed to reverse named callback list." << endl;
203 }
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800204
205 if (output.size() != 3) {
206 cerr << "ReverseNamedCallbackList gave repetition with wrong length." << endl;
207 return false;
208 }
209
210 if (reversed.size() != 3) {
211 cerr << "ReverseNamedCallbackList gave reversal with wrong length." << endl;
212 return false;
213 }
214
215 for (int i = 0; i < 3; i++) {
216 String16 ret;
217 sp<INamedCallback> named_callback =
218 android::interface_cast<INamedCallback>(output[i]);
219 status = named_callback->GetName(&ret);
220
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800221 if (!status.isOk()) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800222 cerr << "Could not query INamedCallback from output" << endl;
223 return false;
224 }
225
226 if (ret != names[i]) {
227 cerr << "Output had wrong INamedCallback" << endl;
228 return false;
229 }
230 }
231
232 for (int i = 0; i < 3; i++) {
233 String16 ret;
234 sp<INamedCallback> named_callback =
235 android::interface_cast<INamedCallback>(reversed[i]);
236 status = named_callback->GetName(&ret);
237
Christopher Wiley433c8bb2015-11-12 14:20:46 -0800238 if (!status.isOk()) {
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800239 cerr << "Could not query INamedCallback from reversed output" << endl;
240 return false;
241 }
242
243 if (ret != names[2 - i]) {
244 cerr << "Reversed output had wrong INamedCallback" << endl;
245 return false;
246 }
247 }
248
249 return true;
250}
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700251} // namespace
252
Brian Carlstromad3b8062015-10-21 08:54:48 -0700253int main(int /* argc */, char * /* argv */ []) {
Christopher Wiley521bb612015-10-22 11:40:30 -0700254 sp<ITestService> service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700255
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700256 if (!GetService(&service)) return 1;
257
Christopher Wileyd6130f22015-10-26 10:24:35 -0700258 if (!ConfirmPrimitiveRepeat(service)) return 1;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700259
Christopher Wiley8949f832015-10-27 15:32:03 -0700260 if (!ConfirmReverseArrays(service)) return 1;
261
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700262 if (!ConfirmReverseLists(service)) return 1;
263
Casey Dahlin7ecd69f2015-11-03 13:52:38 -0800264 if (!ConfirmReverseBinderLists(service)) return 1;
265
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700266 return 0;
267}