From 818904643e1c4da1517b667847000b0c8370443f Mon Sep 17 00:00:00 2001 From: Brad House Date: Tue, 14 Nov 2023 13:09:49 -0500 Subject: [PATCH] additional test coverage --- src/lib/ares__buf.c | 2 +- src/lib/ares__buf.h | 8 +++ test/ares-test-internal.cc | 106 +++++++++++++++++++++++++++++++++++++ 3 files changed, 115 insertions(+), 1 deletion(-) diff --git a/src/lib/ares__buf.c b/src/lib/ares__buf.c index b89f62a2..9eda8935 100644 --- a/src/lib/ares__buf.c +++ b/src/lib/ares__buf.c @@ -901,7 +901,7 @@ ares_status_t ares__buf_append_num_hex(ares__buf_t *buf, size_t num, size_t len) return ARES_SUCCESS; } -static ares_status_t ares__buf_append_str(ares__buf_t *buf, const char *str) +ares_status_t ares__buf_append_str(ares__buf_t *buf, const char *str) { return ares__buf_append(buf, (const unsigned char *)str, ares_strlen(str)); } diff --git a/src/lib/ares__buf.h b/src/lib/ares__buf.h index 5bf4cf8c..b73d8ee2 100644 --- a/src/lib/ares__buf.h +++ b/src/lib/ares__buf.h @@ -90,6 +90,14 @@ ares_status_t ares__buf_append(ares__buf_t *buf, const unsigned char *data, */ ares_status_t ares__buf_append_byte(ares__buf_t *buf, unsigned char byte); +/*! Append a null-terminated string to the dynamic buffer object + * + * \param[in] buf Initialized buffer object + * \param[in] str String to append to buffer object. + * \return ARES_SUCCESS or one of the c-ares error codes + */ +ares_status_t ares__buf_append_str(ares__buf_t *buf, const char *str); + /*! Append a 16bit Big Endian number to the buffer. * * \param[in] buf Initialized buffer object diff --git a/test/ares-test-internal.cc b/test/ares-test-internal.cc index 8ce4e0ae..e5780091 100644 --- a/test/ares-test-internal.cc +++ b/test/ares-test-internal.cc @@ -721,6 +721,112 @@ TEST_F(LibraryTest, DNSRecord) { EXPECT_EQ(ancount, ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER)); EXPECT_EQ(nscount, ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_AUTHORITY)); EXPECT_EQ(arcount, ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ADDITIONAL)); + + /* Iterate and print */ + ares__buf_t *printmsg = ares__buf_create(); + ares__buf_append_str(printmsg, ";; ->>HEADER<<- opcode: "); + ares__buf_append_str(printmsg, ares_dns_opcode_tostr(ares_dns_record_get_opcode(dnsrec))); + ares__buf_append_str(printmsg, ", status: "); + ares__buf_append_str(printmsg, ares_dns_rcode_tostr(ares_dns_record_get_rcode(dnsrec))); + ares__buf_append_str(printmsg, ", id: "); + ares__buf_append_num_dec(printmsg, (size_t)ares_dns_record_get_id(dnsrec), 0); + ares__buf_append_str(printmsg, "\n;; flags: "); + ares__buf_append_num_hex(printmsg, (size_t)ares_dns_record_get_flags(dnsrec), 0); + ares__buf_append_str(printmsg, "; QUERY: "); + ares__buf_append_num_dec(printmsg, ares_dns_record_query_cnt(dnsrec), 0); + ares__buf_append_str(printmsg, ", ANSWER: "); + ares__buf_append_num_dec(printmsg, ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER), 0); + ares__buf_append_str(printmsg, ", AUTHORITY: "); + ares__buf_append_num_dec(printmsg, ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_AUTHORITY), 0); + ares__buf_append_str(printmsg, ", ADDITIONAL: "); + ares__buf_append_num_dec(printmsg, ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ADDITIONAL), 0); + ares__buf_append_str(printmsg, "\n\n"); + ares__buf_append_str(printmsg, ";; QUESTION SECTION:\n"); + for (size_t i = 0; i < ares_dns_record_query_cnt(dnsrec); i++) { + const char *name; + ares_dns_rec_type_t qtype; + ares_dns_class_t qclass; + ares_dns_record_query_get(dnsrec, i, &name, &qtype, &qclass); + ares__buf_append_str(printmsg, ";"); + ares__buf_append_str(printmsg, name); + ares__buf_append_str(printmsg, ".\t\t\t"); + ares__buf_append_str(printmsg, ares_dns_class_tostr(qclass)); + ares__buf_append_str(printmsg, "\t"); + ares__buf_append_str(printmsg, ares_dns_rec_type_tostr(qtype)); + ares__buf_append_str(printmsg, "\n"); + } + ares__buf_append_str(printmsg, "\n"); + for (size_t i = ARES_SECTION_ANSWER; i < ARES_SECTION_ADDITIONAL + 1; i++) { + ares__buf_append_str(printmsg, ";; "); + ares__buf_append_str(printmsg, ares_dns_section_tostr((ares_dns_section_t)i)); + ares__buf_append_str(printmsg, " SECTION:\n"); + for (size_t j = 0; j < ares_dns_record_rr_cnt(dnsrec, (ares_dns_section_t)i); j++) { + const ares_dns_rr_t *rr = ares_dns_record_rr_get(dnsrec, (ares_dns_section_t)i, j); + ares__buf_append_str(printmsg, ares_dns_rr_get_name(rr)); + ares__buf_append_str(printmsg, ".\t\t\t"); + ares__buf_append_str(printmsg, ares_dns_class_tostr(ares_dns_rr_get_class(rr))); + ares__buf_append_str(printmsg, "\t"); + ares__buf_append_str(printmsg, ares_dns_rec_type_tostr(ares_dns_rr_get_type(rr))); + ares__buf_append_str(printmsg, "\t"); + ares__buf_append_num_dec(printmsg, ares_dns_rr_get_ttl(rr), 0); + ares__buf_append_str(printmsg, "\t"); + + size_t keys_cnt; + const ares_dns_rr_key_t *keys = ares_dns_rr_get_keys(ares_dns_rr_get_type(rr), &keys_cnt); + for (size_t k = 0; k