@@ -188,3 +188,95 @@ impl Default for CpioArchive {
188188 Self :: new ( )
189189 }
190190}
191+
192+ #[ cfg( test) ]
193+ mod tests {
194+ use super :: * ;
195+ use std:: fs;
196+ use tempfile:: TempDir ;
197+
198+ #[ test]
199+ fn test_empty_archive ( ) {
200+ let archive = CpioArchive :: new ( ) ;
201+ assert ! ( archive. is_empty( ) ) ;
202+ assert_eq ! ( archive. len( ) , 0 ) ;
203+ }
204+
205+ #[ test]
206+ fn test_archive_from_directory ( ) {
207+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
208+ let file_path = temp_dir. path ( ) . join ( "test.txt" ) ;
209+ fs:: write ( & file_path, b"hello world" ) . unwrap ( ) ;
210+
211+ let archive = CpioArchive :: from_directory ( temp_dir. path ( ) ) . unwrap ( ) ;
212+ assert_eq ! ( archive. len( ) , 1 ) ;
213+ }
214+
215+ #[ test]
216+ fn test_cpio_header_magic ( ) {
217+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
218+ let file_path = temp_dir. path ( ) . join ( "test.txt" ) ;
219+ fs:: write ( & file_path, b"test" ) . unwrap ( ) ;
220+
221+ let archive = CpioArchive :: from_directory ( temp_dir. path ( ) ) . unwrap ( ) ;
222+ let mut output = Vec :: new ( ) ;
223+ archive. write_to ( & mut output) . unwrap ( ) ;
224+
225+ let header = String :: from_utf8_lossy ( & output[ ..6 ] ) ;
226+ assert_eq ! ( header, "070701" , "CPIO header should start with newc magic" ) ;
227+ }
228+
229+ #[ test]
230+ fn test_cpio_trailer ( ) {
231+ let archive = CpioArchive :: new ( ) ;
232+ let mut output = Vec :: new ( ) ;
233+ archive. write_to ( & mut output) . unwrap ( ) ;
234+
235+ let output_str = String :: from_utf8_lossy ( & output) ;
236+ assert ! (
237+ output_str. contains( "TRAILER!!!" ) ,
238+ "Archive should end with TRAILER!!!"
239+ ) ;
240+ }
241+
242+ #[ test]
243+ fn test_multiple_files ( ) {
244+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
245+ fs:: write ( temp_dir. path ( ) . join ( "a.txt" ) , b"aaa" ) . unwrap ( ) ;
246+ fs:: write ( temp_dir. path ( ) . join ( "b.txt" ) , b"bbb" ) . unwrap ( ) ;
247+ fs:: create_dir ( temp_dir. path ( ) . join ( "subdir" ) ) . unwrap ( ) ;
248+ fs:: write ( temp_dir. path ( ) . join ( "subdir/c.txt" ) , b"ccc" ) . unwrap ( ) ;
249+
250+ let archive = CpioArchive :: from_directory ( temp_dir. path ( ) ) . unwrap ( ) ;
251+ assert_eq ! ( archive. len( ) , 4 ) ; // 3 files + 1 directory
252+ }
253+
254+ #[ test]
255+ fn test_symlink_handling ( ) {
256+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
257+ let target = temp_dir. path ( ) . join ( "target.txt" ) ;
258+ let link = temp_dir. path ( ) . join ( "link.txt" ) ;
259+ fs:: write ( & target, b"target content" ) . unwrap ( ) ;
260+
261+ #[ cfg( unix) ]
262+ std:: os:: unix:: fs:: symlink ( & target, & link) . unwrap ( ) ;
263+
264+ let archive = CpioArchive :: from_directory ( temp_dir. path ( ) ) . unwrap ( ) ;
265+
266+ #[ cfg( unix) ]
267+ assert_eq ! ( archive. len( ) , 2 ) ;
268+ }
269+
270+ #[ test]
271+ fn test_output_alignment ( ) {
272+ let temp_dir = TempDir :: new ( ) . unwrap ( ) ;
273+ fs:: write ( temp_dir. path ( ) . join ( "odd.txt" ) , b"123" ) . unwrap ( ) ; // 3 bytes, needs padding
274+
275+ let archive = CpioArchive :: from_directory ( temp_dir. path ( ) ) . unwrap ( ) ;
276+ let mut output = Vec :: new ( ) ;
277+ archive. write_to ( & mut output) . unwrap ( ) ;
278+
279+ // Output should be 4-byte aligned
280+ assert_eq ! ( output. len( ) % 4 , 0 , "CPIO output should be 4-byte aligned" ) ;
281+ }
282+ }
0 commit comments