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