Bläddra i källkod

Prepare tests for mutation testing

• Describe all slices of the input buffers
• Use buf.len() instead of hard-coding lengths
• Remove repeated test in AAAA record
Benjamin Sago 4 år sedan
förälder
incheckning
14c27312d1

+ 13 - 6
dns/src/record/a.rs

@@ -43,25 +43,32 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 127, 0, 0, 1 ];
+        let buf = &[
+            0x7F, 0x00, 0x00, 0x01,  // IPv4 address
+        ];
 
-        assert_eq!(A::read(4, &mut Cursor::new(buf)).unwrap(),
+        assert_eq!(A::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    A { address: Ipv4Addr::new(127, 0, 0, 1) });
     }
 
     #[test]
     fn too_short() {
-        let buf = &[ 127, 0, 1 ];
+        let buf = &[
+            0x7F, 0x00, 0x00,  // Too short IPv4 address
+        ];
 
-        assert_eq!(A::read(3, &mut Cursor::new(buf)),
+        assert_eq!(A::read(buf.len() as _, &mut Cursor::new(buf)),
                    Err(WireError::WrongLength { expected: 4, got: 3 }));
     }
 
     #[test]
     fn too_long() {
-        let buf = &[ 127, 0, 0, 0, 1 ];
+        let buf = &[
+            0x7F, 0x00, 0x00, 0x00,  // IPv4 address
+            0x01,  // Unexpected extra byte
+        ];
 
-        assert_eq!(A::read(5, &mut Cursor::new(buf)),
+        assert_eq!(A::read(buf.len() as _, &mut Cursor::new(buf)),
                    Err(WireError::WrongLength { expected: 4, got: 5 }));
     }
 

+ 17 - 16
dns/src/record/aaaa.rs

@@ -44,33 +44,34 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0 ];
+        let buf = &[
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+            0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,  // IPv6 address
+        ];
 
-        assert_eq!(AAAA::read(16, &mut Cursor::new(buf)).unwrap(),
+        assert_eq!(AAAA::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    AAAA { address: Ipv6Addr::new(0,0,0,0,0,0,0,0) });
     }
 
     #[test]
     fn too_long() {
-        let buf = &[9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9];
-
-        assert_eq!(AAAA::read(19, &mut Cursor::new(buf)),
-                   Err(WireError::WrongLength { expected: 16, got: 19 }));
-    }
-
-    #[test]
-    fn too_empty() {
-        let buf = &[];
-
-        assert_eq!(AAAA::read(0, &mut Cursor::new(buf)),
-                   Err(WireError::WrongLength { expected: 16, got: 0 }));
+        let buf = &[
+            0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,
+            0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09, 0x09,  // IPv6 address
+            0x09,  // Unexpected extra byte
+        ];
+
+        assert_eq!(AAAA::read(buf.len() as _, &mut Cursor::new(buf)),
+                   Err(WireError::WrongLength { expected: 16, got: 17 }));
     }
 
     #[test]
     fn too_short() {
-        let buf = &[ 5,5,5,5,5 ];
+        let buf = &[
+            0x05, 0x05, 0x05, 0x05, 0x05,  // Five arbitrary bytes
+        ];
 
-        assert_eq!(AAAA::read(5, &mut Cursor::new(buf)),
+        assert_eq!(AAAA::read(buf.len() as _, &mut Cursor::new(buf)),
                    Err(WireError::WrongLength { expected: 16, got: 5 }));
     }
 

+ 7 - 4
dns/src/record/caa.rs

@@ -53,11 +53,14 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 0x00, 0x09, 0x69, 0x73, 0x73, 0x75, 0x65, 0x77, 0x69,
-                     0x6c, 0x64, 0x65, 0x6e, 0x74, 0x72, 0x75, 0x73, 0x74,
-                     0x2e, 0x6e, 0x65, 0x74 ];
+        let buf = &[
+            0x00,  // flags
+            0x09,  // tag length
+            0x69, 0x73, 0x73, 0x75, 0x65, 0x77, 0x69, 0x6c, 0x64,  // tag
+            0x65, 0x6e, 0x74, 0x72, 0x75, 0x73, 0x74, 0x2e, 0x6e, 0x65, 0x74,  // value
+        ];
 
-        assert_eq!(CAA::read(22, &mut Cursor::new(buf)).unwrap(),
+        assert_eq!(CAA::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    CAA {
                        critical: false,
                        tag: String::from("issuewild"),

+ 5 - 2
dns/src/record/cname.rs

@@ -32,9 +32,12 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 0x05, 0x62, 0x73, 0x61, 0x67, 0x6f, 0x02, 0x6d, 0x65, 0x00, ];
+        let buf = &[
+            0x05, 0x62, 0x73, 0x61, 0x67, 0x6f, 0x02, 0x6d, 0x65,  // domain
+            0x00,  // domain terminator
+        ];
 
-        assert_eq!(CNAME::read(10, &mut Cursor::new(buf)).unwrap(),
+        assert_eq!(CNAME::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    CNAME {
                        domain: String::from("bsago.me."),
                    });

+ 6 - 3
dns/src/record/mx.rs

@@ -48,10 +48,13 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 0x00, 0x0A, 0x05, 0x62, 0x73, 0x61, 0x67, 0x6f, 0x02,
-                     0x6d, 0x65, 0x00 ];
+        let buf = &[
+            0x00, 0x0A,  // preference
+            0x05, 0x62, 0x73, 0x61, 0x67, 0x6f, 0x02, 0x6d, 0x65,  // exchange
+            0x00,  // exchange terminator
+        ];
 
-        assert_eq!(MX::read(12, &mut Cursor::new(buf)).unwrap(),
+        assert_eq!(MX::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    MX {
                        preference: 10,
                        exchange: String::from("bsago.me."),

+ 6 - 4
dns/src/record/ns.rs

@@ -43,11 +43,13 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 0x01, 0x61, 0x0c, 0x67,
-                     0x74, 0x6c, 0x64, 0x2d, 0x73, 0x65, 0x72, 0x76,
-                     0x65, 0x72, 0x73, 0x03, 0x6e, 0x65, 0x74, 0x00, ];
+        let buf = &[
+            0x01, 0x61, 0x0c, 0x67, 0x74, 0x6c, 0x64, 0x2d, 0x73, 0x65, 0x72,
+            0x76, 0x65, 0x72, 0x73, 0x03, 0x6e, 0x65, 0x74,  // nameserver
+            0x00,  // nameserver terminator
+        ];
 
-        assert_eq!(NS::read(20, &mut Cursor::new(buf)).unwrap(),
+        assert_eq!(NS::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    NS {
                        nameserver: String::from("a.gtld-servers.net."),
                    });

+ 7 - 1
dns/src/record/opt.rs

@@ -101,7 +101,13 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 0x05, 0xAC, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 ];
+        let buf = &[
+            0x05, 0xAC,  // UDP payload size
+            0x00,        // higher bits
+            0x00, 0x00,  // EDNS(0) version
+            0x00, 0x00,  // flags
+            0x00,        // data
+        ];
 
         assert_eq!(OPT::read(&mut Cursor::new(buf)).unwrap(),
                    OPT {

+ 5 - 3
dns/src/record/ptr.rs

@@ -38,10 +38,12 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 0x03, 0x64, 0x6e, 0x73, 0x06, 0x67,
-                     0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x00 ];
+        let buf = &[
+            0x03, 0x64, 0x6e, 0x73, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,  // cname
+            0x00,  // cname terminator
+        ];
 
-        assert_eq!(PTR::read(12, &mut Cursor::new(buf)).unwrap(),
+        assert_eq!(PTR::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    PTR {
                        cname: String::from("dns.google."),
                    });

+ 10 - 8
dns/src/record/soa.rs

@@ -79,16 +79,18 @@ mod test {
     #[test]
     fn parses() {
         let buf = &[
-            0x05, 0x62, 0x73, 0x61, 0x67, 0x6f, 0x02, 0x6d, 0x65, 0x00,
-            0x05, 0x62, 0x73, 0x61, 0x67, 0x6f, 0x02, 0x6d, 0x65, 0x00,
-            0x5d, 0x3c, 0xef, 0x02,
-            0x00, 0x01, 0x51, 0x80,
-            0x00, 0x00, 0x1c, 0x20,
-            0x00, 0x09, 0x3a, 0x80,
-            0x00, 0x00, 0x01, 0x2c,
+            0x05, 0x62, 0x73, 0x61, 0x67, 0x6f, 0x02, 0x6d, 0x65,  // mname
+            0x00,  // mname terminator
+            0x05, 0x62, 0x73, 0x61, 0x67, 0x6f, 0x02, 0x6d, 0x65,  // rname
+            0x00,  // rname terminator
+            0x5d, 0x3c, 0xef, 0x02,  // Serial
+            0x00, 0x01, 0x51, 0x80,  // Refresh interval
+            0x00, 0x00, 0x1c, 0x20,  // Retry interval
+            0x00, 0x09, 0x3a, 0x80,  // Expire limit
+            0x00, 0x00, 0x01, 0x2c,  // Minimum TTL
         ];
 
-        assert_eq!(SOA::read(40, &mut Cursor::new(buf)).unwrap(),
+        assert_eq!(SOA::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    SOA {
                        mname: String::from("bsago.me."),
                        rname: String::from("bsago.me."),

+ 11 - 6
dns/src/record/srv.rs

@@ -58,12 +58,17 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 0x00, 0x01, 0x00, 0x01, 0x92, 0x7c, 0x03, 0x61, 0x74,
-                     0x61, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x04, 0x6e,
-                     0x6f, 0x64, 0x65, 0x03, 0x64, 0x63, 0x31, 0x06, 0x63,
-                     0x6f, 0x6e, 0x73, 0x75, 0x6c, 0x00, ];
-
-        assert_eq!(SRV::read(33, &mut Cursor::new(buf)).unwrap(),
+        let buf = &[
+            0x00, 0x01,  // priority
+            0x00, 0x01,  // weight
+            0x92, 0x7c,  // port
+            0x03, 0x61, 0x74, 0x61, 0x05, 0x6c, 0x6f, 0x63, 0x61, 0x6c, 0x04,
+            0x6e, 0x6f, 0x64, 0x65, 0x03, 0x64, 0x63, 0x31, 0x06, 0x63, 0x6f,
+            0x6e, 0x73, 0x75, 0x6c,  // target
+            0x00,  // target terminator
+        ];
+
+        assert_eq!(SRV::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    SRV {
                        priority: 1,
                        weight: 1,

+ 4 - 2
dns/src/record/txt.rs

@@ -64,9 +64,11 @@ mod test {
 
     #[test]
     fn parses() {
-        let buf = &[ 0x06, 0x74, 0x78, 0x74, 0x20, 0x6d, 0x65 ];
+        let buf = &[
+            0x06, 0x74, 0x78, 0x74, 0x20, 0x6d, 0x65,  // message
+        ];
 
-        assert_eq!(TXT::read(9, &mut Cursor::new(buf)).unwrap(),
+        assert_eq!(TXT::read(buf.len() as _, &mut Cursor::new(buf)).unwrap(),
                    TXT {
                        message: String::from("txt me"),
                    });