Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2022 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 | * bpf_existence_test.cpp - checks that the device runs expected BPF programs |
| 17 | */ |
| 18 | |
| 19 | #include <cstdint> |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 20 | #include <set> |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 21 | #include <string> |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 22 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 23 | #include <android/api-level.h> |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 24 | #include <android-base/properties.h> |
| 25 | #include <android-modules-utils/sdk_level.h> |
| 26 | |
| 27 | #include <gtest/gtest.h> |
| 28 | |
| 29 | using std::find; |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 30 | using std::set; |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 31 | using std::string; |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 32 | |
| 33 | using android::modules::sdklevel::IsAtLeastR; |
| 34 | using android::modules::sdklevel::IsAtLeastS; |
| 35 | using android::modules::sdklevel::IsAtLeastT; |
| 36 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 37 | // Mainline development branches lack the constant for the current development OS. |
| 38 | #ifndef __ANDROID_API_T__ |
| 39 | #define __ANDROID_API_T__ 33 |
| 40 | #endif |
| 41 | |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 42 | class BpfExistenceTest : public ::testing::Test { |
| 43 | }; |
| 44 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 45 | static const set<string> INTRODUCED_R = { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 46 | "/sys/fs/bpf/prog_offload_schedcls_ingress_tether_ether", |
| 47 | "/sys/fs/bpf/prog_offload_schedcls_ingress_tether_rawip", |
| 48 | }; |
| 49 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 50 | static const set<string> INTRODUCED_S = { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 51 | "/sys/fs/bpf/tethering/prog_offload_schedcls_tether_downstream4_ether", |
| 52 | "/sys/fs/bpf/tethering/prog_offload_schedcls_tether_downstream4_rawip", |
| 53 | "/sys/fs/bpf/tethering/prog_offload_schedcls_tether_downstream6_ether", |
| 54 | "/sys/fs/bpf/tethering/prog_offload_schedcls_tether_downstream6_rawip", |
| 55 | "/sys/fs/bpf/tethering/prog_offload_schedcls_tether_upstream4_ether", |
| 56 | "/sys/fs/bpf/tethering/prog_offload_schedcls_tether_upstream4_rawip", |
| 57 | "/sys/fs/bpf/tethering/prog_offload_schedcls_tether_upstream6_ether", |
| 58 | "/sys/fs/bpf/tethering/prog_offload_schedcls_tether_upstream6_rawip", |
| 59 | }; |
| 60 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 61 | static const set<string> REMOVED_S = { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 62 | "/sys/fs/bpf/prog_offload_schedcls_ingress_tether_ether", |
| 63 | "/sys/fs/bpf/prog_offload_schedcls_ingress_tether_rawip", |
| 64 | }; |
| 65 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 66 | static const set<string> INTRODUCED_T = { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 67 | }; |
| 68 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 69 | static const set<string> REMOVED_T = { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 70 | }; |
| 71 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 72 | void addAll(set<string>* a, const set<string>& b) { |
| 73 | a->insert(b.begin(), b.end()); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 74 | } |
| 75 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 76 | void removeAll(set<string>* a, const set<string> b) { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 77 | for (const auto& toRemove : b) { |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 78 | a->erase(toRemove); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 79 | } |
| 80 | } |
| 81 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 82 | void getFileLists(set<string>* expected, set<string>* unexpected) { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 83 | unexpected->clear(); |
| 84 | expected->clear(); |
| 85 | |
| 86 | addAll(unexpected, INTRODUCED_R); |
| 87 | addAll(unexpected, INTRODUCED_S); |
| 88 | addAll(unexpected, INTRODUCED_T); |
| 89 | |
| 90 | if (IsAtLeastR()) { |
| 91 | addAll(expected, INTRODUCED_R); |
| 92 | removeAll(unexpected, INTRODUCED_R); |
| 93 | // Nothing removed in R. |
| 94 | } |
| 95 | |
| 96 | if (IsAtLeastS()) { |
| 97 | addAll(expected, INTRODUCED_S); |
| 98 | removeAll(expected, REMOVED_S); |
| 99 | |
| 100 | addAll(unexpected, REMOVED_S); |
| 101 | removeAll(unexpected, INTRODUCED_S); |
| 102 | } |
| 103 | |
| 104 | // Nothing added or removed in SCv2. |
| 105 | |
| 106 | if (IsAtLeastT()) { |
| 107 | addAll(expected, INTRODUCED_T); |
| 108 | removeAll(expected, REMOVED_T); |
| 109 | |
| 110 | addAll(unexpected, REMOVED_T); |
| 111 | removeAll(unexpected, INTRODUCED_T); |
| 112 | } |
| 113 | } |
| 114 | |
| 115 | void checkFiles() { |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 116 | set<string> mustExist; |
| 117 | set<string> mustNotExist; |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 118 | |
| 119 | getFileLists(&mustExist, &mustNotExist); |
| 120 | |
| 121 | for (const auto& file : mustExist) { |
| 122 | EXPECT_EQ(0, access(file.c_str(), R_OK)) << file << " does not exist"; |
| 123 | } |
| 124 | for (const auto& file : mustNotExist) { |
| 125 | int ret = access(file.c_str(), R_OK); |
| 126 | int err = errno; |
| 127 | EXPECT_EQ(-1, ret) << file << " unexpectedly exists"; |
| 128 | if (ret == -1) { |
| 129 | EXPECT_EQ(ENOENT, err) << " accessing " << file << " failed with errno " << err; |
| 130 | } |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | TEST_F(BpfExistenceTest, TestPrograms) { |
| 135 | // Pre-flight check to ensure test has been updated. |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 136 | uint64_t buildVersionSdk = android_get_device_api_level(); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 137 | ASSERT_NE(0, buildVersionSdk) << "Unable to determine device SDK version"; |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame^] | 138 | if (buildVersionSdk > __ANDROID_API_T__ && buildVersionSdk != __ANDROID_API_FUTURE__) { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 139 | FAIL() << "Unknown OS version " << buildVersionSdk << ", please update this test"; |
| 140 | } |
| 141 | |
| 142 | // Only unconfined root is guaranteed to be able to access everything in /sys/fs/bpf. |
| 143 | ASSERT_EQ(0, getuid()) << "This test must run as root."; |
| 144 | |
| 145 | checkFiles(); |
| 146 | } |