Skip to content

Commit

Permalink
tests: add some tests to scap stats
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Terzolo <andrea.terzolo@polito.it>
Co-authored-by: Melissa Kilby <melissa.kilby.oss@gmail.com>
  • Loading branch information
Andreagit97 and incertum committed May 10, 2023
1 parent 0ea59e4 commit 5dc917f
Show file tree
Hide file tree
Showing 3 changed files with 124 additions and 21 deletions.
48 changes: 41 additions & 7 deletions test/libscap/test_suites/engines/bpf/bpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -92,21 +92,55 @@ TEST(bpf, read_in_order)
scap_close(h);
}

TEST(bpf, scap_stats_check)
{
char error_buffer[FILENAME_MAX] = {0};
int ret = 0;
scap_t* h = open_bpf_engine(error_buffer, &ret, 4 * 4096, LIBSCAP_TEST_BPF_PROBE_PATH);
ASSERT_FALSE(!h || ret != SCAP_SUCCESS) << "unable to open bpf engine: " << error_buffer << std::endl;

scap_stats stats;

ASSERT_EQ(scap_start_capture(h), SCAP_SUCCESS);
ASSERT_EQ(scap_get_stats(h, &stats), SCAP_SUCCESS);
ASSERT_GT(stats.n_evts, 0);
ASSERT_EQ(scap_stop_capture(h), SCAP_SUCCESS);
scap_close(h);
}

TEST(bpf, scap_stats_v2_check_results)
{
char error_buffer[SCAP_LASTERR_SIZE] = {0};
int ret = 0;
scap_t* h = open_bpf_engine(error_buffer, &ret, 4 * 4096, LIBSCAP_TEST_BPF_PROBE_PATH);
ASSERT_FALSE(!h || ret != SCAP_SUCCESS) << "unable to open bpf engine: " << error_buffer << std::endl;

uint32_t flags = PPM_SCAP_STATS_KERNEL_COUNTERS | PPM_SCAP_STATS_LIBBPF_STATS;
uint32_t nstats;
int32_t rc;
const scap_stats_v2* stats_v2;
stats_v2 = scap_get_stats_v2(h, flags, &nstats, &rc);
const char* name = stats_v2[nstats-1].name;
ASSERT_GT(nstats, 0);
const scap_stats_v2* stats_v2 = scap_get_stats_v2(h, flags, &nstats, &rc);
ASSERT_EQ(rc, SCAP_SUCCESS);
ASSERT_GT(strlen(name), 3);
ASSERT_GT(nstats, 0);

/* These names should always be available */
std::unordered_set<std::string> minimal_stats_name = {"n_evts", "sys_enter.run_cnt", "sys_enter.run_time_ns", "sys_exit.run_cnt", "sys_exit.run_time_ns", "signal_deliver.run_cnt", "signal_deliver.run_time_ns"};

uint32_t i = 0;
for(const auto& stat_name : minimal_stats_name)
{
for(i = 0; i < nstats; i++)
{
if(stat_name.compare(stats_v2[i].name) == 0)
{
break;
}
}

if(i == nstats)
{
FAIL() << "unable to find stat '" << stat_name << "' into the array";
}
}
scap_close(h);
}

Expand All @@ -116,11 +150,11 @@ TEST(bpf, scap_stats_v2_check_empty)
int ret = 0;
scap_t* h = open_bpf_engine(error_buffer, &ret, 4 * 4096, LIBSCAP_TEST_BPF_PROBE_PATH);
ASSERT_FALSE(!h || ret != SCAP_SUCCESS) << "unable to open bpf engine: " << error_buffer << std::endl;

uint32_t flags = 0;
uint32_t nstats;
int32_t rc;
const scap_stats_v2* stats_v2;
stats_v2 = scap_get_stats_v2(h, flags, &nstats, &rc);
ASSERT_TRUE(scap_get_stats_v2(h, flags, &nstats, &rc));
ASSERT_EQ(nstats, 0);
ASSERT_EQ(rc, SCAP_SUCCESS);
scap_close(h);
Expand Down
48 changes: 41 additions & 7 deletions test/libscap/test_suites/engines/kmod/kmod.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -148,21 +148,55 @@ TEST(kmod, read_in_order)
scap_close(h);
}

TEST(kmod, scap_stats_check)
{
char error_buffer[FILENAME_MAX] = {0};
int ret = 0;
scap_t* h = open_kmod_engine(error_buffer, &ret, 4 * 4096, LIBSCAP_TEST_KERNEL_MODULE_PATH);
ASSERT_FALSE(!h || ret != SCAP_SUCCESS) << "unable to open kmod engine: " << error_buffer << std::endl;

scap_stats stats;

ASSERT_EQ(scap_start_capture(h), SCAP_SUCCESS);
ASSERT_EQ(scap_get_stats(h, &stats), SCAP_SUCCESS);
ASSERT_GT(stats.n_evts, 0);
ASSERT_EQ(scap_stop_capture(h), SCAP_SUCCESS);
scap_close(h);
}

