5
5
6
6
extern crate libc;
7
7
8
- use std:: fs:: {
9
- File , create_dir, OpenOptions , remove_dir, remove_dir_all, remove_file, rename,
10
- } ;
11
8
use std:: ffi:: CString ;
12
- use std:: io :: { Read , Write , Error , ErrorKind , Result , Seek , SeekFrom } ;
13
- use std:: path :: { PathBuf , Path } ;
14
-
9
+ use std:: fs :: { create_dir , remove_dir , remove_dir_all , remove_file , rename , File , OpenOptions } ;
10
+ use std:: io :: { Error , ErrorKind , Read , Result , Seek , SeekFrom , Write } ;
11
+ use std :: path :: { Path , PathBuf } ;
15
12
16
13
fn main ( ) {
17
14
test_file ( ) ;
@@ -26,6 +23,11 @@ fn main() {
26
23
test_rename ( ) ;
27
24
test_directory ( ) ;
28
25
test_dup_stdout_stderr ( ) ;
26
+
27
+ // These all require unix, if the test is changed to no longer `ignore-windows`, move these to a unix test
28
+ test_file_open_unix_allow_two_args ( ) ;
29
+ test_file_open_unix_needs_three_args ( ) ;
30
+ test_file_open_unix_extra_third_arg ( ) ;
29
31
}
30
32
31
33
fn tmp ( ) -> PathBuf {
@@ -41,7 +43,8 @@ fn tmp() -> PathBuf {
41
43
42
44
#[ cfg( not( windows) ) ]
43
45
return PathBuf :: from ( tmp. replace ( "\\ " , "/" ) ) ;
44
- } ) . unwrap_or_else ( |_| std:: env:: temp_dir ( ) )
46
+ } )
47
+ . unwrap_or_else ( |_| std:: env:: temp_dir ( ) )
45
48
}
46
49
47
50
/// Prepare: compute filename and make sure the file does not exist.
@@ -93,6 +96,39 @@ fn test_file() {
93
96
remove_file ( & path) . unwrap ( ) ;
94
97
}
95
98
99
+ fn test_file_open_unix_allow_two_args ( ) {
100
+ use std:: os:: unix:: ffi:: OsStrExt ;
101
+
102
+ let path = prepare_with_content ( "test_file_open_unix_allow_two_args.txt" , & [ ] ) ;
103
+
104
+ let mut name = path. into_os_string ( ) ;
105
+ name. push ( "\0 " ) ;
106
+ let name_ptr = name. as_bytes ( ) . as_ptr ( ) . cast :: < libc:: c_char > ( ) ;
107
+ let _fd = unsafe { libc:: open ( name_ptr, libc:: O_RDONLY ) } ;
108
+ }
109
+
110
+ fn test_file_open_unix_needs_three_args ( ) {
111
+ use std:: os:: unix:: ffi:: OsStrExt ;
112
+
113
+ let path = prepare_with_content ( "test_file_open_unix_needs_three_args.txt" , & [ ] ) ;
114
+
115
+ let mut name = path. into_os_string ( ) ;
116
+ name. push ( "\0 " ) ;
117
+ let name_ptr = name. as_bytes ( ) . as_ptr ( ) . cast :: < libc:: c_char > ( ) ;
118
+ let _fd = unsafe { libc:: open ( name_ptr, libc:: O_CREAT , 0o666 ) } ;
119
+ }
120
+
121
+ fn test_file_open_unix_extra_third_arg ( ) {
122
+ use std:: os:: unix:: ffi:: OsStrExt ;
123
+
124
+ let path = prepare_with_content ( "test_file_open_unix_extra_third_arg.txt" , & [ ] ) ;
125
+
126
+ let mut name = path. into_os_string ( ) ;
127
+ name. push ( "\0 " ) ;
128
+ let name_ptr = name. as_bytes ( ) . as_ptr ( ) . cast :: < libc:: c_char > ( ) ;
129
+ let _fd = unsafe { libc:: open ( name_ptr, libc:: O_RDONLY , 42 ) } ;
130
+ }
131
+
96
132
fn test_file_clone ( ) {
97
133
let bytes = b"Hello, World!\n " ;
98
134
let path = prepare_with_content ( "miri_test_fs_file_clone.txt" , bytes) ;
@@ -115,7 +151,10 @@ fn test_file_create_new() {
115
151
// Creating a new file that doesn't yet exist should succeed.
116
152
OpenOptions :: new ( ) . write ( true ) . create_new ( true ) . open ( & path) . unwrap ( ) ;
117
153
// Creating a new file that already exists should fail.
118
- assert_eq ! ( ErrorKind :: AlreadyExists , OpenOptions :: new( ) . write( true ) . create_new( true ) . open( & path) . unwrap_err( ) . kind( ) ) ;
154
+ assert_eq ! (
155
+ ErrorKind :: AlreadyExists ,
156
+ OpenOptions :: new( ) . write( true ) . create_new( true ) . open( & path) . unwrap_err( ) . kind( )
157
+ ) ;
119
158
// Optionally creating a new file that already exists should succeed.
120
159
OpenOptions :: new ( ) . write ( true ) . create ( true ) . open ( & path) . unwrap ( ) ;
121
160
@@ -235,7 +274,6 @@ fn test_symlink() {
235
274
symlink_file. read_to_end ( & mut contents) . unwrap ( ) ;
236
275
assert_eq ! ( bytes, contents. as_slice( ) ) ;
237
276
238
-
239
277
#[ cfg( unix) ]
240
278
{
241
279
use std:: os:: unix:: ffi:: OsStrExt ;
@@ -250,7 +288,9 @@ fn test_symlink() {
250
288
// Make the buf one byte larger than it needs to be,
251
289
// and check that the last byte is not overwritten.
252
290
let mut large_buf = vec ! [ 0xFF ; expected_path. len( ) + 1 ] ;
253
- let res = unsafe { libc:: readlink ( symlink_c_ptr, large_buf. as_mut_ptr ( ) . cast ( ) , large_buf. len ( ) ) } ;
291
+ let res = unsafe {
292
+ libc:: readlink ( symlink_c_ptr, large_buf. as_mut_ptr ( ) . cast ( ) , large_buf. len ( ) )
293
+ } ;
254
294
// Check that the resovled path was properly written into the buf.
255
295
assert_eq ! ( & large_buf[ ..( large_buf. len( ) - 1 ) ] , expected_path) ;
256
296
assert_eq ! ( large_buf. last( ) , Some ( & 0xFF ) ) ;
@@ -259,18 +299,21 @@ fn test_symlink() {
259
299
// Test that the resolved path is truncated if the provided buffer
260
300
// is too small.
261
301
let mut small_buf = [ 0u8 ; 2 ] ;
262
- let res = unsafe { libc:: readlink ( symlink_c_ptr, small_buf. as_mut_ptr ( ) . cast ( ) , small_buf. len ( ) ) } ;
302
+ let res = unsafe {
303
+ libc:: readlink ( symlink_c_ptr, small_buf. as_mut_ptr ( ) . cast ( ) , small_buf. len ( ) )
304
+ } ;
263
305
assert_eq ! ( small_buf, & expected_path[ ..small_buf. len( ) ] ) ;
264
306
assert_eq ! ( res, small_buf. len( ) as isize ) ;
265
307
266
308
// Test that we report a proper error for a missing path.
267
309
let bad_path = CString :: new ( "MIRI_MISSING_FILE_NAME" ) . unwrap ( ) ;
268
- let res = unsafe { libc:: readlink ( bad_path. as_ptr ( ) , small_buf. as_mut_ptr ( ) . cast ( ) , small_buf. len ( ) ) } ;
310
+ let res = unsafe {
311
+ libc:: readlink ( bad_path. as_ptr ( ) , small_buf. as_mut_ptr ( ) . cast ( ) , small_buf. len ( ) )
312
+ } ;
269
313
assert_eq ! ( res, -1 ) ;
270
314
assert_eq ! ( Error :: last_os_error( ) . kind( ) , ErrorKind :: NotFound ) ;
271
315
}
272
316
273
-
274
317
// Test that metadata of a symbolic link is correct.
275
318
check_metadata ( bytes, & symlink_path) . unwrap ( ) ;
276
319
// Test that the metadata of a symbolic link is correct when not following it.
0 commit comments