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

Commit 8f35780

Browse files
committed
Support multiple devices in the chrdev tests
1 parent 45670e6 commit 8f35780

File tree

3 files changed

+14
-9
lines changed

3 files changed

+14
-9
lines changed

tests/chrdev/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,3 +14,4 @@ linux-kernel-module = { path = "../.." }
1414
[dev-dependencies]
1515
kernel-module-testlib = { path = "../../testlib" }
1616
libc = "0.2.58"
17+
tempfile = "3"

tests/chrdev/tests/tests.rs

Lines changed: 12 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,12 @@
1+
use std::fs;
12
use std::io::Read;
23
use std::path::PathBuf;
34
use std::process::Command;
4-
use std::{env, fs};
55

66
use libc;
77

8+
use tempfile::TempDir;
9+
810
use kernel_module_testlib::with_kernel_module;
911

1012
fn get_device_major_number() -> libc::dev_t {
@@ -20,8 +22,8 @@ fn get_device_major_number() -> libc::dev_t {
2022
}
2123

2224
fn temporary_file_path() -> PathBuf {
23-
let mut p = env::temp_dir();
24-
p.push("chrdev-test-device");
25+
let mut p = TempDir::new().unwrap().into_path();
26+
p.push("device");
2527
return p;
2628
}
2729

@@ -39,23 +41,25 @@ impl Drop for UnlinkOnDrop<'_> {
3941
}
4042
}
4143

42-
fn mknod(path: &PathBuf, device_number: libc::dev_t) -> UnlinkOnDrop {
44+
fn mknod(path: &PathBuf, major: libc::dev_t, minor: libc::dev_t) -> UnlinkOnDrop {
4345
Command::new("sudo")
4446
.arg("mknod")
4547
.arg(path.to_str().unwrap())
4648
.arg("c")
47-
.arg(device_number.to_string())
48-
.arg("0")
49+
.arg(major.to_string())
50+
.arg(minor.to_string())
4951
.status()
5052
.unwrap();
5153
return UnlinkOnDrop { path };
5254
}
5355

56+
const READ_FILE_MINOR: libc::dev_t = 0;
57+
5458
#[test]
5559
fn test_mknod() {
5660
with_kernel_module(|| {
5761
let device_number = get_device_major_number();
58-
mknod(&temporary_file_path(), device_number);
62+
mknod(&temporary_file_path(), device_number, READ_FILE_MINOR);
5963
});
6064
}
6165

@@ -64,7 +68,7 @@ fn test_read() {
6468
with_kernel_module(|| {
6569
let device_number = get_device_major_number();
6670
let p = temporary_file_path();
67-
let _u = mknod(&p, device_number);
71+
let _u = mknod(&p, device_number, READ_FILE_MINOR);
6872

6973
let mut f = fs::File::open(&p).unwrap();
7074
let mut data = [0; 12];

tests/run_tests.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,7 @@ def main():
5858
KERNEL_MODULE=os.path.join(BASE_DIR, "testmodule.ko"),
5959
RUSTFLAGS="-Dwarnings",
6060
CARGO_TARGET_DIR=os.path.relpath(
61-
os.path.join(BASE_DIR, "target"),
61+
os.path.join(BASE_DIR, "target-test"),
6262
os.path.join(BASE_DIR, path)
6363
),
6464
)

0 commit comments

Comments
 (0)