This repository was archived by the owner on Mar 7, 2021. It is now read-only.
File tree Expand file tree Collapse file tree 5 files changed +83
-14
lines changed Expand file tree Collapse file tree 5 files changed +83
-14
lines changed Original file line number Diff line number Diff line change @@ -38,3 +38,14 @@ pub fn with_kernel_module<F: Fn()>(f: F) {
38
38
let _m = LoadedModule :: load ( env:: var ( "KERNEL_MODULE" ) . unwrap ( ) ) ;
39
39
f ( ) ;
40
40
}
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
+ }
Original file line number Diff line number Diff line change 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} ;
15
2
16
3
#[ test]
17
4
fn test_printk ( ) {
Original file line number Diff line number Diff line change
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" }
Original file line number Diff line number Diff line change
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
+ ) ;
Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments