Skip to content

Commit 11ff66d

Browse files
committed
tests: Add tests for Rust bindings
Add the regression tests (1-60) for the libseccomp crate that is Rust language bindings for the libseccomp library. You can run the tests as follows: ```sh $ sed -i "/^AC_INIT/ s/0.0.0/9.9.9/" configure.ac $ ./autogen.sh $ ./configure --prefix=$(pwd)/src/.libs --enable-rust $ make && make install $ make check-build $ cd tests && ./regression -m rust ``` Based on: #323 Signed-off-by: Manabu Sugimoto <Manabu.Sugimoto@sony.com> Signed-off-by: mayank <mayank.mrinal@sony.com>
1 parent f1c3196 commit 11ff66d

69 files changed

Lines changed: 3591 additions & 5 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

.github/actions/setup/action.yml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,4 +37,8 @@ runs:
3737
shell: bash
3838
- run: |
3939
./autogen.sh
40+
# Specify the tentative version of the libseccomp test library because some
41+
# functions of the Rust bindings are restricted based on the version.
42+
TEST_VERSION=9.9.9
43+
sed -i "/^AC_INIT/ s/0.0.0/$TEST_VERSION/" configure.ac
4044
shell: bash

.github/workflows/continuous-integration.yml

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,11 @@ jobs:
3434
uses: ./.github/actions/setup
3535
- name: Build libseccomp
3636
run: |
37-
./configure --enable-python
37+
./configure --prefix=$(pwd)/src/.libs --enable-python --enable-rust
38+
# make and make install are required to create a pkconfig because
39+
# the Rust bindings need it for the compilation.
40+
make
41+
make install
3842
make check-build
3943
- name: Run tests
4044
run: |
@@ -52,7 +56,12 @@ jobs:
5256
uses: ./.github/actions/setup
5357
- name: Build libseccomp
5458
run: |
55-
./configure --enable-python
59+
./configure --prefix=$(pwd)/src/.libs --enable-python --enable-rust
60+
# make and make install are required to create a pkconfig because
61+
# the Rust bindings need it for the compilation.
62+
# This can be removed when the Rust bindings drop the version check by pkgconfig.
63+
make
64+
make install
5665
make check-build
5766
- name: Run live tests
5867
run: |

Makefile.am

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -41,8 +41,8 @@ AM_MAKEFLAGS_1 =
4141
AM_MAKEFLAGS_ = ${AM_MAKEFLAGS_0}
4242
AM_MAKEFLAGS = ${AM_MAKEFLAGS_@AM_V@}
4343

44-
# enable python during distcheck
45-
AM_DISTCHECK_CONFIGURE_FLAGS = --enable-python
44+
# enable python and rust during distcheck
45+
AM_DISTCHECK_CONFIGURE_FLAGS = --enable-python --enable-rust
4646

4747
check-build: all
4848
${MAKE} ${AM_MAKEFLAGS} -C src check-build

configure.ac

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -123,6 +123,36 @@ AC_DEFINE_UNQUOTED([ENABLE_PYTHON],
123123
[$(test "$enable_python" = "yes" && echo 1 || echo 0)],
124124
[Python bindings build flag.])
125125

126+
dnl ####
127+
dnl rustc checks
128+
dnl ####
129+
AC_CHECK_PROGS(rustc, rustc, "no")
130+
AS_IF([test "$rustc" != no], [
131+
AS_ECHO("checking rustc version... $($rustc -V 2>&1 | cut -d' ' -f 2)")
132+
RUSTC_VER_MAJ=$($rustc -V 2>&1 | cut -d' ' -f 2 | cut -d'.' -f 1);
133+
RUSTC_VER_MIN=$($rustc -V 2>&1 | cut -d' ' -f 2 | cut -d'.' -f 2);
134+
],[
135+
RUSTC_VER_MAJ=0
136+
RUSTC_VER_MIN=0
137+
])
138+
139+
dnl ####
140+
dnl rust binding checks
141+
dnl ####
142+
AC_ARG_ENABLE([rust],
143+
[AS_HELP_STRING([--enable-rust],
144+
[build the rust bindings, requires rustc])])
145+
AS_IF([test "$enable_rust" = yes], [
146+
# rustc version check
147+
AS_IF([test "$RUSTC_VER_MAJ" -eq 1 -a "$RUSTC_VER_MIN" -lt 63], [
148+
AC_MSG_ERROR([rust bindings require rustc 1.63 or higher])
149+
])
150+
])
151+
AM_CONDITIONAL([ENABLE_RUST], [test "$enable_rust" = yes])
152+
AC_DEFINE_UNQUOTED([ENABLE_RUST],
153+
[$(test "$enable_rust" = yes && echo 1 || echo 0)],
154+
[Rust bindings build flag.])
155+
126156
AC_CHECK_TOOL(GPERF, gperf)
127157
if test -z "$GPERF"; then
128158
AC_MSG_ERROR([please install gperf])

