additional test coverage

pull/619/head
Brad House 1 year ago
parent 37082523cb
commit 818904643e
  1. 2
      src/lib/ares__buf.c
  2. 8
      src/lib/ares__buf.h
  3. 106
      test/ares-test-internal.cc

@ -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; 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)); return ares__buf_append(buf, (const unsigned char *)str, ares_strlen(str));
} }

@ -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); 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. /*! Append a 16bit Big Endian number to the buffer.
* *
* \param[in] buf Initialized buffer object * \param[in] buf Initialized buffer object

@ -721,6 +721,112 @@ TEST_F(LibraryTest, DNSRecord) {
EXPECT_EQ(ancount, ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ANSWER)); 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(nscount, ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_AUTHORITY));
EXPECT_EQ(arcount, ares_dns_record_rr_cnt(dnsrec, ARES_SECTION_ADDITIONAL)); 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<keys_cnt; k++) {
char buf[256] = "";
ares__buf_append_str(printmsg, ares_dns_rr_key_tostr(keys[k]));
ares__buf_append_str(printmsg, "=");
switch (ares_dns_rr_key_datatype(keys[k])) {
case ARES_DATATYPE_INADDR:
ares_inet_ntop(AF_INET, ares_dns_rr_get_addr(rr, keys[k]), buf, sizeof(buf));
ares__buf_append_str(printmsg, buf);
break;
case ARES_DATATYPE_INADDR6:
ares_inet_ntop(AF_INET6, ares_dns_rr_get_addr6(rr, keys[k]), buf, sizeof(buf));
ares__buf_append_str(printmsg, buf);
break;
case ARES_DATATYPE_U8:
ares__buf_append_num_dec(printmsg, ares_dns_rr_get_u8(rr, keys[k]), 0);
break;
case ARES_DATATYPE_U16:
ares__buf_append_num_dec(printmsg, ares_dns_rr_get_u16(rr, keys[k]), 0);
break;
case ARES_DATATYPE_U32:
ares__buf_append_num_dec(printmsg, ares_dns_rr_get_u32(rr, keys[k]), 0);
break;
case ARES_DATATYPE_NAME:
case ARES_DATATYPE_STR:
ares__buf_append_byte(printmsg, '"');
ares__buf_append_str(printmsg, ares_dns_rr_get_str(rr, keys[k]));
ares__buf_append_byte(printmsg, '"');
break;
case ARES_DATATYPE_BIN:
/* TODO */
break;
case ARES_DATATYPE_BINP:
ares__buf_append_byte(printmsg, '"');
size_t templen;
ares__buf_append_str(printmsg, (const char *)ares_dns_rr_get_bin(rr, keys[k], &templen));
ares__buf_append_byte(printmsg, '"');
break;
case ARES_DATATYPE_OPT:
/* TODO */
break;
}
ares__buf_append_str(printmsg, " ");
}
ares__buf_append_str(printmsg, "\n");
}
}
ares__buf_append_str(printmsg, ";; SIZE: ");
ares__buf_append_num_dec(printmsg, msglen, 0);
ares__buf_append_str(printmsg, "\n\n");
char *printdata = ares__buf_finish_str(printmsg, NULL);
//printf("%s", printdata);
ares_free(printdata);
ares_dns_record_destroy(dnsrec); ares_dns_record_destroy(dnsrec);
ares_free(msg); ares_free(msg);
} }

Loading…
Cancel
Save