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

Commit 1a0aa88

Browse files
luisgerhorstalex
authored andcommitted
Add tests for filesystem::register (#197)
* Add tests for filesystem::register Check whether loading the module makes the name of the filesystem (testfs) appear in /proc/filesystems * Improve name for /proc/filesystems test * Mark filesystem::Registration<T: FileSystem> as Sync * Make Sync a supertrait of filesystem::FileSystem * Use cstr!() macro in const context * Remove unneeded const_raw_ptr_deref feature * Remove redundant "\x00" from the end of "testfs"
1 parent fd0da95 commit 1a0aa88

File tree

4 files changed

+73
-1
lines changed

4 files changed

+73
-1
lines changed

src/filesystem.rs

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,16 @@ pub struct Registration<T: FileSystem> {
1414
ptr: Box<bindings::file_system_type>,
1515
}
1616

17+
// This is safe because Registration doesn't actually expose any methods.
18+
unsafe impl<T> Sync for Registration<T> where T: FileSystem {}
19+
1720
impl<T: FileSystem> Drop for Registration<T> {
1821
fn drop(&mut self) {
1922
unsafe { bindings::unregister_filesystem(&mut *self.ptr) };
2023
}
2124
}
2225

23-
pub trait FileSystem {
26+
pub trait FileSystem: Sync {
2427
const NAME: &'static CStr;
2528
const FLAGS: FileSystemFlags;
2629
}

tests/filesystem/Cargo.toml

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
[package]
2+
name = "filesystem-tests"
3+
version = "0.1.0"
4+
authors = ["Luis Gerhorst <privat@luisgerhorst.de>", "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+
[features]
12+
default = ["linux-kernel-module"]
13+
14+
[dependencies]
15+
linux-kernel-module = { path = "../..", optional = true }
16+
17+
[dev-dependencies]
18+
kernel-module-testlib = { path = "../../testlib" }

tests/filesystem/src/lib.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
#![no_std]
2+
#![feature(const_str_as_bytes)]
3+
4+
extern crate alloc;
5+
6+
use linux_kernel_module::filesystem::{self, FileSystem, FileSystemFlags};
7+
use linux_kernel_module::{self, cstr, CStr};
8+
9+
struct TestFSModule {
10+
_fs_registration: filesystem::Registration<TestFS>,
11+
}
12+
13+
struct TestFS {}
14+
15+
impl FileSystem for TestFS {
16+
const NAME: &'static CStr = cstr!("testfs");
17+
const FLAGS: FileSystemFlags = FileSystemFlags::FS_REQUIRES_DEV;
18+
}
19+
20+
impl linux_kernel_module::KernelModule for TestFSModule {
21+
fn init() -> linux_kernel_module::KernelResult<Self> {
22+
let fs_registration = filesystem::register::<TestFS>()?;
23+
Ok(TestFSModule {
24+
_fs_registration: fs_registration,
25+
})
26+
}
27+
}
28+
29+
linux_kernel_module::kernel_module!(
30+
TestFSModule,
31+
author: "Fish in a Barrel Contributors",
32+
description: "A module for testing filesystem::register",
33+
license: "GPL"
34+
);

tests/filesystem/tests/tests.rs

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
use std::fs;
2+
3+
use kernel_module_testlib::with_kernel_module;
4+
5+
#[test]
6+
fn test_proc_filesystems() {
7+
let filesystems = fs::read_to_string("/proc/filesystems").unwrap();
8+
assert!(!filesystems.contains("testfs"));
9+
10+
with_kernel_module(|| {
11+
let filesystems = fs::read_to_string("/proc/filesystems").unwrap();
12+
assert!(filesystems.contains("testfs"));
13+
});
14+
15+
let filesystems = fs::read_to_string("/proc/filesystems").unwrap();
16+
assert!(!filesystems.contains("testfs"));
17+
}

0 commit comments

Comments
 (0)