1 /*
2  * Copyright (C) 2022 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 #define TLOG_TAG "binder-test-client"
18 
19 #include <binder/RpcSession.h>
20 #include <binder/RpcTransportTipcTrusty.h>
21 #include <lib/unittest/unittest.h>
22 #include <stdio.h>
23 #include <trusty_unittest.h>
24 #include <uapi/err.h>
25 
26 #include <com/android/trusty/binder/test/ITestService.h>
27 
28 using ::android::String16;
29 using ::android::binder::Status;
30 
31 using com::android::trusty::binder::test::ByteEnum;
32 using com::android::trusty::binder::test::IntEnum;
33 using com::android::trusty::binder::test::ITestService;
34 using com::android::trusty::binder::test::LongEnum;
35 
36 typedef struct {
37     android::sp<android::RpcSession> sess;
38     android::sp<ITestService> srv;
39 } BinderTest_t;
40 
BinderTest_SetUp(BinderTest_t * state)41 static void BinderTest_SetUp(BinderTest_t* state) {
42     int rc;
43     android::binder::unique_fd chan;
44     android::status_t status;
45     android::sp<android::IBinder> root;
46 
47     rc = connect(ITestService::PORT().c_str(), IPC_CONNECT_WAIT_FOR_PORT);
48     ASSERT_GE(rc, 0);
49     chan.reset(rc);
50 
51     state->sess = android::RpcSession::make(
52             android::RpcTransportCtxFactoryTipcTrusty::make());
53     ASSERT_NE(nullptr, state->sess.get());
54 
55     status = state->sess->setupPreconnectedClient(
56             std::move(chan), []() { return android::binder::unique_fd(); });
57     ASSERT_EQ(android::OK, status);
58 
59     root = state->sess->getRootObject();
60     ASSERT_NE(nullptr, root.get());
61 
62     state->srv = ITestService::asInterface(root);
63     ASSERT_NE(nullptr, state->srv.get());
64 
65 test_abort:;
66 }
67 
BinderTest_TearDown(BinderTest_t * state)68 static void BinderTest_TearDown(BinderTest_t* state) {
69     state->srv.clear();
70 }
71 
72 template <typename T, typename U, typename V>
CheckRepeat(const BinderTest_t * state,Status (ITestService::* func)(T,U *),V in)73 static void CheckRepeat(const BinderTest_t* state,
74                         Status (ITestService::*func)(T, U*),
75                         V in) {
76     U out;
77     auto status = (*state->srv.*func)(in, &out);
78     EXPECT_EQ(true, status.isOk());
79     EXPECT_EQ(in, out);
80 }
81 
CheckRepeat(const BinderTest_t * state,const String16 & in)82 static void CheckRepeat(const BinderTest_t* state, const String16& in) {
83     String16 out;
84     auto status = state->srv->RepeatString(in, &out);
85     EXPECT_EQ(true, status.isOk());
86     /*
87         EXPECT_EQ expects the input types to be C-style
88         castable to long; String16 is cannot
89     */
90     EXPECT_EQ(in == out, true);
91 }
92 
93 template <typename T>
CheckReverse(const BinderTest_t * state,Status (ITestService::* func)(const std::vector<T> &,std::vector<T> *,std::vector<T> *),const std::vector<T> & input)94 static void CheckReverse(const BinderTest_t* state,
95                          Status (ITestService::*func)(const std::vector<T>&,
96                                                       std::vector<T>*,
97                                                       std::vector<T>*),
98                          const std::vector<T>& input) {
99     // must be preallocated for Java servers
100     std::vector<T> repeated(input.size());
101     std::vector<T> reversed(input.size());
102     auto status = (*state->srv.*func)(input, &repeated, &reversed);
103     EXPECT_EQ(status.isOk(), true);
104     EXPECT_EQ(repeated == input, true);
105 
106     std::vector<T> reversed_input(input);
107     std::reverse(reversed_input.begin(), reversed_input.end());
108     EXPECT_EQ(reversed == reversed_input, true);
109 }
110 
111 #define CHECK_REPEAT(func, in) \
112     { CheckRepeat(_state, &ITestService::func, in); }
113 
114 #define CHECK_REPEAT_STRING(in) \
115     { CheckRepeat(_state, in); }
116 
117 #define CHECK_REVERSE(func, in) \
118     { CheckReverse(_state, &ITestService::func, in); }
119 
TEST_F(BinderTest,aBoolean)120 TEST_F(BinderTest, aBoolean) {
121     CHECK_REPEAT(RepeatBoolean, true);
122 }
123 
TEST_F(BinderTest,aByte)124 TEST_F(BinderTest, aByte) {
125     CHECK_REPEAT(RepeatByte, int8_t{-128});
126 }
127 
TEST_F(BinderTest,aChar)128 TEST_F(BinderTest, aChar) {
129     CHECK_REPEAT(RepeatChar, char16_t{'A'});
130 }
131 
TEST_F(BinderTest,aInt)132 TEST_F(BinderTest, aInt) {
133     CHECK_REPEAT(RepeatInt, int32_t{1 << 30});
134 }
135 
TEST_F(BinderTest,aLong)136 TEST_F(BinderTest, aLong) {
137     CHECK_REPEAT(RepeatLong, int64_t{1LL << 60});
138 }
139 
TEST_F(BinderTest,aFloat)140 TEST_F(BinderTest, aFloat) {
141     CHECK_REPEAT(RepeatFloat, float{1.0f / 3.0f});
142 }
143 
TEST_F(BinderTest,aDouble)144 TEST_F(BinderTest, aDouble) {
145     CHECK_REPEAT(RepeatDouble, double{1.0 / 3.0});
146 }
147 
TEST_F(BinderTest,aByteEnum)148 TEST_F(BinderTest, aByteEnum) {
149     CHECK_REPEAT(RepeatByteEnum, ByteEnum::BAR);
150 }
151 
TEST_F(BinderTest,aIntEnum)152 TEST_F(BinderTest, aIntEnum) {
153     CHECK_REPEAT(RepeatIntEnum, IntEnum::BAZ);
154 }
155 
TEST_F(BinderTest,aLongEnum)156 TEST_F(BinderTest, aLongEnum) {
157     CHECK_REPEAT(RepeatLongEnum, LongEnum::FOO);
158 }
159 
TEST_F(BinderTest,byteConstants)160 TEST_F(BinderTest, byteConstants) {
161     constexpr int8_t consts[] = {ITestService::BYTE_TEST_CONSTANT};
162     for (const auto& sent : consts) {
163         CHECK_REPEAT(RepeatByte, sent);
164     }
165 }
166 
TEST_F(BinderTest,intConstants)167 TEST_F(BinderTest, intConstants) {
168     constexpr int32_t consts[] = {
169             ITestService::TEST_CONSTANT,   ITestService::TEST_CONSTANT2,
170             ITestService::TEST_CONSTANT3,  ITestService::TEST_CONSTANT4,
171             ITestService::TEST_CONSTANT5,  ITestService::TEST_CONSTANT6,
172             ITestService::TEST_CONSTANT7,  ITestService::TEST_CONSTANT8,
173             ITestService::TEST_CONSTANT9,  ITestService::TEST_CONSTANT10,
174             ITestService::TEST_CONSTANT11, ITestService::TEST_CONSTANT12};
175     for (const auto& sent : consts) {
176         CHECK_REPEAT(RepeatInt, sent);
177     }
178 }
179 
TEST_F(BinderTest,longConstants)180 TEST_F(BinderTest, longConstants) {
181     constexpr int64_t consts[] = {ITestService::LONG_TEST_CONSTANT};
182     for (const auto& sent : consts) {
183         CHECK_REPEAT(RepeatLong, sent);
184     }
185 }
186 
TEST_F(BinderTest,strings)187 TEST_F(BinderTest, strings) {
188     std::vector<String16> strings = {
189             String16("Deliver us from evil."), String16(), String16("\0\0", 2),
190             // This is actually two unicode code points:
191             //   U+10437: The 'small letter yee' character in the deseret
192             //   alphabet U+20AC: A euro sign
193             String16("\xD8\x01\xDC\x37\x20\xAC"),
194             ITestService::STRING_TEST_CONSTANT(),
195             ITestService::STRING_TEST_CONSTANT2()};
196     for (const auto& sent : strings) {
197         CHECK_REPEAT_STRING(sent);
198     }
199 }
200 
TEST_F(BinderTest,booleanArray)201 TEST_F(BinderTest, booleanArray) {
202     std::vector<bool> bools{true, false, false};
203     CHECK_REVERSE(ReverseBoolean, bools);
204 }
205 
TEST_F(BinderTest,byteArray)206 TEST_F(BinderTest, byteArray) {
207     std::vector<uint8_t> bytes{uint8_t{255}, uint8_t{0}, uint8_t{127}};
208     CHECK_REVERSE(ReverseByte, bytes);
209 }
210 
TEST_F(BinderTest,charArray)211 TEST_F(BinderTest, charArray) {
212     std::vector<char16_t> chars{char16_t{'A'}, char16_t{'B'}, char16_t{'C'}};
213     CHECK_REVERSE(ReverseChar, chars);
214 }
215 
TEST_F(BinderTest,intArray)216 TEST_F(BinderTest, intArray) {
217     std::vector<int> ints{1, 2, 3};
218     CHECK_REVERSE(ReverseInt, ints);
219 }
220 
TEST_F(BinderTest,longArray)221 TEST_F(BinderTest, longArray) {
222     std::vector<int64_t> longs{-1LL, 0LL, int64_t{1LL << 60}};
223     CHECK_REVERSE(ReverseLong, longs);
224 }
225 
TEST_F(BinderTest,floatArray)226 TEST_F(BinderTest, floatArray) {
227     std::vector<float> floats{-0.3f, -0.7f, 8.0f};
228     CHECK_REVERSE(ReverseFloat, floats);
229 }
230 
TEST_F(BinderTest,doubleArray)231 TEST_F(BinderTest, doubleArray) {
232     std::vector<double> doubles{1.0 / 3.0, 1.0 / 7.0, 42.0};
233     CHECK_REVERSE(ReverseDouble, doubles);
234 }
235 
TEST_F(BinderTest,byteEnumArray)236 TEST_F(BinderTest, byteEnumArray) {
237     std::vector<ByteEnum> bytes{ByteEnum::BAR, ByteEnum::FOO, ByteEnum::BAZ};
238     CHECK_REVERSE(ReverseByteEnum, bytes);
239 }
240 
TEST_F(BinderTest,byteEnumArray2)241 TEST_F(BinderTest, byteEnumArray2) {
242     std::vector<ByteEnum> bytes{std::begin(android::enum_range<ByteEnum>()),
243                                 std::end(android::enum_range<ByteEnum>())};
244     CHECK_REVERSE(ReverseByteEnum, bytes);
245 }
246 
TEST_F(BinderTest,intEnumArray)247 TEST_F(BinderTest, intEnumArray) {
248     std::vector<IntEnum> ints{IntEnum::BAR, IntEnum::BAZ, IntEnum::FOO};
249     CHECK_REVERSE(ReverseIntEnum, ints);
250 }
251 
TEST_F(BinderTest,longEnumArray)252 TEST_F(BinderTest, longEnumArray) {
253     std::vector<LongEnum> longs{LongEnum::BAR, LongEnum::BAZ, LongEnum::FOO};
254     CHECK_REVERSE(ReverseLongEnum, longs);
255 }
256 
257 PORT_TEST(memref, "com.android.trusty.binder.test")
258