@@ -76,3 +76,93 @@ impl KillableBuildable for KillerBuilder {
7676 Ok ( Killer { signal } )
7777 }
7878}
79+
80+ #[ cfg( test) ]
81+ mod tests {
82+ use super :: * ;
83+ use crate :: get_available_max_process_id;
84+
85+ #[ test]
86+ fn test_validate_process_id_kernel ( ) {
87+ let result = validate_process_id ( KERNEL_PROCESS_ID , 100 ) ;
88+ assert ! ( result. is_err( ) ) ;
89+ assert_eq ! (
90+ result. unwrap_err( ) . to_string( ) ,
91+ "Invalid process id: 0. Reason: Not allowed to kill kernel process"
92+ ) ;
93+ }
94+
95+ #[ test]
96+ fn test_validate_process_id_init ( ) {
97+ let result = validate_process_id ( INIT_PROCESS_ID , 100 ) ;
98+ assert ! ( result. is_err( ) ) ;
99+ assert_eq ! (
100+ result. unwrap_err( ) . to_string( ) ,
101+ "Invalid process id: 1. Reason: Not allowed to kill init process"
102+ ) ;
103+ }
104+
105+ #[ test]
106+ fn test_validate_process_id_too_large ( ) {
107+ let result = validate_process_id ( 101 , 100 ) ;
108+ assert ! ( result. is_err( ) ) ;
109+ assert_eq ! (
110+ result. unwrap_err( ) . to_string( ) ,
111+ "Invalid process id: 101. Reason: Process id is too large. process id: 101, available max process id: 100"
112+ ) ;
113+ }
114+
115+ #[ test]
116+ fn test_validate_process_id_ok ( ) {
117+ let result = validate_process_id ( 100 , 100 ) ;
118+ assert ! ( result. is_ok( ) ) ;
119+ }
120+
121+ #[ test]
122+ fn kii_sigterm ( ) {
123+ let target_process_id = get_available_max_process_id ( ) ;
124+ let kill_output =
125+ kill ( target_process_id, nix:: sys:: signal:: Signal :: SIGTERM ) . expect ( "Failed to kill" ) ;
126+ match kill_output {
127+ KillOutput :: Killed { process_id : _ } => {
128+ panic ! ( "This should not happen" ) ;
129+ }
130+ KillOutput :: MaybeAlreadyTerminated { process_id, source } => {
131+ assert_eq ! ( process_id, target_process_id) ;
132+ assert_eq ! ( source. to_string( ) , "Unix error: ESRCH: No such process" ) ;
133+ }
134+ }
135+ }
136+
137+ #[ test]
138+ fn kii_sigkill ( ) {
139+ let target_process_id = get_available_max_process_id ( ) ;
140+ let kill_output =
141+ kill ( target_process_id, nix:: sys:: signal:: Signal :: SIGKILL ) . expect ( "Failed to kill" ) ;
142+ match kill_output {
143+ KillOutput :: Killed { process_id : _ } => {
144+ panic ! ( "This should not happen" ) ;
145+ }
146+ KillOutput :: MaybeAlreadyTerminated { process_id, source } => {
147+ assert_eq ! ( process_id, target_process_id) ;
148+ assert_eq ! ( source. to_string( ) , "Unix error: ESRCH: No such process" ) ;
149+ }
150+ }
151+ }
152+
153+ #[ test]
154+ fn kii_sigint ( ) {
155+ let target_process_id = get_available_max_process_id ( ) ;
156+ let kill_output =
157+ kill ( target_process_id, nix:: sys:: signal:: Signal :: SIGINT ) . expect ( "Failed to kill" ) ;
158+ match kill_output {
159+ KillOutput :: Killed { process_id : _ } => {
160+ panic ! ( "This should not happen" ) ;
161+ }
162+ KillOutput :: MaybeAlreadyTerminated { process_id, source } => {
163+ assert_eq ! ( process_id, target_process_id) ;
164+ assert_eq ! ( source. to_string( ) , "Unix error: ESRCH: No such process" ) ;
165+ }
166+ }
167+ }
168+ }
0 commit comments