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

Commit b9a7ac2

Browse files
committed
Fixes #132 -- adds tests for more sysctl code
1 parent 1ee4ff5 commit b9a7ac2

File tree

5 files changed

+83
-14
lines changed

5 files changed

+83
-14
lines changed

testlib/src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -38,3 +38,14 @@ pub fn with_kernel_module<F: Fn()>(f: F) {
3838
let _m = LoadedModule::load(env::var("KERNEL_MODULE").unwrap());
3939
f();
4040
}
41+
42+
pub fn assert_dmesg_contains(msgs: &[&[u8]]) {
43+
let output = Command::new("dmesg").output().unwrap();
44+
let lines = output.stdout.split(|x| *x == b'\n').collect::<Vec<_>>();
45+
let mut lines: &[&[u8]] = &lines;
46+
for msg in msgs {
47+
let pos = lines.iter().position(|l| l.ends_with(msg));
48+
assert!(pos.is_some());
49+
lines = &lines[pos.unwrap()..];
50+
}
51+
}

tests/printk/tests/tests.rs

Lines changed: 1 addition & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,4 @@
1-
use std::process::Command;
2-
3-
use kernel_module_testlib::with_kernel_module;
4-
5-
fn assert_dmesg_contains(msgs: &[&[u8]]) {
6-
let output = Command::new("dmesg").output().unwrap();
7-
let lines = output.stdout.split(|x| *x == b'\n').collect::<Vec<_>>();
8-
let mut lines: &[&[u8]] = &lines;
9-
for msg in msgs {
10-
let pos = lines.iter().position(|l| l.ends_with(msg));
11-
assert!(pos.is_some());
12-
lines = &lines[pos.unwrap()..];
13-
}
14-
}
1+
use kernel_module_testlib::{assert_dmesg_contains, with_kernel_module};
152

163
#[test]
174
fn test_printk() {

tests/sysctl-get/Cargo.toml

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
[package]
2+
name = "sysctl-get-tests"
3+
version = "0.1.0"
4+
authors = ["Alex Gaynor <alex.gaynor@gmail.com", "Geoffrey Thomas <geofft@ldpreload.com>"]
5+
edition = "2018"
6+
7+
[lib]
8+
crate-type = ["staticlib"]
9+
test = false
10+
11+
[dependencies]
12+
linux-kernel-module = { path = "../.." }
13+
14+
[dev-dependencies]
15+
kernel-module-testlib = { path = "../../testlib" }

tests/sysctl-get/src/lib.rs

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#![no_std]
2+
#![feature(const_str_as_bytes)]
3+
4+
use core::sync::atomic::{AtomicBool, Ordering};
5+
6+
use linux_kernel_module::{self, cstr, println};
7+
8+
use linux_kernel_module::sysctl::Sysctl;
9+
use linux_kernel_module::Mode;
10+
11+
static A_VAL: AtomicBool = AtomicBool::new(false);
12+
13+
struct SysctlGetTestModule {
14+
sysctl_a: Sysctl<&'static AtomicBool>,
15+
}
16+
17+
impl linux_kernel_module::KernelModule for SysctlGetTestModule {
18+
fn init() -> linux_kernel_module::KernelResult<Self> {
19+
Ok(SysctlGetTestModule {
20+
sysctl_a: Sysctl::register(
21+
cstr!("rust/sysctl-get-tests"),
22+
cstr!("a"),
23+
&A_VAL,
24+
Mode::from_int(0o666),
25+
)?,
26+
})
27+
}
28+
}
29+
30+
impl Drop for SysctlGetTestModule {
31+
fn drop(&mut self) {
32+
println!("A_VAL: {:?}", A_VAL.load(Ordering::Relaxed));
33+
println!(
34+
"SYSCTL_A: {:?}",
35+
self.sysctl_a.get().load(Ordering::Relaxed)
36+
);
37+
}
38+
}
39+
40+
linux_kernel_module::kernel_module!(
41+
SysctlGetTestModule,
42+
author: "Alex Gaynor and Geoffrey Thomas",
43+
description: "A module for testing sysctls",
44+
license: "GPL"
45+
);

tests/sysctl-get/tests/tests.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
use std::fs;
2+
3+
use kernel_module_testlib::{assert_dmesg_contains, with_kernel_module};
4+
5+
#[test]
6+
fn test_get() {
7+
with_kernel_module(|| {
8+
fs::write("/proc/sys/rust/sysctl-get-tests/a", "1").unwrap();
9+
});
10+
assert_dmesg_contains(&[b"A_VAL: true", b"SYSCTL_A: true"]);
11+
}

0 commit comments

Comments
 (0)