xref: /aosp_15_r20/frameworks/native/libs/binder/tests/binderBinderUnitTest.cpp (revision 38e8c45f13ce32b0dcecb25141ffecaf386fa17f)
1 /*
2  * Copyright (C) 2020 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 <binder/Binder.h>
18 #include <binder/IInterface.h>
19 #include <gtest/gtest.h>
20 
21 using android::BBinder;
22 using android::IBinder;
23 using android::OK;
24 using android::sp;
25 
26 const void* kObjectId1 = reinterpret_cast<const void*>(1);
27 const void* kObjectId2 = reinterpret_cast<const void*>(2);
28 void* kObject1 = reinterpret_cast<void*>(101);
29 void* kObject2 = reinterpret_cast<void*>(102);
30 void* kObject3 = reinterpret_cast<void*>(103);
31 
TEST(Binder,AttachObject)32 TEST(Binder, AttachObject) {
33     auto binder = sp<BBinder>::make();
34     EXPECT_EQ(nullptr, binder->attachObject(kObjectId1, kObject1, nullptr, nullptr));
35     EXPECT_EQ(nullptr, binder->attachObject(kObjectId2, kObject2, nullptr, nullptr));
36     EXPECT_EQ(kObject1, binder->attachObject(kObjectId1, kObject3, nullptr, nullptr));
37 }
38 
TEST(Binder,DetachObject)39 TEST(Binder, DetachObject) {
40     auto binder = sp<BBinder>::make();
41     EXPECT_EQ(nullptr, binder->attachObject(kObjectId1, kObject1, nullptr, nullptr));
42     EXPECT_EQ(kObject1, binder->detachObject(kObjectId1));
43     EXPECT_EQ(nullptr, binder->attachObject(kObjectId1, kObject2, nullptr, nullptr));
44 }
45 
TEST(Binder,AttachExtension)46 TEST(Binder, AttachExtension) {
47     auto binder = sp<BBinder>::make();
48     auto ext = sp<BBinder>::make();
49     binder->setExtension(ext);
50     EXPECT_EQ(ext, binder->getExtension());
51 }
52 
53 struct MyCookie {
54     bool* deleted;
55 };
56 
57 class UniqueBinder : public BBinder {
58 public:
UniqueBinder(const void * c)59     UniqueBinder(const void* c) : cookie(reinterpret_cast<const MyCookie*>(c)) {
60         *cookie->deleted = false;
61     }
~UniqueBinder()62     ~UniqueBinder() { *cookie->deleted = true; }
63     const MyCookie* cookie;
64 };
65 
make(const void * arg)66 static sp<IBinder> make(const void* arg) {
67     return sp<UniqueBinder>::make(arg);
68 }
69 
TEST(Binder,LookupOrCreateWeak)70 TEST(Binder, LookupOrCreateWeak) {
71     auto binder = sp<BBinder>::make();
72     bool deleted;
73     MyCookie cookie = {&deleted};
74     sp<IBinder> createdBinder = binder->lookupOrCreateWeak(kObjectId1, make, &cookie);
75     EXPECT_NE(binder, createdBinder);
76 
77     sp<IBinder> lookedUpBinder = binder->lookupOrCreateWeak(kObjectId1, make, &cookie);
78     EXPECT_EQ(createdBinder, lookedUpBinder);
79     EXPECT_FALSE(deleted);
80 }
81 
TEST(Binder,LookupOrCreateWeakDropSp)82 TEST(Binder, LookupOrCreateWeakDropSp) {
83     auto binder = sp<BBinder>::make();
84     bool deleted1 = false;
85     bool deleted2 = false;
86     MyCookie cookie1 = {&deleted1};
87     MyCookie cookie2 = {&deleted2};
88     sp<IBinder> createdBinder = binder->lookupOrCreateWeak(kObjectId1, make, &cookie1);
89     EXPECT_NE(binder, createdBinder);
90 
91     createdBinder.clear();
92     EXPECT_TRUE(deleted1);
93 
94     sp<IBinder> lookedUpBinder = binder->lookupOrCreateWeak(kObjectId1, make, &cookie2);
95     EXPECT_EQ(&cookie2, sp<UniqueBinder>::cast(lookedUpBinder)->cookie);
96     EXPECT_FALSE(deleted2);
97 }
98