-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
test(libebpfdiscoveryskel): Implement unit tests for eBPF data utilit…
…y functions (#11)
- Loading branch information
Showing
10 changed files
with
194 additions
and
9 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,4 +1,19 @@ | ||
set(BPF_C_FLAGS ${BPF_C_FLAGS} -I${PROJECT_SOURCE_DIR}/libebpfdiscoveryshared/headers) | ||
set(TARGET ebpfdiscoveryskel) | ||
|
||
set(BPF_C_FLAGS ${BPF_C_FLAGS} -I${CMAKE_CURRENT_SOURCE_DIR}/src -I${PROJECT_SOURCE_DIR}/libebpfdiscoveryshared/headers) | ||
list(APPEND CMAKE_MODULE_PATH ${CMAKE_CURRENT_SOURCE_DIR}/cmake) | ||
find_package(BpfObject REQUIRED) | ||
bpf_object(ebpfdiscoveryskel src/discovery.bpf.c) | ||
bpf_object(${TARGET} src/discovery.bpf.c) | ||
|
||
if(BUILD_BPF_TESTS) | ||
set(TEST_SKEL_TARGET ebpfdiscoverytestskel) | ||
set(BPF_C_FLAGS ${BPF_C_FLAGS} -I${CMAKE_CURRENT_SOURCE_DIR}/test) | ||
bpf_object(${TEST_SKEL_TARGET} testbpf/discoveryTest.bpf.c) | ||
|
||
list(APPEND TEST_SOURCES test/DataFunctionsTest.cpp) | ||
set(TEST_TARGET test${TARGET}) | ||
|
||
add_executable(${TEST_TARGET} ${TEST_SOURCES}) | ||
target_link_libraries(${TEST_TARGET} GTest::gtest_main ${TEST_SKEL_TARGET}) | ||
gtest_discover_tests(${TEST_TARGET}) | ||
endif() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#include "DiscoveryTest.h" | ||
|
||
#include "discoveryTest.skel.h" | ||
|
||
#include <gtest/gtest.h> | ||
|
||
#include <optional> | ||
#include <tuple> | ||
#include <variant> | ||
|
||
using ebpfdiscovery::bpftest::attachBpfProgram; | ||
using ebpfdiscovery::bpftest::DiscoveryTest; | ||
using ebpfdiscovery::bpftest::triggerTracepoint; | ||
|
||
struct DiscoveryDataFunctionsTestParams { | ||
std::string inputPtrData; | ||
size_t inputLen; | ||
int expectedRet; | ||
}; | ||
|
||
class DiscoveryDataFunctionsTest : public DiscoveryTest, public ::testing::WithParamInterface<DiscoveryDataFunctionsTestParams> {}; | ||
class DataProbeIsBeginningOfHttpRequestTest : public DiscoveryDataFunctionsTest {}; | ||
|
||
TEST_P(DataProbeIsBeginningOfHttpRequestTest, Default) { | ||
const auto& data{GetParam()}; | ||
setInPtr(data.inputPtrData); | ||
setInLen(data.inputLen); | ||
|
||
attachBpfProgram(testSkel->progs.testDataProbeIsBeginningOfHttpRequest); | ||
triggerTracepoint(); | ||
|
||
EXPECT_EQ(getOutRet(), data.expectedRet); | ||
} | ||
|
||
INSTANTIATE_TEST_SUITE_P( | ||
Default, | ||
DataProbeIsBeginningOfHttpRequestTest, | ||
::testing::Values(DiscoveryDataFunctionsTestParams{ | ||
.inputPtrData = std::string("GET / HTTP/1.1\r\n"), .inputLen = 16, .expectedRet = (int)true})); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,81 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#pragma once | ||
|
||
#include "DiscoveryTestConstants.h" | ||
|
||
#include "discoveryTest.skel.h" | ||
|
||
#include <bpf/bpf.h> | ||
#include <bpf/libbpf.h> | ||
|
||
#include <gtest/gtest.h> | ||
|
||
namespace ebpfdiscovery::bpftest { | ||
|
||
class DiscoveryTest : public ::testing::Test { | ||
public: | ||
DiscoveryTest() : testSkel{nullptr}, testBss{nullptr} { | ||
} | ||
|
||
bool isLoaded() { | ||
return testSkel != nullptr && testBss != nullptr; | ||
} | ||
|
||
void setInPtr(const std::string& str) { | ||
checkLoaded(); | ||
inPtrSrc.clear(); | ||
std::copy(str.begin(), str.end(), std::back_inserter(inPtrSrc)); | ||
testBss->inPtr = inPtrSrc.data(); | ||
} | ||
|
||
void setInLen(size_t len) { | ||
checkLoaded(); | ||
testBss->inLen = len; | ||
} | ||
|
||
int getOutRet() { | ||
checkLoaded(); | ||
return testBss->outRet; | ||
} | ||
|
||
protected: | ||
void SetUp() override { | ||
testSkel = discoveryTest_bpf__open_and_load(); | ||
if (testSkel == nullptr) { | ||
GTEST_SKIP() << "Couldn't open and load BPF object for test execution."; | ||
} | ||
|
||
testBss = testSkel->bss; | ||
testBss->runnerPid = getpid(); | ||
} | ||
|
||
void TearDown() override { | ||
if (testSkel != nullptr) { | ||
discoveryTest_bpf__destroy(testSkel); | ||
} | ||
} | ||
|
||
discoveryTest_bpf* testSkel; | ||
discoveryTest_bpf::discoveryTest_bpf__bss* testBss; | ||
|
||
std::vector<char> inPtrData; | ||
|
||
void checkLoaded() { | ||
if (!isLoaded()) { | ||
throw std::runtime_error("DiscoveryTest is uninitialized"); | ||
} | ||
} | ||
}; | ||
|
||
void attachBpfProgram(bpf_program* prog) { | ||
auto link = bpf_program__attach(prog); | ||
if (link == nullptr) { | ||
throw std::runtime_error("couldn't attach bpf program"); | ||
} | ||
} | ||
|
||
void triggerTracepoint() { | ||
usleep(1); | ||
} | ||
|
||
} // namespace ebpfdiscovery::bpftest |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,4 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#pragma once | ||
|
||
#define DISCOVERY_TEST_MAX_INPUT_LEN 1024 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#pragma once | ||
|
||
#include "DataFunctions.h" | ||
|
||
#include "TestDefine.h" | ||
|
||
TEST_ENTRY int BPF_PROG(testDataProbeIsBeginningOfHttpRequest) { | ||
CHECK_TEST_RUNNER(runnerPid); | ||
|
||
if (inPtr != NULL && inLen < DISCOVERY_TEST_MAX_INPUT_LEN) { | ||
outRet = dataProbeIsBeginningOfHttpRequest(inPtr, inLen); | ||
} | ||
|
||
return 0; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,21 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#pragma once | ||
|
||
#include "DiscoveryTestConstants.h" | ||
|
||
#include "vmlinux.h" | ||
|
||
#include <bpf/bpf_helpers.h> | ||
#include <bpf/bpf_tracing.h> | ||
|
||
__u32 runnerPid = 0; | ||
|
||
char* inPtr = NULL; | ||
size_t inLen = 0; | ||
int outRet = 0; | ||
|
||
#define TEST_ENTRY SEC("fentry/do_nanosleep") | ||
#define CHECK_TEST_RUNNER(runnerPid) \ | ||
if ((bpf_get_current_pid_tgid() >> 32) != runnerPid) { \ | ||
return 0; \ | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,8 @@ | ||
// SPDX-License-Identifier: GPL-2.0 | ||
#include "DataFunctionsTest.h" | ||
|
||
#include "vmlinux.h" | ||
|
||
#include <bpf/bpf_helpers.h> | ||
|
||
char _license[] SEC("license") = "GPL"; |