blob: 142e01334de2f533d0e518afc03121f054971c75 [file] [log] [blame]
Lorenzo Colitti3b38b122022-01-12 16:06:07 +09001/*
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 Colitti8db39c42022-01-13 16:11:31 +090016 * bpf_existence_test.cpp - checks that the device has expected BPF programs and maps
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090017 */
18
19#include <cstdint>
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090020#include <set>
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090021#include <string>
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090022
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090023#include <android/api-level.h>
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090024#include <android-base/properties.h>
25#include <android-modules-utils/sdk_level.h>
26
27#include <gtest/gtest.h>
28
29using std::find;
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090030using std::set;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090031using std::string;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090032
33using android::modules::sdklevel::IsAtLeastR;
34using android::modules::sdklevel::IsAtLeastS;
35using android::modules::sdklevel::IsAtLeastT;
36
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090037// 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 Colitti8db39c42022-01-13 16:11:31 +090042#define PLATFORM "/sys/fs/bpf/"
43#define TETHERING "/sys/fs/bpf/tethering/"
44
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090045class BpfExistenceTest : public ::testing::Test {
46};
47
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090048static const set<string> INTRODUCED_R = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090049 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 Colitti3b38b122022-01-12 16:06:07 +090054};
55
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090056static const set<string> INTRODUCED_S = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090057 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 Colitti3b38b122022-01-12 16:06:07 +090075};
76
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090077static const set<string> REMOVED_S = {
Lorenzo Colitti8db39c42022-01-13 16:11:31 +090078 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 Colitti3b38b122022-01-12 16:06:07 +090083};
84
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090085static const set<string> INTRODUCED_T = {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090086};
87
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090088static const set<string> REMOVED_T = {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090089};
90
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090091void addAll(set<string>* a, const set<string>& b) {
92 a->insert(b.begin(), b.end());
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090093}
94
Lorenzo Colitti32999372022-01-14 09:37:25 +090095void removeAll(set<string>* a, const set<string>& b) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090096 for (const auto& toRemove : b) {
Lorenzo Colitti32bd0712022-01-13 15:44:29 +090097 a->erase(toRemove);
Lorenzo Colitti3b38b122022-01-12 16:06:07 +090098 }
99}
100
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900101void getFileLists(set<string>* expected, set<string>* unexpected) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900102 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
134void checkFiles() {
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900135 set<string> mustExist;
136 set<string> mustNotExist;
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900137
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
153TEST_F(BpfExistenceTest, TestPrograms) {
154 // Pre-flight check to ensure test has been updated.
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900155 uint64_t buildVersionSdk = android_get_device_api_level();
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900156 ASSERT_NE(0, buildVersionSdk) << "Unable to determine device SDK version";
Lorenzo Colitti32bd0712022-01-13 15:44:29 +0900157 if (buildVersionSdk > __ANDROID_API_T__ && buildVersionSdk != __ANDROID_API_FUTURE__) {
Lorenzo Colitti3b38b122022-01-12 16:06:07 +0900158 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}