summaryrefslogtreecommitdiff
path: root/test/test-parse_dns_udp.c
blob: 7fec5775955f08beab32a1c8899921603bf641f5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
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;
}