@@ -51,76 +51,112 @@ fn init_logger() {
5151#[ cfg( test) ]
5252mod tests {
5353 use super :: * ;
54+ use std:: fs;
55+ use zircon_object:: object:: task:: * ;
5456
5557 /// test with cmd line
56- async fn test ( cmdline : & str ) {
58+ async fn test ( cmdline : & str ) -> i64 {
5759 kernel_hal_unix:: init ( ) ;
5860
5961 let args: Vec < String > = cmdline. split ( ' ' ) . map ( |s| s. into ( ) ) . collect ( ) ;
6062 let envs =
6163 vec ! [ "PATH=/usr/sbin:/usr/bin:/sbin:/bin:/usr/x86_64-alpine-linux-musl/bin" . into( ) ] ; // TODO
6264 let hostfs = HostFS :: new ( "../rootfs" ) ;
6365 let proc = run ( args, envs, hostfs) ;
64- let proc: Arc < dyn KernelObject > = proc;
65- proc. wait_signal ( Signal :: PROCESS_TERMINATED ) . await ;
66+ let procobj: Arc < dyn KernelObject > = proc. clone ( ) ;
67+ procobj. wait_signal ( Signal :: PROCESS_TERMINATED ) . await ;
68+ if let Status :: Exited ( code) = proc. status ( ) {
69+ return code;
70+ }
71+ -1
6672 }
6773
6874 // test using busybox
6975
7076 #[ async_std:: test]
7177 async fn test_busybox ( ) {
72- test ( "/bin/busybox" ) . await ;
78+ assert_eq ! ( test( "/bin/busybox" ) . await , 0 ) ;
7379 }
7480
7581 #[ async_std:: test]
7682 async fn test_uname ( ) {
77- test ( "/bin/busybox uname -a" ) . await ;
83+ assert_eq ! ( test( "/bin/busybox uname -a" ) . await , 0 ) ;
7884 }
7985
8086 #[ async_std:: test]
8187 async fn test_date ( ) {
82- test ( "/bin/busybox date" ) . await ;
88+ assert_eq ! ( test( "/bin/busybox date" ) . await , 0 ) ;
8389 }
8490
8591 #[ async_std:: test]
8692 async fn test_dir ( ) {
87- test ( "/bin/busybox pwd" ) . await ;
88- test ( "/bin/busybox ls -a" ) . await ;
89- test ( "/bin/busybox dirname /bin/busybox" ) . await ;
93+ assert_eq ! ( test( "/bin/busybox pwd" ) . await , 0 ) ;
94+ assert_eq ! ( test( "/bin/busybox ls -a" ) . await , 0 ) ;
95+ assert_eq ! ( test( "/bin/busybox dirname /bin/busybox" ) . await , 0 ) ;
96+ }
97+
98+ #[ async_std:: test]
99+ async fn test_create_remove_file ( ) {
100+ test ( "/bin/busybox rm testfile" ) . await ; // can't remove
101+ fs:: read ( "../rootfs/testfile" ) . unwrap_err ( ) ;
102+ test ( "/bin/busybox touch testfile" ) . await ;
103+ fs:: read ( "../rootfs/testfile" ) . unwrap ( ) ;
104+ test ( "/bin/busybox touch testfile" ) . await ;
105+ fs:: read ( "../rootfs/testfile" ) . unwrap ( ) ;
106+ test ( "/bin/busybox rm testfile" ) . await ;
107+ fs:: read ( "../rootfs/testfile" ) . unwrap_err ( ) ;
90108 }
91109
92110 #[ async_std:: test]
93111 async fn test_create_remove_dir ( ) {
112+ test ( "/bin/busybox rmdir test" ) . await ; // can't remove
113+ fs:: read_dir ( "../rootfs/test" ) . unwrap_err ( ) ;
94114 test ( "/bin/busybox mkdir test" ) . await ;
115+ fs:: read_dir ( "../rootfs/test" ) . unwrap ( ) ;
95116 test ( "/bin/busybox rmdir test" ) . await ;
117+ fs:: read_dir ( "../rootfs/test" ) . unwrap_err ( ) ;
96118 }
97119
98120 #[ async_std:: test]
99121 async fn test_readfile ( ) {
100- test ( "/bin/busybox cat /etc/profile" ) . await ;
122+ assert_eq ! ( test( "/bin/busybox cat /etc/profile" ) . await , 0 ) ;
123+ assert_eq ! ( test( "/bin/busybox cat /etc/profila" ) . await , 1 ) ; // can't open
101124 }
102125
103126 #[ async_std:: test]
104127 async fn test_cp_mv ( ) {
128+ test ( "/bin/busybox cp /etc/hostnama /etc/hostname.bak" ) . await ; // can't move
129+ fs:: read ( "../rootfs/etc/hostname.bak" ) . unwrap_err ( ) ;
105130 test ( "/bin/busybox cp /etc/hostname /etc/hostname.bak" ) . await ;
131+ fs:: read ( "../rootfs/etc/hostname.bak" ) . unwrap ( ) ;
106132 test ( "/bin/busybox mv /etc/hostname.bak /etc/hostname.mv" ) . await ;
133+ fs:: read ( "../rootfs/etc/hostname.bak" ) . unwrap_err ( ) ;
107134 }
108135
109136 #[ async_std:: test]
110137 async fn test_link ( ) {
138+ test ( "/bin/busybox ln /etc/hostnama /etc/hostname.ln" ) . await ; // can't ln
139+ fs:: read ( "../rootfs/etc/hostname.ln" ) . unwrap_err ( ) ;
111140 test ( "/bin/busybox ln /etc/hostname /etc/hostname.ln" ) . await ;
141+ fs:: read ( "../rootfs/etc/hostname.ln" ) . unwrap ( ) ;
112142 test ( "/bin/busybox unlink /etc/hostname.ln" ) . await ;
143+ fs:: read ( "../rootfs/etc/hostname.ln" ) . unwrap_err ( ) ;
113144 }
114145
115146 #[ async_std:: test]
116147 async fn test_env ( ) {
117- test ( "/bin/busybox env" ) . await ;
148+ assert_eq ! ( test( "/bin/busybox env" ) . await , 0 ) ;
118149 }
119150
120151 // syscall unit test
121152
122153 #[ async_std:: test]
123154 async fn test_pipe ( ) {
124- test ( "/bin/testpipe1" ) . await ;
155+ assert_eq ! ( test( "/bin/testpipe1" ) . await , 0 ) ;
156+ }
157+
158+ #[ async_std:: test]
159+ async fn test_time ( ) {
160+ assert_eq ! ( test( "/bin/testtime" ) . await , 0 ) ;
125161 }
126162}
0 commit comments