Skip to content

Commit

Permalink
Add more unit tests to increase coverage (#355)
Browse files Browse the repository at this point in the history
* update

* update

* increase coverage

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* update

* Update

* update

---------

Co-authored-by: Officeyutong <[email protected]>
  • Loading branch information
kenlig and Officeyutong authored Oct 29, 2024
1 parent 327ba8d commit 808ce59
Show file tree
Hide file tree
Showing 11 changed files with 163 additions and 10 deletions.
83 changes: 79 additions & 4 deletions .github/workflows/test-examples.yml
Original file line number Diff line number Diff line change
Expand Up @@ -28,11 +28,20 @@ jobs:
- uses: actions/checkout@v2
with:
submodules: 'recursive'
- name: Install lcov (Ubuntu)
if: ${{matrix.container.name=='ubuntu'}}
run: |
apt-get install -y lcov tree
- name: Install lcov (Fedora)
if: ${{matrix.container.name =='fedora'}}
run: |
dnf install -y lcov tree
- name: Build and install runtime (with llvm-jit)
if: ${{matrix.enable_jit}}
run: |
cmake -Bbuild -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
-DBPFTIME_LLVM_JIT=1 \
-DTEST_LCOV=YES \
-DBUILD_BPFTIME_DAEMON=1 \
-DCMAKE_CXX_FLAGS="-DDEFAULT_LOGGER_OUTPUT_PATH='\"console\"'"
cmake --build build --config RelWithDebInfo --target install -j
Expand All @@ -41,12 +50,26 @@ jobs:
run: |
cmake -Bbuild -DCMAKE_BUILD_TYPE:STRING=RelWithDebInfo \
-DBPFTIME_LLVM_JIT=0 \
-DTEST_LCOV=YES \
-DBUILD_BPFTIME_DAEMON=1 \
-DCMAKE_CXX_FLAGS="-DDEFAULT_LOGGER_OUTPUT_PATH='\"console\"'"
cmake --build build --config RelWithDebInfo --target install -j
- name: Build basic examples
run: |
make -C example -j
- name: Package gcno files
run: |
find . -type f -name "*.gcno" | tar --create --gzip --file=gcno_files.tar.gz --files-from=-
ls -la
pwd
tree .
- name: Upload gcno file
uses: actions/upload-artifact@v4
with:
name: gcno-files-${{matrix.enable_jit && 'jit' || 'no-jit'}}-${{matrix.container.name}}
include-hidden-files: true
path: |
./gcno_files.tar.gz
- name: Upload build results (without jit)
uses: actions/upload-artifact@v4
if: ${{!matrix.enable_jit}}
Expand Down Expand Up @@ -104,68 +127,84 @@ jobs:
victim: ./victim
syscall_trace: true
expected_str: " 0 test.txt"
name: opensnoop-libbpf-tools
- path: libbpf-tools/statsnoop
executable: ./statsnoop
victim: ./victim
syscall_trace: true
expected_str: "victim 0 0 /sys"
name: statsnoop
- path: malloc
executable: ./malloc
victim: ./victim
syscall_trace: false
expected_str: "malloc calls: "
name: malloc
- path: minimal
executable: ./uprobe
victim: ./victim
syscall_trace: false
expected_str: ""
name: minimal
- path: opensnoop
executable: ./opensnoop
victim: ./victim
syscall_trace: true
expected_str: " 0 test.txt"
name: opensnoop
- path: sslsniff
executable: ./sslsniff
victim: /bin/wget https://www.google.com
syscall_trace: false
expected_str: "----- DATA -----"
name: sslsniff
- path: libbpf-tools/bashreadline
executable: ./readline
victim: /bin/bash
syscall_trace: false
expected_str: "info"
name: bashreadline
- path: libbpf-tools/syscount
executable: ./syscount
victim: /bin/bash
syscall_trace: false
expected_str: "info"
name: syscount
- path: libbpf-tools/funclatency
executable: ./funclatency -i 1 ./victim:plus
victim: ./victim
syscall_trace: false
expected_str: "|*"
name: funclatency
- path: libbpf-tools/mountsnoop
executable: ./mountsnoop
victim: ./victim
syscall_trace: true
expected_str: mount(
name: mountsnoop
- path: libbpf-tools/sigsnoop
executable: ./sigsnoop
victim: ./victim
syscall_trace: true
expected_str: "victim 0 -1 0"
name: sigsnoop
- path: tailcall_minimal
executable: ./tailcall_minimal
victim: ./victim
syscall_trace: false
expected_str: "See /sys/kernel/debug/tracing/trace_pipe for output (15)"
name: tailcall_minimal
- path: usdt_minimal
executable: ./usdt_minimal
victim: ./victim
syscall_trace: false
expected_str: "bpf:"
name: usdt_minimal

steps:
- uses: actions/checkout@v2
with:
submodules: 'recursive'
- name: Download prebuilt runtime (with jit)
if: ${{matrix.enable_jit}}
uses: actions/download-artifact@v4
Expand All @@ -178,6 +217,16 @@ jobs:
with:
name: runtime-package-no-jit-${{matrix.container.name}}
path: ~/.bpftime
- name: Download gcno files
uses: actions/download-artifact@v4
with:
name: gcno-files-${{matrix.enable_jit && 'jit' || 'no-jit'}}-${{matrix.container.name}}
path: .
- name: Extract gcno files
run: |
ls -lah .
tar -zxvf gcno_files.tar.gz
pwd
- name: Install which(required by funclatency on fedora)
if: ${{matrix.container.name=='fedora' && matrix.examples.path=='libbpf-tools/funclatency'}}
run: |
Expand All @@ -188,9 +237,6 @@ jobs:
- name: Show downloaded artifacts
run: |
ls ~/.bpftime
- uses: actions/checkout@v2
with:
submodules: 'recursive'
- name: Build test assets
run: |
make -C example/${{matrix.examples.path}} -j
Expand All @@ -212,9 +258,38 @@ jobs:
ROOT=$(pwd)
cd example/${{matrix.examples.path}}
python3 $ROOT/.github/script/run_example.py "${{matrix.examples.executable}}" "${{matrix.examples.victim}}" "${{matrix.examples.expected_str}}" "/github/home/.bpftime/bpftime -i /github/home/.bpftime" 0
- name: Generate example coverage (Ubuntu)
if: "matrix.container.image == 'ubuntu-2204'"
run: |
apt-get install -y gpg lcov
lcov --capture --directory . --directory .github --directory /github/home/.bpftime --output-file coverage-example.info --gcov-tool $(which gcov-12)
lcov --remove coverage-example.info '/usr/*' --output-file coverage-example.info
lcov --list coverage-example.info
- name: Generate example coverage (Fedora)
if: "matrix.container.image == 'fedora-39'"
run: |
dnf install -y lcov
lcov --capture --directory . --output-file coverage-example.info
lcov --remove coverage-example.info '/usr/*' --output-file coverage-example.info
lcov --list coverage-example.info
- name: Upload example coverage
uses: actions/upload-artifact@v4
with:
name: coverage-example-${{matrix.container.name}}-${{matrix.enable_jit}}-${{matrix.examples.name}}-${{matrix.privilege_options.enable}}
include-hidden-files: false
path: |
./coverage-example.info
- uses: codecov/codecov-action@v4
with:
fail_ci_if_error: true # optional (default = false)
files: ./coverage-example.info
flags: coverage-example-${{matrix.container.name}}-${{matrix.enable_jit}}-${{matrix.examples.name}}-${{matrix.privilege_options.enable}}
token: ${{ secrets.CODECOV_TOKEN }} # required
verbose: true # optional (default = false)

- name: Setup tmate session
# Setup SSH when manually triggered and failing, so we can debug CI more conveniently
if: "${{ failure() && github.event_name == 'workflow_dispatch' }}"
uses: mxschmitt/action-tmate@v3
with:
limit-access-to-actor: true
limit-access-to-actor: false
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,4 @@ RUN git submodule update --init --recursive
ENV CXX=g++
ENV CC=gcc
RUN make release
ENV PATH="${PATH}:/root/.bpftime/"
ENV PATH="${PATH}:/root/.bpftime/"
7 changes: 5 additions & 2 deletions attach/base_attach_impl/base_attach_impl.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
#include <functional>
#include <optional>
#include "attach_private_data.hpp"
#include <stdexcept>
namespace bpftime
{
namespace attach
Expand Down Expand Up @@ -71,7 +72,8 @@ inline uint64_t bpftime_set_retval(uint64_t value)
} else {
spdlog::error(
"Called bpftime_set_retval, but no retval callback was set");
assert(false);
throw std::invalid_argument(
"Called bpftime_set_retval, but no retval callback was set");
}
return 0;
}
Expand All @@ -87,7 +89,8 @@ inline uint64_t bpftime_override_return(uint64_t ctx, uint64_t value)
} else {
spdlog::error(
"Called bpftime_override_return, but no retval callback was set");
assert(false);
throw std::invalid_argument(
"Called bpftime_override_return, but no retval callback was set");
}
return 0;
}
Expand Down
2 changes: 2 additions & 0 deletions attach/frida_uprobe_attach_impl/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,8 @@ set(TEST_SOURCES
test/test_filter_attach_with_override.cpp
test/test_replace_attach_with_override.cpp
test/test_attach_with_unified_interface.cpp
test/test_attach_private_data_parsing.cpp
test/test_base_attach_impl.cpp
)
option(TEST_LCOV "option for lcov" OFF)
add_executable(bpftime_frida_uprobe_attach_tests ${TEST_SOURCES})
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <catch2/catch_test_macros.hpp>
#include <cerrno>
#include <memory>
#include <frida_attach_private_data.hpp>
using namespace bpftime;

