@@ -502,3 +502,188 @@ pub fn get_gid(meta: &Metadata) -> Option<u32> {
502
502
None
503
503
}
504
504
}
505
+
506
+ #[ cfg( test) ]
507
+ mod tests {
508
+ use super :: * ;
509
+
510
+ #[ test]
511
+ fn test_format_mode ( ) {
512
+ #[ cfg( unix) ]
513
+ {
514
+ // Regular file with rwxr-xr-- permissions (0754 in octal)
515
+ let mode = 0o100754 ;
516
+ assert_eq ! ( format_mode( mode) , "-rwxr-xr--" ) ;
517
+
518
+ // Directory with rwxr-xr-x permissions (0755 in octal)
519
+ let mode = 0o40755 ;
520
+ assert_eq ! ( format_mode( mode) , "drwxr-xr-x" ) ;
521
+
522
+ // Symbolic link with rwxrwxrwx permissions (0777 in octal)
523
+ let mode = 0o120777 ;
524
+ assert_eq ! ( format_mode( mode) , "lrwxrwxrwx" ) ;
525
+
526
+ // File with setuid bit (4755 in octal)
527
+ let mode = 0o104755 ;
528
+ assert_eq ! ( format_mode( mode) , "-rwsr-xr-x" ) ;
529
+
530
+ // File with setgid bit (2755 in octal)
531
+ let mode = 0o102755 ;
532
+ assert_eq ! ( format_mode( mode) , "-rwxr-sr-x" ) ;
533
+
534
+ // Directory with sticky bit (1755 in octal)
535
+ let mode = 0o41755 ;
536
+ assert_eq ! ( format_mode( mode) , "drwxr-xr-t" ) ;
537
+ }
538
+
539
+ #[ cfg( windows) ]
540
+ {
541
+ const FILE_ATTRIBUTE_READONLY : u32 = 0x1 ;
542
+ const FILE_ATTRIBUTE_HIDDEN : u32 = 0x2 ;
543
+ const FILE_ATTRIBUTE_DIRECTORY : u32 = 0x10 ;
544
+
545
+ let mode = FILE_ATTRIBUTE_READONLY ;
546
+ assert_eq ! ( format_mode( mode) , "Readonly" ) ;
547
+
548
+ let mode = FILE_ATTRIBUTE_HIDDEN ;
549
+ assert_eq ! ( format_mode( mode) , "Hidden" ) ;
550
+
551
+ let mode = FILE_ATTRIBUTE_DIRECTORY ;
552
+ assert_eq ! ( format_mode( mode) , "Directory" ) ;
553
+
554
+ let mode = FILE_ATTRIBUTE_READONLY | FILE_ATTRIBUTE_HIDDEN ;
555
+ assert_eq ! ( format_mode( mode) , "Hidden, Readonly" ) ;
556
+ }
557
+ }
558
+
559
+ #[ test]
560
+ fn test_mode_user_permissions ( ) {
561
+ let mode = 0o754 ; // rwxr-xr--
562
+
563
+ assert ! ( mode_user_read( mode) ) ;
564
+ assert ! ( mode_user_write( mode) ) ;
565
+ assert ! ( mode_user_exec( mode) ) ;
566
+ assert ! ( mode_user_all( mode) ) ;
567
+
568
+ let mode = 0o654 ; // rw-r-xr--
569
+
570
+ assert ! ( mode_user_read( mode) ) ;
571
+ assert ! ( mode_user_write( mode) ) ;
572
+ assert ! ( !mode_user_exec( mode) ) ;
573
+ assert ! ( !mode_user_all( mode) ) ;
574
+ }
575
+
576
+ #[ test]
577
+ fn test_mode_group_permissions ( ) {
578
+ // Test group permission checks
579
+ let mode = 0o754 ; // rwxr-xr--
580
+
581
+ assert ! ( mode_group_read( mode) ) ;
582
+ assert ! ( !mode_group_write( mode) ) ;
583
+ assert ! ( mode_group_exec( mode) ) ;
584
+ assert ! ( !mode_group_all( mode) ) ;
585
+
586
+ let mode = 0o774 ; // rwxrwxr--
587
+
588
+ assert ! ( mode_group_read( mode) ) ;
589
+ assert ! ( mode_group_write( mode) ) ;
590
+ assert ! ( mode_group_exec( mode) ) ;
591
+ assert ! ( mode_group_all( mode) ) ;
592
+ }
593
+
594
+ #[ test]
595
+ fn test_mode_other_permissions ( ) {
596
+ let mode = 0o754 ; // rwxr-xr--
597
+
598
+ assert ! ( mode_other_read( mode) ) ;
599
+ assert ! ( !mode_other_write( mode) ) ;
600
+ assert ! ( !mode_other_exec( mode) ) ;
601
+ assert ! ( !mode_other_all( mode) ) ;
602
+
603
+ let mode = 0o757 ; // rwxr-xrwx
604
+
605
+ assert ! ( mode_other_read( mode) ) ;
606
+ assert ! ( mode_other_write( mode) ) ;
607
+ assert ! ( mode_other_exec( mode) ) ;
608
+ assert ! ( mode_other_all( mode) ) ;
609
+ }
610
+
611
+ #[ test]
612
+ fn test_mode_special_bits ( ) {
613
+ // Test setuid bit
614
+ let mode = 0o4755 ; // rwsr-xr-x
615
+ assert ! ( mode_suid( mode) ) ;
616
+
617
+ // Test setgid bit
618
+ let mode = 0o2755 ; // rwxr-sr-x
619
+ assert ! ( mode_sgid( mode) ) ;
620
+
621
+ // Test sticky bit (Unix only)
622
+ #[ cfg( unix) ]
623
+ {
624
+ let mode = 0o1755 ; // rwxr-xr-t
625
+ assert ! ( mode_sticky( mode) ) ;
626
+ }
627
+ }
628
+
629
+ #[ test]
630
+ fn test_mode_file_types ( ) {
631
+ // Test directory
632
+ #[ cfg( unix) ]
633
+ {
634
+ let mode = 0o40755 ; // drwxr-xr-x
635
+ assert ! ( mode_is_directory( mode) ) ;
636
+ assert ! ( !mode_is_link( mode) ) ;
637
+ }
638
+
639
+ // Test symbolic link
640
+ #[ cfg( unix) ]
641
+ {
642
+ let mode = 0o120755 ; // lrwxr-xr-x
643
+ assert ! ( mode_is_link( mode) ) ;
644
+ assert ! ( !mode_is_directory( mode) ) ;
645
+ }
646
+
647
+ // Test block device
648
+ let mode = 0o60644 ; // brw-r--r--
649
+ assert ! ( mode_is_block_device( mode) ) ;
650
+
651
+ // Test character device
652
+ let mode = 0o20644 ; // crw-r--r--
653
+ assert ! ( mode_is_char_device( mode) ) ;
654
+
655
+ // Test FIFO/pipe
656
+ let mode = 0o10644 ; // prw-r--r--
657
+ assert ! ( mode_is_pipe( mode) ) ;
658
+
659
+ // Test socket
660
+ let mode = 0o140644 ; // srw-r--r--
661
+ assert ! ( mode_is_socket( mode) ) ;
662
+ }
663
+
664
+ #[ test]
665
+ fn test_get_uid_gid ( ) {
666
+ // These functions are platform-specific, so we test the behavior
667
+ // rather than the actual values
668
+
669
+ #[ cfg( unix) ]
670
+ {
671
+ // On Unix, we should get Some value
672
+ use std:: fs:: File ;
673
+ if let Ok ( meta) = File :: open ( "Cargo.toml" ) . and_then ( |f| f. metadata ( ) ) {
674
+ assert ! ( get_uid( & meta) . is_some( ) ) ;
675
+ assert ! ( get_gid( & meta) . is_some( ) ) ;
676
+ }
677
+ }
678
+
679
+ #[ cfg( not( unix) ) ]
680
+ {
681
+ // On non-Unix platforms, we should get None
682
+ use std:: fs:: File ;
683
+ if let Ok ( meta) = File :: open ( "Cargo.toml" ) . and_then ( |f| f. metadata ( ) ) {
684
+ assert ! ( get_uid( & meta) . is_none( ) ) ;
685
+ assert ! ( get_gid( & meta) . is_none( ) ) ;
686
+ }
687
+ }
688
+ }
689
+ }
0 commit comments