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