TEST_CASE("Test illegal parsing")
{
SECTION("Test bad strings")
{
auto priv = std::make_unique<
bpftime::attach::frida_attach_private_data>();
REQUIRE(priv->initialize_from_string("aaa:") == -EINVAL);
}
}
32 changes: 32 additions & 0 deletions attach/frida_uprobe_attach_impl/test/test_base_attach_impl.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
#include "catch2/catch_test_macros.hpp"
#include <base_attach_impl.hpp>
#include <catch2/catch_test_macros.hpp>
#include <cstdint>

using namespace bpftime::attach;
using namespace bpftime;

TEST_CASE("Test bpftime_set_retval")
{
curr_thread_override_return_callback.reset();
REQUIRE_THROWS(bpftime_set_retval(0));
bool called = false;
curr_thread_override_return_callback = [&](uint64_t, uint64_t) {
called = true;
};
bpftime_set_retval(0);
REQUIRE(called);
}

TEST_CASE("Test bpftime_override_return")
{
curr_thread_override_return_callback.reset();
REQUIRE_THROWS(bpftime_override_return(0, 0));

bool called = false;
curr_thread_override_return_callback = [&](uint64_t, uint64_t) {
called = true;
};
bpftime_override_return(0, 0);
REQUIRE(called);
}
5 changes: 5 additions & 0 deletions attach/text_segment_transformer/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,3 +15,8 @@ target_include_directories(bpftime_text_segment_transformer
${FRIDA_GUM_INSTALL_DIR}
${SPDLOG_INCLUDE}
)

