|
| 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