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 <optional>
18 #include <vector>
19 
20 #include <utils/String16.h>
21 #include <utils/String8.h>
22 
23 #include "aidl_test_client.h"
24 #include "gmock/gmock.h"
25 
26 using android::BBinder;
27 using android::IBinder;
28 using android::sp;
29 using android::String16;
30 using android::String8;
31 using android::binder::Status;
32 
33 using android::aidl::tests::BackendType;
34 using android::aidl::tests::ByteEnum;
35 using android::aidl::tests::INamedCallback;
36 using android::aidl::tests::IntEnum;
37 using android::aidl::tests::ITestService;
38 using android::aidl::tests::LongEnum;
39 using android::aidl::tests::SimpleParcelable;
40 using android::aidl::tests::StructuredParcelable;
41 
42 using testing::Eq;
43 using testing::Ne;
44 
45 struct RepeatNullableTest : public AidlTest {
46   template <typename T>
DoTestRepeatNullableTest47   void DoTest(Status (ITestService::*func)(const std::optional<T>&, std::optional<T>*),
48               std::optional<T> input) {
49     std::optional<T> output;
50     auto status = (*service.*func)(input, &output);
51     ASSERT_TRUE(status.isOk());
52     ASSERT_TRUE(output.has_value());
53     ASSERT_THAT(*output, Eq(*input));
54 
55     input.reset();
56     status = (*service.*func)(input, &output);
57     ASSERT_TRUE(status.isOk());
58     ASSERT_FALSE(output.has_value());
59   }
60 };
61 
TEST_F(RepeatNullableTest,intArray)62 TEST_F(RepeatNullableTest, intArray) {
63   DoTest(&ITestService::RepeatNullableIntArray, std::make_optional(std::vector<int32_t>{1, 2, 3}));
64 }
65 
TEST_F(RepeatNullableTest,byteEnumArray)66 TEST_F(RepeatNullableTest, byteEnumArray) {
67   DoTest(&ITestService::RepeatNullableByteEnumArray,
68          std::make_optional(std::vector<ByteEnum>{ByteEnum::FOO, ByteEnum::BAR}));
69 }
70 
TEST_F(RepeatNullableTest,intEnumArray)71 TEST_F(RepeatNullableTest, intEnumArray) {
72   DoTest(&ITestService::RepeatNullableIntEnumArray,
73          std::make_optional(std::vector<IntEnum>{IntEnum::FOO, IntEnum::BAR}));
74 }
75 
TEST_F(RepeatNullableTest,longEnumArray)76 TEST_F(RepeatNullableTest, longEnumArray) {
77   DoTest(&ITestService::RepeatNullableLongEnumArray,
78          std::make_optional(std::vector<LongEnum>{LongEnum::FOO, LongEnum::BAR}));
79 }
80 
TEST_F(RepeatNullableTest,string)81 TEST_F(RepeatNullableTest, string) {
82   DoTest(&ITestService::RepeatNullableString, std::optional<String16>("Blooob"));
83 }
84 
TEST_F(RepeatNullableTest,stringArray)85 TEST_F(RepeatNullableTest, stringArray) {
86   std::vector<std::optional<String16>> input;
87   input.push_back(String16("Wat"));
88   input.push_back(String16("Blooob"));
89   input.push_back(String16("Wat"));
90   input.push_back(std::nullopt);
91   input.push_back(String16("YEAH"));
92   input.push_back(String16("OKAAAAY"));
93 
94   DoTest(&ITestService::RepeatNullableStringList, std::make_optional(input));
95 }
96 
TEST_F(RepeatNullableTest,parcelable)97 TEST_F(RepeatNullableTest, parcelable) {
98   auto input = std::make_optional<ITestService::Empty>();
99   std::optional<ITestService::Empty> output;
100   auto status = service->RepeatNullableParcelable(input, &output);
101   ASSERT_TRUE(status.isOk());
102   ASSERT_TRUE(output.has_value());
103   ASSERT_THAT(*output, Eq(*input));
104 
105   input.reset();
106   status = service->RepeatNullableParcelable(input, &output);
107   ASSERT_TRUE(status.isOk());
108   ASSERT_FALSE(output.has_value());
109 }
110 
TEST_F(RepeatNullableTest,parcelableArray)111 TEST_F(RepeatNullableTest, parcelableArray) {
112   std::vector<std::optional<ITestService::Empty>> input;
113   input.push_back(ITestService::Empty());
114   input.push_back(std::nullopt);
115   DoTest(&ITestService::RepeatNullableParcelableArray, std::make_optional(input));
116 }
117 
TEST_F(RepeatNullableTest,parcelableList)118 TEST_F(RepeatNullableTest, parcelableList) {
119   std::vector<std::optional<ITestService::Empty>> input;
120   input.push_back(ITestService::Empty());
121   input.push_back(std::nullopt);
122   DoTest(&ITestService::RepeatNullableParcelableList, std::make_optional(input));
123 }
124 
TEST_F(AidlTest,nullBinder)125 TEST_F(AidlTest, nullBinder) {
126   auto status = service->TakesAnIBinder(nullptr);
127 
128   if (backend == BackendType::JAVA) {
129     ASSERT_TRUE(status.isOk()) << status;
130   } else if (backend == BackendType::NDK) {
131     ASSERT_THAT(status.transactionError(), Eq(android::UNEXPECTED_NULL)) << status;
132   } else {
133     ASSERT_THAT(status.exceptionCode(), Eq(android::binder::Status::EX_NULL_POINTER)) << status;
134   }
135 }
136 
TEST_F(AidlTest,binderListWithNull)137 TEST_F(AidlTest, binderListWithNull) {
138   std::vector<sp<IBinder>> input{new BBinder(), nullptr};
139   auto status = service->TakesAnIBinderList(input);
140 
141   if (backend == BackendType::JAVA) {
142     ASSERT_TRUE(status.isOk()) << status;
143   } else if (backend == BackendType::NDK) {
144     ASSERT_THAT(status.transactionError(), Eq(android::UNEXPECTED_NULL)) << status;
145   } else {
146     ASSERT_THAT(status.exceptionCode(), Eq(android::binder::Status::EX_NULL_POINTER)) << status;
147   }
148 }
149 
TEST_F(AidlTest,nonNullBinder)150 TEST_F(AidlTest, nonNullBinder) {
151   sp<IBinder> input = new BBinder();
152   auto status = service->TakesAnIBinder(input);
153   ASSERT_TRUE(status.isOk());
154 }
155 
TEST_F(AidlTest,binderListWithoutNull)156 TEST_F(AidlTest, binderListWithoutNull) {
157   std::vector<sp<IBinder>> input{new BBinder(), new BBinder()};
158   auto status = service->TakesAnIBinderList(input);
159   ASSERT_TRUE(status.isOk());
160 }
161 
TEST_F(AidlTest,nullBinderToAnnotatedMethod)162 TEST_F(AidlTest, nullBinderToAnnotatedMethod) {
163   auto status = service->TakesANullableIBinder(nullptr);
164   ASSERT_TRUE(status.isOk());
165 }
166 
TEST_F(AidlTest,binderListWithNullToAnnotatedMethod)167 TEST_F(AidlTest, binderListWithNullToAnnotatedMethod) {
168   std::vector<sp<IBinder>> input{new BBinder(), nullptr};
169   auto status = service->TakesANullableIBinderList(input);
170   ASSERT_TRUE(status.isOk());
171 }
172 
TEST_F(AidlTest,binderArray)173 TEST_F(AidlTest, binderArray) {
174   std::vector<sp<IBinder>> repeated;
175   if (backend == BackendType::JAVA) {
176     // Java can only modify out-argument arrays in-place
177     repeated.resize(2);
178   }
179 
180   std::vector<sp<IBinder>> reversed;
181   std::vector<sp<IBinder>> input{new BBinder(), new BBinder()};
182   auto status = service->ReverseIBinderArray(input, &repeated, &reversed);
183   ASSERT_TRUE(status.isOk()) << status;
184 
185   EXPECT_THAT(input, Eq(repeated));
186   std::reverse(std::begin(reversed), std::end(reversed));
187   EXPECT_THAT(input, Eq(reversed));
188 }
189 
TEST_F(AidlTest,nullableBinderArray)190 TEST_F(AidlTest, nullableBinderArray) {
191   std::optional<std::vector<sp<IBinder>>> repeated;
192   if (backend == BackendType::JAVA) {
193     // Java can only modify out-argument arrays in-place
194     repeated = std::vector<sp<IBinder>>();
195     repeated->resize(2);
196   }
197 
198   std::optional<std::vector<sp<IBinder>>> reversed;
199   std::optional<std::vector<sp<IBinder>>> input = std::vector<sp<IBinder>>{new BBinder(), nullptr};
200   auto status = service->ReverseNullableIBinderArray(input, &repeated, &reversed);
201   ASSERT_TRUE(status.isOk()) << status;
202 
203   EXPECT_THAT(input, Eq(repeated));
204   ASSERT_TRUE(reversed);
205   std::reverse(std::begin(*reversed), std::end(*reversed));
206   EXPECT_THAT(input, Eq(reversed));
207 }
208 
TEST_F(AidlTest,nonNullBinderToAnnotatedMethod)209 TEST_F(AidlTest, nonNullBinderToAnnotatedMethod) {
210   sp<IBinder> input = new BBinder();
211   auto status = service->TakesANullableIBinder(input);
212   ASSERT_TRUE(status.isOk());
213 }
214 
TEST_F(AidlTest,binderListWithoutNullToAnnotatedMethod)215 TEST_F(AidlTest, binderListWithoutNullToAnnotatedMethod) {
216   std::vector<sp<IBinder>> input{new BBinder(), new BBinder()};
217   auto status = service->TakesANullableIBinderList(input);
218   ASSERT_TRUE(status.isOk());
219 }
220 
TEST_F(AidlTest,interface)221 TEST_F(AidlTest, interface) {
222   sp<INamedCallback> callback;
223   auto status = service->GetCallback(false, &callback);
224   ASSERT_TRUE(status.isOk());
225   ASSERT_THAT(callback.get(), Ne(nullptr));
226 }
227 
TEST_F(AidlTest,nullInterface)228 TEST_F(AidlTest, nullInterface) {
229   sp<INamedCallback> callback;
230   auto status = service->GetCallback(true, &callback);
231   ASSERT_TRUE(status.isOk());
232   ASSERT_THAT(callback.get(), Eq(nullptr));
233 }
234