Skip to content
This repository was archived by the owner on Mar 7, 2021. It is now read-only.

Commit 1d3caa2

Browse files
authored
Initial sysctl tests (#53)
1 parent c866fb9 commit 1d3caa2

File tree

5 files changed

+73
-4
lines changed

5 files changed

+73
-4
lines changed

tests/run_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,7 +63,7 @@ def main():
6363
)
6464
# TODO: qemu
6565
run(
66-
os.path.join(BASE_DIR, path, "tests"),
66+
os.path.join(BASE_DIR, path, "tests"), "--test-threads=1",
6767
environ=dict(
6868
os.environ,
6969
KERNEL_MODULE=os.path.join(BASE_DIR, "testmodule.ko")

tests/sysctl/Cargo.toml

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
[package]
2+
name = "sysctl-tests"
3+
version = "0.1.0"
4+
authors = ["Alex Gaynor <[email protected]", "Geoffrey Thomas <[email protected]>"]
5+
6+
[lib]
7+
crate-type = ["staticlib"]
8+
9+
[dependencies]
10+
linux-kernel-module = { path = "../.." }

tests/sysctl/src/lib.rs

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#![no_std]
2+
3+
use core::sync::atomic::AtomicBool;
4+
5+
#[macro_use]
6+
extern crate linux_kernel_module;
7+
8+
use linux_kernel_module::sysctl::Sysctl;
9+
use linux_kernel_module::Mode;
10+
11+
struct SysctlTestModule {
12+
sysctl_a: Sysctl<AtomicBool>,
13+
sysctl_b: Sysctl<AtomicBool>,
14+
}
15+
16+
impl linux_kernel_module::KernelModule for SysctlTestModule {
17+
fn init() -> linux_kernel_module::KernelResult<Self> {
18+
Ok(SysctlTestModule {
19+
sysctl_a: Sysctl::register("rust/sysctl-tests\x00", "a\x00", AtomicBool::new(false), Mode::from_int(0o666))?,
20+
sysctl_b: Sysctl::register("rust/sysctl-tests\x00", "b\x00", AtomicBool::new(false), Mode::from_int(0o666))?,
21+
})
22+
}
23+
}
24+
25+
kernel_module!(
26+
SysctlTestModule,
27+
author: "Alex Gaynor and Geoffrey Thomas",
28+
description: "A module for testing sysctls",
29+
license: "GPL"
30+
);

tests/sysctl/tests.rs

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
extern crate kernel_module_tests;
2+
3+
use std::fs;
4+
5+
use kernel_module_tests::with_kernel_module;
6+
7+
#[test]
8+
fn test_read_bool_default() {
9+
with_kernel_module(|| {
10+
assert_eq!(
11+
fs::read_to_string("/proc/sys/rust/sysctl-tests/a").unwrap(),
12+
"0\n"
13+
);
14+
});
15+
}
16+
17+
#[test]
18+
fn test_write_bool() {
19+
with_kernel_module(|| {
20+
fs::write("/proc/sys/rust/sysctl-tests/a", "1").unwrap();
21+
assert_eq!(
22+
fs::read_to_string("/proc/sys/rust/sysctl-tests/a").unwrap(),
23+
"1\n"
24+
);
25+
});
26+
}

tests/testlib.rs

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7,31 +7,34 @@ struct LoadedModule {
77

88
impl LoadedModule {
99
fn load(name: String) -> LoadedModule {
10-
Command::new("sudo")
10+
let status = Command::new("sudo")
1111
.arg("insmod")
1212
.arg(&name)
1313
.status()
1414
.unwrap();
15+
assert!(status.success());
1516
return LoadedModule { name };
1617
}
1718
}
1819

1920
impl Drop for LoadedModule {
2021
fn drop(&mut self) {
21-
Command::new("sudo")
22+
let status = Command::new("sudo")
2223
.arg("rmmod")
2324
.arg(&self.name)
2425
.status()
2526
.unwrap();
27+
assert!(status.success());
2628
}
2729
}
2830

2931
pub fn with_kernel_module<F: Fn()>(f: F) {
30-
Command::new("sudo")
32+
let status = Command::new("sudo")
3133
.arg("dmesg")
3234
.arg("-C")
3335
.status()
3436
.unwrap();
37+
assert!(status.success());
3538
let _m = LoadedModule::load(env::var("KERNEL_MODULE").unwrap());
3639
f();
3740
}

0 commit comments

Comments
 (0)