blob: 89cb6b86d14564dc8e3b06f718f6122013602ad5 [file] [log] [blame]
Christopher Wileyb5e698c2015-10-17 09:32:22 -07001/*
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 Wileyd6130f22015-10-26 10:24:35 -070018#include <vector>
Christopher Wileyb5e698c2015-10-17 09:32:22 -070019
20#include <binder/IServiceManager.h>
Christopher Wileyd6130f22015-10-26 10:24:35 -070021#include <utils/String8.h>
Christopher Wileyb5e698c2015-10-17 09:32:22 -070022#include <utils/String16.h>
23#include <utils/StrongPointer.h>
24
Christopher Wiley521bb612015-10-22 11:40:30 -070025#include "android/aidl/tests/ITestService.h"
Christopher Wileyb5e698c2015-10-17 09:32:22 -070026
27// libutils:
28using android::OK;
29using android::sp;
30using android::status_t;
31using android::String16;
Christopher Wileyd6130f22015-10-26 10:24:35 -070032using android::String8;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070033
34// libbinder:
35using android::getService;
36
37// generated
Christopher Wiley521bb612015-10-22 11:40:30 -070038using android::aidl::tests::ITestService;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070039
40using std::cerr;
41using std::cout;
42using std::endl;
Christopher Wileyd6130f22015-10-26 10:24:35 -070043using std::vector;
Christopher Wileyb5e698c2015-10-17 09:32:22 -070044
45namespace {
46
Christopher Wiley521bb612015-10-22 11:40:30 -070047const char kServiceName[] = "android.aidl.tests.ITestService";
Christopher Wileyb5e698c2015-10-17 09:32:22 -070048
Christopher Wiley521bb612015-10-22 11:40:30 -070049bool GetService(sp<ITestService>* service) {
50 cout << "Retrieving test service binder" << endl;
Christopher Wiley33ad81e2015-10-21 14:41:11 -070051 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 Wileyd6130f22015-10-26 10:24:35 -070060template <typename T>
61bool 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
73bool ConfirmPrimitiveRepeat(const sp<ITestService>& s) {
74 cout << "Confirming passing and returning primitives." << endl;
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 Wileyb5e698c2015-10-17 09:32:22 -0700104} // namespace
105
Brian Carlstromad3b8062015-10-21 08:54:48 -0700106int main(int /* argc */, char * /* argv */ []) {
Christopher Wiley521bb612015-10-22 11:40:30 -0700107 sp<ITestService> service;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700108
Christopher Wiley33ad81e2015-10-21 14:41:11 -0700109 if (!GetService(&service)) return 1;
110
Christopher Wileyd6130f22015-10-26 10:24:35 -0700111 if (!ConfirmPrimitiveRepeat(service)) return 1;
Christopher Wileyb5e698c2015-10-17 09:32:22 -0700112
113 return 0;
114}