blob: 10afe8603f312f39971cfab55e5677450877ae2d [file] [log] [blame]
Ken Chende0bb522021-10-25 20:49:37 +08001/*
2 * Copyright (C) 2018 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 <fstream>
18#include <iostream>
19#include <string>
20#include <vector>
21
22#include <fcntl.h>
23#include <inttypes.h>
24#include <linux/inet_diag.h>
25#include <linux/sock_diag.h>
26#include <net/if.h>
27#include <sys/socket.h>
28#include <sys/types.h>
29#include <unistd.h>
30
31#include <gtest/gtest.h>
32
33#include <android-base/stringprintf.h>
34#include <android-base/strings.h>
35
Maciej Żenczykowski2efccc52022-06-16 10:57:13 -070036#define BPF_MAP_MAKE_VISIBLE_FOR_TESTING
Ken Chende0bb522021-10-25 20:49:37 +080037#include "bpf/BpfMap.h"
38#include "bpf/BpfUtils.h"
39
40using ::testing::Test;
41
42namespace android {
43namespace bpf {
44
45using base::Result;
46using base::unique_fd;
47
48constexpr uint32_t TEST_MAP_SIZE = 10;
49constexpr uint32_t TEST_KEY1 = 1;
50constexpr uint32_t TEST_VALUE1 = 10;
51constexpr const char PINNED_MAP_PATH[] = "/sys/fs/bpf/testMap";
52
53class BpfMapTest : public testing::Test {
54 protected:
55 BpfMapTest() {}
56
57 void SetUp() {
58 EXPECT_EQ(0, setrlimitForTest());
59 if (!access(PINNED_MAP_PATH, R_OK)) {
60 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
61 }
62 }
63
64 void TearDown() {
65 if (!access(PINNED_MAP_PATH, R_OK)) {
66 EXPECT_EQ(0, remove(PINNED_MAP_PATH));
67 }
68 }
69
70 void checkMapInvalid(BpfMap<uint32_t, uint32_t>& map) {
71 EXPECT_FALSE(map.isValid());
72 EXPECT_EQ(-1, map.getMap().get());
73 }
74
75 void checkMapValid(BpfMap<uint32_t, uint32_t>& map) {
76 EXPECT_LE(0, map.getMap().get());
77 EXPECT_TRUE(map.isValid());
78 }
79
80 void writeToMapAndCheck(BpfMap<uint32_t, uint32_t>& map, uint32_t key, uint32_t value) {
81 ASSERT_RESULT_OK(map.writeValue(key, value, BPF_ANY));
82 uint32_t value_read;
83 ASSERT_EQ(0, findMapEntry(map.getMap(), &key, &value_read));
84 checkValueAndStatus(value, value_read);
85 }
86
87 void checkValueAndStatus(uint32_t refValue, Result<uint32_t> value) {
88 ASSERT_RESULT_OK(value);
89 ASSERT_EQ(refValue, value.value());
90 }
91
92 void populateMap(uint32_t total, BpfMap<uint32_t, uint32_t>& map) {
93 for (uint32_t key = 0; key < total; key++) {
94 uint32_t value = key * 10;
95 EXPECT_RESULT_OK(map.writeValue(key, value, BPF_ANY));
96 }
97 }
98
99 void expectMapEmpty(BpfMap<uint32_t, uint32_t>& map) {
100 Result<bool> isEmpty = map.isEmpty();
101 ASSERT_RESULT_OK(isEmpty);
102 ASSERT_TRUE(isEmpty.value());
103 }
104};
105
106TEST_F(BpfMapTest, constructor) {
107 BpfMap<uint32_t, uint32_t> testMap1;
108 checkMapInvalid(testMap1);
109
110 BpfMap<uint32_t, uint32_t> testMap2(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
111 checkMapValid(testMap2);
112}
113
114TEST_F(BpfMapTest, basicHelpers) {
115 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
116 uint32_t key = TEST_KEY1;
117 uint32_t value_write = TEST_VALUE1;
118 writeToMapAndCheck(testMap, key, value_write);
119 Result<uint32_t> value_read = testMap.readValue(key);
120 checkValueAndStatus(value_write, value_read);
121 Result<uint32_t> key_read = testMap.getFirstKey();
122 checkValueAndStatus(key, key_read);
123 ASSERT_RESULT_OK(testMap.deleteValue(key));
124 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_read));
125 ASSERT_EQ(ENOENT, errno);
126}
127
128TEST_F(BpfMapTest, reset) {
129 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
130 uint32_t key = TEST_KEY1;
131 uint32_t value_write = TEST_VALUE1;
132 writeToMapAndCheck(testMap, key, value_write);
133
134 testMap.reset(-1);
135 checkMapInvalid(testMap);
136 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
137 ASSERT_EQ(EBADF, errno);
138}
139
140TEST_F(BpfMapTest, moveConstructor) {
141 BpfMap<uint32_t, uint32_t> testMap1(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
142 BpfMap<uint32_t, uint32_t> testMap2;
143 testMap2 = std::move(testMap1);
144 uint32_t key = TEST_KEY1;
145 checkMapInvalid(testMap1);
146 uint32_t value = TEST_VALUE1;
147 writeToMapAndCheck(testMap2, key, value);
148}
149
150TEST_F(BpfMapTest, SetUpMap) {
151 EXPECT_NE(0, access(PINNED_MAP_PATH, R_OK));
152 BpfMap<uint32_t, uint32_t> testMap1(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
153 ASSERT_EQ(0, bpfFdPin(testMap1.getMap(), PINNED_MAP_PATH));
154 EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
155 checkMapValid(testMap1);
156 BpfMap<uint32_t, uint32_t> testMap2;
157 EXPECT_RESULT_OK(testMap2.init(PINNED_MAP_PATH));
158 checkMapValid(testMap2);
159 uint32_t key = TEST_KEY1;
160 uint32_t value = TEST_VALUE1;
161 writeToMapAndCheck(testMap1, key, value);
162 Result<uint32_t> value_read = testMap2.readValue(key);
163 checkValueAndStatus(value, value_read);
164}
165
166TEST_F(BpfMapTest, iterate) {
167 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
168 populateMap(TEST_MAP_SIZE, testMap);
169 int totalCount = 0;
170 int totalSum = 0;
171 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
172 BpfMap<uint32_t, uint32_t>& map) {
173 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
174 totalCount++;
175 totalSum += key;
176 return map.deleteValue(key);
177 };
178 EXPECT_RESULT_OK(testMap.iterate(iterateWithDeletion));
179 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
180 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) / 2, (uint32_t)totalSum);
181 expectMapEmpty(testMap);
182}
183
184TEST_F(BpfMapTest, iterateWithValue) {
185 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
186 populateMap(TEST_MAP_SIZE, testMap);
187 int totalCount = 0;
188 int totalSum = 0;
189 const auto iterateWithDeletion = [&totalCount, &totalSum](const uint32_t& key,
190 const uint32_t& value,
191 BpfMap<uint32_t, uint32_t>& map) {
192 EXPECT_GE((uint32_t)TEST_MAP_SIZE, key);
193 EXPECT_EQ(value, key * 10);
194 totalCount++;
195 totalSum += value;
196 return map.deleteValue(key);
197 };
198 EXPECT_RESULT_OK(testMap.iterateWithValue(iterateWithDeletion));
199 EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
200 EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) * 5, (uint32_t)totalSum);
201 expectMapEmpty(testMap);
202}
203
204TEST_F(BpfMapTest, mapIsEmpty) {
205 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
206 expectMapEmpty(testMap);
207 uint32_t key = TEST_KEY1;
208 uint32_t value_write = TEST_VALUE1;
209 writeToMapAndCheck(testMap, key, value_write);
210 Result<bool> isEmpty = testMap.isEmpty();
211 ASSERT_RESULT_OK(isEmpty);
212 ASSERT_FALSE(isEmpty.value());
213 ASSERT_RESULT_OK(testMap.deleteValue(key));
214 ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
215 ASSERT_EQ(ENOENT, errno);
216 expectMapEmpty(testMap);
217 int entriesSeen = 0;
218 EXPECT_RESULT_OK(testMap.iterate(
219 [&entriesSeen](const unsigned int&,
220 const BpfMap<unsigned int, unsigned int>&) -> Result<void> {
221 entriesSeen++;
222 return {};
223 }));
224 EXPECT_EQ(0, entriesSeen);
225 EXPECT_RESULT_OK(testMap.iterateWithValue(
226 [&entriesSeen](const unsigned int&, const unsigned int&,
227 const BpfMap<unsigned int, unsigned int>&) -> Result<void> {
228 entriesSeen++;
229 return {};
230 }));
231 EXPECT_EQ(0, entriesSeen);
232}
233
234TEST_F(BpfMapTest, mapClear) {
235 BpfMap<uint32_t, uint32_t> testMap(BPF_MAP_TYPE_HASH, TEST_MAP_SIZE, BPF_F_NO_PREALLOC);
236 populateMap(TEST_MAP_SIZE, testMap);
237 Result<bool> isEmpty = testMap.isEmpty();
238 ASSERT_RESULT_OK(isEmpty);
239 ASSERT_FALSE(*isEmpty);
240 ASSERT_RESULT_OK(testMap.clear());
241 expectMapEmpty(testMap);
242}
243
244} // namespace bpf
245} // namespace android