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 | * |
Lorenzo Colitti | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 16 | * bpf_existence_test.cpp - checks that the device has expected BPF programs and maps |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 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 | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 42 | #define PLATFORM "/sys/fs/bpf/" |
| 43 | #define TETHERING "/sys/fs/bpf/tethering/" |
| 44 | |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 45 | class BpfExistenceTest : public ::testing::Test { |
| 46 | }; |
| 47 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 48 | static const set<string> INTRODUCED_R = { |
Lorenzo Colitti | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 49 | PLATFORM "map_offload_tether_ingress_map", |
| 50 | PLATFORM "map_offload_tether_limit_map", |
| 51 | PLATFORM "map_offload_tether_stats_map", |
| 52 | PLATFORM "prog_offload_schedcls_ingress_tether_ether", |
| 53 | PLATFORM "prog_offload_schedcls_ingress_tether_rawip", |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 54 | }; |
| 55 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 56 | static const set<string> INTRODUCED_S = { |
Lorenzo Colitti | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 57 | TETHERING "map_offload_tether_dev_map", |
| 58 | TETHERING "map_offload_tether_downstream4_map", |
| 59 | TETHERING "map_offload_tether_downstream64_map", |
| 60 | TETHERING "map_offload_tether_downstream6_map", |
| 61 | TETHERING "map_offload_tether_error_map", |
| 62 | TETHERING "map_offload_tether_limit_map", |
| 63 | TETHERING "map_offload_tether_stats_map", |
| 64 | TETHERING "map_offload_tether_upstream4_map", |
| 65 | TETHERING "map_offload_tether_upstream6_map", |
| 66 | TETHERING "map_test_tether_downstream6_map", |
| 67 | TETHERING "prog_offload_schedcls_tether_downstream4_ether", |
| 68 | TETHERING "prog_offload_schedcls_tether_downstream4_rawip", |
| 69 | TETHERING "prog_offload_schedcls_tether_downstream6_ether", |
| 70 | TETHERING "prog_offload_schedcls_tether_downstream6_rawip", |
| 71 | TETHERING "prog_offload_schedcls_tether_upstream4_ether", |
| 72 | TETHERING "prog_offload_schedcls_tether_upstream4_rawip", |
| 73 | TETHERING "prog_offload_schedcls_tether_upstream6_ether", |
| 74 | TETHERING "prog_offload_schedcls_tether_upstream6_rawip", |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 75 | }; |
| 76 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 77 | static const set<string> REMOVED_S = { |
Lorenzo Colitti | 8db39c4 | 2022-01-13 16:11:31 +0900 | [diff] [blame] | 78 | PLATFORM "map_offload_tether_ingress_map", |
| 79 | PLATFORM "map_offload_tether_limit_map", |
| 80 | PLATFORM "map_offload_tether_stats_map", |
| 81 | PLATFORM "prog_offload_schedcls_ingress_tether_ether", |
| 82 | PLATFORM "prog_offload_schedcls_ingress_tether_rawip", |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 83 | }; |
| 84 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 85 | static const set<string> INTRODUCED_T = { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 86 | }; |
| 87 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 88 | static const set<string> REMOVED_T = { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 89 | }; |
| 90 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 91 | void addAll(set<string>* a, const set<string>& b) { |
| 92 | a->insert(b.begin(), b.end()); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 93 | } |
| 94 | |
Lorenzo Colitti | 3299937 | 2022-01-14 09:37:25 +0900 | [diff] [blame^] | 95 | void removeAll(set<string>* a, const set<string>& b) { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 96 | for (const auto& toRemove : b) { |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 97 | a->erase(toRemove); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 98 | } |
| 99 | } |
| 100 | |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 101 | void getFileLists(set<string>* expected, set<string>* unexpected) { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 102 | unexpected->clear(); |
| 103 | expected->clear(); |
| 104 | |
| 105 | addAll(unexpected, INTRODUCED_R); |
| 106 | addAll(unexpected, INTRODUCED_S); |
| 107 | addAll(unexpected, INTRODUCED_T); |
| 108 | |
| 109 | if (IsAtLeastR()) { |
| 110 | addAll(expected, INTRODUCED_R); |
| 111 | removeAll(unexpected, INTRODUCED_R); |
| 112 | // Nothing removed in R. |
| 113 | } |
| 114 | |
| 115 | if (IsAtLeastS()) { |
| 116 | addAll(expected, INTRODUCED_S); |
| 117 | removeAll(expected, REMOVED_S); |
| 118 | |
| 119 | addAll(unexpected, REMOVED_S); |
| 120 | removeAll(unexpected, INTRODUCED_S); |
| 121 | } |
| 122 | |
| 123 | // Nothing added or removed in SCv2. |
| 124 | |
| 125 | if (IsAtLeastT()) { |
| 126 | addAll(expected, INTRODUCED_T); |
| 127 | removeAll(expected, REMOVED_T); |
| 128 | |
| 129 | addAll(unexpected, REMOVED_T); |
| 130 | removeAll(unexpected, INTRODUCED_T); |
| 131 | } |
| 132 | } |
| 133 | |
| 134 | void checkFiles() { |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 135 | set<string> mustExist; |
| 136 | set<string> mustNotExist; |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 137 | |
| 138 | getFileLists(&mustExist, &mustNotExist); |
| 139 | |
| 140 | for (const auto& file : mustExist) { |
| 141 | EXPECT_EQ(0, access(file.c_str(), R_OK)) << file << " does not exist"; |
| 142 | } |
| 143 | for (const auto& file : mustNotExist) { |
| 144 | int ret = access(file.c_str(), R_OK); |
| 145 | int err = errno; |
| 146 | EXPECT_EQ(-1, ret) << file << " unexpectedly exists"; |
| 147 | if (ret == -1) { |
| 148 | EXPECT_EQ(ENOENT, err) << " accessing " << file << " failed with errno " << err; |
| 149 | } |
| 150 | } |
| 151 | } |
| 152 | |
| 153 | TEST_F(BpfExistenceTest, TestPrograms) { |
| 154 | // Pre-flight check to ensure test has been updated. |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 155 | uint64_t buildVersionSdk = android_get_device_api_level(); |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 156 | ASSERT_NE(0, buildVersionSdk) << "Unable to determine device SDK version"; |
Lorenzo Colitti | 32bd071 | 2022-01-13 15:44:29 +0900 | [diff] [blame] | 157 | if (buildVersionSdk > __ANDROID_API_T__ && buildVersionSdk != __ANDROID_API_FUTURE__) { |
Lorenzo Colitti | 3b38b12 | 2022-01-12 16:06:07 +0900 | [diff] [blame] | 158 | FAIL() << "Unknown OS version " << buildVersionSdk << ", please update this test"; |
| 159 | } |
| 160 | |
| 161 | // Only unconfined root is guaranteed to be able to access everything in /sys/fs/bpf. |
| 162 | ASSERT_EQ(0, getuid()) << "This test must run as root."; |
| 163 | |
| 164 | checkFiles(); |
| 165 | } |