tests/.gitignore

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -68,3 +68,8 @@ util.pyc
6868
58-live-tsync_notify
6969
59-basic-empty_binary_tree
7070
60-sim-precompute
71+
/rust/target
72+
/rust/utils/target
73+
Cargo.lock
74+
.crates.toml
75+
.crates2.json

tests/Makefile.am

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,32 @@ miniseq_LDADD =
3434

3535
TESTS = regression
3636

37+
if ENABLE_RUST
38+
rust_BINDINGS_TEST = yes
39+
else
40+
rust_BINDINGS_TEST = no
41+
endif
42+
43+
rust_TESTS_DIR = ./rust
44+
rust_CARGO_TOML = Cargo.toml
45+
rust_LINK_TYPE = static
46+
rust_LIBSECCOMP_LIBRARY_DIR = ../src/.libs/lib
47+
export LIBSECCOMP_LINK_TYPE =$(rust_LINK_TYPE)
48+
export LIBSECCOMP_LIB_PATH =$(shell realpath $(rust_LIBSECCOMP_LIBRARY_DIR))
49+
50+
define check_rust_bindings
51+
echo "Check format and lint for rust bindings"; \
52+
cargo fmt --manifest-path $(rust_TESTS_DIR)/$(rust_CARGO_TOML) -- --check || exit 1; \
53+
cargo clippy --manifest-path $(rust_TESTS_DIR)/$(rust_CARGO_TOML) \
54+
--all-targets --all-features -- -D warnings || exit 1;
55+
endef
56+
57+
define build_rust_bindings
58+
echo "Build test programs for rust bindings"; \
59+
cargo build --manifest-path $(rust_TESTS_DIR)/$(rust_CARGO_TOML) || exit 1; \
60+
cargo install --quiet --root $(rust_TESTS_DIR) --path $(rust_TESTS_DIR) || exit 1;
61+
endef
62+
3763
check_PROGRAMS = \
3864
miniseq \
3965
01-sim-allow \
@@ -240,6 +266,14 @@ EXTRA_PROGRAMS = 00-test
240266

241267
check-build:
242268
${MAKE} ${AM_MAKEFLAGS} ${check_PROGRAMS}
269+
@if [ "$(rust_BINDINGS_TEST)" = "yes" ]; then \
270+
$(call check_rust_bindings) \
271+
$(call build_rust_bindings) \
272+
fi
243273

244274
clean-local:
245275
${RM} -f 00-test *.pyc
276+
if [ "$(rust_BINDINGS_TEST)" = "yes" ]; then \
277+
cargo clean --manifest-path $(rust_TESTS_DIR)/$(rust_CARGO_TOML); \
278+
${RM} -rf $(rust_TESTS_DIR)/{Cargo.lock,bin}; \
279+
fi

tests/regression

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,8 @@ GLBL_SYS_RESOLVER="../tools/scmp_sys_resolver"
6262
GLBL_SYS_SIM="../tools/scmp_bpf_sim"
6363
GLBL_SYS_API="../tools/scmp_api_level"
6464

65+
RUST_TESTS_BIN="./rust/bin"
66+
6567
####
6668
# functions
6769

