]> git.ipfire.org Git - thirdparty/suricata.git/commitdiff
rust/dns: copy over dns unit tests
authorJason Ish <ish@unx.ca>
Wed, 7 Jun 2017 20:06:44 +0000 (14:06 -0600)
committerVictor Julien <victor@inliniac.net>
Mon, 12 Jun 2017 17:27:36 +0000 (19:27 +0200)
Only the tests that make sense were copied over, those testing
correlation of responses to requests were not.

Also, remove compiler warning when not built with
unit tests.

src/app-layer-dns-tcp-rust.c
src/app-layer-dns-udp-rust.c

index 8c94f7d6bb6fa4be8ee232d4a285f774d115f5af..4ca1490d36a7952b0f68fb8fb68ec1b876446bd2 100644 (file)
@@ -23,6 +23,8 @@
 #include "app-layer-parser.h"
 #include "app-layer-dns-common.h"
 
+#include "util-unittest.h"
+
 #ifdef HAVE_RUST
 
 #include "app-layer-dns-tcp-rust.h"
@@ -192,13 +194,125 @@ void RegisterRustDNSTCPParsers(void)
 }
 
 #ifdef UNITTESTS
-#endif /* UNITTESTS */
 
-void RustDNSTCPParserRegisterTests(void)
+#include "util-unittest-helper.h"
+
+static int RustDNSTCPParserTestMultiRecord(void)
 {
-#if 0
-    UtRegisterTest("DNSTCPParserTestMultiRecord", DNSTCPParserTestMultiRecord);
-#endif
+    /* This is a buffer containing 20 DNS requests each prefixed by
+     * the request length for transport over TCP.  It was generated with Scapy,
+     * where each request is:
+     *    DNS(id=i, rd=1, qd=DNSQR(qname="%d.google.com" % i, qtype="A"))
+     * where i is 0 to 19.
+     */
+    uint8_t req[] = {
+        0x00, 0x1e, 0x00, 0x00, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x30,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1e, 0x00, 0x01, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x31,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1e, 0x00, 0x02, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x32,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1e, 0x00, 0x03, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x33,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1e, 0x00, 0x04, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x34,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1e, 0x00, 0x05, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x35,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1e, 0x00, 0x06, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x36,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1e, 0x00, 0x07, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x37,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1e, 0x00, 0x08, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x38,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1e, 0x00, 0x09, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x01, 0x39,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1f, 0x00, 0x0a, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x31,
+        0x30, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
+        0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00,
+        0x01, 0x00, 0x1f, 0x00, 0x0b, 0x01, 0x00, 0x00,
+        0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
+        0x31, 0x31, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
+        0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01,
+        0x00, 0x01, 0x00, 0x1f, 0x00, 0x0c, 0x01, 0x00,
+        0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x02, 0x31, 0x32, 0x06, 0x67, 0x6f, 0x6f, 0x67,
+        0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00,
+        0x01, 0x00, 0x01, 0x00, 0x1f, 0x00, 0x0d, 0x01,
+        0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x02, 0x31, 0x33, 0x06, 0x67, 0x6f, 0x6f,
+        0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00,
+        0x00, 0x01, 0x00, 0x01, 0x00, 0x1f, 0x00, 0x0e,
+        0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x02, 0x31, 0x34, 0x06, 0x67, 0x6f,
+        0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d,
+        0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x1f, 0x00,
+        0x0f, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x02, 0x31, 0x35, 0x06, 0x67,
+        0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f,
+        0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00, 0x1f,
+        0x00, 0x10, 0x01, 0x00, 0x00, 0x01, 0x00, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x02, 0x31, 0x36, 0x06,
+        0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63,
+        0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01, 0x00,
+        0x1f, 0x00, 0x11, 0x01, 0x00, 0x00, 0x01, 0x00,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x31, 0x37,
+        0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03,
+        0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00, 0x01,
+        0x00, 0x1f, 0x00, 0x12, 0x01, 0x00, 0x00, 0x01,
+        0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02, 0x31,
+        0x38, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
+        0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01, 0x00,
+        0x01, 0x00, 0x1f, 0x00, 0x13, 0x01, 0x00, 0x00,
+        0x01, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x02,
+        0x31, 0x39, 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c,
+        0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x01,
+        0x00, 0x01
+    };
+    size_t reqlen = sizeof(req);
+
+    RSDNSState *state = rs_dns_state_new();
+
+    Flow *f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 53);
+    FAIL_IF_NULL(f);
+    f->proto = IPPROTO_TCP;
+    f->alproto = ALPROTO_DNS;
+    f->alstate = state;
+
+    FAIL_IF(RustDNSTCPParseRequest(f, f->alstate, NULL, req, reqlen,
+                    NULL) < 0);
+    FAIL_IF(rs_dns_state_get_tx_count(state) != 20);
+
+    UTHFreeFlow(f);
+    PASS;
+}
+
+static void RustDNSTCPParserRegisterTests(void)
+{
+    UtRegisterTest("RustDNSTCPParserTestMultiRecord",
+            RustDNSTCPParserTestMultiRecord);
 }
 
