1 /*
2 * Copyright (C) 2016 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 "aidl_test_client_utf8_strings.h"
18
19 #include <android-base/logging.h>
20 #include <binder/Status.h>
21 #include <utils/StrongPointer.h>
22
23 #include <memory>
24 #include <string>
25 #include <vector>
26
27 #include "android/aidl/tests/ITestService.h"
28 #include "test_helpers.h"
29
30 // libutils:
31 using android::sp;
32
33 // libbinder:
34 using android::binder::Status;
35
36 // generated
37 using android::aidl::tests::ITestService;
38
39 using std::unique_ptr;
40 using std::string;
41 using std::vector;
42
43 namespace android {
44 namespace aidl {
45 namespace tests {
46 namespace client {
47
ConfirmUtf8InCppStringRepeat(const sp<ITestService> & s)48 bool ConfirmUtf8InCppStringRepeat(const sp<ITestService>& s) {
49 const vector<string> utf8_inputs = {
50 string("Deliver us from evil."),
51 string(),
52 string("\0\0", 2),
53 // Similarly, the utf8 encodings of the small letter yee and euro sign.
54 string("\xF0\x90\x90\xB7\xE2\x82\xAC"),
55 };
56 LOG(INFO) << "Confirming repeating utf8 strings works.";
57
58 for (const auto& input : utf8_inputs) {
59 string reply;
60 Status status = s->RepeatUtf8CppString(input, &reply);
61 if (!status.isOk() || input != reply) {
62 LOG(ERROR) << "Failed while requesting service to repeat utf8 string=\""
63 << input
64 << "\". Got status=" << status.toString8()
65 << " and output=" << reply;
66 return false;
67 }
68 }
69
70 unique_ptr<string> ret;
71 Status repeat_null_status = s->RepeatNullableUtf8CppString(nullptr, &ret);
72 if (!repeat_null_status.isOk() || ret) {
73 LOG(ERROR) << "RepeatNullableUtf8CppString(null) did not return null";
74 return false;
75 }
76
77 for (const auto& input : utf8_inputs) {
78 unique_ptr<string> reply;
79 Status status = s->RepeatNullableUtf8CppString(
80 unique_ptr<string>(new string(input)), &reply);
81 if (!status.isOk()) {
82 LOG(ERROR) << "Got status=" << status.toString8() << " while repeating "
83 "nullable utf8 string " << input;
84 return false;
85 }
86 if (!reply) {
87 LOG(ERROR) << "Got null reply while repeating nullable utf8 string "
88 << input;
89 return false;
90 }
91 if (input != *reply) {
92 LOG(ERROR) << "Failed while requesting service to repeat utf8 string=\""
93 << input
94 << "\". Got status=" << status.toString8()
95 << " and output=" << *reply;
96 return false;
97 }
98 }
99
100 return true;
101 }
102
ConfirmUtf8InCppStringArrayReverse(const sp<ITestService> & s)103 bool ConfirmUtf8InCppStringArrayReverse(const sp<ITestService>& s) {
104 LOG(INFO) << "Confirming passing and returning utf8 string arrays works.";
105 if (!ReverseArray(s, &ITestService::ReverseUtf8CppString,
106 {string{"a"}, string{}, string{"\xc3\xb8"}})) {
107 return false;
108 }
109
110 return true;
111 }
112
ConfirmUtf8InCppStringListReverse(const sp<ITestService> & s)113 bool ConfirmUtf8InCppStringListReverse(const sp<ITestService>& s) {
114 LOG(INFO) << "Confirming reversing a list of utf8 strings works";
115 unique_ptr<vector<unique_ptr<string>>> input, reversed, repeated;
116 Status status = s->ReverseUtf8CppStringList(input, &reversed, &repeated);
117 if (!status.isOk() || reversed || repeated) {
118 LOG(ERROR) << "Reversing null list of utf8 strings failed.";
119 return false;
120 }
121
122 input.reset(new vector<unique_ptr<string>>);
123 input->emplace_back(new string("Deliver us from evil."));
124 input->emplace_back(nullptr);
125 input->emplace_back(new string("\xF0\x90\x90\xB7\xE2\x82\xAC"));
126
127 status = s->ReverseUtf8CppStringList(input, &repeated, &reversed);
128 if (!status.isOk() || !reversed || !repeated) {
129 LOG(ERROR) << "Reversing list of utf8 strings failed.";
130 return false;
131 }
132 if (reversed->size() != input->size() || repeated->size() != input->size()) {
133 LOG(ERROR) << "Bad output sizes.";
134 return false;
135 }
136
137 for (size_t i = 0; i < input->size(); ++i) {
138 const string* input_str = (*input)[i].get();
139 const string* repeated_str = (*repeated)[i].get();
140 const string* reversed_str = (*reversed)[(reversed->size() - 1) - i].get();
141 if (!input_str) {
142 if(repeated_str || reversed_str) {
143 LOG(ERROR) << "Expected null values, but got non-null.";
144 return false;
145 }
146 // 3 nullptrs to strings. No need to compare values.
147 continue;
148 }
149 if (!repeated_str || !reversed_str) {
150 LOG(ERROR) << "Expected non-null values, but got null.";
151 return false;
152 }
153 if (*input_str != *repeated_str || *input_str != *reversed_str) {
154 LOG(ERROR) << "Expected '" << *input_str << "' but got "
155 << "repeated='" << *repeated_str << "' and "
156 << "reversed='" << *reversed_str;
157 return false;
158 }
159 }
160 return true;
161 }
162
163
164 } // namespace client
165 } // namespace tests
166 } // namespace aidl
167 } // namespace android
168