From: Ahmad Gani <reyuki@gnuweeb.org>
To: Ammar Faizi <ammarfaizi2@gnuweeb.org>
Cc: Ahmad Gani <reyuki@gnuweeb.org>,
Alviro Iskandar Setiawan <alviro.iskandar@gnuweeb.org>,
GNU/Weeb Mailing List <gwml@vger.gnuweeb.org>
Subject: [PATCH gwproxy v3 5/9] dnsparser: Update unit test of dns parser
Date: Tue, 5 Aug 2025 13:49:25 +0700 [thread overview]
Message-ID: <20250805064933.109080-6-reyuki@gnuweeb.org> (raw)
In-Reply-To: <20250805064933.109080-1-reyuki@gnuweeb.org>
Previous unit test is not working correctly, updated it to fit current
changes.
Signed-off-by: Ahmad Gani <reyuki@gnuweeb.org>
---
src/gwproxy/dnsparser.c | 184 ++++++++++++++++++++++------------------
1 file changed, 103 insertions(+), 81 deletions(-)
diff --git a/src/gwproxy/dnsparser.c b/src/gwproxy/dnsparser.c
index 9e84f539ccde..f65450fa922c 100644
--- a/src/gwproxy/dnsparser.c
+++ b/src/gwproxy/dnsparser.c
@@ -1,6 +1,7 @@
#define _DEFAULT_SOURCE
#include <endian.h>
#include <gwproxy/dnsparser.h>
+#include <gwproxy/net.h>
static ssize_t construct_qname(uint8_t *dst, size_t dst_len, const char *qname)
{
@@ -246,105 +247,126 @@ ssize_t construct_question(gwdns_question_part *question)
#ifdef RUNTEST
-void test_simulate_ipv4query(void)
+void test_parse_ipv4(void)
{
- char buff[UDP_MSG_LIMIT];
- gwdns_query_pkt *send_pkt;
+ gwdns_answ_data d;
+ uint16_t txid;
+
uint8_t recv_pkt[] = {
- /* Header (12 bytes) */
- 0x00, 0x00, /* transaction ID - STUB! */
- 0x81, 0x80, /* Flags: QR=1, AA=0, RD=1, RA=1, RCODE=0 */
- 0x00, 0x01, /* QDCOUNT = 1 */
- 0x00, 0x06, /* ANCOUNT = 6 */
- 0x00, 0x00, /* NSCOUNT = 0 */
- 0x00, 0x00, /* ARCOUNT = 0 */
-
- /* Question Section */
- /* Pointer label compression may be used in answers */
- 0x06, 'g','o','o','g','l','e',
- 0x03, 'c','o','m',
- 0x00, /* Terminate name */
- 0x00, 0x01, /* QTYPE = A */
- 0x00, 0x01, /* QCLASS = IN */
-
- /* Answer Section (6 records) */
- /* Each Answer record: name pointer, type, class, ttl, rdlength, rdata */
- /* First Answer */
- 0xC0, 0x0C, /* Name: pointer to offset 0x0C (start of question name) */
- 0x00, 0x01, /* TYPE = A */
- 0x00, 0x01, /* CLASS = IN */
- 0x00, 0x00, 0x08, 0x62, /* TTL = 0x00000862 = 2146 sec */
- 0x00, 0x04, /* RDLENGTH = 4 */
- 0x4A, 0x7D, 0x18, 0x71, /* RDATA = 74.125.24.113 */
-
- /* Second Answer */
- 0xC0, 0x0C,
- 0x00, 0x01,
- 0x00, 0x01,
- 0x00, 0x00, 0x08, 0x62,
- 0x00, 0x04,
- 0x4A, 0x7D, 0x18, 0x65, /* 74.125.24.101 */
+ 0x23, 0xc6, 0x81, 0x80, 0x00, 0x01, 0x00, 0x06, 0x00, 0x00, 0x00, 0x00,
+ 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65, 0x03, 0x63, 0x6f, 0x6d, 0x00,
+ 0x00, 0x01, 0x00, 0x01, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
+ 0x00, 0x35, 0x00, 0x04, 0x4a, 0x7d, 0x18, 0x8a, 0xc0, 0x0c, 0x00, 0x01,
+ 0x00, 0x01, 0x00, 0x00, 0x00, 0x35, 0x00, 0x04, 0x4a, 0x7d, 0x18, 0x66,
+ 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x35, 0x00, 0x04,
+ 0x4a, 0x7d, 0x18, 0x64, 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00,
+ 0x00, 0x35, 0x00, 0x04, 0x4a, 0x7d, 0x18, 0x8b, 0xc0, 0x0c, 0x00, 0x01,
+ 0x00, 0x01, 0x00, 0x00, 0x00, 0x35, 0x00, 0x04, 0x4a, 0x7d, 0x18, 0x65,
+ 0xc0, 0x0c, 0x00, 0x01, 0x00, 0x01, 0x00, 0x00, 0x00, 0x35, 0x00, 0x04,
+ 0x4a, 0x7d, 0x18, 0x71
+ };
- /* Third Answer */
- 0xC0, 0x0C,
- 0x00, 0x01,
+ memcpy(&txid, recv_pkt, 2);
+ assert(!serialize_answ(txid, recv_pkt, sizeof(recv_pkt), &d));
+ for (size_t i = 0; i < d.hdr.ancount; i++) {
+ struct gwp_sockaddr gs;
+ gwdns_serialized_answ *answ;
+ char buff[FULL_ADDRSTRLEN];
+
+ memset(&gs, 0, sizeof(gs));
+ answ = d.rr_answ[i];
+ assert(answ->rdlength == 4);
+ gs.sa.sa_family = AF_INET;
+ memcpy(&gs.i4.sin_addr, answ->rdata, 4);
+ convert_ssaddr_to_str(buff, &gs);
+ printf("IPv4: %s\n", buff);
+ }
+ free_serialize_answ(&d);
+}
+
+void test_parse_ipv6(void)
+{
+ gwdns_answ_data d;
+ uint16_t txid;
+ int ret;
+
+ uint8_t recv_pkt[] = {
+ 0x45, 0x67,
+ 0x81, 0x80,
0x00, 0x01,
- 0x00, 0x00, 0x08, 0x62,
0x00, 0x04,
- 0x4A, 0x7D, 0x18, 0x8B, /* 74.125.24.139 */
+ 0x00, 0x00,
+ 0x00, 0x00,
- /* Fourth Answer */
- 0xC0, 0x0C,
- 0x00, 0x01,
+ 0x06, 0x67, 0x6f, 0x6f, 0x67, 0x6c, 0x65,
+ 0x03, 0x63, 0x6f, 0x6d,
+ 0x00,
+ 0x00, 0x1c,
0x00, 0x01,
- 0x00, 0x00, 0x08, 0x62,
- 0x00, 0x04,
- 0x4A, 0x7D, 0x18, 0x8A, /* 74.125.24.138 */
- /* Fifth Answer */
- 0xC0, 0x0C,
+ 0xc0, 0x0c,
+ 0x00, 0x1c,
0x00, 0x01,
+ 0x00, 0x00, 0x09, 0x06,
+ 0x00, 0x10,
+ 0x24, 0x04, 0x68, 0x00, 0x40, 0x03, 0x0c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71,
+
+ 0xc0, 0x0c,
+ 0x00, 0x1c, 0x00, 0x01, 0x00, 0x00, 0x09, 0x06,
+ 0x00, 0x10,
+ 0x24, 0x04, 0x68, 0x00, 0x40, 0x03, 0x0c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x8a,
+
+ 0xc0, 0x0c,
+ 0x00, 0x1c,
0x00, 0x01,
- 0x00, 0x00, 0x08, 0x62,
- 0x00, 0x04,
- 0x4A, 0x7D, 0x18, 0x64, /* 74.125.24.100 */
+ 0x00, 0x00, 0x09, 0x06,
+ 0x00, 0x10,
+ 0x24, 0x04, 0x68, 0x00, 0x40, 0x03, 0x0c, 0x02, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x65,
- /* Sixth Answer */
- 0xC0, 0x0C,
- 0x00, 0x01,
+ 0xc0, 0x0c,
+ 0x00, 0x1c,
0x00, 0x01,
- 0x00, 0x00, 0x08, 0x62,
- 0x00, 0x04,
- 0x4A, 0x7D, 0x18, 0x66, /* 74.125.24.102 */
+ 0x00, 0x00, 0x0c, 0x16,
+ 0x00, 0x10,
+ 0x24, 0x04, 0x68, 0x00, 0x40, 0x03, 0x0c, 0x11, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x71
};
- gwdns_answ_data d;
- char first_label[] = "google";
- char second_label[] = "com";
-
- memset(&d, 0, sizeof(d));
- gwdns_question_part q = {
- .domain = "google.com",
- .dst_buffer = (uint8_t *)buff,
- .dst_len = sizeof(buff)
- };
- assert(construct_question(&q) > 0);
-
- assert(buff[12] == 6);
- assert(!memcmp(&buff[13], first_label, 6));
-
- assert(buff[13 + 6] == 3);
- assert(!memcmp(&buff[13 + 6 + 1], second_label, 3));
- // fill the STUB
- memcpy(recv_pkt, buff, 2);
-
- send_pkt = (void *)buff;
- assert(!serialize_answ(send_pkt->hdr.id, recv_pkt, sizeof(recv_pkt), &d));
+ memcpy(&txid, recv_pkt, sizeof(txid));
+
+ ret = serialize_answ(txid, recv_pkt, sizeof(recv_pkt), &d);
+ assert(!ret);
+ for (size_t i = 0; i < d.hdr.ancount; i++) {
+ struct gwp_sockaddr gs;
+ gwdns_serialized_answ *answ;
+ char buff[FULL_ADDRSTRLEN];
+
+ memset(&gs, 0, sizeof(gs));
+ answ = d.rr_answ[i];
+ assert(answ->rdlength == 16);
+ gs.sa.sa_family = AF_INET6;
+ memcpy(&gs.i6.sin6_addr, answ->rdata, 16);
+ convert_ssaddr_to_str(buff, &gs);
+ printf("IPv6: %s\n", buff);
+ }
+ free_serialize_answ(&d);
}
+/*
+ * test mock data of recv in both IPv4 and IPv6
+ *
+ * the mock data are produced by this script:
+ * https://gist.github.com/realyukii/d7b450b4ddc305c66a2d8cd5600f23c4
+ */
void run_all_tests(void)
{
- test_simulate_ipv4query();
+ /*
+ * We can't use serialize_answ to parse multiple response at once.
+ * The caller MUST call serialize_answ more than one time if there's
+ * more than one response, because txid is passed to only verify one
+ * response.
+ */
+ test_parse_ipv4();
+ test_parse_ipv6();
fprintf(stderr, "all tests passed!\n");
}
--
Ahmad Gani
next prev parent reply other threads:[~2025-08-05 6:50 UTC|newest]
Thread overview: 27+ messages / expand[flat|nested] mbox.gz Atom feed top
2025-08-05 6:49 [PATCH gwproxy v3 0/9] Initial work for DNS lookup implementation Ahmad Gani
2025-08-05 6:49 ` [PATCH gwproxy v3 1/9] dnslookup: Split common functionality and struct into net.h and net.c Ahmad Gani
2025-08-05 9:26 ` Ammar Faizi
2025-08-05 6:49 ` [PATCH gwproxy v3 2/9] dnslookup: Add a new parameter, default_port Ahmad Gani
2025-08-05 9:26 ` Ammar Faizi
2025-08-05 10:15 ` reyuki
2025-08-05 10:26 ` Ammar Faizi
2025-08-05 10:43 ` Ahmad Gani
2025-08-05 10:46 ` Ammar Faizi
2025-08-05 12:45 ` Ahmad Gani
2025-08-05 6:49 ` [PATCH gwproxy v3 3/9] dnslookup: Allow only port string number Ahmad Gani
2025-08-05 6:49 ` [PATCH gwproxy v3 4/9] dnslookup: Initial work for implementation of C-ares-like getaddrinfo function Ahmad Gani
2025-08-05 6:49 ` Ahmad Gani [this message]
2025-08-05 9:27 ` [PATCH gwproxy v3 5/9] dnsparser: Update unit test of dns parser Ammar Faizi
2025-08-05 6:49 ` [PATCH gwproxy v3 6/9] dnsparser: Fix serialize_answ function Ahmad Gani
2025-08-05 9:26 ` Ammar Faizi
2025-08-05 12:47 ` Ahmad Gani
2025-08-05 13:04 ` Ammar Faizi
2025-08-05 13:12 ` Ahmad Gani
2025-08-05 13:51 ` Ahmad Gani
2025-08-05 14:02 ` Ammar Faizi
2025-08-05 6:49 ` [PATCH gwproxy v3 7/9] dnsparser: Transaction id creation is delegated to caller Ahmad Gani
2025-08-05 6:49 ` [PATCH gwproxy v3 8/9] dnslookup: Make gw_ares_getaddrinfo asynchronous Ahmad Gani
2025-08-05 6:49 ` [PATCH gwproxy v3 9/9] dnslookup: code style changes Ahmad Gani
2025-08-05 9:26 ` Ammar Faizi
2025-08-05 13:22 ` [PATCH gwproxy v3 0/9] Initial work for DNS lookup implementation Ammar Faizi
2025-08-05 13:28 ` Ahmad Gani
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20250805064933.109080-6-reyuki@gnuweeb.org \
--to=reyuki@gnuweeb.org \
--cc=alviro.iskandar@gnuweeb.org \
--cc=ammarfaizi2@gnuweeb.org \
--cc=gwml@vger.gnuweeb.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox