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
113 namespace {
114
ConfirmUtf8InCppStringCollectionReverse(const sp<ITestService> & s,Status (ITestService::* m)(const unique_ptr<vector<unique_ptr<string>>> &,unique_ptr<vector<unique_ptr<string>>> *,unique_ptr<vector<unique_ptr<string>>> *))115 bool ConfirmUtf8InCppStringCollectionReverse(const sp<ITestService>& s,
116 Status (ITestService::*m)(const unique_ptr<vector<unique_ptr<string>>>&,
117 unique_ptr<vector<unique_ptr<string>>>*,
118 unique_ptr<vector<unique_ptr<string>>>*)) {
119 (void)m;
120 LOG(INFO) << "Confirming reversing a list of utf8 strings works";
121 unique_ptr<vector<unique_ptr<string>>> input, reversed, repeated;
122 Status status = (s.get()->*m)(input, &reversed, &repeated);
123 if (!status.isOk() || reversed || repeated) {
124 LOG(ERROR) << "Reversing null list of utf8 strings failed.";
125 return false;
126 }
127
128 input.reset(new vector<unique_ptr<string>>);
129 input->emplace_back(new string("Deliver us from evil."));
130 input->emplace_back(nullptr);
131 input->emplace_back(new string("\xF0\x90\x90\xB7\xE2\x82\xAC"));
132
133 status = s->ReverseUtf8CppStringList(input, &repeated, &reversed);
134 if (!status.isOk() || !reversed || !repeated) {
135 LOG(ERROR) << "Reversing list of utf8 strings failed.";
136 return false;
137 }
138 if (reversed->size() != input->size() || repeated->size() != input->size()) {
139 LOG(ERROR) << "Bad output sizes.";
140 return false;
141 }
142
143 for (size_t i = 0; i < input->size(); ++i) {
144 const string* input_str = (*input)[i].get();
145 const string* repeated_str = (*repeated)[i].get();
146 const string* reversed_str = (*reversed)[(reversed->size() - 1) - i].get();
147 if (!input_str) {
148 if(repeated_str || reversed_str) {
149 LOG(ERROR) << "Expected null values, but got non-null.";
150 return false;
151 }
152 // 3 nullptrs to strings. No need to compare values.
153 continue;
154 }
155 if (!repeated_str || !reversed_str) {
156 LOG(ERROR) << "Expected non-null values, but got null.";
157 return false;
158 }
159 if (*input_str != *repeated_str || *input_str != *reversed_str) {
160 LOG(ERROR) << "Expected '" << *input_str << "' but got "
161 << "repeated='" << *repeated_str << "' and "
162 << "reversed='" << *reversed_str;
163 return false;
164 }
165 }
166 return true;
167 }
168
169 } // namespace
170
ConfirmUtf8InCppStringListReverse(const sp<ITestService> & s)171 bool ConfirmUtf8InCppStringListReverse(const sp<ITestService>& s) {
172 return ConfirmUtf8InCppStringCollectionReverse(s,
173 &ITestService::ReverseUtf8CppStringList);
174 }
175
ConfirmUtf8InCppNullableStringArrayReverse(const sp<ITestService> & s)176 bool ConfirmUtf8InCppNullableStringArrayReverse(const sp<ITestService>& s) {
177 return ConfirmUtf8InCppStringCollectionReverse(s,
178 &ITestService::ReverseNullableUtf8CppString);
179 }
180
181 } // namespace client
182 } // namespace tests
183 } // namespace aidl
184 } // namespace android
185