@@ -22,7 +22,8 @@ use gvfs_fuse::config::AppConfig;
22
22
use gvfs_fuse:: RUN_TEST_WITH_FUSE ;
23
23
use gvfs_fuse:: { gvfs_mount, gvfs_unmount, test_enable_with} ;
24
24
use log:: { error, info} ;
25
- use std:: fs:: File ;
25
+ use std:: fs:: { File , OpenOptions } ;
26
+ use std:: io:: { Read , Seek , SeekFrom , Write } ;
26
27
use std:: path:: Path ;
27
28
use std:: sync:: Arc ;
28
29
use std:: thread:: sleep;
@@ -89,11 +90,6 @@ impl Drop for FuseTest {
89
90
fn test_fuse_with_memory_fs ( ) {
90
91
tracing_subscriber:: fmt ( ) . init ( ) ;
91
92
92
- panic:: set_hook ( Box :: new ( |info| {
93
- error ! ( "A panic occurred: {:?}" , info) ;
94
- process:: exit ( 1 ) ;
95
- } ) ) ;
96
-
97
93
let mount_point = "target/gvfs" ;
98
94
let _ = fs:: create_dir_all ( mount_point) ;
99
95
@@ -104,26 +100,31 @@ fn test_fuse_with_memory_fs() {
104
100
} ;
105
101
106
102
test. setup ( ) ;
107
- test_fuse_filesystem ( mount_point) ;
103
+
104
+ let test_dir = Path :: new ( & test. mount_point ) . join ( "test_dir" ) ;
105
+ run_tests ( & test_dir) ;
106
+ }
107
+
108
+ fn run_tests ( test_dir : & Path ) {
109
+ fs:: create_dir_all ( test_dir) . expect ( "Failed to create test dir" ) ;
110
+ test_fuse_filesystem ( test_dir) ;
111
+ test_big_file ( test_dir) ;
112
+ test_open_file_flag ( test_dir) ;
108
113
}
109
114
110
115
#[ test]
111
116
fn fuse_it_test_fuse ( ) {
112
117
test_enable_with ! ( RUN_TEST_WITH_FUSE ) ;
118
+ let mount_point = Path :: new ( "target/gvfs" ) ;
119
+ let test_dir = mount_point. join ( "test_dir" ) ;
113
120
114
- test_fuse_filesystem ( "target/gvfs/gvfs_test" ) ;
121
+ run_tests ( & test_dir ) ;
115
122
}
116
123
117
- fn test_fuse_filesystem ( mount_point : & str ) {
124
+ fn test_fuse_filesystem ( test_path : & Path ) {
118
125
info ! ( "Test startup" ) ;
119
- let base_path = Path :: new ( mount_point) ;
120
-
121
- if !file_exists ( base_path) {
122
- fs:: create_dir_all ( base_path) . expect ( "Failed to create test dir" ) ;
123
- }
124
-
125
126
//test create file
126
- let test_file = base_path . join ( "test_create" ) ;
127
+ let test_file = test_path . join ( "test_create" ) ;
127
128
let file = File :: create ( & test_file) . expect ( "Failed to create file" ) ;
128
129
assert ! ( file. metadata( ) . is_ok( ) , "Failed to get file metadata" ) ;
129
130
assert ! ( file_exists( & test_file) ) ;
@@ -140,16 +141,16 @@ fn test_fuse_filesystem(mount_point: &str) {
140
141
assert ! ( !file_exists( & test_file) ) ;
141
142
142
143
//test create directory
143
- let test_dir = base_path . join ( "test_dir" ) ;
144
+ let test_dir = test_path . join ( "test_dir" ) ;
144
145
fs:: create_dir ( & test_dir) . expect ( "Failed to create directory" ) ;
145
146
146
147
//test create file in directory
147
- let test_file = base_path . join ( "test_dir/test_file" ) ;
148
+ let test_file = test_path . join ( "test_dir/test_file" ) ;
148
149
let file = File :: create ( & test_file) . expect ( "Failed to create file" ) ;
149
150
assert ! ( file. metadata( ) . is_ok( ) , "Failed to get file metadata" ) ;
150
151
151
152
//test write file in directory
152
- let test_file = base_path . join ( "test_dir/test_read" ) ;
153
+ let test_file = test_path . join ( "test_dir/test_read" ) ;
153
154
fs:: write ( & test_file, "read test" ) . expect ( "Failed to write file" ) ;
154
155
155
156
//test read file in directory
@@ -167,6 +168,121 @@ fn test_fuse_filesystem(mount_point: &str) {
167
168
info ! ( "Success test" ) ;
168
169
}
169
170
171
+ #[ allow( clippy:: needless_range_loop) ]
172
+ fn test_big_file ( test_dir : & Path ) {
173
+ if !file_exists ( test_dir) {
174
+ fs:: create_dir_all ( test_dir) . expect ( "Failed to create test dir" ) ;
175
+ }
176
+
177
+ let test_file = test_dir. join ( "test_big_file" ) ;
178
+ let mut file = File :: create ( & test_file) . expect ( "Failed to create file" ) ;
179
+ assert ! ( file. metadata( ) . is_ok( ) , "Failed to get file metadata" ) ;
180
+ assert ! ( file_exists( & test_file) ) ;
181
+
182
+ let round_size: usize = 1024 * 1024 ;
183
+ let round: u8 = 10 ;
184
+
185
+ for i in 0 ..round {
186
+ let mut content = vec ! [ 0 ; round_size] ;
187
+ for j in 0 ..round_size {
188
+ content[ j] = ( i as usize + j) as u8 ;
189
+ }
190
+
191
+ file. write_all ( & content) . expect ( "Failed to write file" ) ;
192
+ }
193
+ file. flush ( ) . expect ( "Failed to flush file" ) ;
194
+
195
+ file = File :: open ( & test_file) . expect ( "Failed to open file" ) ;
196
+ for i in 0 ..round {
197
+ let mut buffer = vec ! [ 0 ; round_size] ;
198
+ file. read_exact ( & mut buffer) . unwrap ( ) ;
199
+
200
+ for j in 0 ..round_size {
201
+ assert_eq ! ( buffer[ j] , ( i as usize + j) as u8 , "File content mismatch" ) ;
202
+ }
203
+ }
204
+
205
+ fs:: remove_file ( & test_file) . expect ( "Failed to delete file" ) ;
206
+ assert ! ( !file_exists( & test_file) ) ;
207
+ }
208
+
209
+ fn test_open_file_flag ( test_dir : & Path ) {
210
+ // test open file with read and write create flag
211
+ let file_path = test_dir. join ( "test_open_file" ) ;
212
+ let mut file = OpenOptions :: new ( )
213
+ . write ( true )
214
+ . read ( true )
215
+ . create ( true )
216
+ . open ( & file_path)
217
+ . expect ( "Failed to open file" ) ;
218
+ // test file offset is 0
219
+ let offset = file. stream_position ( ) . expect ( "Failed to seek file" ) ;
220
+ assert_eq ! ( offset, 0 , "File offset mismatch" ) ;
221
+
222
+ // test write can be done
223
+ let write_content = "write content" ;
224
+ file. write_all ( write_content. as_bytes ( ) )
225
+ . expect ( "Failed to write file" ) ;
226
+ let mut content = vec ! [ 0 ; write_content. len( ) ] ;
227
+
228
+ // test read can be done
229
+ file. seek ( SeekFrom :: Start ( 0 ) ) . expect ( "Failed to seek file" ) ;
230
+ file. read_exact ( & mut content) . expect ( "Failed to read file" ) ;
231
+ assert_eq ! ( content, write_content. as_bytes( ) , "File content mismatch" ) ;
232
+
233
+ // test open file with read flag
234
+ let mut file = OpenOptions :: new ( )
235
+ . read ( true )
236
+ . open ( & file_path)
237
+ . expect ( "Failed to open file" ) ;
238
+ // test reaad can be done
239
+ let mut content = vec ! [ 0 ; write_content. len( ) ] ;
240
+ file. read_exact ( & mut content) . expect ( "Failed to read file" ) ;
241
+ assert_eq ! ( content, write_content. as_bytes( ) , "File content mismatch" ) ;
242
+
243
+ // test write can be have error
244
+ let result = file. write_all ( write_content. as_bytes ( ) ) ;
245
+ if let Err ( e) = result {
246
+ assert_eq ! ( e. to_string( ) , "Bad file descriptor (os error 9)" ) ;
247
+ }
248
+
249
+ // test open file with truncate file
250
+ // test file size is not 0
251
+ let old_file_size = file. metadata ( ) . expect ( "Failed to get file metadata" ) . len ( ) ;
252
+ assert_eq ! ( old_file_size, write_content. len( ) as u64 ) ;
253
+
254
+ let mut file = OpenOptions :: new ( )
255
+ . write ( true )
256
+ . truncate ( true )
257
+ . open ( & file_path)
258
+ . expect ( "Failed to open file" ) ;
259
+ // validate file size is 0
260
+ let file_size = file. metadata ( ) . expect ( "Failed to get file metadata" ) . len ( ) ;
261
+ assert_eq ! ( file_size, 0 , "File size mismatch" ) ;
262
+ // validate file offset is 0
263
+ let offset = file. stream_position ( ) . expect ( "Failed to seek file" ) ;
264
+ assert_eq ! ( offset, 0 , "File offset mismatch" ) ;
265
+
266
+ file. write_all ( write_content. as_bytes ( ) )
267
+ . expect ( "Failed to write file" ) ;
268
+
269
+ // test open file with append flag
270
+ let mut file = OpenOptions :: new ( )
271
+ . append ( true )
272
+ . open ( & file_path)
273
+ . expect ( "Failed to open file" ) ;
274
+ // test append
275
+ file. write_all ( write_content. as_bytes ( ) )
276
+ . expect ( "Failed to write file" ) ;
277
+ let file_len = file. metadata ( ) . expect ( "Failed to get file metadata" ) . len ( ) ;
278
+ // validate file size is 2 * write_content.len()
279
+ assert_eq ! (
280
+ file_len,
281
+ 2 * write_content. len( ) as u64 ,
282
+ "File size mismatch"
283
+ ) ;
284
+ }
285
+
170
286
fn file_exists < P : AsRef < Path > > ( path : P ) -> bool {
171
287
fs:: metadata ( path) . is_ok ( )
172
288
}
0 commit comments