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