Skip to content

Commit 869c4e2

Browse files
author
Steven Moreland
committed
libbinder: integration test for stability
Due to the number of backends involved and amount of code passing stability around, as well as prebuilts being used and multiple build systems being introduced, validate the stability of binder objects end to end. Bug: 385355208 Test: atest binderStabilityTest Change-Id: I25fb93b85740e8380a333cd2f05afc1a3c35ec5b
1 parent 0624877 commit 869c4e2

4 files changed

Lines changed: 120 additions & 2 deletions

File tree

libs/binder/TEST_MAPPING

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@
3636
{
3737
"name": "binderStabilityTest"
3838
},
39+
{
40+
"name": "binderStabilityIntegrationTest"
41+
},
3942
{
4043
"name": "binderRpcWireProtocolTest"
4144
},

libs/binder/include/binder/Stability.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@
2020
#include <binder/IBinder.h>
2121
#include <string>
2222

23+
class BinderStabilityIntegrationTest_ExpectedStabilityForItsPartition_Test;
24+
2325
namespace android {
2426

2527
class BpBinder;
@@ -127,6 +129,8 @@ class Stability final {
127129
// through Parcel)
128130
friend ::android::ProcessState;
129131

132+
friend ::BinderStabilityIntegrationTest_ExpectedStabilityForItsPartition_Test;
133+
130134
static void tryMarkCompilationUnit(IBinder* binder);
131135

132136
// Currently, we use int16_t for Level so that it can fit in BBinder.
@@ -156,11 +160,11 @@ class Stability final {
156160
uint32_t flags);
157161

158162
// get stability information as encoded on the wire
159-
static int16_t getRepr(IBinder* binder);
163+
LIBBINDER_EXPORTED static int16_t getRepr(IBinder* binder);
160164

161165
// whether a transaction on binder is allowed, if the transaction
162166
// is done from a context with a specific stability level
163-
static bool check(int16_t provided, Level required);
167+
LIBBINDER_EXPORTED static bool check(int16_t provided, Level required);
164168

165169
static bool isDeclaredLevel(int32_t level);
166170
static std::string levelString(int32_t level);

libs/binder/tests/Android.bp

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -802,6 +802,28 @@ cc_test {
802802
require_root: true,
803803
}
804804

805+
cc_test {
806+
name: "binderStabilityIntegrationTest",
807+
defaults: ["binder_test_defaults"],
808+
srcs: [
809+
"binderStabilityIntegrationTest.cpp",
810+
],
811+
812+
shared_libs: [
813+
"libbinder",
814+
"libutils",
815+
],
816+
static_libs: [
817+
"libprocpartition",
818+
],
819+
820+
test_suites: [
821+
"general-tests",
822+
"vts",
823+
],
824+
require_root: true,
825+
}
826+
805827
cc_test {
806828
name: "binderAllocationLimits",
807829
defaults: ["binder_test_defaults"],
Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,89 @@
1+
/*
2+
* Copyright (C) 2025 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/IServiceManager.h>
19+
#include <binder/Stability.h>
20+
#include <gtest/gtest.h>
21+
#include <procpartition/procpartition.h>
22+
23+
using namespace android;
24+
using android::internal::Stability; // for testing only!
25+
using android::procpartition::getPartition;
26+
using android::procpartition::Partition;
27+
28+
class BinderStabilityIntegrationTest : public testing::Test,
29+
public ::testing::WithParamInterface<String16> {
30+
public:
31+
virtual ~BinderStabilityIntegrationTest() {}
32+
};
33+
34+
TEST_P(BinderStabilityIntegrationTest, ExpectedStabilityForItsPartition) {
35+
const String16& serviceName = GetParam();
36+
37+
sp<IBinder> binder = defaultServiceManager()->checkService(serviceName);
38+
if (!binder) GTEST_SKIP() << "Could not get service, may have gone away.";
39+
40+
pid_t pid;
41+
status_t res = binder->getDebugPid(&pid);
42+
if (res != OK) {
43+
GTEST_SKIP() << "Could not talk to service to get PID, res: " << statusToString(res);
44+
}
45+
46+
Partition partition = getPartition(pid);
47+
48+
Stability::Level level = Stability::Level::UNDECLARED;
49+
switch (partition) {
50+
case Partition::SYSTEM:
51+
case Partition::SYSTEM_EXT:
52+
level = Stability::Level::SYSTEM;
53+
break;
54+
case Partition::VENDOR:
55+
case Partition::ODM:
56+
level = Stability::Level::VENDOR;
57+
break;
58+
case Partition::UNKNOWN:
59+
GTEST_SKIP() << "Not sure of partition of process.";
60+
return;
61+
default:
62+
ADD_FAILURE() << "Unrecognized partition for service: " << partition;
63+
return;
64+
}
65+
66+
ASSERT_TRUE(Stability::check(Stability::getRepr(binder.get()), level))
67+
<< "Binder hosted on partition " << partition
68+
<< " should have corresponding stability set.";
69+
}
70+
71+
std::string PrintTestParam(
72+
const testing::TestParamInfo<BinderStabilityIntegrationTest::ParamType>& info) {
73+
std::string name = String8(info.param).c_str();
74+
for (size_t i = 0; i < name.size(); i++) {
75+
bool alnum = false;
76+
alnum |= (name[i] >= 'a' && name[i] <= 'z');
77+
alnum |= (name[i] >= 'A' && name[i] <= 'Z');
78+
alnum |= (name[i] >= '0' && name[i] <= '9');
79+
alnum |= (name[i] == '_');
80+
if (!alnum) name[i] = '_';
81+
}
82+
83+
// index for uniqueness
84+
return std::to_string(info.index) + "__" + name;
85+
}
86+
87+
INSTANTIATE_TEST_CASE_P(RegisteredServices, BinderStabilityIntegrationTest,
88+
::testing::ValuesIn(defaultServiceManager()->listServices()),
89+
PrintTestParam);

0 commit comments

Comments
 (0)