@@ -9,8 +9,9 @@ use uutests::new_ucmd;
99use uutests:: util:: TestScenario ;
1010use uutests:: util_name;
1111
12- const ALGOS : [ & str ; 11 ] = [
13- "sysv" , "bsd" , "crc" , "md5" , "sha1" , "sha224" , "sha256" , "sha384" , "sha512" , "blake2b" , "sm3" ,
12+ const ALGOS : [ & str ; 12 ] = [
13+ "sysv" , "bsd" , "crc" , "md5" , "sha1" , "sha2" , "sha224" , "sha256" , "sha384" , "sha512" , "blake2b" ,
14+ "sm3" ,
1415] ;
1516
1617#[ test]
@@ -2316,4 +2317,198 @@ mod format_mix {
23162317 . stdout_contains ( "bar: OK" )
23172318 . stderr_contains ( "cksum: WARNING: 1 line is improperly formatted" ) ;
23182319 }
2320+
2321+ #[ test]
2322+ fn test_sha2_requires_length ( ) {
2323+ // Test that sha2 algorithm requires a length parameter
2324+ new_ucmd ! ( )
2325+ . arg ( "-a" )
2326+ . arg ( "sha2" )
2327+ . arg ( "lorem_ipsum.txt" )
2328+ . fails_with_code ( 1 )
2329+ . no_stdout ( )
2330+ . stderr_contains ( "--bits required for SHA2" ) ;
2331+ }
2332+
2333+ #[ test]
2334+ fn test_sha2_with_length_224 ( ) {
2335+ // Test sha2 with 224-bit length (equivalent to sha224)
2336+ let result_sha2 = new_ucmd ! ( )
2337+ . arg ( "-a" )
2338+ . arg ( "sha2" )
2339+ . arg ( "-l" )
2340+ . arg ( "224" )
2341+ . pipe_in ( "test\n " )
2342+ . succeeds ( )
2343+ . stdout_str ( )
2344+ . to_owned ( ) ;
2345+
2346+ let result_sha224 = new_ucmd ! ( )
2347+ . arg ( "-a" )
2348+ . arg ( "sha224" )
2349+ . pipe_in ( "test\n " )
2350+ . succeeds ( )
2351+ . stdout_str ( )
2352+ . to_owned ( ) ;
2353+
2354+ // The outputs should be identical (both produce SHA224)
2355+ assert_eq ! ( result_sha2, result_sha224) ;
2356+ assert ! ( result_sha2. contains( "SHA224" ) ) ;
2357+ }
2358+
2359+ #[ test]
2360+ fn test_sha2_with_length_256 ( ) {
2361+ // Test sha2 with 256-bit length (equivalent to sha256)
2362+ let result_sha2 = new_ucmd ! ( )
2363+ . arg ( "-a" )
2364+ . arg ( "sha2" )
2365+ . arg ( "-l" )
2366+ . arg ( "256" )
2367+ . pipe_in ( "test\n " )
2368+ . succeeds ( )
2369+ . stdout_str ( )
2370+ . to_owned ( ) ;
2371+
2372+ let result_sha256 = new_ucmd ! ( )
2373+ . arg ( "-a" )
2374+ . arg ( "sha256" )
2375+ . pipe_in ( "test\n " )
2376+ . succeeds ( )
2377+ . stdout_str ( )
2378+ . to_owned ( ) ;
2379+
2380+ // The outputs should be identical (both produce SHA256)
2381+ assert_eq ! ( result_sha2, result_sha256) ;
2382+ assert ! ( result_sha2. contains( "SHA256" ) ) ;
2383+ }
2384+
2385+ #[ test]
2386+ fn test_sha2_with_length_384 ( ) {
2387+ // Test sha2 with 384-bit length (equivalent to sha384)
2388+ let result_sha2 = new_ucmd ! ( )
2389+ . arg ( "-a" )
2390+ . arg ( "sha2" )
2391+ . arg ( "-l" )
2392+ . arg ( "384" )
2393+ . pipe_in ( "test\n " )
2394+ . succeeds ( )
2395+ . stdout_str ( )
2396+ . to_owned ( ) ;
2397+
2398+ let result_sha384 = new_ucmd ! ( )
2399+ . arg ( "-a" )
2400+ . arg ( "sha384" )
2401+ . pipe_in ( "test\n " )
2402+ . succeeds ( )
2403+ . stdout_str ( )
2404+ . to_owned ( ) ;
2405+
2406+ // The outputs should be identical (both produce SHA384)
2407+ assert_eq ! ( result_sha2, result_sha384) ;
2408+ assert ! ( result_sha2. contains( "SHA384" ) ) ;
2409+ }
2410+
2411+ #[ test]
2412+ fn test_sha2_with_length_512 ( ) {
2413+ // Test sha2 with 512-bit length (equivalent to sha512)
2414+ let result_sha2 = new_ucmd ! ( )
2415+ . arg ( "-a" )
2416+ . arg ( "sha2" )
2417+ . arg ( "-l" )
2418+ . arg ( "512" )
2419+ . pipe_in ( "test\n " )
2420+ . succeeds ( )
2421+ . stdout_str ( )
2422+ . to_owned ( ) ;
2423+
2424+ let result_sha512 = new_ucmd ! ( )
2425+ . arg ( "-a" )
2426+ . arg ( "sha512" )
2427+ . pipe_in ( "test\n " )
2428+ . succeeds ( )
2429+ . stdout_str ( )
2430+ . to_owned ( ) ;
2431+
2432+ // The outputs should be identical (both produce SHA512)
2433+ assert_eq ! ( result_sha2, result_sha512) ;
2434+ assert ! ( result_sha2. contains( "SHA512" ) ) ;
2435+ }
2436+
2437+ #[ test]
2438+ fn test_sha2_invalid_length ( ) {
2439+ // Test sha2 with invalid length parameters
2440+ new_ucmd ! ( )
2441+ . arg ( "-a" )
2442+ . arg ( "sha2" )
2443+ . arg ( "-l" )
2444+ . arg ( "128" )
2445+ . pipe_in ( "test\n " )
2446+ . fails_with_code ( 1 )
2447+ . stderr_contains ( "Invalid output size for SHA2" ) ;
2448+
2449+ new_ucmd ! ( )
2450+ . arg ( "-a" )
2451+ . arg ( "sha2" )
2452+ . arg ( "-l" )
2453+ . arg ( "1024" )
2454+ . pipe_in ( "test\n " )
2455+ . fails_with_code ( 1 )
2456+ . stderr_contains ( "Invalid output size for SHA2" ) ;
2457+ }
2458+
2459+ #[ test]
2460+ fn test_sha2_with_raw_output ( ) {
2461+ // Test sha2 with raw (binary) output
2462+ let result = new_ucmd ! ( )
2463+ . arg ( "-a" )
2464+ . arg ( "sha2" )
2465+ . arg ( "-l" )
2466+ . arg ( "256" )
2467+ . arg ( "--raw" )
2468+ . pipe_in ( "test\n " )
2469+ . succeeds ( ) ;
2470+
2471+ // Raw output should be binary, not hex - check raw bytes instead of trying to convert to string
2472+ let stdout_bytes = result. stdout ( ) ;
2473+ // Raw output should not be empty and should be exactly 32 bytes for SHA256
2474+ assert_eq ! ( stdout_bytes. len( ) , 32 ) ;
2475+ // Should not contain ASCII text like "SHA256"
2476+ assert ! ( !stdout_bytes. starts_with( b"SHA256" ) ) ;
2477+ }
2478+
2479+ #[ test]
2480+ fn test_sha2_with_base64_output ( ) {
2481+ // Test sha2 with base64 output
2482+ let result = new_ucmd ! ( )
2483+ . arg ( "-a" )
2484+ . arg ( "sha2" )
2485+ . arg ( "-l" )
2486+ . arg ( "256" )
2487+ . arg ( "--base64" )
2488+ . pipe_in ( "test\n " )
2489+ . succeeds ( )
2490+ . stdout_str ( )
2491+ . to_owned ( ) ;
2492+
2493+ // Should contain base64 characters and SHA256 label
2494+ assert ! ( result. contains( "SHA256" ) ) ;
2495+ // Base64 should end with = or == or contain +/
2496+ assert ! ( result. contains( '=' ) || result. contains( '+' ) || result. contains( '/' ) ) ;
2497+ }
2498+
2499+ #[ test]
2500+ fn test_sha2_with_file ( ) {
2501+ // Test sha2 with actual file input
2502+ let ( at, mut ucmd) = at_and_ucmd ! ( ) ;
2503+ at. write ( "test_file.txt" , "Hello, world!\n " ) ;
2504+
2505+ ucmd. arg ( "-a" )
2506+ . arg ( "sha2" )
2507+ . arg ( "-l" )
2508+ . arg ( "256" )
2509+ . arg ( "test_file.txt" )
2510+ . succeeds ( )
2511+ . stdout_contains ( "SHA256" )
2512+ . stdout_contains ( "test_file.txt" ) ;
2513+ }
23192514}
0 commit comments