Skip to content

Commit

Permalink
Replace StatusTuple::code() with StatusTuple::ok() in tests/
Browse files Browse the repository at this point in the history
  • Loading branch information
yzhao1012 authored and yonghong-song committed Dec 14, 2021
1 parent 15340c4 commit b2f78da
Show file tree
Hide file tree
Showing 13 changed files with 167 additions and 167 deletions.
22 changes: 11 additions & 11 deletions tests/cc/test_array_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ TEST_CASE("test array table", "[array_table]") {
ebpf::BPF bpf(0, nullptr, false);
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

ebpf::BPFArrayTable<int> t = bpf.get_array_table<int>("myarray");

Expand All @@ -52,24 +52,24 @@ TEST_CASE("test array table", "[array_table]") {
v1 = 42;
// update element
res = t.update_value(i, v1);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
res = t.get_value(i, v2);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(v2 == 42);

// update another element
i = 2;
v1 = 69;
res = t.update_value(i, v1);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
res = t.get_value(i, v2);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(v2 == 69);

// get non existing element
i = 1024;
res = t.get_value(i, v2);
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());
}

SECTION("full table") {
Expand All @@ -84,7 +84,7 @@ TEST_CASE("test array table", "[array_table]") {
int v = dist(rng);

res = t.update_value(i, v);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

// save it in the local table to compare later on
localtable[i] = v;
Expand All @@ -105,7 +105,7 @@ TEST_CASE("percpu array table", "[percpu_array_table]") {
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

ebpf::BPFPercpuArrayTable<uint64_t> t = bpf.get_percpu_array_table<uint64_t>("myarray");
size_t ncpus = ebpf::BPFTable::get_possible_cpu_count();
Expand All @@ -131,9 +131,9 @@ TEST_CASE("percpu array table", "[percpu_array_table]") {
i = 1;
// update element
res = t.update_value(i, v1);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
res = t.get_value(i, v2);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(v2.size() == ncpus);
for (size_t j = 0; j < ncpus; j++) {
REQUIRE(v2.at(j) == 42 * j);
Expand All @@ -142,7 +142,7 @@ TEST_CASE("percpu array table", "[percpu_array_table]") {
// get non existing element
i = 1024;
res = t.get_value(i, v2);
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());
}
}
#endif
60 changes: 30 additions & 30 deletions tests/cc/test_bpf_table.cc
Original file line number Diff line number Diff line change
Expand Up @@ -30,35 +30,35 @@ TEST_CASE("test bpf table", "[bpf_table]") {
ebpf::StatusTuple res(0);
std::vector<std::pair<std::string, std::string>> elements;
res = bpf->init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

ebpf::BPFTable t = bpf->get_table("myhash");

// update element
std::string value;
res = t.update_value("0x07", "0x42");
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
res = t.get_value("0x7", value);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(value == "0x42");

// update another element
res = t.update_value("0x11", "0x777");
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
res = t.get_value("0x11", value);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(value == "0x777");

// remove value
res = t.remove_value("0x11");
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
res = t.get_value("0x11", value);
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());

res = t.update_value("0x15", "0x888");
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
res = t.get_table_offline(elements);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(elements.size() == 2);

// check that elements match what is in the table
Expand All @@ -73,22 +73,22 @@ TEST_CASE("test bpf table", "[bpf_table]") {
}

res = t.clear_table_non_atomic();
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
res = t.get_table_offline(elements);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(elements.size() == 0);

// delete bpf_module, call to key/leaf printf/scanf must fail
delete bpf;

res = t.update_value("0x07", "0x42");
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());

res = t.get_value("0x07", value);
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());

res = t.remove_value("0x07");
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());
}