if (${TEST_LCOV})
target_compile_options(bpftime_text_segment_transformer PRIVATE -fprofile-arcs -ftest-coverage -fprofile-update=atomic)
target_link_options(bpftime_text_segment_transformer PRIVATE -fprofile-arcs)
endif()
11 changes: 8 additions & 3 deletions daemon/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,9 @@ set(PLACEHOLDER_INPUT ${CMAKE_CURRENT_SOURCE_DIR}/assets/placeholder)
set(PLACEHOLDER_OUTPUT placeholder.c)

add_custom_command(
OUTPUT placeholder.c
COMMAND embedfile placeholder ${PLACEHOLDER_INPUT} ${PLACEHOLDER_OUTPUT}
DEPENDS ${PLACEHOLDER_INPUT})
OUTPUT placeholder.c
COMMAND embedfile placeholder ${PLACEHOLDER_INPUT} ${PLACEHOLDER_OUTPUT}
DEPENDS ${PLACEHOLDER_INPUT})

add_library(libbpftime_daemon STATIC
user/bpf_tracer.cpp
Expand Down Expand Up @@ -63,3 +63,8 @@ install(TARGETS bpftime_daemon CONFIGURATIONS Release Debug RelWithDebInfo DESTI
if(BPFTIME_ENABLE_UNIT_TESTING)
add_subdirectory(test)
endif()

if(${TEST_LCOV})
target_compile_options(bpftime_daemon PUBLIC -fprofile-arcs -ftest-coverage -fprofile-update=atomic)
target_link_options(bpftime_daemon PUBLIC -fprofile-arcs)
endif()
5 changes: 5 additions & 0 deletions runtime/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -245,3 +245,8 @@ if(BPFTIME_ENABLE_UNIT_TESTING AND BPFTIME_BUILD_WITH_LIBBPF)
add_subdirectory(test)
add_subdirectory(unit-test)
endif()

if (${TEST_LCOV})
target_compile_options(${PROJECT_NAME} PUBLIC -fprofile-arcs -ftest-coverage -fprofile-update=atomic)
target_link_options(${PROJECT_NAME} PUBLIC -fprofile-arcs)
endif()
6 changes: 6 additions & 0 deletions runtime/agent/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,12 @@ set_target_properties(bpftime-agent PROPERTIES CXX_STANDARD 20 LINK_DEPENDS ${CM
if(UNIX AND NOT APPLE)
target_link_options(bpftime-agent PRIVATE -Wl,--version-script=${CMAKE_CURRENT_SOURCE_DIR}/agent.version)
endif()

if (${TEST_LCOV})
target_compile_options(bpftime-agent PRIVATE -fprofile-arcs -ftest-coverage -fprofile-update=atomic)
target_link_options(bpftime-agent PRIVATE -fprofile-arcs)
endif()

if(${BPFTIME_BUILD_WITH_LIBBPF})
target_include_directories(bpftime-agent
PRIVATE
Expand Down
5 changes: 5 additions & 0 deletions runtime/syscall-server/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -49,3 +49,8 @@ if(${ENABLE_EBPF_VERIFIER})
target_include_directories(bpftime-syscall-server PRIVATE ${BPFTIME_VERIFIER_INCLUDE})
target_compile_definitions(bpftime-syscall-server PRIVATE ENABLE_EBPF_VERIFIER ENABLE_BPFTIME_VERIFIER)
endif()

if (${TEST_LCOV})
target_compile_options(bpftime-syscall-server PRIVATE -fprofile-arcs -ftest-coverage -fprofile-update=atomic)
target_link_options(bpftime-syscall-server PRIVATE -fprofile-arcs)
endif()

0 comments on commit 808ce59

Please sign in to comment.