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