summaryrefslogtreecommitdiff
path: root/libs/binder/tests/binderStabilityIntegrationTest.cpp
blob: cbc4180480f9ea8a770cd16d5f746186cd5b7191 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
/*
 * Copyright (C) 2025 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

#include <binder/Binder.h>
#include <binder/IServiceManager.h>
#include <binder/Stability.h>
#include <gtest/gtest.h>
#include <procpartition/procpartition.h>

using namespace android;
using android::internal::Stability; // for testing only!
using android::procpartition::getPartition;
using android::procpartition::Partition;

class BinderStabilityIntegrationTest : public testing::Test,
                                       public ::testing::WithParamInterface<String16> {
public:
    virtual ~BinderStabilityIntegrationTest() {}
};

TEST_P(BinderStabilityIntegrationTest, ExpectedStabilityForItsPartition) {
    const String16& serviceName = GetParam();

    sp<IBinder> binder = defaultServiceManager()->checkService(serviceName);
    if (!binder) GTEST_SKIP() << "Could not get service, may have gone away.";

    pid_t pid;
    status_t res = binder->getDebugPid(&pid);
    if (res != OK) {
        GTEST_SKIP() << "Could not talk to service to get PID, res: " << statusToString(res);
    }

    Partition partition = getPartition(pid);

    Stability::Level level = Stability::Level::UNDECLARED;
    switch (partition) {
        case Partition::PRODUCT:
        case Partition::SYSTEM:
        case Partition::SYSTEM_EXT:
            level = Stability::Level::SYSTEM;
            break;
        case Partition::VENDOR:
        case Partition::ODM:
            level = Stability::Level::VENDOR;
            break;
        case Partition::UNKNOWN:
            GTEST_SKIP() << "Not sure of partition of process.";
            return;
        default:
            ADD_FAILURE() << "Unrecognized partition for service: " << partition;
            return;
    }

    ASSERT_TRUE(Stability::check(Stability::getRepr(binder.get()), level))
            << "Binder hosted on partition " << partition
            << " should have corresponding stability set.";
}

std::string PrintTestParam(
        const testing::TestParamInfo<BinderStabilityIntegrationTest::ParamType>& info) {
    std::string name = String8(info.param).c_str();
    for (size_t i = 0; i < name.size(); i++) {
        bool alnum = false;
        alnum |= (name[i] >= 'a' && name[i] <= 'z');
        alnum |= (name[i] >= 'A' && name[i] <= 'Z');
        alnum |= (name[i] >= '0' && name[i] <= '9');
        alnum |= (name[i] == '_');
        if (!alnum) name[i] = '_';
    }

    // index for uniqueness
    return std::to_string(info.index) + "__" + name;
}

INSTANTIATE_TEST_CASE_P(RegisteredServices, BinderStabilityIntegrationTest,
                        ::testing::ValuesIn(defaultServiceManager()->listServices()),
                        PrintTestParam);