@@ -252,4 +252,47 @@ mod unix_tests {
252252 let _ = UnixListener :: bind ( & sock_path) . unwrap ( ) ;
253253 assert ! ( is_socket( file_mode( sock_path) ) ) ;
254254 }
255+
256+ mod to_string {
257+ use super :: * ;
258+ use std:: fs:: Permissions ;
259+ use std:: os:: unix:: fs:: PermissionsExt ;
260+ use std:: process:: Command ;
261+
262+ fn shells ( chmod_to : & str , expect_mode : & str ) {
263+ let tmp_dir = tempdir ( ) . unwrap ( ) ;
264+ // We're gonna be mucking around with setuid files, so exercise a little bit of caution
265+ std:: fs:: set_permissions ( tmp_dir. path ( ) , Permissions :: from_mode ( 0o700 ) ) . unwrap ( ) ;
266+ let f = & tmp_dir. path ( ) . join ( "f" ) ;
267+ std:: fs:: write ( f, & [ 0 ] ) . unwrap ( ) ;
268+ std:: fs:: set_permissions ( f, Permissions :: from_mode ( 0 ) ) . unwrap ( ) ;
269+ let chmod = Command :: new ( "chmod" ) . arg ( chmod_to) . arg ( f) . output ( ) . unwrap ( ) ;
270+ println ! ( "chmod {:#?}" , chmod) ;
271+ assert_eq ! ( to_string( file_mode( f) ) , expect_mode) ;
272+ // For good measure, also compare against ls
273+ let ls = Command :: new ( "ls" ) . arg ( "-l" ) . arg ( f) . output ( ) . unwrap ( ) ;
274+ println ! ( "{:#?}" , ls) ;
275+ assert_eq ! ( std:: str :: from_utf8( & ls. stdout[ 0 ..10 ] ) , Ok ( expect_mode) ) ;
276+ }
277+
278+ #[ test]
279+ fn rwx ( ) {
280+ shells ( "a+r" , "-r--r--r--" ) ;
281+ shells ( "a+w" , "--w--w--w-" ) ;
282+ shells ( "a+x" , "---x--x--x" ) ;
283+ }
284+
285+ #[ test]
286+ fn extrabits ( ) {
287+ shells ( "+t" , "---------T" ) ;
288+ shells ( "+xt" , "---x--x--t" ) ;
289+ shells ( "+s" , "---S--S---" ) ;
290+ shells ( "+xs" , "---s--s--x" ) ;
291+ }
292+
293+ #[ test]
294+ fn nothing_with_left_beef ( ) {
295+ shells ( "u+wx,g+r" , "--wxr-----" ) ;
296+ }
297+ }
255298}
0 commit comments