#if LINUX_VERSION_CODE >= KERNEL_VERSION(4, 6, 0)
Expand All @@ -100,7 +100,7 @@ TEST_CASE("test bpf percpu tables", "[bpf_percpu_table]") {
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

ebpf::BPFTable t = bpf.get_table("myhash");
size_t ncpus = ebpf::BPFTable::get_possible_cpu_count();
Expand All @@ -113,9 +113,9 @@ TEST_CASE("test bpf percpu tables", "[bpf_percpu_table]") {
// update element
std::vector<std::string> value;
res = t.update_value("0x07", v1);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
res = t.get_value("0x07", value);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
for (size_t i = 0; i < ncpus; i++) {
REQUIRE(42 * i == std::stoul(value.at(i), nullptr, 16));
}
Expand All @@ -130,7 +130,7 @@ TEST_CASE("test bpf hash table", "[bpf_hash_table]") {
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

auto t = bpf.get_hash_table<int, int>("myhash");

Expand All @@ -140,34 +140,34 @@ TEST_CASE("test bpf hash table", "[bpf_hash_table]") {
key = 0x08;
value = 0x43;
res = t.update_value(key, value);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(t[key] == value);

// update another element
key = 0x12;
value = 0x778;
res = t.update_value(key, value);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
key = 0x31;
value = 0x123;
res = t.update_value(key, value);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
key = 0x12;
value = 0;
res = t.get_value(key, value);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(value == 0x778);

// remove value and dump table
key = 0x12;
res = t.remove_value(key);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
auto values = t.get_table_offline();
REQUIRE(values.size() == 2);

// clear table
res = t.clear_table_non_atomic();
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
values = t.get_table_offline();
REQUIRE(values.size() == 0);
}
Expand All @@ -193,13 +193,13 @@ TEST_CASE("test bpf stack table", "[bpf_stack_table]") {
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
std::string getuid_fnname = bpf.get_syscall_fnname("getuid");
res = bpf.attach_kprobe(getuid_fnname, "on_sys_getuid");
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(getuid() >= 0);
res = bpf.detach_kprobe(getuid_fnname);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

auto id = bpf.get_hash_table<int, int>("id");
auto stack_traces = bpf.get_stack_table("stack_traces");
Expand Down Expand Up @@ -246,13 +246,13 @@ TEST_CASE("test bpf stack_id table", "[bpf_stack_table]") {
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
std::string getuid_fnname = bpf.get_syscall_fnname("getuid");
res = bpf.attach_kprobe(getuid_fnname, "on_sys_getuid");
REQUIRE(res.code() == 0);
REQUIRE(res.ok());
REQUIRE(getuid() >= 0);
res = bpf.detach_kprobe(getuid_fnname);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

auto id = bpf.get_hash_table<int, int>("id");
auto stack_traces = bpf.get_stackbuildid_table("stack_traces");
Expand Down
12 changes: 6 additions & 6 deletions tests/cc/test_cg_storage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ int test(struct bpf_sock_ops *skops)
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

auto cg_storage = bpf.get_cg_storage_table<int>("cg_storage1");
struct bpf_cgroup_storage_key key = {0};
Expand All @@ -57,10 +57,10 @@ int test(struct bpf_sock_ops *skops)
// all the following lookup/update will fail since
// cgroup local storage only created during prog attachment time.
res = cg_storage.get_value(key, val);
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());

res = cg_storage.update_value(key, val);
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());
}
}
#endif
Expand All @@ -87,7 +87,7 @@ int test(struct bpf_sock_ops *skops)
ebpf::BPF bpf;
ebpf::StatusTuple res(0);
res = bpf.init(BPF_PROGRAM);
REQUIRE(res.code() == 0);
REQUIRE(res.ok());

auto cg_storage = bpf.get_percpu_cg_storage_table<long long>("cg_storage1");
struct bpf_cgroup_storage_key key = {0};
Expand All @@ -96,10 +96,10 @@ int test(struct bpf_sock_ops *skops)
// all the following lookup/update will fail since
// cgroup local storage only created during prog attachment time.
res = cg_storage.get_value(key, val);
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());

res = cg_storage.update_value(key, val);
REQUIRE(res.code() != 0);
REQUIRE(!res.ok());
}
}

Expand Down
Loading

0 comments on commit b2f78da

Please sign in to comment.