Skip to content

Commit

Permalink
Build kernel module in temporary directory
Browse files Browse the repository at this point in the history
This avoids poluting the build directory

# Conflicts:
#	test/test_kmod/mod.rs
  • Loading branch information
bachp committed Sep 2, 2018
1 parent 8a4d872 commit 73c4c58
Showing 1 changed file with 53 additions and 23 deletions.
76 changes: 53 additions & 23 deletions test/test_kmod/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,33 @@
use std::fs::copy;
use std::path::PathBuf;
use std::process::Command;
use tempfile::{tempdir, TempDir};

fn compile_kernel_module() -> (String, String) {
let _m = ::FORK_MTX.lock().expect("Mutex got poisoned by another test");
fn compile_kernel_module() -> (PathBuf, String, TempDir) {
let _m = ::FORK_MTX
.lock()
.expect("Mutex got poisoned by another test");

let tmp_dir = tempdir().expect("unable to create temporary build directory");

copy(
"test/test_kmod/hello_mod/hello.c",
&tmp_dir.path().join("hello.c"),
).expect("unable to copy hello.c to temporary build directory");
copy(
"test/test_kmod/hello_mod/Makefile",
&tmp_dir.path().join("Makefile"),
).expect("unable to copy Makefile to temporary build directory");

let status = Command::new("make")
.current_dir("test/test_kmod/hello_mod")
.current_dir(tmp_dir.path())
.status()
.expect("failed to run make");

assert!(status.success());

// Return the relative path of the build kernel module
("test/test_kmod/hello_mod/hello.ko".to_owned(), "hello".to_owned())
(tmp_dir.path().join("hello.ko"), "hello".to_owned(), tmp_dir)
}

use nix::errno::Errno;
Expand All @@ -26,60 +42,72 @@ use std::io::Read;
fn test_finit_and_delete_module() {
skip_if_not_root!("test_finit_module");

let (kmod_path, kmod_name) = compile_kernel_module();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

let f = File::open(kmod_path).expect("unable to open kernel module");
finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty())
.expect("unable to load kernel module");

delete_module(&CString::new(kmod_name).unwrap(), DeleteModuleFlags::empty())
.expect("unable to unload kernel module");
delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
).expect("unable to unload kernel module");
}

#[test]
fn test_finit_and_delete_modul_with_params() {
skip_if_not_root!("test_finit_module_with_params");

let (kmod_path, kmod_name) = compile_kernel_module();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

let f = File::open(kmod_path).expect("unable to open kernel module");
finit_module(&f, &CString::new("who=Rust number=2018").unwrap(), ModuleInitFlags::empty())
.expect("unable to load kernel module");

delete_module(&CString::new(kmod_name).unwrap(), DeleteModuleFlags::empty())
.expect("unable to unload kernel module");
finit_module(
&f,
&CString::new("who=Rust number=2018").unwrap(),
ModuleInitFlags::empty(),
).expect("unable to load kernel module");

delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
).expect("unable to unload kernel module");
}

#[test]
fn test_init_and_delete_module() {
skip_if_not_root!("test_init_module");

let (kmod_path, kmod_name) = compile_kernel_module();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

let mut f = File::open(kmod_path).expect("unable to open kernel module");
let mut contents: Vec<u8> = Vec::new();
f.read_to_end(&mut contents)
.expect("unable to read kernel module content to buffer");
init_module(&mut contents, &CString::new("").unwrap()).expect("unable to load kernel module");

delete_module(&CString::new(kmod_name).unwrap(), DeleteModuleFlags::empty())
.expect("unable to unload kernel module");
delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
).expect("unable to unload kernel module");
}

#[test]
fn test_init_and_delete_module_with_params() {
skip_if_not_root!("test_init_module");

let (kmod_path, kmod_name) = compile_kernel_module();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

let mut f = File::open(kmod_path).expect("unable to open kernel module");
let mut contents: Vec<u8> = Vec::new();
f.read_to_end(&mut contents)
.expect("unable to read kernel module content to buffer");
init_module(&mut contents, &CString::new("who=Nix number=2015").unwrap()).expect("unable to load kernel module");
init_module(&mut contents, &CString::new("who=Nix number=2015").unwrap())
.expect("unable to load kernel module");

delete_module(&CString::new(kmod_name).unwrap(), DeleteModuleFlags::empty())
.expect("unable to unload kernel module");
delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
).expect("unable to unload kernel module");
}

#[test]
Expand All @@ -98,7 +126,7 @@ fn test_finit_module_invalid() {
fn test_finit_module_twice_and_delete_module() {
skip_if_not_root!("test_finit_module_twice_and_delete_module");

let (kmod_path, kmod_name) = compile_kernel_module();
let (kmod_path, kmod_name, _kmod_dir) = compile_kernel_module();

let f = File::open(kmod_path).expect("unable to open kernel module");
finit_module(&f, &CString::new("").unwrap(), ModuleInitFlags::empty())
Expand All @@ -108,8 +136,10 @@ fn test_finit_module_twice_and_delete_module() {

assert_eq!(result.unwrap_err(), Error::Sys(Errno::EEXIST));

delete_module(&CString::new(kmod_name).unwrap(), DeleteModuleFlags::empty())
.expect("unable to unload kernel module");
delete_module(
&CString::new(kmod_name).unwrap(),
DeleteModuleFlags::empty(),
).expect("unable to unload kernel module");
}

#[test]
Expand Down

0 comments on commit 73c4c58

Please sign in to comment.