| 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 | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 43 | |
| 44 | using std::cerr; |
| 45 | using std::cout; |
| 46 | using std::endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 47 | using std::vector; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 48 | |
| 49 | namespace { |
| 50 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 51 | const char kServiceName[] = "android.aidl.tests.ITestService"; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 52 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 53 | bool GetService(sp<ITestService>* service) { |
| 54 | cout << "Retrieving test service binder" << endl; |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 55 | 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 Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 64 | template <typename T> |
| 65 | bool RepeatPrimitive(const sp<ITestService>& service, |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 66 | Status(ITestService::*func)(T, T*), |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 67 | const T input) { |
| 68 | T reply; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 69 | Status status = (*service.*func)(input, &reply); |
| 70 | if (!status.isOk() || input != reply) { |
| 71 | cerr << "Failed to repeat primitive. status=" << status.toString8() |
| 72 | << "." << endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 73 | return false; |
| 74 | } |
| 75 | return true; |
| 76 | } |
| 77 | |
| 78 | bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 79 | cout << "Confirming passing and returning primitives works." << endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 80 | |
| 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 Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 98 | Status status = s->RepeatString(input, &reply); |
| 99 | if (!status.isOk() || input != reply) { |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 100 | cerr << "Failed while requesting service to repeat String16=\"" |
| 101 | << String8(input).string() |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 102 | << "\". Got status=" << status.toString8() << endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 103 | return false; |
| 104 | } |
| 105 | } |
| 106 | return true; |
| 107 | } |
| 108 | |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 109 | template <typename T> |
| 110 | bool ReverseArray(const sp<ITestService>& service, |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 111 | Status(ITestService::*func)(const vector<T>&, |
| 112 | vector<T>*, |
| 113 | vector<T>*), |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 114 | vector<T> input) { |
| 115 | vector<T> actual_reversed; |
| 116 | vector<T> actual_repeated; |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 117 | 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 Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 121 | 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 | |
| 137 | bool 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 Wiley | 56c9bf3 | 2015-10-30 10:41:12 -0700 | [diff] [blame] | 162 | bool 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 Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 172 | |
| 173 | bool ConfirmReverseBinderLists(const sp<ITestService>& s) { |
| Christopher Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 174 | Status status; |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 175 | 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 Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 189 | if (!status.isOk()) { |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 190 | 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 Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 201 | if (!status.isOk()) { |
| 202 | cerr << "Failed to reverse named callback list." << endl; |
| 203 | } |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 204 | |
| 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 Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 221 | if (!status.isOk()) { |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 222 | 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 Wiley | 433c8bb | 2015-11-12 14:20:46 -0800 | [diff] [blame] | 238 | if (!status.isOk()) { |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 239 | 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 Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 251 | } // namespace |
| 252 | |
| Brian Carlstrom | ad3b806 | 2015-10-21 08:54:48 -0700 | [diff] [blame] | 253 | int main(int /* argc */, char * /* argv */ []) { |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 254 | sp<ITestService> service; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 255 | |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 256 | if (!GetService(&service)) return 1; |
| 257 | |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 258 | if (!ConfirmPrimitiveRepeat(service)) return 1; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 259 | |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 260 | if (!ConfirmReverseArrays(service)) return 1; |
| 261 | |
| Christopher Wiley | 56c9bf3 | 2015-10-30 10:41:12 -0700 | [diff] [blame] | 262 | if (!ConfirmReverseLists(service)) return 1; |
| 263 | |
| Casey Dahlin | 7ecd69f | 2015-11-03 13:52:38 -0800 | [diff] [blame] | 264 | if (!ConfirmReverseBinderLists(service)) return 1; |
| 265 | |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 266 | return 0; |
| 267 | } |