@@ -24,15 +24,6 @@ pub fn test(sfs: ScopedProtocol<SimpleFileSystem>) -> Result<(), fs::Error> {
24
24
let read = String :: from_utf8 ( read) . expect ( "Should be valid utf8" ) ;
25
25
assert_eq ! ( read. as_str( ) , data_to_write) ;
26
26
27
- // test copy from non-existent file: does the error type work as expected?
28
- let err = fs. copy ( cstr16 ! ( "not_found" ) , cstr16 ! ( "abc" ) ) ;
29
- let expected_err = fs:: Error :: Io ( IoError {
30
- path : PathBuf :: from ( cstr16 ! ( "not_found" ) ) ,
31
- context : IoErrorContext :: OpenError ,
32
- uefi_error : uefi:: Error :: new ( Status :: NOT_FOUND , ( ) ) ,
33
- } ) ;
34
- assert_eq ! ( err, Err ( expected_err) ) ;
35
-
36
27
// test rename file + path buf replaces / with \
37
28
fs. rename (
38
29
PathBuf :: from ( cstr16 ! ( "/foo_dir/foo_cpy" ) ) ,
@@ -64,5 +55,116 @@ pub fn test(sfs: ScopedProtocol<SimpleFileSystem>) -> Result<(), fs::Error> {
64
55
// file should not be available after remove all
65
56
assert ! ( !fs. try_exists( cstr16!( "foo_dir\\ 1" ) ) ?) ;
66
57
58
+ test_copy_error ( & mut fs) ?;
59
+ test_copy_success ( & mut fs) ?;
60
+ test_copy_success_chunks ( & mut fs) ?;
61
+
62
+ Ok ( ( ) )
63
+ }
64
+
65
+ fn test_copy_error ( fs : & mut FileSystem ) -> Result < ( ) , fs:: Error > {
66
+ let file1_path = cstr16 ! ( "file1" ) ;
67
+ let dir_path = cstr16 ! ( "dir" ) ;
68
+
69
+ // Test copy when the destination exists but the source does not. Verify
70
+ // that the destination is not deleted or altered.
71
+ fs. write ( file1_path, "data1" ) ?;
72
+ assert_eq ! (
73
+ fs. copy( cstr16!( "src" ) , file1_path) ,
74
+ Err ( fs:: Error :: Io ( IoError {
75
+ path: PathBuf :: from( cstr16!( "src" ) ) ,
76
+ context: IoErrorContext :: OpenError ,
77
+ uefi_error: uefi:: Error :: new( Status :: NOT_FOUND , ( ) ) ,
78
+ } ) )
79
+ ) ;
80
+ assert_eq ! ( fs. read( file1_path) ?, b"data1" ) ;
81
+
82
+ // Test copy when the source is a directory. Verify that the destination is
83
+ // not deleted or altered.
84
+ fs. create_dir ( dir_path) ?;
85
+ assert_eq ! (
86
+ fs. copy( dir_path, file1_path) ,
87
+ Err ( fs:: Error :: Io ( IoError {
88
+ path: PathBuf :: from( dir_path) ,
89
+ context: IoErrorContext :: NotAFile ,
90
+ uefi_error: uefi:: Error :: new( Status :: INVALID_PARAMETER , ( ) ) ,
91
+ } ) )
92
+ ) ;
93
+ assert_eq ! ( fs. read( file1_path) ?, b"data1" ) ;
94
+
95
+ // Test copy when the source is valid but the destination is a
96
+ // directory. Verify that the directory is not deleted.
97
+ assert_eq ! (
98
+ fs. copy( file1_path, dir_path) ,
99
+ Err ( fs:: Error :: Io ( IoError {
100
+ path: PathBuf :: from( dir_path) ,
101
+ context: IoErrorContext :: OpenError ,
102
+ uefi_error: uefi:: Error :: new( Status :: INVALID_PARAMETER , ( ) ) ,
103
+ } ) )
104
+ ) ;
105
+ assert_eq ! ( fs. try_exists( dir_path) , Ok ( true ) ) ;
106
+
107
+ // Clean up temporary files.
108
+ fs. remove_file ( file1_path) ?;
109
+ fs. remove_dir ( dir_path) ?;
110
+
111
+ Ok ( ( ) )
112
+ }
113
+
114
+ fn test_copy_success ( fs : & mut FileSystem ) -> Result < ( ) , fs:: Error > {
115
+ let file1_path = cstr16 ! ( "file1" ) ;
116
+ let file2_path = cstr16 ! ( "file2" ) ;
117
+
118
+ // Test a successful copy where the destination does not already exist.
119
+ fs. write ( file1_path, "data1" ) ?;
120
+ assert_eq ! ( fs. try_exists( file2_path) , Ok ( false ) ) ;
121
+ fs. copy ( file1_path, file2_path) ?;
122
+ assert_eq ! ( fs. read( file1_path) ?, b"data1" ) ;
123
+ assert_eq ! ( fs. read( file2_path) ?, b"data1" ) ;
124
+
125
+ // Test that when copying a smaller file over a larger file, the file is
126
+ // properly truncated. Also check that the original file is unchanged.
127
+ fs. write ( file2_path, "some long data" ) ?;
128
+ fs. copy ( file1_path, file2_path) ?;
129
+ assert_eq ! ( fs. read( file1_path) ?, b"data1" ) ;
130
+ assert_eq ! ( fs. read( file2_path) ?, b"data1" ) ;
131
+
132
+ // Clean up temporary files.
133
+ fs. remove_file ( file1_path) ?;
134
+ fs. remove_file ( file2_path) ?;
135
+
136
+ Ok ( ( ) )
137
+ }
138
+
139
+ fn test_copy_success_chunks ( fs : & mut FileSystem ) -> Result < ( ) , fs:: Error > {
140
+ let file1_path = cstr16 ! ( "file1" ) ;
141
+ let file2_path = cstr16 ! ( "file2" ) ;
142
+
143
+ // Test copy of a large file, where the file's size is an even multiple of
144
+ // the 1MiB chunk size.
145
+ let chunk_size = 1024 * 1024 ;
146
+ let mut big_buf = Vec :: with_capacity ( 5 * chunk_size) ;
147
+ for i in 0 ..( 4 * chunk_size) {
148
+ let byte = u8:: try_from ( i % 255 ) . unwrap ( ) ;
149
+ big_buf. push ( byte) ;
150
+ }
151
+ fs. write ( file1_path, & big_buf) ?;
152
+ fs. copy ( file1_path, file2_path) ?;
153
+ assert_eq ! ( fs. read( file1_path) ?, big_buf) ;
154
+ assert_eq ! ( fs. read( file2_path) ?, big_buf) ;
155
+
156
+ // Test copy of a large file, where the file's size is not an even multiple
157
+ // of the 1MiB chunk size.
158
+ big_buf. extend ( b"some extra data" ) ;
159
+ assert_ne ! ( big_buf. len( ) % chunk_size, 0 ) ;
160
+ fs. write ( file1_path, & big_buf) ?;
161
+ fs. copy ( file1_path, file2_path) ?;
162
+ assert_eq ! ( fs. read( file1_path) ?, big_buf) ;
163
+ assert_eq ! ( fs. read( file2_path) ?, big_buf) ;
164
+
165
+ // Clean up temporary files.
166
+ fs. remove_file ( file1_path) ?;
167
+ fs. remove_file ( file2_path) ?;
168
+
67
169
Ok ( ( ) )
68
170
}
0 commit comments