summaryrefslogtreecommitdiff
path: root/test/test-parse_dns_udp.c
diff options
context:
space:
mode:
authordaniel <daniel@planethacker.net>2025-05-06 16:57:32 -0700
committerdaniel <daniel@planethacker.net>2025-05-06 16:57:32 -0700
commit2278df1493e064c197913e49b5d1935942d83448 (patch)
tree42f06ab2f76e2ddf228bafbb03f79621975a4534 /test/test-parse_dns_udp.c
initial import
Diffstat (limited to 'test/test-parse_dns_udp.c')
-rw-r--r--test/test-parse_dns_udp.c42
1 files changed, 42 insertions, 0 deletions
diff --git a/test/test-parse_dns_udp.c b/test/test-parse_dns_udp.c
new file mode 100644
index 0000000..7fec577
--- /dev/null
+++ b/test/test-parse_dns_udp.c
@@ -0,0 +1,42 @@
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <string.h>
+#include "../include/dns.h"
+
+#define ASSERT(x) do { \
+ if (!(x)) { \
+ fprintf(stderr, "FAIL: %s:%d: %s\n", __FILE__, __LINE__, #x); \
+ return EXIT_FAILURE; \
+ } \
+} while (0)
+
+int main(void) {
+ // Simple DNS query for "example.com" A record
+ // Manually constructed:
+ // - Header: 12 bytes
+ // - Question: example.com (as labels) + QTYPE A + QCLASS IN
+ uint8_t pkt[] = {
+ 0x12, 0x34, // Transaction ID
+ 0x01, 0x00, // Flags: standard query
+ 0x00, 0x01, // QDCOUNT: 1
+ 0x00, 0x00, // ANCOUNT
+ 0x00, 0x00, // NSCOUNT
+ 0x00, 0x00, // ARCOUNT
+ 0x07, 'e','x','a','m','p','l','e',
+ 0x03, 'c','o','m',
+ 0x00, // null terminator
+ 0x00, 0x01, // QTYPE A
+ 0x00, 0x01 // QCLASS IN
+ };
+
+ struct dns_question qs[MAX_DNS_QUESTIONS];
+ size_t count = parse_dns_udp(pkt, sizeof(pkt), qs, MAX_DNS_QUESTIONS);
+
+ ASSERT(count == 1);
+ ASSERT(strcmp(qs[0].name, "example.com") == 0);
+ ASSERT(qs[0].qtype == 1);
+
+ printf("PASS: parse_dns_udp\n");
+ return EXIT_SUCCESS;
+}