forked from elast0ny/shared_memory
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathgeneral.rs
More file actions
117 lines (92 loc) · 2.87 KB
/
general.rs
File metadata and controls
117 lines (92 loc) · 2.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
use std::path::Path;
use shared_memory_extended::ShmemConf;
#[test]
fn create_new() {
let mut s = ShmemConf::new().size(4090).create().unwrap();
assert!(s.is_owner());
assert!(!s.get_os_id().is_empty());
assert!(s.len() >= 4090);
assert!(!s.as_ptr().is_null());
unsafe {
assert_eq!(s.as_slice().len(), s.len());
assert_eq!(s.as_slice_mut().len(), s.len());
}
}
#[test]
fn create_with_flink() {
let flink = Path::new("create_new1");
let mut s = ShmemConf::new().flink(flink).size(4090).create().unwrap();
assert!(s.is_owner());
assert!(!s.get_os_id().is_empty());
assert!(flink.is_file());
assert!(s.len() >= 4090);
assert!(!s.as_ptr().is_null());
unsafe {
assert_eq!(s.as_slice().len(), s.len());
assert_eq!(s.as_slice_mut().len(), s.len());
}
drop(s);
assert!(!flink.is_file());
}
#[test]
fn open_os_id() {
let s1 = ShmemConf::new().size(4090).create().unwrap();
// Open with the unique os id
let os_id = s1.get_os_id().to_string();
let mut s2 = ShmemConf::new().os_id(&os_id).open().unwrap();
assert!(!s2.is_owner());
assert!(!s2.get_os_id().is_empty());
assert!(s2.len() >= 4090);
assert!(!s2.as_ptr().is_null());
unsafe {
assert_eq!(s2.as_slice().len(), s2.len());
assert_eq!(s2.as_slice_mut().len(), s2.len());
}
// Drop the owner of the mapping
drop(s1);
// Make sure it can be openned again
assert!(ShmemConf::new().os_id(&os_id).open().is_err());
drop(s2);
}
#[test]
fn open_flink() {
let flink = Path::new("create_new2");
let s1 = ShmemConf::new().flink(flink).size(4090).create().unwrap();
// Open with file base link
let mut s2 = ShmemConf::new().flink(flink).open().unwrap();
assert!(!s2.is_owner());
assert!(!s2.get_os_id().is_empty());
assert!(flink.is_file());
assert!(s2.len() >= 4090);
assert!(!s2.as_ptr().is_null());
unsafe {
assert_eq!(s2.as_slice().len(), s2.len());
assert_eq!(s2.as_slice_mut().len(), s2.len());
}
// Drop the owner of the mapping
drop(s1);
// Make sure it can be openned again
assert!(ShmemConf::new().flink(flink).open().is_err());
drop(s2);
}
#[test]
fn share_data() {
let s1 = ShmemConf::new()
.size(core::mem::size_of::<u32>())
.create()
.unwrap();
// Open with the unique os id
let os_id = s1.get_os_id().to_string();
let s2 = ShmemConf::new().os_id(os_id).open().unwrap();
let ptr1 = s1.as_ptr() as *mut u32;
let ptr2 = s2.as_ptr() as *mut u32;
// Confirm that the two pointers are different
assert_ne!(ptr1, ptr2);
// Write a value from s1 and read it from s2
unsafe {
let shared_val = 0xBADC0FEE;
ptr1.write_volatile(shared_val);
let read_val = ptr2.read_volatile();
assert_eq!(read_val, shared_val);
}
}