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<StructuredParcelable>();
99   input->f = 42;
100 
101   std::optional<StructuredParcelable> output;
102   auto status = service->RepeatNullableParcelable(input, &output);
103   ASSERT_TRUE(status.isOk());
104   ASSERT_TRUE(output.has_value());
105   ASSERT_THAT(*output, Eq(*input));
106 
107   input.reset();
108   status = service->RepeatNullableParcelable(input, &output);
109   ASSERT_TRUE(status.isOk());
110   ASSERT_FALSE(output.has_value());
111 }
112 
TEST_F(AidlTest,nullBinder)113 TEST_F(AidlTest, nullBinder) {
114   auto status = service->TakesAnIBinder(nullptr);
115 
116   if (backend == BackendType::JAVA) {
117     ASSERT_TRUE(status.isOk()) << status;
118   } else {
119     ASSERT_THAT(status.exceptionCode(), Eq(android::binder::Status::EX_NULL_POINTER)) << status;
120   }
121 }
122 
TEST_F(AidlTest,binderListWithNull)123 TEST_F(AidlTest, binderListWithNull) {
124   if (!cpp_java_tests) GTEST_SKIP() << "Service does not support the CPP/Java-only tests.";
125 
126   std::vector<sp<IBinder>> input{new BBinder(), nullptr};
127   auto status = cpp_java_tests->TakesAnIBinderList(input);
128 
129   if (backend == BackendType::JAVA) {
130     ASSERT_TRUE(status.isOk()) << status;
131   } else {
132     ASSERT_THAT(status.exceptionCode(), Eq(android::binder::Status::EX_NULL_POINTER));
133   }
134 }
135 
TEST_F(AidlTest,nonNullBinder)136 TEST_F(AidlTest, nonNullBinder) {
137   sp<IBinder> input = new BBinder();
138   auto status = service->TakesAnIBinder(input);
139   ASSERT_TRUE(status.isOk());
140 }
141 
TEST_F(AidlTest,binderListWithoutNull)142 TEST_F(AidlTest, binderListWithoutNull) {
143   if (!cpp_java_tests) GTEST_SKIP() << "Service does not support the CPP/Java-only tests.";
144 
145   std::vector<sp<IBinder>> input{new BBinder(), new BBinder()};
146   auto status = cpp_java_tests->TakesAnIBinderList(input);
147   ASSERT_TRUE(status.isOk());
148 }
149 
TEST_F(AidlTest,nullBinderToAnnotatedMethod)150 TEST_F(AidlTest, nullBinderToAnnotatedMethod) {
151   auto status = service->TakesANullableIBinder(nullptr);
152   ASSERT_TRUE(status.isOk());
153 }
154 
TEST_F(AidlTest,binderListWithNullToAnnotatedMethod)155 TEST_F(AidlTest, binderListWithNullToAnnotatedMethod) {
156   if (!cpp_java_tests) GTEST_SKIP() << "Service does not support the CPP/Java-only tests.";
157 
158   std::vector<sp<IBinder>> input{new BBinder(), nullptr};
159   auto status = cpp_java_tests->TakesANullableIBinderList(input);
160   ASSERT_TRUE(status.isOk());
161 }
162 
TEST_F(AidlTest,nonNullBinderToAnnotatedMethod)163 TEST_F(AidlTest, nonNullBinderToAnnotatedMethod) {
164   sp<IBinder> input = new BBinder();
165   auto status = service->TakesANullableIBinder(input);
166   ASSERT_TRUE(status.isOk());
167 }
168 
TEST_F(AidlTest,binderListWithoutNullToAnnotatedMethod)169 TEST_F(AidlTest, binderListWithoutNullToAnnotatedMethod) {
170   if (!cpp_java_tests) GTEST_SKIP() << "Service does not support the CPP/Java-only tests.";
171 
172   std::vector<sp<IBinder>> input{new BBinder(), new BBinder()};
173   auto status = cpp_java_tests->TakesANullableIBinderList(input);
174   ASSERT_TRUE(status.isOk());
175 }
176 
TEST_F(AidlTest,interface)177 TEST_F(AidlTest, interface) {
178   sp<INamedCallback> callback;
179   auto status = service->GetCallback(false, &callback);
180   ASSERT_TRUE(status.isOk());
181   ASSERT_THAT(callback.get(), Ne(nullptr));
182 }
183 
TEST_F(AidlTest,nullInterface)184 TEST_F(AidlTest, nullInterface) {
185   sp<INamedCallback> callback;
186   auto status = service->GetCallback(true, &callback);
187   ASSERT_TRUE(status.isOk());
188   ASSERT_THAT(callback.get(), Eq(nullptr));
189 }
190