From mboxrd@z Thu Jan 1 00:00:00 1970 Return-Path: X-Spam-Checker-Version: SpamAssassin 3.4.6 (2021-04-09) on server-vie001.gnuweeb.org X-Spam-Level: X-Spam-Status: No, score=-1.2 required=5.0 tests=ALL_TRUSTED,DKIM_SIGNED, DKIM_VALID,DKIM_VALID_AU,DKIM_VALID_EF,URIBL_ZEN_BLOCKED_OPENDNS autolearn=ham autolearn_force=no version=3.4.6 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=gnuweeb.org; s=new2025; t=1754376638; bh=lOACOZIThhHZz6FSQ47w7vdhT4jVGrUZ8jjEesWyL28=; h=From:To:Cc:Subject:Date:Message-ID:In-Reply-To:References: MIME-Version:Content-Transfer-Encoding:Message-ID:Date:From: Reply-To:Subject:To:Cc:In-Reply-To:References:Resent-Date: Resent-From:Resent-To:Resent-Cc:User-Agent:Content-Type: Content-Transfer-Encoding; b=ps4fXbn9wRmeLEO+UArWOKQVXJNqPttNuN/qTXCrZIT+ayqTJyVTC5L7Zmz0HmKqo 1bu9h30OFfKZsySXdpJRLP5yEiRYLrQplJvHHGel+6uulPRqZAdyei4jtd2tS3qGny OW3Wbwj4l+aq3ohGXbRVqSAf0oyOO11jcUXlM7LAYP/k7xGwnwI7Jx7osWM1Kuectm W9YkQFYMsICq/NbwS/R8fuYSAtttRqW5DOz0SgQmnXbOfoKL9uWq2dUMf61SdlC0PL InL/pXqOQrmrUQJ094NlwebRp5xiF2PSPspzUBeWKDqum04LMLG336tE8/Lu4RJG9U ke4YWDN2Bcxlg== Received: from zero (unknown [182.253.151.158]) by server-vie001.gnuweeb.org (Postfix) with ESMTPSA id B7EC3312801F; Tue, 5 Aug 2025 06:50:37 +0000 (UTC) From: Ahmad Gani To: Ammar Faizi Cc: Ahmad Gani , Alviro Iskandar Setiawan , GNU/Weeb Mailing List Subject: [PATCH gwproxy v3 5/9] dnsparser: Update unit test of dns parser Date: Tue, 5 Aug 2025 13:49:25 +0700 Message-ID: <20250805064933.109080-6-reyuki@gnuweeb.org> X-Mailer: git-send-email 2.50.1 In-Reply-To: <20250805064933.109080-1-reyuki@gnuweeb.org> References: <20250805064933.109080-1-reyuki@gnuweeb.org> MIME-Version: 1.0 Content-Transfer-Encoding: 8bit List-Id: Previous unit test is not working correctly, updated it to fit current changes. Signed-off-by: Ahmad Gani --- 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 #include +#include 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