+#endif /* UNITTESTS */
+
 #endif /* HAVE_RUST */
index cc189a1e49ed084dc50b5636d12ac623ba621ff8..c27bef5b920b860a02cd0a66493108607d7f128f 100644 (file)
 #include "app-layer-parser.h"
 #include "app-layer-dns-common.h"
 
+#include "util-unittest.h"
+
 #ifdef HAVE_RUST
 
 #include "app-layer-dns-udp-rust.h"
 #include "rust-dns-dns-gen.h"
 
+static void RustDNSUDPParserRegisterTests(void);
+
 static int RustDNSUDPParseRequest(Flow *f, void *state,
         AppLayerParserState *pstate, uint8_t *input, uint32_t input_len,
         void *local_data)
@@ -186,12 +190,189 @@ void RegisterRustDNSUDPParsers(void)
         SCLogInfo("Parsed disabled for %s protocol. Protocol detection"
                 "still on.", proto_name);
     }
-#if 0
 #ifdef UNITTESTS
     AppLayerParserRegisterProtocolUnittests(IPPROTO_UDP, ALPROTO_DNS,
-            DNSUDPParserRegisterTests);
-#endif
+            RustDNSUDPParserRegisterTests);
 #endif
 }
 
+#ifdef UNITTESTS
+
+#include "util-unittest-helper.h"
+
+static int RustDNSUDPParserTest01 (void)
+{
+    /* query: abcdefghijk.com
+     * TTL: 86400
+     * serial 20130422 refresh 28800 retry 7200 exp 604800 min ttl 86400
+     * ns, hostmaster */
+    uint8_t buf[] = { 0x00, 0x3c, 0x85, 0x00, 0x00, 0x01, 0x00, 0x00,
+                      0x00, 0x01, 0x00, 0x00, 0x0b, 0x61, 0x62, 0x63,
+                      0x64, 0x65, 0x66, 0x67, 0x68, 0x69, 0x6a, 0x6b,
+                      0x03, 0x63, 0x6f, 0x6d, 0x00, 0x00, 0x0f, 0x00,
+                      0x01, 0x00, 0x00, 0x06, 0x00, 0x01, 0x00, 0x01,
+                      0x51, 0x80, 0x00, 0x25, 0x02, 0x6e, 0x73, 0x00,
+                      0x0a, 0x68, 0x6f, 0x73, 0x74, 0x6d, 0x61, 0x73,
+                      0x74, 0x65, 0x72, 0xc0, 0x2f, 0x01, 0x33, 0x2a,
+                      0x76, 0x00, 0x00, 0x70, 0x80, 0x00, 0x00, 0x1c,
+                      0x20, 0x00, 0x09, 0x3a, 0x80, 0x00, 0x01, 0x51,
+                      0x80};
+    size_t buflen = sizeof(buf);
+    Flow *f = NULL;
+
+    f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 53);
+    FAIL_IF_NULL(f);
+    f->proto = IPPROTO_UDP;
+    f->alproto = ALPROTO_DNS;
+    f->alstate = rs_dns_state_new();
+    FAIL_IF_NULL(f->alstate);
+
+    FAIL_IF_NOT(RustDNSUDPParseResponse(f, f->alstate, NULL, buf, buflen,
+                    NULL));
+
+    UTHFreeFlow(f);
+    PASS;
+}
+
+static int RustDNSUDPParserTest02 (void)
+{
+    uint8_t buf[] = {
+        0x6D,0x08,0x84,0x80,0x00,0x01,0x00,0x08,0x00,0x00,0x00,0x01,0x03,0x57,0x57,0x57,
+        0x04,0x54,0x54,0x54,0x54,0x03,0x56,0x56,0x56,0x03,0x63,0x6F,0x6D,0x02,0x79,0x79,
+        0x00,0x00,0x01,0x00,0x01,0xC0,0x0C,0x00,0x05,0x00,0x01,0x00,0x00,0x0E,0x10,0x00,
+        0x02,0xC0,0x0C,0xC0,0x31,0x00,0x05,0x00,0x01,0x00,0x00,0x0E,0x10,0x00,0x02,0xC0,
+        0x31,0xC0,0x3F,0x00,0x05,0x00,0x01,0x00,0x00,0x0E,0x10,0x00,0x02,0xC0,0x3F,0xC0,
+        0x4D,0x00,0x05,0x00,0x01,0x00,0x00,0x0E,0x10,0x00,0x02,0xC0,0x4D,0xC0,0x5B,0x00,
+        0x05,0x00,0x01,0x00,0x00,0x0E,0x10,0x00,0x02,0xC0,0x5B,0xC0,0x69,0x00,0x05,0x00,
+        0x01,0x00,0x00,0x0E,0x10,0x00,0x02,0xC0,0x69,0xC0,0x77,0x00,0x05,0x00,0x01,0x00,
+        0x00,0x0E,0x10,0x00,0x02,0xC0,0x77,0xC0,0x85,0x00,0x05,0x00,0x01,0x00,0x00,0x0E,
+        0x10,0x00,0x02,0xC0,0x85,0x00,0x00,0x29,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00,
+    };
+    size_t buflen = sizeof(buf);
+    Flow *f = NULL;
+
+    f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 53);
+    FAIL_IF_NULL(f);
+    f->proto = IPPROTO_UDP;
+    f->alproto = ALPROTO_DNS;
+    f->alstate = rs_dns_state_new();
+    FAIL_IF_NULL(f->alstate);
+
+    FAIL_IF_NOT(RustDNSUDPParseResponse(f, f->alstate, NULL, buf, buflen,
+                    NULL));
+
+    UTHFreeFlow(f);
+    PASS;
+}
+
+static int RustDNSUDPParserTest03 (void)
+{
+    uint8_t buf[] = {
+        0x6F,0xB4,0x84,0x80,0x00,0x01,0x00,0x02,0x00,0x02,0x00,0x03,0x03,0x57,0x57,0x77,
+        0x0B,0x56,0x56,0x56,0x56,0x56,0x56,0x56,0x56,0x56,0x56,0x56,0x03,0x55,0x55,0x55,
+        0x02,0x79,0x79,0x00,0x00,0x01,0x00,0x01,0xC0,0x0C,0x00,0x05,0x00,0x01,0x00,0x00,
+        0x0E,0x10,0x00,0x02,0xC0,0x10,0xC0,0x34,0x00,0x01,0x00,0x01,0x00,0x00,0x0E,0x10,
+        0x00,0x04,0xC3,0xEA,0x04,0x19,0xC0,0x34,0x00,0x02,0x00,0x01,0x00,0x00,0x0E,0x10,
+        0x00,0x0A,0x03,0x6E,0x73,0x31,0x03,0x61,0x67,0x62,0xC0,0x20,0xC0,0x46,0x00,0x02,
+        0x00,0x01,0x00,0x00,0x0E,0x10,0x00,0x06,0x03,0x6E,0x73,0x32,0xC0,0x56,0xC0,0x52,
+        0x00,0x01,0x00,0x01,0x00,0x00,0x0E,0x10,0x00,0x04,0xC3,0xEA,0x04,0x0A,0xC0,0x68,
+        0x00,0x01,0x00,0x01,0x00,0x00,0x0E,0x10,0x00,0x04,0xC3,0xEA,0x05,0x14,0x00,0x00,
+        0x29,0x05,0x00,0x00,0x00,0x00,0x00,0x00,0x00
+    };
+    size_t buflen = sizeof(buf);
+    Flow *f = NULL;
+
+    f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 53);
+    FAIL_IF_NULL(f);
+    f->proto = IPPROTO_UDP;
+    f->alproto = ALPROTO_DNS;
+    f->alstate = rs_dns_state_new();
+    FAIL_IF_NULL(f->alstate);
+
+    FAIL_IF_NOT(RustDNSUDPParseResponse(f, f->alstate, NULL, buf, buflen,
+                    NULL));
+
+    UTHFreeFlow(f);
+    PASS;
+}
+
+/** \test TXT records in answer */
+static int RustDNSUDPParserTest04 (void)
+{
+    uint8_t buf[] = {
+        0xc2,0x2f,0x81,0x80,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x0a,0x41,0x41,0x41,
+        0x41,0x41,0x4f,0x31,0x6b,0x51,0x41,0x05,0x3d,0x61,0x75,0x74,0x68,0x03,0x73,0x72,
+        0x76,0x06,0x74,0x75,0x6e,0x6e,0x65,0x6c,0x03,0x63,0x6f,0x6d,0x00,0x00,0x10,0x00,
+        0x01,
+        /* answer record start */
+        0xc0,0x0c,0x00,0x10,0x00,0x01,0x00,0x00,0x00,0x03,0x00,0x22,
+        /* txt record starts: */
+        0x20, /* <txt len 32 */  0x41,0x68,0x76,0x4d,0x41,0x41,0x4f,0x31,0x6b,0x41,0x46,
+        0x45,0x35,0x54,0x45,0x39,0x51,0x54,0x6a,0x46,0x46,0x4e,0x30,0x39,0x52,0x4e,0x31,
+        0x6c,0x59,0x53,0x44,0x6b,0x00, /* <txt len 0 */   0xc0,0x1d,0x00,0x02,0x00,0x01,
+        0x00,0x09,0x3a,0x80,0x00,0x09,0x06,0x69,0x6f,0x64,0x69,0x6e,0x65,0xc0,0x21,0xc0,
+        0x6b,0x00,0x01,0x00,0x01,0x00,0x09,0x3a,0x80,0x00,0x04,0x0a,0x1e,0x1c,0x5f
+    };
+    size_t buflen = sizeof(buf);
+    Flow *f = NULL;
+
+    f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 53);
+    FAIL_IF_NULL(f);
+    f->proto = IPPROTO_UDP;
+    f->alproto = ALPROTO_DNS;
+    f->alstate = rs_dns_state_new();
+    FAIL_IF_NULL(f->alstate);
+
+    FAIL_IF_NOT(RustDNSUDPParseResponse(f, f->alstate, NULL, buf, buflen,
+                    NULL));
+
+    UTHFreeFlow(f);
+    PASS;
+}
+
+/** \test TXT records in answer, bad txtlen */
+static int RustDNSUDPParserTest05 (void)
+{
+    uint8_t buf[] = {
+        0xc2,0x2f,0x81,0x80,0x00,0x01,0x00,0x01,0x00,0x01,0x00,0x01,0x0a,0x41,0x41,0x41,
+        0x41,0x41,0x4f,0x31,0x6b,0x51,0x41,0x05,0x3d,0x61,0x75,0x74,0x68,0x03,0x73,0x72,
+        0x76,0x06,0x74,0x75,0x6e,0x6e,0x65,0x6c,0x03,0x63,0x6f,0x6d,0x00,0x00,0x10,0x00,
+        0x01,
+        /* answer record start */
+        0xc0,0x0c,0x00,0x10,0x00,0x01,0x00,0x00,0x00,0x03,0x00,0x22,
+        /* txt record starts: */
+        0x40, /* <txt len 64 */  0x41,0x68,0x76,0x4d,0x41,0x41,0x4f,0x31,0x6b,0x41,0x46,
+        0x45,0x35,0x54,0x45,0x39,0x51,0x54,0x6a,0x46,0x46,0x4e,0x30,0x39,0x52,0x4e,0x31,
+        0x6c,0x59,0x53,0x44,0x6b,0x00, /* <txt len 0 */   0xc0,0x1d,0x00,0x02,0x00,0x01,
+        0x00,0x09,0x3a,0x80,0x00,0x09,0x06,0x69,0x6f,0x64,0x69,0x6e,0x65,0xc0,0x21,0xc0,
+        0x6b,0x00,0x01,0x00,0x01,0x00,0x09,0x3a,0x80,0x00,0x04,0x0a,0x1e,0x1c,0x5f
+    };
+    size_t buflen = sizeof(buf);
+    Flow *f = NULL;
+
+    f = UTHBuildFlow(AF_INET, "1.2.3.4", "1.2.3.5", 1024, 53);
+    FAIL_IF_NULL(f);
+    f->proto = IPPROTO_UDP;
+    f->alproto = ALPROTO_DNS;
+    f->alstate = rs_dns_state_new();
+    FAIL_IF_NULL(f->alstate);
+
+    FAIL_IF(RustDNSUDPParseResponse(f, f->alstate, NULL, buf, buflen,
+                    NULL) != -1);
+
+    UTHFreeFlow(f);
+    PASS;
+}
+
+static void RustDNSUDPParserRegisterTests(void)
+{
+    UtRegisterTest("RustDNSUDPParserTest01", RustDNSUDPParserTest01);
+    UtRegisterTest("RustDNSUDPParserTest02", RustDNSUDPParserTest02);
+    UtRegisterTest("RustDNSUDPParserTest03", RustDNSUDPParserTest03);
+    UtRegisterTest("RustDNSUDPParserTest04", RustDNSUDPParserTest04);
+    UtRegisterTest("RustDNSUDPParserTest05", RustDNSUDPParserTest05);
+}
+
+#endif
+
 #endif /* HAVE_RUST */