This repository has been archived by the owner on Mar 7, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 120
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
170 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
[package] | ||
name = "chrdev-tests" | ||
version = "0.1.0" | ||
authors = ["Alex Gaynor <[email protected]>", "Geoffrey Thomas <[email protected]>"] | ||
edition = "2018" | ||
|
||
[lib] | ||
crate-type = ["staticlib"] | ||
|
||
[dependencies] | ||
linux-kernel-module = { path = "../.." } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
#![no_std] | ||
#![feature(const_str_as_bytes)] | ||
|
||
use linux_kernel_module; | ||
|
||
struct CycleFile; | ||
|
||
impl linux_kernel_module::chrdev::FileOperations for CycleFile { | ||
const VTABLE: linux_kernel_module::chrdev::FileOperationsVtable = | ||
linux_kernel_module::chrdev::FileOperationsVtable::new::<Self>(); | ||
|
||
fn read( | ||
&mut self, | ||
buf: &linux_kernel_module::user_ptr::UserSlicePtrWriter, | ||
) -> linux_kernel_module::KernelResult<()> { | ||
for c in b"123456789".iter().cycle().take(buf.len()) { | ||
buf.write(c)?; | ||
} | ||
return Ok(()); | ||
} | ||
} | ||
|
||
struct ChrdevTestModule { | ||
_dev_region: linux_kernel_module::chrdev::DeviceNumberRegion, | ||
_chrdev_registration: linux_kernel_module::chrdev::DeviceRegistration<CycleFile>, | ||
} | ||
|
||
impl linux_kernel_module::KernelModule for ChrdevTestModule { | ||
fn init() -> linux_kernel_module::KernelResult<Self> { | ||
let dev_region = | ||
linux_kernel_module::chrdev::DeviceNumberRegion::allocate(0..1, "chrdev-tests\x00")?; | ||
let chrdev_registration = dev_region.register_device::<CycleFile>()?; | ||
Ok(ChrdevTestModule { | ||
_dev_region: dev_region, | ||
_chrdev_registration: chrdev_registration, | ||
}) | ||
} | ||
} | ||
|
||
linux_kernel_module::kernel_module!( | ||
ChrdevTestModule, | ||
author: "Alex Gaynor and Geoffrey Thomas", | ||
description: "A module for testing character devices", | ||
license: "GPL" | ||
); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
use kernel_module_tests::with_kernel_module; | ||
use std::fs; | ||
|
||
fn get_device_number() -> u32 { | ||
let devices = fs::read_to_string("/proc/devices").unwrap(); | ||
let dev_no_line = devices | ||
.lines() | ||
.find(|l| l.ends_with("chrdev-tests")) | ||
.unwrap(); | ||
let elements = dev_no_line.rsplitn(2, " ").collect::<Vec<_>>(); | ||
assert_eq!(elements.len(), 2); | ||
assert_eq!(elements[0], "chrdev-tests"); | ||
return elements[1].trim().parse().unwrap(); | ||
} | ||
|
||
fn temporary_file_path() -> PathBuf { | ||
let p = env::temp_dir(); | ||
p.push("chrdev-test-device"); | ||
return p | ||
} | ||
|
||
struct<'a> UnlinkOnDelete<'a> { | ||
path: &'a Path, | ||
} | ||
|
||
impl Drop for UnlinkOnDrop { | ||
fn drop(&mut self) { | ||
fs::remove_file(self.path); | ||
} | ||
} | ||
|
||
fn mknod(path: &Path, mode: u32, device_number: u32) -> UnlinkOnDrop { | ||
let result = libc::mknod(CString::new(p).unwrap(), mode, device_number); | ||
assert_eq(result, 0); | ||
return UnlinkOnDrop{path}; | ||
} | ||
|
||
#[test] | ||
fn test_mknod() { | ||
with_kernel_module(|| { | ||
let device_number = get_device_number(); | ||
let _u = mknod(temporary_file_path(), 0o600, device_number); | ||
}); | ||
} | ||
|
||
#[test] | ||
fn test_read() { | ||
with_kernel_module(|| { | ||
let device_number = get_device_number(); | ||
let p = temporary_file_path() | ||
let _u = mknod(temporary_file_path(), 0o600, device_number); | ||
|
||
let f = fs::File::open(p); | ||
let data = vec![0; 12]; | ||
f.read_exact(&mut data).unwrap(); | ||
assert_eq(data, "123456789123") | ||
}); | ||
|
||
} |