1 /*
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 "aidl_test_client_primitives.h"
18
19 #include <iostream>
20 #include <vector>
21
22 #include <utils/String16.h>
23 #include <utils/String8.h>
24 #include <binder/Value.h>
25 #include <binder/Map.h>
26
27 #include "android/aidl/tests/INamedCallback.h"
28
29 #include "test_helpers.h"
30
31 // libutils:
32 using android::sp;
33 using android::String16;
34 using android::String8;
35
36 // libbinder:
37 using android::binder::Status;
38 using android::binder::Value;
39 using android::binder::Map;
40
41 // generated
42 using android::aidl::tests::ITestService;
43 using android::aidl::tests::INamedCallback;
44
45 using std::cerr;
46 using std::cout;
47 using std::endl;
48 using std::vector;
49
50 namespace android {
51 namespace aidl {
52 namespace tests {
53 namespace client {
54
ConfirmPrimitiveRepeat(const sp<ITestService> & s)55 bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) {
56 cout << "Confirming passing and returning primitives works." << endl;
57
58 Map test_map;
59 test_map["first_val"] = int8_t{-128};
60 test_map["second_val"] = int32_t{1 << 30};
61 test_map["third_val"] = String16("OHAI");
62
63 if (!RepeatPrimitive(s, &ITestService::RepeatBoolean, true) ||
64 !RepeatPrimitive(s, &ITestService::RepeatByte, int8_t{-128}) ||
65 !RepeatPrimitive(s, &ITestService::RepeatChar, char16_t{'A'}) ||
66 !RepeatPrimitive(s, &ITestService::RepeatInt, int32_t{1 << 30}) ||
67 !RepeatPrimitive(s, &ITestService::RepeatLong, int64_t{1ll << 60}) ||
68 !RepeatPrimitive(s, &ITestService::RepeatFloat, float{1.0f/3.0f}) ||
69 !RepeatPrimitive(s, &ITestService::RepeatDouble, double{1.0/3.0}) ||
70 !RepeatPrimitive(s, &ITestService::RepeatMap, test_map) ||
71 !RepeatPrimitive(
72 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT) ||
73 !RepeatPrimitive(
74 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT2) ||
75 !RepeatPrimitive(
76 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT3) ||
77 !RepeatPrimitive(
78 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT4) ||
79 !RepeatPrimitive(
80 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT5) ||
81 !RepeatPrimitive(
82 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT6) ||
83 !RepeatPrimitive(
84 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT7) ||
85 !RepeatPrimitive(
86 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT8) ||
87 !RepeatPrimitive(
88 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT9) ||
89 !RepeatPrimitive(
90 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT10) ||
91 !RepeatPrimitive(
92 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT11) ||
93 !RepeatPrimitive(
94 s, &ITestService::RepeatInt, ITestService::TEST_CONSTANT12)
95 ) {
96 return false;
97 }
98
99 vector<String16> inputs = {
100 String16("Deliver us from evil."),
101 String16(),
102 String16("\0\0", 2),
103 // This is actually two unicode code points:
104 // U+10437: The 'small letter yee' character in the deseret alphabet
105 // U+20AC: A euro sign
106 String16("\xD8\x01\xDC\x37\x20\xAC"),
107 ITestService::STRING_TEST_CONSTANT(),
108 ITestService::STRING_TEST_CONSTANT2(),
109 };
110 for (const auto& input : inputs) {
111 String16 reply;
112 Status status = s->RepeatString(input, &reply);
113 if (!status.isOk() || input != reply) {
114 cerr << "Failed while requesting service to repeat String16=\""
115 << String8(input).string()
116 << "\". Got status=" << status.toString8() << endl;
117 return false;
118 }
119 }
120 return true;
121 }
122
ConfirmReverseArrays(const sp<ITestService> & s)123 bool ConfirmReverseArrays(const sp<ITestService>& s) {
124 cout << "Confirming passing and returning arrays works." << endl;
125
126 if (!ReverseArray(s, &ITestService::ReverseBoolean,
127 {true, false, false}) ||
128 !ReverseArray(s, &ITestService::ReverseByte,
129 {uint8_t{255}, uint8_t{0}, uint8_t{127}}) ||
130 !ReverseArray(s, &ITestService::ReverseChar,
131 {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}}) ||
132 !ReverseArray(s, &ITestService::ReverseInt,
133 {1, 2, 3}) ||
134 !ReverseArray(s, &ITestService::ReverseLong,
135 {-1ll, 0ll, int64_t{1ll << 60}}) ||
136 !ReverseArray(s, &ITestService::ReverseFloat,
137 {-0.3f, -0.7f, 8.0f}) ||
138 !ReverseArray(s, &ITestService::ReverseDouble,
139 {1.0/3.0, 1.0/7.0, 42.0}) ||
140 !ReverseArray(s, &ITestService::ReverseString,
141 {String16{"f"}, String16{"a"}, String16{"b"}})) {
142 return false;
143 }
144
145 return true;
146 }
147
ConfirmReverseLists(const sp<ITestService> & s)148 bool ConfirmReverseLists(const sp<ITestService>& s) {
149 cout << "Confirming passing and returning List<T> works." << endl;
150
151 if (!ReverseArray(s, &ITestService::ReverseStringList,
152 {String16{"f"}, String16{"a"}, String16{"b"}})) {
153 return false;
154 }
155
156 return true;
157 }
158
ConfirmReverseBinderLists(const sp<ITestService> & s)159 bool ConfirmReverseBinderLists(const sp<ITestService>& s) {
160 Status status;
161 cout << "Confirming passing and returning List<T> works with binders." << endl;
162
163 vector<String16> names = {
164 String16{"Larry"},
165 String16{"Curly"},
166 String16{"Moe"}
167 };
168
169 vector<sp<IBinder>> input;
170
171 for (int i = 0; i < 3; i++) {
172 sp<INamedCallback> got;
173
174 status = s->GetOtherTestService(names[i], &got);
175 if (!status.isOk()) {
176 cerr << "Could not retrieve service for test." << endl;
177 return false;
178 }
179
180 input.push_back(INamedCallback::asBinder(got));
181 }
182
183 vector<sp<IBinder>> output;
184 vector<sp<IBinder>> reversed;
185
186 status = s->ReverseNamedCallbackList(input, &output, &reversed);
187 if (!status.isOk()) {
188 cerr << "Failed to reverse named callback list." << endl;
189 }
190
191 if (output.size() != 3) {
192 cerr << "ReverseNamedCallbackList gave repetition with wrong length." << endl;
193 return false;
194 }
195
196 if (reversed.size() != 3) {
197 cerr << "ReverseNamedCallbackList gave reversal with wrong length." << endl;
198 return false;
199 }
200
201 for (int i = 0; i < 3; i++) {
202 String16 ret;
203 sp<INamedCallback> named_callback =
204 android::interface_cast<INamedCallback>(output[i]);
205 status = named_callback->GetName(&ret);
206
207 if (!status.isOk()) {
208 cerr << "Could not query INamedCallback from output" << endl;
209 return false;
210 }
211
212 if (ret != names[i]) {
213 cerr << "Output had wrong INamedCallback" << endl;
214 return false;
215 }
216 }
217
218 for (int i = 0; i < 3; i++) {
219 String16 ret;
220 sp<INamedCallback> named_callback =
221 android::interface_cast<INamedCallback>(reversed[i]);
222 status = named_callback->GetName(&ret);
223
224 if (!status.isOk()) {
225 cerr << "Could not query INamedCallback from reversed output" << endl;
226 return false;
227 }
228
229 if (ret != names[2 - i]) {
230 cerr << "Reversed output had wrong INamedCallback" << endl;
231 return false;
232 }
233 }
234
235 return true;
236 }
237
238 } // namespace client
239 } // namespace tests
240 } // namespace aidl
241 } // namespace android
242