libbpf_android: remove libnetdutils dep

Remove libnetdutils dependency because bpf is being used for things other
than networking these days, and we don't want to make libnetdutils
vendor-available in the future. libbase provides an alternative type now.

Bug: 140330870
Test: atest libbpf_android_test netd_integration_test netd_unit_test
    libnetdbpf_test bpf_module_test
Change-Id: I72ae8cd7f58a49bfc7dcb914a332a4c4bad5dea5
This commit is contained in:
Steven Moreland 2020-01-10 17:49:35 -08:00
commit e7cd2a72d0

View file

@ -41,8 +41,8 @@ using ::testing::Test;
namespace android {
namespace bpf {
using base::Result;
using base::unique_fd;
using netdutils::StatusOr;
constexpr uint32_t TEST_MAP_SIZE = 10;
constexpr uint32_t TEST_KEY1 = 1;
@ -93,27 +93,27 @@ class BpfMapTest : public testing::Test {
}
void writeToMapAndCheck(BpfMap<uint32_t, uint32_t>& map, uint32_t key, uint32_t value) {
ASSERT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
ASSERT_TRUE(map.writeValue(key, value, BPF_ANY));
uint32_t value_read;
ASSERT_EQ(0, findMapEntry(map.getMap(), &key, &value_read));
checkValueAndStatus(value, value_read);
}
void checkValueAndStatus(uint32_t refValue, StatusOr<uint32_t> value) {
ASSERT_TRUE(isOk(value.status()));
void checkValueAndStatus(uint32_t refValue, Result<uint32_t> value) {
ASSERT_TRUE(value);
ASSERT_EQ(refValue, value.value());
}
void populateMap(uint32_t total, BpfMap<uint32_t, uint32_t>& map) {
for (uint32_t key = 0; key < total; key++) {
uint32_t value = key * 10;
EXPECT_TRUE(isOk(map.writeValue(key, value, BPF_ANY)));
EXPECT_TRUE(map.writeValue(key, value, BPF_ANY));
}
}
void expectMapEmpty(BpfMap<uint32_t, uint32_t>& map) {
auto isEmpty = map.isEmpty();
ASSERT_TRUE(isOk(isEmpty));
Result<bool> isEmpty = map.isEmpty();
ASSERT_TRUE(isEmpty);
ASSERT_TRUE(isEmpty.value());
}
};
@ -138,11 +138,11 @@ TEST_F(BpfMapTest, basicHelpers) {
uint32_t key = TEST_KEY1;
uint32_t value_write = TEST_VALUE1;
writeToMapAndCheck(testMap, key, value_write);
StatusOr<uint32_t> value_read = testMap.readValue(key);
Result<uint32_t> value_read = testMap.readValue(key);
checkValueAndStatus(value_write, value_read);
StatusOr<uint32_t> key_read = testMap.getFirstKey();
Result<uint32_t> key_read = testMap.getFirstKey();
checkValueAndStatus(key, key_read);
ASSERT_TRUE(isOk(testMap.deleteValue(key)));
ASSERT_TRUE(testMap.deleteValue(key));
ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_read));
ASSERT_EQ(ENOENT, errno);
}
@ -183,12 +183,12 @@ TEST_F(BpfMapTest, SetUpMap) {
EXPECT_EQ(0, access(PINNED_MAP_PATH, R_OK));
checkMapValid(testMap1);
BpfMap<uint32_t, uint32_t> testMap2;
EXPECT_OK(testMap2.init(PINNED_MAP_PATH));
EXPECT_TRUE(testMap2.init(PINNED_MAP_PATH));
checkMapValid(testMap2);
uint32_t key = TEST_KEY1;
uint32_t value = TEST_VALUE1;
writeToMapAndCheck(testMap1, key, value);
StatusOr<uint32_t> value_read = testMap2.readValue(key);
Result<uint32_t> value_read = testMap2.readValue(key);
checkValueAndStatus(value, value_read);
}
@ -206,7 +206,7 @@ TEST_F(BpfMapTest, iterate) {
totalSum += key;
return map.deleteValue(key);
};
EXPECT_OK(testMap.iterate(iterateWithDeletion));
EXPECT_TRUE(testMap.iterate(iterateWithDeletion));
EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) / 2, (uint32_t)totalSum);
expectMapEmpty(testMap);
@ -228,7 +228,7 @@ TEST_F(BpfMapTest, iterateWithValue) {
totalSum += value;
return map.deleteValue(key);
};
EXPECT_OK(testMap.iterateWithValue(iterateWithDeletion));
EXPECT_TRUE(testMap.iterateWithValue(iterateWithDeletion));
EXPECT_EQ((int)TEST_MAP_SIZE, totalCount);
EXPECT_EQ(((1 + TEST_MAP_SIZE - 1) * (TEST_MAP_SIZE - 1)) * 5, (uint32_t)totalSum);
expectMapEmpty(testMap);
@ -242,27 +242,27 @@ TEST_F(BpfMapTest, mapIsEmpty) {
uint32_t key = TEST_KEY1;
uint32_t value_write = TEST_VALUE1;
writeToMapAndCheck(testMap, key, value_write);
auto isEmpty = testMap.isEmpty();
ASSERT_TRUE(isOk(isEmpty));
Result<bool> isEmpty = testMap.isEmpty();
ASSERT_TRUE(isEmpty);
ASSERT_FALSE(isEmpty.value());
ASSERT_TRUE(isOk(testMap.deleteValue(key)));
ASSERT_TRUE(testMap.deleteValue(key));
ASSERT_GT(0, findMapEntry(testMap.getMap(), &key, &value_write));
ASSERT_EQ(ENOENT, errno);
expectMapEmpty(testMap);
int entriesSeen = 0;
EXPECT_OK(testMap.iterate(
[&entriesSeen](const unsigned int&,
const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
entriesSeen++;
return netdutils::status::ok;
}));
EXPECT_TRUE(testMap.iterate(
[&entriesSeen](const unsigned int&,
const BpfMap<unsigned int, unsigned int>&) -> Result<void> {
entriesSeen++;
return {};
}));
EXPECT_EQ(0, entriesSeen);
EXPECT_OK(testMap.iterateWithValue(
[&entriesSeen](const unsigned int&, const unsigned int&,
const BpfMap<unsigned int, unsigned int>&) -> netdutils::Status {
entriesSeen++;
return netdutils::status::ok;
}));
EXPECT_TRUE(testMap.iterateWithValue(
[&entriesSeen](const unsigned int&, const unsigned int&,
const BpfMap<unsigned int, unsigned int>&) -> Result<void> {
entriesSeen++;
return {};
}));
EXPECT_EQ(0, entriesSeen);
}
@ -271,10 +271,10 @@ TEST_F(BpfMapTest, mapClear) {
BpfMap<uint32_t, uint32_t> testMap(mMapFd.release());
populateMap(TEST_MAP_SIZE, testMap);
auto isEmpty = testMap.isEmpty();
ASSERT_TRUE(isOk(isEmpty));
ASSERT_FALSE(isEmpty.value());
ASSERT_TRUE(isOk(testMap.clear()));
Result<bool> isEmpty = testMap.isEmpty();
ASSERT_TRUE(isEmpty);
ASSERT_FALSE(*isEmpty);
ASSERT_TRUE(testMap.clear());
expectMapEmpty(testMap);
}