@@ -104,7 +106,7 @@ optional arguments:
104106
-h show this help message and exit
105107
-j JOBS run up to JOBS test jobs in parallel
106108
can also be set via LIBSECCOMP_TSTCFG_JOBS env variable
107-
-m MODE specified the test mode [c (default), python]
109+
-m MODE specified the test mode [c (default), python, rust]
108110
can also be set via LIBSECCOMP_TSTCFG_MODE_LIST env variable
109111
-a specifies all tests are to be run
110112
-b BATCH_NAME specifies batch of tests to be run
@@ -266,6 +268,8 @@ function run_test_command() {
266268
else
267269
cmd="$cmd /usr/bin/env python ${srcdir}/$2.py $3"
268270
fi
271+
elif [[ $mode == "rust" ]]; then
272+
cmd="${RUST_TESTS_BIN}/$2 $3"
269273
else
270274
cmd="$2 $3"
271275
fi
@@ -1115,6 +1119,9 @@ while getopts "ab:gj:l:m:s:t:T:vh" opt; do
11151119
verify_deps python
11161120
mode_list="$mode_list python"
11171121
;;
1122+
rust)
1123+
mode_list="$mode_list rust"
1124+
;;
11181125
*)
11191126
usage
11201127
exit 1
@@ -1160,6 +1167,10 @@ if [[ -z $mode_list ]]; then
11601167
[[ "$(grep "ENABLE_PYTHON" ../configure.h | \
11611168
awk '{ print $3 }')" = "1" ]] && \
11621169
mode_list="$mode_list python"
1170+
# rust tests
1171+
[[ "$(grep "ENABLE_RUST" ../configure.h | \
1172+
awk '{ print $3 }')" = "1" ]] && \
1173+
mode_list="$mode_list rust"
11631174
fi
11641175
fi
11651176

tests/rust/01-sim-allow.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
// SPDX-License-Identifier: LGPL-2.1-only
2+
//
3+
// Copyright 2023 Sony Group Corporation
4+
//
5+
// Seccomp Library test program
6+
//
7+
8+
use anyhow::Result;
9+
use libseccomp::*;
10+
use utils::*;
11+
12+
fn main() -> Result<()> {
13+
let opts = util_getopt();
14+
let ctx = ScmpFilterContext::new(ScmpAction::Allow)?;
15+
16+
util_filter_output(&opts, &ctx)
17+
}

tests/rust/02-sim-basic.rs

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
// SPDX-License-Identifier: LGPL-2.1-only
2+
//
3+
// Copyright 2023 Sony Group Corporation
4+
//
5+
// Seccomp Library test program
6+
//
7+
8+
use anyhow::Result;
9+
use libseccomp::*;
10+
use utils::*;
11+
12+
fn main() -> Result<()> {
13+
let opts = util_getopt();
14+
let mut ctx = ScmpFilterContext::new(ScmpAction::KillThread)?;
15+
16+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("read")?)?;
17+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("write")?)?;
18+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("close")?)?;
19+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("rt_sigreturn")?)?;
20+
21+
util_filter_output(&opts, &ctx)
22+
}

tests/rust/03-sim-basic_chains.rs

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
// SPDX-License-Identifier: LGPL-2.1-only
2+
//
3+
// Copyright 2023 Sony Group Corporation
4+
//
5+
// Seccomp Library test program
6+
//
7+
8+
use anyhow::Result;
9+
use libseccomp::*;
10+
use std::io::{stderr, stdin, stdout};
11+
use std::os::unix::io::AsRawFd;
12+
use utils::*;
13+
14+
fn main() -> Result<()> {
15+
let opts = util_getopt();
16+
let mut ctx = ScmpFilterContext::new(ScmpAction::KillThread)?;
17+
18+
ctx.add_rule_conditional_exact(
19+
ScmpAction::Allow,
20+
ScmpSyscall::from_name("read")?,
21+
&[scmp_cmp!($arg0 == stdin().as_raw_fd() as u64)],
22+
)?;
23+
ctx.add_rule_conditional_exact(
24+
ScmpAction::Allow,
25+
ScmpSyscall::from_name("write")?,
26+
&[scmp_cmp!($arg0 == stdout().as_raw_fd() as u64)],
27+
)?;
28+
ctx.add_rule_conditional_exact(
29+
ScmpAction::Allow,
30+
ScmpSyscall::from_name("write")?,
31+
&[scmp_cmp!($arg0 == stderr().as_raw_fd() as u64)],
32+
)?;
33+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("close")?)?;
34+
ctx.add_rule_exact(ScmpAction::Allow, ScmpSyscall::from_name("rt_sigreturn")?)?;
35+
36+
util_filter_output(&opts, &ctx)
37+
}

0 commit comments

Comments
 (0)