TEST(kmod, scap_stats_v2_check_results)
{
char error_buffer[SCAP_LASTERR_SIZE] = {0};
int ret = 0;
scap_t* h = open_kmod_engine(error_buffer, &ret, 4 * 4096, LIBSCAP_TEST_KERNEL_MODULE_PATH);
ASSERT_FALSE(!h || ret != SCAP_SUCCESS) << "unable to open kmod engine: " << error_buffer << std::endl;

uint32_t flags = PPM_SCAP_STATS_KERNEL_COUNTERS | PPM_SCAP_STATS_LIBBPF_STATS;
uint32_t nstats;
int32_t rc;
const scap_stats_v2* stats_v2;
stats_v2 = scap_get_stats_v2(h, flags, &nstats, &rc);
const char* name = stats_v2[nstats-1].name;
ASSERT_GT(nstats, 0);
const scap_stats_v2* stats_v2 = scap_get_stats_v2(h, flags, &nstats, &rc);
ASSERT_EQ(rc, SCAP_SUCCESS);
ASSERT_GT(strlen(name), 3);
ASSERT_GT(nstats, 0);

/* These names should always be available */
std::unordered_set<std::string> minimal_stats_name = {"n_evts"};

uint32_t i = 0;
for(const auto& stat_name : minimal_stats_name)
{
for(i = 0; i < nstats; i++)
{
if(stat_name.compare(stats_v2[i].name) == 0)
{
break;
}
}

if(i == nstats)
{
FAIL() << "unable to find stat '" << stat_name << "' into the array";
}
}
scap_close(h);
}

Expand All @@ -172,11 +206,11 @@ TEST(kmod, scap_stats_v2_check_empty)
int ret = 0;
scap_t* h = open_kmod_engine(error_buffer, &ret, 4 * 4096, LIBSCAP_TEST_KERNEL_MODULE_PATH);
ASSERT_FALSE(!h || ret != SCAP_SUCCESS) << "unable to open kmod engine: " << error_buffer << std::endl;

uint32_t flags = 0;
uint32_t nstats;
int32_t rc;
const scap_stats_v2* stats_v2;
stats_v2 = scap_get_stats_v2(h, flags, &nstats, &rc);
ASSERT_TRUE(scap_get_stats_v2(h, flags, &nstats, &rc));
ASSERT_EQ(nstats, 0);
ASSERT_EQ(rc, SCAP_SUCCESS);
scap_close(h);
Expand Down
49 changes: 42 additions & 7 deletions test/libscap/test_suites/engines/modern_bpf/modern_bpf.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -193,22 +193,57 @@ TEST(modern_bpf, read_in_order_one_buffer_shared_between_all_possible_CPUs)
scap_close(h);
}

TEST(modern_bpf, scap_stats_check)
{
char error_buffer[FILENAME_MAX] = {0};
int ret = 0;
/* We use buffers of 1 MB to be sure that we don't have drops */
scap_t* h = open_modern_bpf_engine(error_buffer, &ret, 1 * 1024 * 1024, 0, false);
ASSERT_EQ(!h || ret != SCAP_SUCCESS, false) << "unable to open modern bpf engine with one single shared ring buffer: " << error_buffer << std::endl;

scap_stats stats;

ASSERT_EQ(scap_start_capture(h), SCAP_SUCCESS);
ASSERT_EQ(scap_get_stats(h, &stats), SCAP_SUCCESS);
ASSERT_GT(stats.n_evts, 0);
ASSERT_EQ(scap_stop_capture(h), SCAP_SUCCESS);
scap_close(h);
}

TEST(modern_bpf, scap_stats_v2_check_results)
{
char error_buffer[FILENAME_MAX] = {0};
int ret = 0;
/* We use buffers of 1 MB to be sure that we don't have drops */
scap_t* h = open_modern_bpf_engine(error_buffer, &ret, 1 * 1024 * 1024, 0, false);
ASSERT_EQ(!h || ret != SCAP_SUCCESS, false) << "unable to open modern bpf engine with one single shared ring buffer: " << error_buffer << std::endl;

uint32_t flags = PPM_SCAP_STATS_KERNEL_COUNTERS | PPM_SCAP_STATS_LIBBPF_STATS;
uint32_t nstats;
int32_t rc;
const scap_stats_v2* stats_v2;
stats_v2 = scap_get_stats_v2(h, flags, &nstats, &rc);
const char* name = stats_v2[nstats-1].name;
ASSERT_GT(nstats, 0);
const scap_stats_v2* stats_v2 = scap_get_stats_v2(h, flags, &nstats, &rc);
ASSERT_EQ(rc, SCAP_SUCCESS);
ASSERT_GT(strlen(name), 3);
ASSERT_GT(nstats, 0);

/* These names should always be available */
std::unordered_set<std::string> minimal_stats_name = {"n_evts", "sys_enter.run_cnt", "sys_enter.run_time_ns", "sys_exit.run_cnt", "sys_exit.run_time_ns", "signal_deliver.run_cnt", "signal_deliver.run_time_ns"};

uint32_t i = 0;
for(const auto& stat_name : minimal_stats_name)
{
for(i = 0; i < nstats; i++)
{
if(stat_name.compare(stats_v2[i].name) == 0)
{
break;
}
}

if(i == nstats)
{
FAIL() << "unable to find stat '" << stat_name << "' into the array";
}
}
scap_close(h);
}

Expand All @@ -219,11 +254,11 @@ TEST(modern_bpf, scap_stats_v2_check_empty)
/* We use buffers of 1 MB to be sure that we don't have drops */
scap_t* h = open_modern_bpf_engine(error_buffer, &ret, 1 * 1024 * 1024, 0, false);
ASSERT_EQ(!h || ret != SCAP_SUCCESS, false) << "unable to open modern bpf engine with one single shared ring buffer: " << error_buffer << std::endl;

uint32_t flags = 0;
uint32_t nstats;
int32_t rc;
const scap_stats_v2* stats_v2;
stats_v2 = scap_get_stats_v2(h, flags, &nstats, &rc);
ASSERT_TRUE(scap_get_stats_v2(h, flags, &nstats, &rc));
ASSERT_EQ(nstats, 0);
ASSERT_EQ(rc, SCAP_SUCCESS);
scap_close(h);
Expand Down

0 comments on commit 5dc917f

Please sign in to comment.