From: Philippe Antoine Date: Thu, 4 Jan 2024 11:48:12 +0000 (+0100) Subject: rust: fix assertions_on_constants for assert!(false) X-Git-Tag: suricata-7.0.9~36 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=a40b37ba4452179195f5b710ac99ce9464437cdf;p=thirdparty%2Fsuricata.git rust: fix assertions_on_constants for assert!(false) using panic! instead with a string message (cherry picked from commit a8199bf2ca16e8394b6bf5c41ba1bafe88f6ff53) --- diff --git a/rust/src/dcerpc/dcerpc_udp.rs b/rust/src/dcerpc/dcerpc_udp.rs index 05d49973c8..461050c00f 100644 --- a/rust/src/dcerpc/dcerpc_udp.rs +++ b/rust/src/dcerpc/dcerpc_udp.rs @@ -410,9 +410,7 @@ mod tests { 0x1c, 0x7d, 0xcf, 0x11, ]; - if let Ok((_rem, _header)) = parser::parse_dcerpc_udp_header(request) { - { assert!(false); } - } + assert!(parser::parse_dcerpc_udp_header(request).is_err()); } #[test] @@ -425,13 +423,9 @@ mod tests { 0x79, 0xbe, 0x01, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x68, 0x00, 0x00, 0x00, 0x0a, 0x00, ]; - match parser::parse_dcerpc_udp_header(request) { - Ok((rem, header)) => { - assert_eq!(4, header.rpc_vers); - assert_eq!(80, request.len() - rem.len()); - } - _ => { assert!(false); } - } + let (rem, header) = parser::parse_dcerpc_udp_header(request).unwrap(); + assert_eq!(4, header.rpc_vers); + assert_eq!(80, request.len() - rem.len()); } #[test] @@ -444,14 +438,10 @@ mod tests { 0x79, 0xbe, 0x01, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xff, 0xff, 0xff, 0xff, 0x68, 0x00, 0x00, 0x00, 0x0a, 0x00, ]; - match parser::parse_dcerpc_udp_header(request) { - Ok((rem, header)) => { - assert_eq!(4, header.rpc_vers); - assert_eq!(80, request.len() - rem.len()); - assert_eq!(0, rem.len()); - } - _ => { assert!(false); } - } + let (rem, header) = parser::parse_dcerpc_udp_header(request).unwrap(); + assert_eq!(4, header.rpc_vers); + assert_eq!(80, request.len() - rem.len()); + assert_eq!(0, rem.len()); } #[test] diff --git a/rust/src/detect/byte_math.rs b/rust/src/detect/byte_math.rs index 80bd3d5ee1..0586a92e5d 100644 --- a/rust/src/detect/byte_math.rs +++ b/rust/src/detect/byte_math.rs @@ -517,14 +517,8 @@ mod tests { ..Default::default() }; - match parse_bytemath(args) { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath(args).unwrap(); + assert_eq!(val, bmd); } #[test] @@ -623,52 +617,26 @@ mod tests { ..Default::default() }; - match parse_bytemath( + let (_, val) = parse_bytemath( "bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, string dec", - ) { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + ).unwrap(); + assert_eq!(val, bmd); bmd.flags = DETECT_BYTEMATH_FLAG_RVALUE_VAR; bmd.base = BASE_DEFAULT; - match parse_bytemath("bytes 4, offset 3933, oper +, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper +, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); bmd.flags = DETECT_BYTEMATH_FLAG_RVALUE_VAR | DETECT_BYTEMATH_FLAG_STRING; bmd.base = ByteMathBase::BaseHex; - match parse_bytemath( - "bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, string hex", - ) { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, string hex").unwrap(); + assert_eq!(val, bmd); bmd.base = ByteMathBase::BaseOct; - match parse_bytemath( + let (_, val) = parse_bytemath( "bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, string oct", - ) { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + ).unwrap(); + assert_eq!(val, bmd); } #[test] @@ -774,40 +742,25 @@ mod tests { }; bmd.bitmask_val = 0x12345678; - match parse_bytemath( + let (_, val) = parse_bytemath( "bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, bitmask 0x12345678", - ) { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + ).unwrap(); + assert_eq!(val, bmd); + bmd.bitmask_val = 0xffff1234; - match parse_bytemath( + let (_, val) = parse_bytemath( "bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, bitmask ffff1234", - ) { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + ).unwrap(); + assert_eq!(val, bmd); + bmd.bitmask_val = 0xffff1234; - match parse_bytemath( + let (_, val) = parse_bytemath( "bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, bitmask 0Xffff1234", - ) { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + ).unwrap(); + assert_eq!(val, bmd); + } #[test] fn test_parser_endian_valid() { @@ -824,49 +777,29 @@ mod tests { ..Default::default() }; - match parse_bytemath( + let (_, val) = parse_bytemath( "bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, endian big", - ) { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + ).unwrap(); + assert_eq!(val, bmd); + bmd.endian = ByteMathEndian::LittleEndian; - match parse_bytemath( + let (_, val) = parse_bytemath( "bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, endian little", - ) { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + ).unwrap(); + assert_eq!(val, bmd); + bmd.endian = ByteMathEndian::EndianDCE; - match parse_bytemath("bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, dce") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper +, rvalue myrvalue, result foo, dce").unwrap(); + assert_eq!(val, bmd); + bmd.endian = DETECT_BYTEMATH_ENDIAN_DEFAULT; bmd.flags = DETECT_BYTEMATH_FLAG_RVALUE_VAR; - match parse_bytemath("bytes 4, offset 3933, oper +, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper +, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); + } #[test] @@ -920,61 +853,31 @@ mod tests { ..Default::default() }; - match parse_bytemath("bytes 4, offset 3933, oper +, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper +, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); + bmd.oper = ByteMathOperator::Subtraction; - match parse_bytemath("bytes 4, offset 3933, oper -, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper -, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); + bmd.oper = ByteMathOperator::Multiplication; - match parse_bytemath("bytes 4, offset 3933, oper *, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper *, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); + bmd.oper = ByteMathOperator::Division; - match parse_bytemath("bytes 4, offset 3933, oper /, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper /, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); + bmd.oper = ByteMathOperator::RightShift; - match parse_bytemath("bytes 4, offset 3933, oper >>, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper >>, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); + bmd.oper = ByteMathOperator::LeftShift; - match parse_bytemath("bytes 4, offset 3933, oper <<, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 3933, oper <<, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); + } #[test] @@ -1013,33 +916,18 @@ mod tests { ..Default::default() }; - match parse_bytemath("bytes 4, offset 47303, oper *, rvalue 4294967295 , result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 47303, oper *, rvalue 4294967295 , result foo").unwrap(); + assert_eq!(val, bmd); + bmd.rvalue = 1; - match parse_bytemath("bytes 4, offset 47303, oper *, rvalue 1, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 47303, oper *, rvalue 1, result foo").unwrap(); + assert_eq!(val, bmd); + bmd.rvalue = 0; - match parse_bytemath("bytes 4, offset 47303, oper *, rvalue 0, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 47303, oper *, rvalue 0, result foo").unwrap(); + assert_eq!(val, bmd); + } #[test] @@ -1064,24 +952,14 @@ mod tests { ..Default::default() }; - match parse_bytemath("bytes 4, offset -65535, oper *, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset -65535, oper *, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); + bmd.offset = 65535; - match parse_bytemath("bytes 4, offset 65535, oper *, rvalue myrvalue, result foo") { - Ok((_, val)) => { - assert_eq!(val, bmd); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = parse_bytemath("bytes 4, offset 65535, oper *, rvalue myrvalue, result foo").unwrap(); + assert_eq!(val, bmd); + } #[test] diff --git a/rust/src/detect/uint.rs b/rust/src/detect/uint.rs index 312dad0ca9..8c758e3a5d 100644 --- a/rust/src/detect/uint.rs +++ b/rust/src/detect/uint.rs @@ -409,24 +409,12 @@ mod tests { #[test] fn test_parse_uint_unit() { - match detect_parse_uint::(" 2kb") { - Ok((_, val)) => { - assert_eq!(val.arg1, 2048); - } - Err(_) => { - assert!(false); - } - } - if let Ok((_, _val)) = detect_parse_uint::("2kb") { - assert!(false); - } - match detect_parse_uint::("3MB") { - Ok((_, val)) => { - assert_eq!(val.arg1, 3 * 1024 * 1024); - } - Err(_) => { - assert!(false); - } - } + let (_, val) = detect_parse_uint::(" 2kb").unwrap(); + assert_eq!(val.arg1, 2048); + + assert!(detect_parse_uint::("2kb").is_err()); + + let (_, val) = detect_parse_uint::("3MB").unwrap(); + assert_eq!(val.arg1, 3 * 1024 * 1024); } } diff --git a/rust/src/dhcp/parser.rs b/rust/src/dhcp/parser.rs index 48acccff2c..698462f301 100644 --- a/rust/src/dhcp/parser.rs +++ b/rust/src/dhcp/parser.rs @@ -242,42 +242,36 @@ mod tests { let pcap = include_bytes!("discover.pcap"); let payload = &pcap[24 + 16 + 42..]; - match dhcp_parse(payload) { - Ok((_rem, message)) => { - let header = message.header; - assert_eq!(header.opcode, BOOTP_REQUEST); - assert_eq!(header.htype, 1); - assert_eq!(header.hlen, 6); - assert_eq!(header.hops, 0); - assert_eq!(header.txid, 0x00003d1d); - assert_eq!(header.seconds, 0); - assert_eq!(header.flags, 0); - assert_eq!(header.clientip, &[0, 0, 0, 0]); - assert_eq!(header.yourip, &[0, 0, 0, 0]); - assert_eq!(header.serverip, &[0, 0, 0, 0]); - assert_eq!(header.giaddr, &[0, 0, 0, 0]); - assert_eq!( - &header.clienthw[..(header.hlen as usize)], - &[0x00, 0x0b, 0x82, 0x01, 0xfc, 0x42] - ); - assert!(header.servername.iter().all(|&x| x == 0)); - assert!(header.bootfilename.iter().all(|&x| x == 0)); - assert_eq!(header.magic, &[0x63, 0x82, 0x53, 0x63]); + let (_rem, message) = dhcp_parse(payload).unwrap(); + let header = message.header; + assert_eq!(header.opcode, BOOTP_REQUEST); + assert_eq!(header.htype, 1); + assert_eq!(header.hlen, 6); + assert_eq!(header.hops, 0); + assert_eq!(header.txid, 0x00003d1d); + assert_eq!(header.seconds, 0); + assert_eq!(header.flags, 0); + assert_eq!(header.clientip, &[0, 0, 0, 0]); + assert_eq!(header.yourip, &[0, 0, 0, 0]); + assert_eq!(header.serverip, &[0, 0, 0, 0]); + assert_eq!(header.giaddr, &[0, 0, 0, 0]); + assert_eq!( + &header.clienthw[..(header.hlen as usize)], + &[0x00, 0x0b, 0x82, 0x01, 0xfc, 0x42] + ); + assert!(header.servername.iter().all(|&x| x == 0)); + assert!(header.bootfilename.iter().all(|&x| x == 0)); + assert_eq!(header.magic, &[0x63, 0x82, 0x53, 0x63]); - assert!(!message.malformed_options); - assert!(!message.truncated_options); + assert!(!message.malformed_options); + assert!(!message.truncated_options); - assert_eq!(message.options.len(), 5); - assert_eq!(message.options[0].code, DHCP_OPT_TYPE); - assert_eq!(message.options[1].code, DHCP_OPT_CLIENT_ID); - assert_eq!(message.options[2].code, DHCP_OPT_REQUESTED_IP); - assert_eq!(message.options[3].code, DHCP_OPT_PARAMETER_LIST); - assert_eq!(message.options[4].code, DHCP_OPT_END); - } - _ => { - assert!(false); - } - } + assert_eq!(message.options.len(), 5); + assert_eq!(message.options[0].code, DHCP_OPT_TYPE); + assert_eq!(message.options[1].code, DHCP_OPT_CLIENT_ID); + assert_eq!(message.options[2].code, DHCP_OPT_REQUESTED_IP); + assert_eq!(message.options[3].code, DHCP_OPT_PARAMETER_LIST); + assert_eq!(message.options[4].code, DHCP_OPT_END); } #[test] diff --git a/rust/src/sip/parser.rs b/rust/src/sip/parser.rs index cdf5fb136c..996201e42b 100644 --- a/rust/src/sip/parser.rs +++ b/rust/src/sip/parser.rs @@ -281,17 +281,11 @@ mod tests { \r\n" .as_bytes(); - match sip_parse_request(buf) { - Ok((_, req)) => { - assert_eq!(req.method, "REGISTER"); - assert_eq!(req.path, "sip:sip.cybercity.dk"); - assert_eq!(req.version, "SIP/2.0"); - assert_eq!(req.headers["Content-Length"], "0"); - } - _ => { - assert!(false); - } - } + let (_, req) = sip_parse_request(buf).unwrap(); + assert_eq!(req.method, "REGISTER"); + assert_eq!(req.path, "sip:sip.cybercity.dk"); + assert_eq!(req.version, "SIP/2.0"); + assert_eq!(req.headers["Content-Length"], "0"); } #[test] @@ -317,16 +311,10 @@ mod tests { \r\n" .as_bytes(); - match sip_parse_response(buf) { - Ok((_, resp)) => { - assert_eq!(resp.version, "SIP/2.0"); - assert_eq!(resp.code, "401"); - assert_eq!(resp.reason, "Unauthorized"); - } - _ => { - assert!(false); - } - } + let (_, resp) = sip_parse_response(buf).unwrap(); + assert_eq!(resp.version, "SIP/2.0"); + assert_eq!(resp.code, "401"); + assert_eq!(resp.reason, "Unauthorized"); } #[test]