• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (C) 2021 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 #include "aidl_test_client.h"
17 #include "gmock/gmock.h"
18 
19 #include "android/aidl/tests/BnNamedCallback.h"
20 #include "android/aidl/tests/BnTestService.h"
21 
22 using android::binder::Status;
23 
24 using android::String16;
25 using android::aidl::tests::INamedCallback;
26 using android::aidl::tests::ITestService;
27 using android::aidl::tests::ITestServiceDelegator;
28 
29 static constexpr int8_t kCustomByte = 8;
30 
31 static_assert(std::is_same<ITestService::DefaultDelegator, ITestServiceDelegator>::value);
32 
33 struct CustomDelegator : public ITestServiceDelegator {
34  public:
CustomDelegatorCustomDelegator35   CustomDelegator(sp<ITestService>& impl) : ITestServiceDelegator(impl) {}
36 
37   // Change RepeatByte to always return the same byte.
RepeatByteCustomDelegator38   Status RepeatByte(int8_t /* token */, int8_t* _aidl_return) override {
39     *_aidl_return = kCustomByte;
40     return Status::ok();
41   }
42 };
43 
TEST_F(AidlTest,Delegator)44 TEST_F(AidlTest, Delegator) {
45   auto delegator = sp<ITestServiceDelegator>::make(service);
46 
47   int8_t returned_value;
48   auto status = delegator->RepeatByte(12, &returned_value);
49   ASSERT_TRUE(status.isOk()) << status;
50   EXPECT_EQ(12, returned_value);
51 }
52 
TEST_F(AidlTest,CustomDelegator)53 TEST_F(AidlTest, CustomDelegator) {
54   auto delegator = sp<CustomDelegator>::make(service);
55 
56   int8_t returned_value;
57   auto status = delegator->RepeatByte(12, &returned_value);
58   ASSERT_TRUE(status.isOk()) << status;
59   EXPECT_EQ(kCustomByte, returned_value);
60 }
61 
TEST_F(AidlTest,DelegatorWithBinders)62 TEST_F(AidlTest, DelegatorWithBinders) {
63   auto delegator = sp<ITestServiceDelegator>::make(service);
64 
65   sp<INamedCallback> callback;
66   auto status = delegator->GetOtherTestService(String16("callback1"), &callback);
67   ASSERT_TRUE(status.isOk()) << status;
68 
69   bool verified = false;
70   status = delegator->VerifyName(callback, String16("callback1"), &verified);
71   ASSERT_TRUE(status.isOk()) << status;
72   EXPECT_TRUE(verified);
73 }
74 
TEST_F(AidlTest,DelegatorSimpl)75 TEST_F(AidlTest, DelegatorSimpl) {
76   auto delegator = sp<ITestServiceDelegator>::make(service);
77   sp<ITestService> impl = delegator->getImpl();
78 }
79 
TEST_F(AidlTest,DelegateWrapAndGet)80 TEST_F(AidlTest, DelegateWrapAndGet) {
81   auto delegator = delegate(service);
82   auto delegator2 = delegate(service);
83   EXPECT_EQ(delegator, delegator2);
84 }
85 
TEST_F(AidlTest,DelegateWrapAndUnwrap)86 TEST_F(AidlTest, DelegateWrapAndUnwrap) {
87   sp<ITestServiceDelegator> delegator = sp<ITestServiceDelegator>::cast(delegate(service));
88   EXPECT_NE(service, delegator);
89 
90   sp<ITestService> service2 = delegator->getImpl();
91   EXPECT_EQ(service, service2);
92 
93   auto delegator2 = delegate(sp<ITestService>::cast(delegator));
94   EXPECT_EQ(service, delegator2);
95 }
96 
TEST_F(AidlTest,DelegatorSetAndGetBinder)97 TEST_F(AidlTest, DelegatorSetAndGetBinder) {
98   auto delegator = sp<ITestServiceDelegator>::make(service);
99 
100   sp<INamedCallback> callback;
101   auto status = delegator->GetCallback(false, &callback);
102   ASSERT_TRUE(status.isOk()) << status;
103 
104   // callback will be wrapped for first time and the delegator will be sent
105   bool already_existing;
106   status = delegator->SetOtherTestService(String16("same_one"), callback, &already_existing);
107   ASSERT_TRUE(status.isOk()) << status;
108   ASSERT_FALSE(already_existing);
109 
110   // unwrap the delegator here and get the original binder
111   sp<INamedCallback> callback2;
112   status = delegator->GetOtherTestService(String16("same_one"), &callback2);
113   ASSERT_TRUE(status.isOk()) << status;
114   EXPECT_EQ(callback, callback2);
115 }
116 
TEST_F(AidlTest,DelegatorSettingSameBinders)117 TEST_F(AidlTest, DelegatorSettingSameBinders) {
118   auto delegator = sp<ITestServiceDelegator>::make(service);
119 
120   sp<INamedCallback> callback;
121   auto status = delegator->GetCallback(false, &callback);
122   ASSERT_TRUE(status.isOk()) << status;
123 
124   bool already_existing = false;
125   status = delegator->SetOtherTestService(String16("same_two"), callback, &already_existing);
126   ASSERT_TRUE(status.isOk()) << status;
127   ASSERT_FALSE(already_existing);
128 
129   status = delegator->SetOtherTestService(String16("same_two"), callback, &already_existing);
130   ASSERT_TRUE(status.isOk()) << status;
131   EXPECT_TRUE(already_existing);
132 }
133 
TEST_F(AidlTest,DelegatorSameBinders)134 TEST_F(AidlTest, DelegatorSameBinders) {
135   auto delegator = sp<ITestServiceDelegator>::make(service);
136 
137   sp<INamedCallback> callback1;
138   auto status = delegator->GetOtherTestService(String16("callback1"), &callback1);
139   ASSERT_TRUE(status.isOk()) << status;
140 
141   sp<INamedCallback> callback1Copy;
142   status = delegator->GetOtherTestService(String16("callback1"), &callback1Copy);
143   ASSERT_TRUE(status.isOk()) << status;
144   EXPECT_EQ(callback1, callback1Copy);
145 }
146 
TEST_F(AidlTest,DelegatorWithCallback)147 TEST_F(AidlTest, DelegatorWithCallback) {
148   auto delegator = sp<ITestServiceDelegator>::make(service);
149 
150   sp<INamedCallback> callback;
151   auto status = delegator->GetCallback(false, &callback);
152   ASSERT_TRUE(status.isOk()) << status;
153 
154   String16 name;
155   status = callback->GetName(&name);
156   ASSERT_TRUE(status.isOk()) << status;
157   EXPECT_TRUE(name.size() != 0);
158 }
159 
TEST_F(AidlTest,DelegatorWithNullCallback)160 TEST_F(AidlTest, DelegatorWithNullCallback) {
161   auto delegator = sp<ITestServiceDelegator>::make(service);
162 
163   sp<INamedCallback> callback;
164   auto status = delegator->GetCallback(true, &callback);
165   ASSERT_TRUE(status.isOk()) << status;
166   EXPECT_TRUE(callback == nullptr);
167 }
168