blob: 23ca80dc032923a31b7b2edb79eaa023db5f0c6b [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"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070026
27// libutils:
28using android::OK;
29using android::sp;
30using android::status_t;
31using android::String16;
Christopher Wileyd6130f22015-10-26 10:24:35 -070032using android::String8;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070033
34// libbinder:
35using android::getService;
36
37// generated
Christopher Wiley521bb612015-10-22 11:40:30 -070038using android::aidl::tests::ITestService;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070039
40using std::cerr;
41using std::cout;
42using std::endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070043using std::vector;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070044
45namespace {
46
Christopher Wiley521bb612015-10-22 11:40:30 -070047const char kServiceName[] = "android.aidl.tests.ITestService";
Christopher Wileyb5e698c2015-10-17 09:32:22 -070048
Christopher Wiley521bb612015-10-22 11:40:30 -070049bool GetService(sp<ITestService>* service) {
50 cout << "Retrieving test service binder" << endl;
Christopher Wiley33ad81e2015-10-21 14:41:11 -070051 status_t status = getService(String16(kServiceName), service);
52 if (status != OK) {
53 cerr << "Failed to get service binder: '" << kServiceName
54 << "' status=" << status << endl;
55 return false;
56 }
57 return true;
58}
59
Christopher Wileyd6130f22015-10-26 10:24:35 -070060template <typename T>
61bool RepeatPrimitive(const sp<ITestService>& service,
62 status_t(ITestService::*func)(T, T*),
63 const T input) {
64 T reply;
65 status_t status = (*service.*func)(input, &reply);
66 if (status != OK || input != reply) {
67 cerr << "Failed to repeat primitive. status=" << status << "." << endl;
68 return false;
69 }
70 return true;
71}
72
73bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) {
Christopher Wiley8949f832015-10-27 15:32:03 -070074 cout << "Confirming passing and returning primitives works." << endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070075
76 if (!RepeatPrimitive(s, &ITestService::RepeatBoolean, true) ||
77 !RepeatPrimitive(s, &ITestService::RepeatByte, int8_t{-128}) ||
78 !RepeatPrimitive(s, &ITestService::RepeatChar, char16_t{'A'}) ||
79 !RepeatPrimitive(s, &ITestService::RepeatInt, int32_t{1 << 30}) ||
80 !RepeatPrimitive(s, &ITestService::RepeatLong, int64_t{1ll << 60}) ||
81 !RepeatPrimitive(s, &ITestService::RepeatFloat, float{1.0f/3.0f}) ||
82 !RepeatPrimitive(s, &ITestService::RepeatDouble, double{1.0/3.0})) {
83 return false;
84 }
85
86 vector<String16> inputs = {
87 String16("Deliver us from evil."),
88 String16(),
89 String16("\0\0", 2),
90 };
91 for (const auto& input : inputs) {
92 String16 reply;
93 status_t status = s->RepeatString(input, &reply);
94 if (status != OK || input != reply) {
95 cerr << "Failed while requesting service to repeat String16=\""
96 << String8(input).string()
97 << "\". Got status=" << status << endl;
98 return false;
99 }
100 }
101 return true;
102}
103
Christopher Wiley8949f832015-10-27 15:32:03 -0700104template <typename T>
105bool ReverseArray(const sp<ITestService>& service,
106 status_t(ITestService::*func)(const vector<T>&,
107 vector<T>*,
108 vector<T>*),
109 vector<T> input) {
110 vector<T> actual_reversed;
111 vector<T> actual_repeated;
112 status_t status = (*service.*func)(input, &actual_repeated, &actual_reversed);
113 if (status != OK) {
114 cerr << "Failed to repeat array. status=" << status << "." << endl;
115 return false;
116 }
117 if (input != actual_repeated) {
118 cerr << "Repeated version of array did not match" << endl;
119 cerr << "input.size()=" << input.size()
120 << " repeated.size()=" << actual_repeated.size() << endl;
121 return false;
122 }
123 std::reverse(input.begin(), input.end());
124 if (input != actual_reversed) {
125 cerr << "Reversed version of array did not match" << endl;
126 return false;
127 }
128 return true;
129}
130
131bool ConfirmReverseArrays(const sp<ITestService>& s) {
132 cout << "Confirming passing and returning arrays works." << endl;
133
134 if (!ReverseArray(s, &ITestService::ReverseBoolean,
135 {true, false, false}) ||
136 !ReverseArray(s, &ITestService::ReverseByte,
137 {int8_t{-128}, int8_t{0}, int8_t{127}}) ||
138 !ReverseArray(s, &ITestService::ReverseChar,
139 {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}}) ||
140 !ReverseArray(s, &ITestService::ReverseInt,
141 {1, 2, 3}) ||
142 !ReverseArray(s, &ITestService::ReverseLong,
143 {-1ll, 0ll, int64_t{1ll << 60}}) ||
144 !ReverseArray(s, &ITestService::ReverseFloat,
145 {-0.3f, -0.7f, 8.0f}) ||
146 !ReverseArray(s, &ITestService::ReverseDouble,
147 {1.0/3.0, 1.0/7.0, 42.0}) ||
148 !ReverseArray(s, &ITestService::ReverseString,
149 {String16{"f"}, String16{"a"}, String16{"b"}})) {
150 return false;
151 }
152
153 return true;
154}
155
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700156bool ConfirmReverseLists(const sp<ITestService>& s) {
157 cout << "Confirming passing and returning List<T> works." << endl;
158
159 if (!ReverseArray(s, &ITestService::ReverseStringList,
160 {String16{"f"}, String16{"a"}, String16{"b"}})) {
161 return false;
162 }
163
164 return true;
165}
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700166} // namespace
167
Brian Carlstromad3b8062015-10-21 08:54:48 -0700168int main(int /* argc */, char * /* argv */ []) {
Christopher Wiley521bb612015-10-22 11:40:30 -0700169 sp<ITestService> service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700170
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700171 if (!GetService(&service)) return 1;
172
Christopher Wileyd6130f22015-10-26 10:24:35 -0700173 if (!ConfirmPrimitiveRepeat(service)) return 1;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700174
Christopher Wiley8949f832015-10-27 15:32:03 -0700175 if (!ConfirmReverseArrays(service)) return 1;
176
Christopher Wiley56c9bf32015-10-30 10:41:12 -0700177 if (!ConfirmReverseLists(service)) return 1;
178
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700179 return 0;
180}