| 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" |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 26 | |
| 27 | // libutils: |
| 28 | using android::OK; |
| 29 | using android::sp; |
| 30 | using android::status_t; |
| 31 | using android::String16; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 32 | using android::String8; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 33 | |
| 34 | // libbinder: |
| 35 | using android::getService; |
| 36 | |
| 37 | // generated |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 38 | using android::aidl::tests::ITestService; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 39 | |
| 40 | using std::cerr; |
| 41 | using std::cout; |
| 42 | using std::endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 43 | using std::vector; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 44 | |
| 45 | namespace { |
| 46 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 47 | const char kServiceName[] = "android.aidl.tests.ITestService"; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 48 | |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 49 | bool GetService(sp<ITestService>* service) { |
| 50 | cout << "Retrieving test service binder" << endl; |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 51 | status_t status = getService(String16(kServiceName), service); |
| 52 | if (status != OK) { |
| 53 | cerr << "Failed to get service binder: '" << kServiceName |
| 54 | << "' status=" << status << endl; |
| 55 | return false; |
| 56 | } |
| 57 | return true; |
| 58 | } |
| 59 | |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 60 | template <typename T> |
| 61 | bool RepeatPrimitive(const sp<ITestService>& service, |
| 62 | status_t(ITestService::*func)(T, T*), |
| 63 | const T input) { |
| 64 | T reply; |
| 65 | status_t status = (*service.*func)(input, &reply); |
| 66 | if (status != OK || input != reply) { |
| 67 | cerr << "Failed to repeat primitive. status=" << status << "." << endl; |
| 68 | return false; |
| 69 | } |
| 70 | return true; |
| 71 | } |
| 72 | |
| 73 | bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) { |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 74 | cout << "Confirming passing and returning primitives works." << endl; |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 75 | |
| 76 | if (!RepeatPrimitive(s, &ITestService::RepeatBoolean, true) || |
| 77 | !RepeatPrimitive(s, &ITestService::RepeatByte, int8_t{-128}) || |
| 78 | !RepeatPrimitive(s, &ITestService::RepeatChar, char16_t{'A'}) || |
| 79 | !RepeatPrimitive(s, &ITestService::RepeatInt, int32_t{1 << 30}) || |
| 80 | !RepeatPrimitive(s, &ITestService::RepeatLong, int64_t{1ll << 60}) || |
| 81 | !RepeatPrimitive(s, &ITestService::RepeatFloat, float{1.0f/3.0f}) || |
| 82 | !RepeatPrimitive(s, &ITestService::RepeatDouble, double{1.0/3.0})) { |
| 83 | return false; |
| 84 | } |
| 85 | |
| 86 | vector<String16> inputs = { |
| 87 | String16("Deliver us from evil."), |
| 88 | String16(), |
| 89 | String16("\0\0", 2), |
| 90 | }; |
| 91 | for (const auto& input : inputs) { |
| 92 | String16 reply; |
| 93 | status_t status = s->RepeatString(input, &reply); |
| 94 | if (status != OK || input != reply) { |
| 95 | cerr << "Failed while requesting service to repeat String16=\"" |
| 96 | << String8(input).string() |
| 97 | << "\". Got status=" << status << endl; |
| 98 | return false; |
| 99 | } |
| 100 | } |
| 101 | return true; |
| 102 | } |
| 103 | |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 104 | template <typename T> |
| 105 | bool ReverseArray(const sp<ITestService>& service, |
| 106 | status_t(ITestService::*func)(const vector<T>&, |
| 107 | vector<T>*, |
| 108 | vector<T>*), |
| 109 | vector<T> input) { |
| 110 | vector<T> actual_reversed; |
| 111 | vector<T> actual_repeated; |
| 112 | status_t status = (*service.*func)(input, &actual_repeated, &actual_reversed); |
| 113 | if (status != OK) { |
| 114 | cerr << "Failed to repeat array. status=" << status << "." << endl; |
| 115 | return false; |
| 116 | } |
| 117 | if (input != actual_repeated) { |
| 118 | cerr << "Repeated version of array did not match" << endl; |
| 119 | cerr << "input.size()=" << input.size() |
| 120 | << " repeated.size()=" << actual_repeated.size() << endl; |
| 121 | return false; |
| 122 | } |
| 123 | std::reverse(input.begin(), input.end()); |
| 124 | if (input != actual_reversed) { |
| 125 | cerr << "Reversed version of array did not match" << endl; |
| 126 | return false; |
| 127 | } |
| 128 | return true; |
| 129 | } |
| 130 | |
| 131 | bool ConfirmReverseArrays(const sp<ITestService>& s) { |
| 132 | cout << "Confirming passing and returning arrays works." << endl; |
| 133 | |
| 134 | if (!ReverseArray(s, &ITestService::ReverseBoolean, |
| 135 | {true, false, false}) || |
| 136 | !ReverseArray(s, &ITestService::ReverseByte, |
| 137 | {int8_t{-128}, int8_t{0}, int8_t{127}}) || |
| 138 | !ReverseArray(s, &ITestService::ReverseChar, |
| 139 | {char16_t{'A'}, char16_t{'B'}, char16_t{'C'}}) || |
| 140 | !ReverseArray(s, &ITestService::ReverseInt, |
| 141 | {1, 2, 3}) || |
| 142 | !ReverseArray(s, &ITestService::ReverseLong, |
| 143 | {-1ll, 0ll, int64_t{1ll << 60}}) || |
| 144 | !ReverseArray(s, &ITestService::ReverseFloat, |
| 145 | {-0.3f, -0.7f, 8.0f}) || |
| 146 | !ReverseArray(s, &ITestService::ReverseDouble, |
| 147 | {1.0/3.0, 1.0/7.0, 42.0}) || |
| 148 | !ReverseArray(s, &ITestService::ReverseString, |
| 149 | {String16{"f"}, String16{"a"}, String16{"b"}})) { |
| 150 | return false; |
| 151 | } |
| 152 | |
| 153 | return true; |
| 154 | } |
| 155 | |
| Christopher Wiley | 56c9bf3 | 2015-10-30 10:41:12 -0700 | [diff] [blame^] | 156 | bool ConfirmReverseLists(const sp<ITestService>& s) { |
| 157 | cout << "Confirming passing and returning List<T> works." << endl; |
| 158 | |
| 159 | if (!ReverseArray(s, &ITestService::ReverseStringList, |
| 160 | {String16{"f"}, String16{"a"}, String16{"b"}})) { |
| 161 | return false; |
| 162 | } |
| 163 | |
| 164 | return true; |
| 165 | } |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 166 | } // namespace |
| 167 | |
| Brian Carlstrom | ad3b806 | 2015-10-21 08:54:48 -0700 | [diff] [blame] | 168 | int main(int /* argc */, char * /* argv */ []) { |
| Christopher Wiley | 521bb61 | 2015-10-22 11:40:30 -0700 | [diff] [blame] | 169 | sp<ITestService> service; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 170 | |
| Christopher Wiley | 33ad81e | 2015-10-21 14:41:11 -0700 | [diff] [blame] | 171 | if (!GetService(&service)) return 1; |
| 172 | |
| Christopher Wiley | d6130f2 | 2015-10-26 10:24:35 -0700 | [diff] [blame] | 173 | if (!ConfirmPrimitiveRepeat(service)) return 1; |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 174 | |
| Christopher Wiley | 8949f83 | 2015-10-27 15:32:03 -0700 | [diff] [blame] | 175 | if (!ConfirmReverseArrays(service)) return 1; |
| 176 | |
| Christopher Wiley | 56c9bf3 | 2015-10-30 10:41:12 -0700 | [diff] [blame^] | 177 | if (!ConfirmReverseLists(service)) return 1; |
| 178 | |
| Christopher Wiley | b5e698c | 2015-10-17 09:32:22 -0700 | [diff] [blame] | 179 | return 0; |
| 180 | } |