@@ -12,6 +12,8 @@ use std::os::unix::fs::{MetadataExt, PermissionsExt};
1212use std:: process:: Command ;
1313#[ cfg( any( target_os = "linux" , target_os = "android" ) ) ]
1414use std:: thread:: sleep;
15+ #[ cfg( not( windows) ) ]
16+ use subprocess:: Exec ;
1517use uucore:: process:: { getegid, geteuid} ;
1618use uutests:: at_and_ucmd;
1719use uutests:: new_ucmd;
@@ -1808,3 +1810,37 @@ fn test_install_symlink_same_file() {
18081810 "'{target_dir}/{file}' and '{target_link}/{file}' are the same file"
18091811 ) ) ;
18101812}
1813+
1814+ #[ test]
1815+ #[ cfg( target_os = "linux" ) ]
1816+ fn test_install_set_owner_nonexistent_userid ( ) {
1817+ let captured = Exec :: shell (
1818+ "cat /etc/passwd|cut -d ':' -f 3|grep '^1...$'|sort -n|tail -n 1|awk '{print $1+1}'" ,
1819+ )
1820+ . capture ( ) ;
1821+ let next_userid = match captured {
1822+ Ok ( output) => {
1823+ let userid_str = output. stdout_str ( ) . trim_end ( ) . to_owned ( ) ;
1824+ match userid_str. parse :: < u32 > ( ) {
1825+ Ok ( userid) => userid,
1826+ Err ( e) => panic ! ( "cannot convert {} to userid, {}" , userid_str, e) ,
1827+ }
1828+ }
1829+ Err ( e) => panic ! ( "cannot capture output: {}" , e) ,
1830+ } ;
1831+
1832+ let ts = TestScenario :: new ( util_name ! ( ) ) ;
1833+ let at = & ts. fixtures ;
1834+ at. touch ( "a" ) ;
1835+ at. touch ( "c" ) ;
1836+
1837+ if let Ok ( result) = run_ucmd_as_root ( & ts, & [ format ! ( "-o{}" , next_userid) . as_str ( ) , "a" , "b" ] ) {
1838+ result. success ( ) ;
1839+ assert ! ( at. file_exists( "b" ) ) ;
1840+
1841+ let metadata = fs:: metadata ( at. plus ( "b" ) ) . unwrap ( ) ;
1842+ assert_eq ! ( metadata. uid( ) , next_userid) ;
1843+ } else {
1844+ print ! ( "Test skipped; requires root user" ) ;
1845+ }
1846+ }
0 commit comments