Commit f5f65728 authored by Lubos Slovak's avatar Lubos Slovak

Fixed dname_deep_copy().

- Now copying also non-FQDN domain names.
- Added unittest for dname deep copy.

fixes #1396 @1h
parent a0d8c40a
......@@ -475,6 +475,7 @@ knot_dname_t *knot_dname_new_from_wire(const uint8_t *name, uint size,
dname->size = size;
if (knot_dname_find_labels(dname, 1) != 0) {
dbg_dname("Could not find labels in dname (new from wire).\n");
knot_dname_free(&dname);
return NULL;
}
......@@ -611,8 +612,35 @@ int knot_dname_from_wire(const uint8_t *name, uint size,
knot_dname_t *knot_dname_deep_copy(const knot_dname_t *dname)
{
return knot_dname_new_from_wire(dname->name, dname->size,
dname->node);
//return knot_dname_new_from_wire(dname->name, dname->size, dname->node);
/* dname_new_from_wire() does not accept non-FQDN dnames, so we
* do the copy by hand. It's faster anyway */
knot_dname_t *copy = knot_dname_new();
CHECK_ALLOC(copy, NULL);
copy->labels = (uint8_t *)(malloc(dname->label_count));
if (copy->labels == NULL) {
knot_dname_free(&copy);
return NULL;
}
copy->name = (uint8_t *)(malloc(dname->size));
if (copy->name == NULL) {
knot_dname_free(&copy);
return NULL;
}
memcpy(copy->labels, dname->labels, dname->label_count);
copy->label_count = dname->label_count;
memcpy(copy->name, dname->name, dname->size);
copy->size = dname->size;
copy->node = dname->node;
return copy;
}
/*----------------------------------------------------------------------------*/
......
......@@ -116,6 +116,9 @@ knot_dname_t *knot_dname_new_from_str(const char *name, unsigned int size,
* e.g. to knot_dname_to_str() may result in crash. Decide whether it
* is OK to retain this and check the data in other functions before
* calling this one, or if it should verify the given data.
*
* \warning Actually, right now this function does not accept non-FQDN dnames.
* For some reason there is a check for this.
*/
knot_dname_t *knot_dname_new_from_wire(const uint8_t *name,
unsigned int size,
......
......@@ -493,7 +493,7 @@ static int test_dname_is_subdomain()
for (int i = 0; i < TEST_DOMAINS_NON_FQDN; ++i) {
dnames_non_fqdn[i] = knot_dname_new_from_str(
test_domains_non_fqdn[i].str,
test_domains_non_fqdn[i].size, NULL);
strlen(test_domains_non_fqdn[i].str), NULL);
assert(dnames_non_fqdn[i] != NULL);
}
......@@ -609,6 +609,60 @@ static int test_dname_is_subdomain()
return (errors == 0);
}
static int test_dname_deep_copy() {
int errors = 0;
knot_dname_t *dnames_fqdn[TEST_DOMAINS_OK];
knot_dname_t *dnames_non_fqdn[TEST_DOMAINS_NON_FQDN];
knot_dname_t *dnames_fqdn_copy[TEST_DOMAINS_OK];
knot_dname_t *dnames_non_fqdn_copy[TEST_DOMAINS_NON_FQDN];
for (int i = 0; i < TEST_DOMAINS_OK; ++i) {
dnames_fqdn[i] = knot_dname_new_from_wire(
(const uint8_t *)test_domains_ok[i].wire,
test_domains_ok[i].size, NODE_ADDRESS);
assert(dnames_fqdn[i] != NULL);
}
for (int i = 0; i < TEST_DOMAINS_NON_FQDN; ++i) {
dnames_non_fqdn[i] = knot_dname_new_from_str(
test_domains_non_fqdn[i].str,
strlen(test_domains_non_fqdn[i].str),
NODE_ADDRESS);
// note("Created name: %.*s\n", dnames_non_fqdn[i]->size,
// dnames_non_fqdn[i]->name);
assert(dnames_non_fqdn[i] != NULL);
}
/*
* Create copies of the domain names.
*/
for (int i = 0; i < TEST_DOMAINS_OK; ++i) {
// note("Testing %d. FQDN domain.\n", i);
dnames_fqdn_copy[i] = knot_dname_deep_copy(dnames_fqdn[i]);
assert(dnames_fqdn_copy[i] != NULL);
errors += check_domain_name(dnames_fqdn_copy[i],
test_domains_ok, i, 1);
knot_dname_free(&dnames_fqdn_copy[i]);
knot_dname_free(&dnames_fqdn[i]);
}
for (int i = 0; i < TEST_DOMAINS_NON_FQDN; ++i) {
// note("Testing %d. non-FQDN domain: ", i);
// note("%.*s\n", dnames_non_fqdn[i]->size,
// dnames_non_fqdn[i]->name);
dnames_non_fqdn_copy[i] =
knot_dname_deep_copy(dnames_non_fqdn[i]);
assert(dnames_non_fqdn_copy[i] != NULL);
errors += check_domain_name(dnames_non_fqdn_copy[i],
test_domains_non_fqdn, i, 1);
knot_dname_free(&dnames_non_fqdn_copy[i]);
knot_dname_free(&dnames_non_fqdn[i]);
}
return (errors == 0);
}
static int check_wires(const uint8_t *wire1, uint size1,
uint8_t *wire2, uint size2)
{
......@@ -798,7 +852,7 @@ static int test_dname_getters(uint type)
return (errors == 0);
}
static const int KNOT_DNAME_TEST_COUNT = 15;
static const int KNOT_DNAME_TEST_COUNT = 16;
/*! This helper routine should report number of
* scheduled tests for given parameters.
......@@ -873,6 +927,9 @@ static int knot_dname_tests_run(int argc, char *argv[])
ok((res = test_dname_is_subdomain()), "dname: is subdomain");
res_final *= res;
ok((res = test_dname_deep_copy()), "dname: deep copy");
res_final *= res;
endskip; /* create failed */
return res_final;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment