blob: b6aed0db2801c32f80916b1368316cb84cdd662e [file] [log] [blame]
Steven Moreland63a2d512021-06-25 01:10:15 +00001/*
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>
Devin Moore3faaa002022-07-27 15:54:06 +000018#include <binder/IInterface.h>
Steven Moreland63a2d512021-06-25 01:10:15 +000019#include <gtest/gtest.h>
20
21using android::BBinder;
Devin Moore3faaa002022-07-27 15:54:06 +000022using android::IBinder;
Steven Moreland63a2d512021-06-25 01:10:15 +000023using android::OK;
24using android::sp;
25
26const void* kObjectId1 = reinterpret_cast<const void*>(1);
27const void* kObjectId2 = reinterpret_cast<const void*>(2);
28void* kObject1 = reinterpret_cast<void*>(101);
29void* kObject2 = reinterpret_cast<void*>(102);
30void* kObject3 = reinterpret_cast<void*>(103);
31
32TEST(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
39TEST(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}
Steven Moreland55a12542022-03-31 21:53:11 +000045
46TEST(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}
Devin Moore3faaa002022-07-27 15:54:06 +000052
53struct MyCookie {
54 bool* deleted;
55};
56
57class UniqueBinder : public BBinder {
58public:
59 UniqueBinder(const void* c) : cookie(reinterpret_cast<const MyCookie*>(c)) {
60 *cookie->deleted = false;
61 }
62 ~UniqueBinder() { *cookie->deleted = true; }
63 const MyCookie* cookie;
64};
65
66static sp<IBinder> make(const void* arg) {
67 return sp<UniqueBinder>::make(arg);
68}
69
70TEST(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
82TEST(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}