Skip to content

Commit

Permalink
Introduce unit tests for the new BpfBytecode class
Browse files Browse the repository at this point in the history
For now, BpfBytecode only stores the sections, so move there the
corresponding test from tests/codegen/general.cpp. The advantage is that
now the test is executed for all LLVM versions instead of just LLVM 12.
  • Loading branch information
viktormalik authored and danobi committed Jan 17, 2024
1 parent 426c06f commit 0f4ff11
Show file tree
Hide file tree
Showing 3 changed files with 38 additions and 17 deletions.
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ endif()

add_executable(bpftrace_test
ast.cpp
bpfbytecode.cpp
bpftrace.cpp
child.cpp
clang_parser.cpp
Expand Down
37 changes: 37 additions & 0 deletions tests/bpfbytecode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
#include "bpfbytecode.h"
#include "driver.h"
#include "mocks.h"
#include "passes/codegen_llvm.h"
#include "passes/semantic_analyser.h"

#include "gtest/gtest.h"

namespace bpftrace {
namespace test {
namespace bpfbytecode {

BpfBytecode codegen(const std::string &input)
{
auto bpftrace = get_mock_bpftrace();

Driver driver(*bpftrace);
EXPECT_EQ(driver.parse_str(input), 0);

ast::SemanticAnalyser semantics(driver.root.get(), *bpftrace);
EXPECT_EQ(semantics.analyse(), 0);

ast::CodegenLLVM codegen(driver.root.get(), *bpftrace);
return codegen.compile();
}

TEST(bpfbytecode, populate_sections)
{
auto bytecode = codegen("kprobe:foo { 1 } kprobe:bar { 1 }");

EXPECT_TRUE(bytecode.hasSection("s_kprobe:foo_1"));
EXPECT_TRUE(bytecode.hasSection("s_kprobe:bar_2"));
}

} // namespace bpfbytecode
} // namespace test
} // namespace bpftrace
17 changes: 0 additions & 17 deletions tests/codegen/general.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -47,23 +47,6 @@ class MockBPFtrace : public BPFtrace
}
};

TEST(codegen, populate_sections)
{
auto bpftrace = get_mock_bpftrace();
Driver driver(*bpftrace);

ASSERT_EQ(driver.parse_str("kprobe:foo { 1 } kprobe:bar { 1 }"), 0);
// Override to mockbpffeature.
bpftrace->feature_ = std::make_unique<MockBPFfeature>(true);
ast::SemanticAnalyser semantics(driver.root.get(), *bpftrace);
ASSERT_EQ(semantics.analyse(), 0);
ast::CodegenLLVM codegen(driver.root.get(), *bpftrace);
auto bytecode = codegen.compile();

EXPECT_TRUE(bytecode.hasSection("s_kprobe:foo_1"));
EXPECT_TRUE(bytecode.hasSection("s_kprobe:bar_2"));
}

TEST(codegen, printf_offsets)
{
auto bpftrace = get_mock_bpftrace();
Expand Down

0 comments on commit 0f4ff11

Please sign in to comment.