From: TCY16 Date: Wed, 25 May 2022 13:21:29 +0000 (+0200) Subject: finalize tests, fix some limit bugs found during testing X-Git-Tag: 1.8.2-rc.1~3^2~8 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=e87512976d7d960da9b6c3148b88d557a59f83bf;p=thirdparty%2Fldns.git finalize tests, fix some limit bugs found during testing --- diff --git a/edns.c b/edns.c index 10f39d99..f77aa73a 100644 --- a/edns.c +++ b/edns.c @@ -196,7 +196,9 @@ ldns_edns_option_list_clone(ldns_edns_option_list *old_list) return NULL; } - new_list->_option_count = old_list->_option_count; + if (old_list->_option_count == 0) { + return new_list; + } /* adding options also updates the total options size */ for (i = 0; i < old_list->_option_count; i++) { @@ -239,17 +241,11 @@ ldns_edns_option_list_get_count(const ldns_edns_option_list *option_list) } } -void -ldns_edns_option_list_set_count(ldns_edns_option_list *option_list, size_t count) -{ - assert(option_list); - option_list->_option_count = count; -} - ldns_edns_option * ldns_edns_option_list_get_option(const ldns_edns_option_list *option_list, size_t index) { if (option_list && index < ldns_edns_option_list_get_count(option_list)) { + assert(option_list->_options[index]); return option_list->_options[index]; } else { return NULL; @@ -275,7 +271,7 @@ ldns_edns_option_list_set_option(ldns_edns_option_list *option_list, assert(option_list != NULL); - if (index < ldns_edns_option_list_get_count(option_list)) { + if (index > ldns_edns_option_list_get_count(option_list)) { return NULL; } @@ -379,7 +375,7 @@ ldns_edns_option_list_pop(ldns_edns_option_list *option_list) option_list->_options_size -= (ldns_edns_get_size(pop) + 4); } - ldns_edns_option_list_set_count(option_list, count - 1); + option_list->_option_count = count - 1; return pop; } @@ -413,7 +409,7 @@ ldns_edns_option_list2wireformat_buffer(const ldns_edns_option_list *option_list data = ldns_edns_get_data(edns); /* make sure the option fits */ - if (ldns_buffer_capacity(buffer) > size + 4) { + if (!(ldns_buffer_available(buffer, size + 4))) { ldns_buffer_free(buffer); return NULL; } diff --git a/ldns/edns.h b/ldns/edns.h index 553b3447..8382364c 100644 --- a/ldns/edns.h +++ b/ldns/edns.h @@ -214,13 +214,6 @@ void ldns_edns_option_list_deep_free(ldns_edns_option_list *options_list); */ size_t ldns_edns_option_list_get_count(const ldns_edns_option_list *options_list); -/** - * sets the number of options in the EDNS options list. - * \param[in] options_list the EDNS options_list with the associated counter - * \param[in] count the new cnumber of EDNS options in the list - */ -void ldns_edns_option_list_set_count(ldns_edns_option_list *options_list, size_t count); - /** * returns the EDNS option as the specified index in the list of EDNS options. * \param[in] options_list the EDNS options_list to read from @@ -245,7 +238,7 @@ size_t ldns_edns_option_list_get_options_size(const ldns_edns_option_list *optio * \param[in] option the EDNS option to add to the list * \return the EDNS option previously located at the index */ -ldns_edns_option * ldns_edns_option_list_set_option(ldns_edns_option_list *options_list, +ldns_edns_option *ldns_edns_option_list_set_option(ldns_edns_option_list *options_list, ldns_edns_option *option, size_t index); /** diff --git a/test/16-unit-tests-edns.tpkg/16-unit-tests-edns.c b/test/16-unit-tests-edns.tpkg/16-unit-tests-edns.c index dc2e7fc7..1607df0b 100644 --- a/test/16-unit-tests-edns.tpkg/16-unit-tests-edns.c +++ b/test/16-unit-tests-edns.tpkg/16-unit-tests-edns.c @@ -1,37 +1,251 @@ #include "config.h" - #include +static int +check_option_entries(ldns_edns_option *edns, ldns_edns_option_code code, + size_t size, uint8_t *hex_data) +{ + size_t i; + uint8_t *edns_data; + ldns_buffer *buf; + + if (ldns_edns_get_size(edns) != size) { + printf("Error: EDNS size is incorrect\n"); + return 0; + } + if (ldns_edns_get_code(edns) != code) { + printf("Error: EDNS code is incorrect\n"); + return 0; + } + + edns_data = ldns_edns_get_data(edns); + if (!(edns_data)) { + printf("Error: EDNS data is not returned\n"); + return 0; + } + for (i = 0; i < size; i++) { + if (edns_data[i] != hex_data[i]) { + printf("Error: EDNS data is incorrect\n"); + return 0; + } + } + + buf = ldns_edns_get_wireformat_buffer(edns); + if (ldns_buffer_read_u16(buf) != code) { + printf("Error: EDNS type is incorrect\n"); + return 0; + } + if (ldns_buffer_read_u16(buf) != size) { + printf("Error: EDNS length is incorrect\n"); + return 0; + } + + for (i = 0; i < size; i++) { + if (ldns_buffer_read_u8_at(buf, i+4) != hex_data[i]) { + printf("Error: EDNS data is incorrect: %d, %d\n", + ldns_buffer_read_u8_at(buf, i+4), hex_data[i]); + return 0; + } + } + + return 1; +} + static int check_option() { ldns_edns_option *edns; + ldns_edns_option *clone; uint8_t *data = LDNS_XMALLOC(uint8_t, 4); - data[0] = 74; - data[1] = 65; - data[2] = 73; - data[3] = 74; + + uint8_t hex_data[] = {74, 65, 73, 74}; + + /* Fill the data with "test" in hex */ + data[0] = hex_data[0]; + data[1] = hex_data[1]; + data[2] = hex_data[2]; + data[3] = hex_data[3]; edns = ldns_edns_new(LDNS_EDNS_EDE, 4, data); - if (ldns_edns_get_size(edns) != 4) { - printf("Error: EDNS size is incorrect\n"); - return 1; + if (!(check_option_entries(edns, LDNS_EDNS_EDE, 4, hex_data))) { + return 0; } - if (ldns_edns_get_code(edns) != LDNS_EDNS_EDE) { - printf("Error: EDNS code is incorrect\n"); - return 1; + + ldns_edns_free(edns); + + edns = ldns_edns_new_from_data(LDNS_EDNS_EDE, 4, hex_data); + + if (!(check_option_entries(edns, LDNS_EDNS_EDE, 4, hex_data))) { + return 0; + } + + clone = ldns_edns_clone(edns); + + if (!(check_option_entries(clone, LDNS_EDNS_EDE, 4, hex_data))) { + return 0; } - // if (ldns_edns_get_data(edns)) {} - // if (ldns_edns_get_wireformat_buffer(edns)) {} + ldns_edns_deep_free(edns); + ldns_edns_deep_free(clone); + + return 1; +} + +static int check_option_list_entries(ldns_edns_option_list *list, + ldns_edns_option *option, size_t count, ldns_edns_option_code code, size_t size, + uint8_t *hex_data) +{ + size_t c = ldns_edns_option_list_get_count(list); + + if (c != count) { + printf("Error: EDNS list count is incorrect\n"); + return 0; + } + if (!(option)) { + printf("Error: EDNS list option setter doesn't return option\n"); + return 0; + } - // ldns_edns_option *edns2 = ldns_edns_new_from_data(LDNS_EDNS_EDE, size_t size, const void *data); + if (!(check_option_entries(option, code, size, hex_data))) { + printf("Error: EDNS list option is incorrect\n"); + return 0; + } - return 0; + return 1; +} + +static int +check_option_list() +{ + size_t size, i; + ldns_edns_option_list* list; + ldns_edns_option_list* clone; + ldns_edns_option *option; + ldns_edns_option *copy; + ldns_edns_option *pop; + ldns_buffer *buf; + uint8_t hex_data[] = {74, 65, 73, 74}; + uint8_t hex_data2[] = {74, 65, 73, 74, 74}; + + list = ldns_edns_option_list_new(); // don't verify, this function asserts + + /* Add first option */ + option = ldns_edns_new_from_data(LDNS_EDNS_EDE, 4, hex_data); + + if (ldns_edns_option_list_get_count(list)) { + printf("Error: EDNS list count is incorrect after init\n"); + return 0; + } + + ldns_edns_option_list_push(list, option); + + copy = ldns_edns_option_list_get_option(list, 0); + + if (!(check_option_list_entries(list, copy, 1, LDNS_EDNS_EDE, 4, hex_data))) { + printf("Error: EDNS list entries are incorrect\n"); + return 0; + } + + size = ldns_edns_option_list_get_options_size(list); + + if (size != 8) { // size of the data + 4 for the code and size + printf("Error: EDNS list total option size is incorrect\n"); + return 0; + } + + /* Add second option */ + option = ldns_edns_new_from_data(LDNS_EDNS_PADDING, 5, hex_data2); + + ldns_edns_option_list_push(list, option); + + if (!(check_option_list_entries(list, option, 2, LDNS_EDNS_PADDING, 5, hex_data2))) { + printf("Error: EDNS list entries are incorrect\n"); + return 0; + } + + buf = ldns_edns_option_list2wireformat_buffer(list); + + if (!(buf)) { + printf("Error: EDNS list entries list2wireformat buffer is NULL\n"); + return 0; + } + + /* Verify the wireformat options with the hex data */ + ldns_buffer_skip(buf, 4); + + for (i = 0; i < 4; i++) { + if (ldns_buffer_read_u8(buf) != hex_data[i]) { + printf("Error: EDNS data is incorrect: %d, %d\n", + ldns_buffer_read_u8_at(buf, i), hex_data[i]); + return 0; + } + } + + ldns_buffer_skip(buf, 4); + + for (i = 0; i < 5; i++) { + if (ldns_buffer_read_u8(buf) != hex_data2[i]) { + printf("Error: EDNS data is incorrect: %d, %d\n", + ldns_buffer_read_u8_at(buf, i), hex_data2[i]); + return 0; + } + } + + /* Replace the first option with a copy of the second */ + option = ldns_edns_new_from_data(LDNS_EDNS_PADDING, 5, hex_data2); + + pop = ldns_edns_option_list_set_option(list, option, 0); + + if (!(check_option_list_entries(list, pop, 2, LDNS_EDNS_EDE, 4, hex_data))) { + printf("Error: EDNS list entries are incorrect\n"); + return 0; + } + + ldns_edns_deep_free(pop); + + /* Remove one option from the list */ + + pop = ldns_edns_option_list_pop(list); + + if (!(check_option_list_entries(list, option, 1, LDNS_EDNS_PADDING, 5, hex_data2))) { + printf("Error: EDNS list entries are incorrect\n"); + return 0; + } + + ldns_edns_deep_free(pop); + + /* Clone the list */ + clone = ldns_edns_option_list_clone(list); + + if (!(clone)) { + printf("Error: EDNS list clone does not exist\n"); + return 0; + } + + if (!(check_option_list_entries(clone, option, 1, LDNS_EDNS_PADDING, 5, hex_data2))) { + printf("Error: EDNS list entries are incorrect\n"); + return 0; + } + + /* Remove final entry from cloned list */ + pop = ldns_edns_option_list_pop(list); + + ldns_edns_deep_free(pop); + + if (ldns_edns_option_list_get_count(clone) == 0) { + printf("Error: EDNS list entries are incorrect at zero\n"); + return 0; + } + + ldns_edns_option_list_free(clone); + + ldns_edns_option_list_deep_free(list); + + return 1; } int main(void) @@ -42,6 +256,10 @@ int main(void) printf("check_option() failed.\n"); result = EXIT_FAILURE; } + if (!check_option_list()) { + printf("check_option_list() failed.\n"); + result = EXIT_FAILURE; + } exit(result); }