]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rfb: add unittests for nom7 parsers
authorHaleema Khan <hsadia538@gmail.com>
Fri, 16 Dec 2022 00:16:24 +0000 (05:16 +0500)
committerVictor Julien <vjulien@oisf.net>
Thu, 22 Dec 2022 09:26:14 +0000 (10:26 +0100)
Task: #5741

rust/src/rfb/parser.rs

index 786b831a1405cf0fdce659a34d28e5fe23ca12c2..40ef4d50fbb0b62605602c53698ff3aa812c2496 100644 (file)
@@ -1,4 +1,4 @@
-/* Copyright (C) 2020 Open Information Security Foundation
+/* Copyright (C) 2020-2022 Open Information Security Foundation
  *
  * You can copy, redistribute or modify this Program under the terms of
  * the GNU General Public License version 2 as published by the Free
@@ -229,7 +229,7 @@ mod tests {
                 // Check the first message.
                 assert_eq!(message.major, "003");
 
-                // And we should have 6 bytes left.
+                // And we should have 0 bytes left.
                 assert_eq!(remainder.len(), 0);
             }
             Err(Err::Incomplete(_)) => {
@@ -262,7 +262,7 @@ mod tests {
                 assert_eq!(message.height, 800);
                 assert_eq!(message.pixel_format.bits_per_pixel, 32);
 
-                // And we should have 6 bytes left.
+                // And we should have 0 bytes left.
                 assert_eq!(remainder.len(), 0);
             }
             Err(Err::Incomplete(_)) => {
@@ -275,4 +275,117 @@ mod tests {
         }
     }
 
+    #[test]
+    fn test_parse_pixel_format() {
+        let buf = [
+            0x20, /* Bits per pixel: 32 */
+            0x18, /* Depth: 24 */
+            0x00, /* Big endian flag: False */
+            0x01, /* True color flag: True */
+            0x00, 0xff, /* Red maximum: 255 */
+            0x00, 0xff, /* Green maximum: 255 */
+            0x00, 0xff, /* Blue maximum: 255 */
+            0x10, /* Red shift: 16 */
+            0x08, /* Green shift: 8 */
+            0x00, /* Blue shift: 0 */
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0xa0,
+        ];
+
+        let result = parse_pixel_format(&buf);
+        match result {
+            Ok((remainder, message)) => {
+                assert_eq!(message.bits_per_pixel, 32);
+                assert_eq!(message.depth, 24);
+                assert_eq!(message.big_endian_flag, 0);
+                assert_eq!(message.true_colour_flag, 1);
+                assert_eq!(message.red_max, 255);
+                assert_eq!(message.green_max, 255);
+                assert_eq!(message.blue_max, 255);
+                assert_eq!(message.red_shift, 16);
+                assert_eq!(message.green_shift, 8);
+                assert_eq!(message.blue_shift, 0);
+                assert_eq!(remainder.len(), 5);
+            }
+            Err(Err::Incomplete(_)) => {
+                panic!("Result should not have been incomplete.");
+            }
+            Err(Err::Error(err)) | Err(Err::Failure(err)) => {
+                panic!("Result should not be an error: {:?}.", err);
+            }
+        }
+    }
+
+    #[test]
+    fn test_parse_supported_security_types() {
+        let buf = [
+            0x01, /* Number of security types: 1 */
+            0x02, /* Security type: VNC (2) */
+            0x00, 0x01, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0x10, 0x08, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x01, 0xa0,
+        ];
+
+        let result = parse_supported_security_types(&buf);
+        match result {
+            Ok((remainder, message)) => {
+                assert_eq!(message.number_of_types, 1);
+                assert_eq!(message.types[0], 2);
+                assert_eq!(remainder.len(), 19);
+            }
+            Err(Err::Incomplete(_)) => {
+                panic!("Result should not have been incomplete.");
+            }
+            Err(Err::Error(err)) | Err(Err::Failure(err)) => {
+                panic!("Result should not be an error: {:?}.", err);
+            }
+        }
+    }
+
+    #[test]
+    fn test_parse_vnc_auth() {
+        let buf = [
+            0x54, 0x7b, 0x7a, 0x6f, 0x36, 0xa1, 0x54, 0xdb, 0x03, 0xa2, 0x57, 0x5c, 0x6f, 0x2a,
+            0x4e, 0xc5, /* Authentication challenge: 547b7a6f36a154db03a2575c6f2a4ec5 */
+            0x00, 0x00, 0x00, 0x01, 0xa0,
+        ];
+
+        let result = parse_vnc_auth(&buf);
+        match result {
+            Ok((remainder, message)) => {
+                assert_eq!(
+                    hex::encode(message.secret),
+                    "547b7a6f36a154db03a2575c6f2a4ec5"
+                );
+                assert_eq!(remainder.len(), 5);
+            }
+            Err(Err::Incomplete(_)) => {
+                panic!("Result should not have been incomplete.");
+            }
+            Err(Err::Error(err)) | Err(Err::Failure(err)) => {
+                panic!("Result should not be an error: {:?}.", err);
+            }
+        }
+    }
+
+    #[test]
+    fn test_parse_client_init() {
+        let buf = [
+            0x00, /*Share desktop flag: False*/
+            0x7b, 0x7a, 0x6f, 0x36, 0xa1, 0x54, 0xdb, 0x03, 0xa2, 0x57, 0x5c, 0x6f, 0x2a, 0x4e,
+            0xc5, 0x00, 0x00, 0x00, 0x01, 0xa0,
+        ];
+
+        let result = parse_client_init(&buf);
+        match result {
+            Ok((remainder, message)) => {
+                assert_eq!(message.shared, 0);
+                assert_eq!(remainder.len(), 20);
+            }
+            Err(Err::Incomplete(_)) => {
+                panic!("Result should not have been incomplete.");
+            }
+            Err(Err::Error(err)) | Err(Err::Failure(err)) => {
+                panic!("Result should not be an error: {:?}.", err);
+            }
+        }
+    }
 }