| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 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 <iostream> |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 18 | #include <vector> |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 19 | |
| 20 | #include <binder/IServiceManager.h> |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 21 | #include <utils/String8.h> |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 22 | #include <utils/String16.h> |
| 23 | #include <utils/StrongPointer.h> |
| 24 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 25 | #include "android/aidl/tests/ITestService.h" |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 26 | #include "android/aidl/tests/INamedCallback.h" |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 27 | |
| 28 | // libutils: |
| 29 | using android::OK; |
| 30 | using android::sp; |
| 31 | using android::status_t; |
| 32 | using android::String16; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 33 | using android::String8; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 34 | |
| 35 | // libbinder: |
| 36 | using android::getService; |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 37 | using android::IBinder; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 38 | using android::binder::Status; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 39 | |
| 40 | // generated |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 41 | using android::aidl::tests::ITestService; |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 42 | using android::aidl::tests::INamedCallback; |
| Christopher Wiley | 95d44b0 | 2015-11-19 07:11:30 -0800 | [diff] [blame] | 43 | using android::aidl::tests::SimpleParcelable; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 44 | |
| 45 | using std::cerr; |
| 46 | using std::cout; |
| 47 | using std::endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 48 | using std::vector; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 49 | |
| 50 | namespace { |
| 51 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 52 | const char kServiceName[] = "android.aidl.tests.ITestService"; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 53 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 54 | bool GetService(sp<ITestService>* service) { |
| 55 | cout << "Retrieving test service binder" << endl; |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 56 | status_t status = getService(String16(kServiceName), service); |
| 57 | if (status != OK) { |
| 58 | cerr << "Failed to get service binder: '" << kServiceName |
| 59 | << "' status=" << status << endl; |
| 60 | return false; |
| 61 | } |
| 62 | return true; |
| 63 | } |
| 64 | |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 65 | template <typename T> |
| 66 | bool RepeatPrimitive(const sp<ITestService>& service, |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 67 | Status(ITestService::*func)(T, T*), |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 68 | const T input) { |
| 69 | T reply; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 70 | Status status = (*service.*func)(input, &reply); |
| 71 | if (!status.isOk() || input != reply) { |
| 72 | cerr << "Failed to repeat primitive. status=" << status.toString8() |
| 73 | << "." << endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 74 | return false; |
| 75 | } |
| 76 | return true; |
| 77 | } |
| 78 | |
| 79 | bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 80 | cout << "Confirming passing and returning primitives works." << endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 81 | |
| 82 | if (!RepeatPrimitive(s, &ITestService::RepeatBoolean, true) || |
| 83 | !RepeatPrimitive(s, &ITestService::RepeatByte, int8_t{-128}) || |
| 84 | !RepeatPrimitive(s, &ITestService::RepeatChar, char16_t{'A'}) || |
| 85 | !RepeatPrimitive(s, &ITestService::RepeatInt, int32_t{1 << 30}) || |
| 86 | !RepeatPrimitive(s, &ITestService::RepeatLong, int64_t{1ll << 60}) || |
| 87 | !RepeatPrimitive(s, &ITestService::RepeatFloat, float{1.0f/3.0f}) || |
| 88 | !RepeatPrimitive(s, &ITestService::RepeatDouble, double{1.0/3.0})) { |
| 89 | return false; |
| 90 | } |
| 91 | |
| 92 | vector<String16> inputs = { |
| 93 | String16("Deliver us from evil."), |
| 94 | String16(), |
| 95 | String16("\0\0", 2), |
| 96 | }; |
| 97 | for (const auto& input : inputs) { |
| 98 | String16 reply; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 99 | Status status = s->RepeatString(input, &reply); |
| 100 | if (!status.isOk() || input != reply) { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 101 | cerr << "Failed while requesting service to repeat String16=\"" |
| 102 | << String8(input).string() |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 103 | << "\". Got status=" << status.toString8() << endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 104 | return false; |
| 105 | } |
| 106 | } |
| 107 | return true; |
| 108 | } |
| 109 | |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 110 | template <typename T> |
| 111 | bool ReverseArray(const sp<ITestService>& service, |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 112 | Status(ITestService::*func)(const vector<T>&, |
| 113 | vector<T>*, |
| 114 | vector<T>*), |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 115 | vector<T> input) { |
| 116 | vector<T> actual_reversed; |
| 117 | vector<T> actual_repeated; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 118 | Status status = (*service.*func)(input, &actual_repeated, &actual_reversed); |
| 119 | if (!status.isOk()) { |
| 120 | cerr << "Failed to repeat array. status=" << status.toString8() << "." |
| 121 | << endl; |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 122 | return false; |
| 123 | } |
| 124 | if (input != actual_repeated) { |
| 125 | cerr << "Repeated version of array did not match" << endl; |
| 126 | cerr << "input.size()=" << input.size() |
| 127 | << " repeated.size()=" << actual_repeated.size() << endl; |
| 128 | return false; |
| 129 | } |
| 130 | std::reverse(input.begin(), input.end()); |
| 131 | if (input != actual_reversed) { |
| 132 | cerr << "Reversed version of array did not match" << endl; |
| 133 | return false; |
| 134 | } |
| 135 | return true; |
| 136 | } |
| 137 | |
| 138 | bool ConfirmReverseArrays(const sp<ITestService>& s) { |
| 139 | cout << "Confirming passing and returning arrays works." << endl; |
| 140 | |
| 141 | if (!ReverseArray(s, &ITestService::ReverseBoolean, |
| 142 | {true, false, false}) || |
| 143 | !ReverseArray(s, &ITestService::ReverseByte, |
| 144 | {int8_t{-128}, int8_t{0}, int8_t{127}}) || |
| 145 | !ReverseArray(s, &ITestService::ReverseChar, |
| 146 | {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}}) || |
| 147 | !ReverseArray(s, &ITestService::ReverseInt, |
| 148 | {1, 2, 3}) || |
| 149 | !ReverseArray(s, &ITestService::ReverseLong, |
| 150 | {-1ll, 0ll, int64_t{1ll << 60}}) || |
| 151 | !ReverseArray(s, &ITestService::ReverseFloat, |
| 152 | {-0.3f, -0.7f, 8.0f}) || |
| 153 | !ReverseArray(s, &ITestService::ReverseDouble, |
| 154 | {1.0/3.0, 1.0/7.0, 42.0}) || |
| 155 | !ReverseArray(s, &ITestService::ReverseString, |
| 156 | {String16{"f"}, String16{"a"}, String16{"b"}})) { |
| 157 | return false; |
| 158 | } |
| 159 | |
| 160 | return true; |
| 161 | } |
| 162 | |
| Christopher Wiley | 56c9bf3 | 2015-10-30 10:41:12 -0700 | [diff] [blame] | 163 | bool ConfirmReverseLists(const sp<ITestService>& s) { |
| 164 | cout << "Confirming passing and returning List<T> works." << endl; |
| 165 | |
| 166 | if (!ReverseArray(s, &ITestService::ReverseStringList, |
| 167 | {String16{"f"}, String16{"a"}, String16{"b"}})) { |
| 168 | return false; |
| 169 | } |
| 170 | |
| 171 | return true; |
| 172 | } |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 173 | |
| Christopher Wiley | 95d44b0 | 2015-11-19 07:11:30 -0800 | [diff] [blame] | 174 | bool ConfirmParcelables(const sp<ITestService>& s) { |
| 175 | cout << "Confirming passing and returning Parcelables works." << endl; |
| 176 | |
| 177 | SimpleParcelable input("Booya", 42); |
| 178 | SimpleParcelable out_param, returned; |
| 179 | Status status = s->RepeatParcelable(input, &out_param, &returned); |
| 180 | if (!status.isOk()) { |
| 181 | cout << "Binder call failed." << endl; |
| 182 | return false; |
| 183 | } |
| 184 | if (input != out_param || input != returned) { |
| 185 | cout << "Failed to repeat parcelables." << endl; |
| 186 | return false; |
| 187 | } |
| 188 | |
| 189 | cout << "Attempting to reverse an array of parcelables." << endl; |
| 190 | const vector<SimpleParcelable> original{SimpleParcelable("first", 0), |
| 191 | SimpleParcelable("second", 1), |
| 192 | SimpleParcelable("third", 2)}; |
| 193 | vector<SimpleParcelable> repeated; |
| 194 | vector<SimpleParcelable> reversed; |
| 195 | status = s->ReverseParcelables(original, &repeated, &reversed); |
| 196 | if (!status.isOk()) { |
| 197 | cout << "Binder call failed." << endl; |
| 198 | return false; |
| 199 | } |
| 200 | std::reverse(reversed.begin(), reversed.end()); |
| 201 | if (repeated != original || reversed != original) { |
| 202 | cout << "Failed to reverse an array of parcelables." << endl; |
| 203 | return false; |
| 204 | } |
| 205 | |
| 206 | return true; |
| 207 | } |
| 208 | |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 209 | bool ConfirmReverseBinderLists(const sp<ITestService>& s) { |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 210 | Status status; |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 211 | cout << "Confirming passing and returning List<T> works with binders." << endl; |
| 212 | |
| 213 | vector<String16> names = { |
| 214 | String16{"Larry"}, |
| 215 | String16{"Curly"}, |
| 216 | String16{"Moe"} |
| 217 | }; |
| 218 | |
| 219 | vector<sp<IBinder>> input; |
| 220 | |
| 221 | for (int i = 0; i < 3; i++) { |
| 222 | sp<INamedCallback> got; |
| 223 | |
| 224 | status = s->GetOtherTestService(names[i], &got); |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 225 | if (!status.isOk()) { |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 226 | cerr << "Could not retrieve service for test." << endl; |
| 227 | return false; |
| 228 | } |
| 229 | |
| 230 | input.push_back(INamedCallback::asBinder(got)); |
| 231 | } |
| 232 | |
| 233 | vector<sp<IBinder>> output; |
| 234 | vector<sp<IBinder>> reversed; |
| 235 | |
| 236 | status = s->ReverseNamedCallbackList(input, &output, &reversed); |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 237 | if (!status.isOk()) { |
| 238 | cerr << "Failed to reverse named callback list." << endl; |
| 239 | } |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 240 | |
| 241 | if (output.size() != 3) { |
| 242 | cerr << "ReverseNamedCallbackList gave repetition with wrong length." << endl; |
| 243 | return false; |
| 244 | } |
| 245 | |
| 246 | if (reversed.size() != 3) { |
| 247 | cerr << "ReverseNamedCallbackList gave reversal with wrong length." << endl; |
| 248 | return false; |
| 249 | } |
| 250 | |
| 251 | for (int i = 0; i < 3; i++) { |
| 252 | String16 ret; |
| 253 | sp<INamedCallback> named_callback = |
| 254 | android::interface_cast<INamedCallback>(output[i]); |
| 255 | status = named_callback->GetName(&ret); |
| 256 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 257 | if (!status.isOk()) { |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 258 | cerr << "Could not query INamedCallback from output" << endl; |
| 259 | return false; |
| 260 | } |
| 261 | |
| 262 | if (ret != names[i]) { |
| 263 | cerr << "Output had wrong INamedCallback" << endl; |
| 264 | return false; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | for (int i = 0; i < 3; i++) { |
| 269 | String16 ret; |
| 270 | sp<INamedCallback> named_callback = |
| 271 | android::interface_cast<INamedCallback>(reversed[i]); |
| 272 | status = named_callback->GetName(&ret); |
| 273 | |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 274 | if (!status.isOk()) { |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 275 | cerr << "Could not query INamedCallback from reversed output" << endl; |
| 276 | return false; |
| 277 | } |
| 278 | |
| 279 | if (ret != names[2 - i]) { |
| 280 | cerr << "Reversed output had wrong INamedCallback" << endl; |
| 281 | return false; |
| 282 | } |
| 283 | } |
| 284 | |
| 285 | return true; |
| 286 | } |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 287 | } // namespace |
| 288 | |
| Brian Carlstrom | ad3b806 | 2015-10-21 08:54:48 -0700 | [diff] [blame] | 289 | int main(int /* argc */, char * /* argv */ []) { |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 290 | sp<ITestService> service; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 291 | |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 292 | if (!GetService(&service)) return 1; |
| 293 | |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 294 | if (!ConfirmPrimitiveRepeat(service)) return 1; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 295 | |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 296 | if (!ConfirmReverseArrays(service)) return 1; |
| 297 | |
| Christopher Wiley | 56c9bf3 | 2015-10-30 10:41:12 -0700 | [diff] [blame] | 298 | if (!ConfirmReverseLists(service)) return 1; |
| 299 | |
| Christopher Wiley | 95d44b0 | 2015-11-19 07:11:30 -0800 | [diff] [blame] | 300 | if (!ConfirmParcelables(service)) return 1; |
| 301 | |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 302 | if (!ConfirmReverseBinderLists(service)) return 1; |
| 303 | |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 304 | return 0; |
| 305 | } |