Skip to content

Commit

Permalink
fix minor valgrind reports in unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
Fabian Mauchle committed May 17, 2019
1 parent 24f1cc2 commit 964038e
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 61 deletions.
17 changes: 8 additions & 9 deletions tests/t_resizeattr.c
Original file line number Diff line number Diff line change
Expand Up @@ -10,24 +10,23 @@ int test_resize(int start_size, int target_size, uint8_t shouldfail) {

uint8_t *value = malloc(start_size);
struct tlv *attr;
int result = 1;

memset(value, 42, start_size);
attr = maketlv(1,start_size,value);

if (!resizeattr(attr, target_size))
return shouldfail;
result = shouldfail;
else if (shouldfail)
return 0;

if (attr->l != target_size)
return 0;

if (memcmp(attr->v, value, target_size <= start_size ? target_size : start_size))
return 0;
result = 0;
else if (attr->l != target_size)
result = 0;
else if (memcmp(attr->v, value, target_size <= start_size ? target_size : start_size))
result = 0;

freetlv(attr);
free(value);
return 1;
return result;
}

int main (int argc, char *argv[])
Expand Down
124 changes: 72 additions & 52 deletions tests/t_rewrite.c
Original file line number Diff line number Diff line change
Expand Up @@ -46,14 +46,20 @@ void _list_clear(struct list *list) {
free(data);
}

void _tlv_list_clear(struct list *list) {
struct tlv *tlv;
while ( (tlv = (struct tlv *)list_shift(list)) )
freetlv(tlv);
}

void _reset_rewrite(struct rewrite *rewrite) {
rewrite->whitelist_mode = 0;
rewrite->removeattrs = NULL;
rewrite->removevendorattrs = NULL;
_list_clear(rewrite->addattrs);
_tlv_list_clear(rewrite->addattrs);
_list_clear(rewrite->modattrs);
_list_clear(rewrite->modvattrs);
_list_clear(rewrite->supattrs);
_tlv_list_clear(rewrite->supattrs);
}

int
Expand Down Expand Up @@ -87,8 +93,8 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - empty rewrite\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -105,8 +111,8 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - removeattrs\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -126,8 +132,8 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - removevendorattrs full\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -147,8 +153,8 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - removevendorattrs last element\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -165,8 +171,8 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - removevendorattrs non-rfc\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -192,8 +198,8 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - removevendorattrs sub-attribute\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -208,8 +214,8 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - addattribute simple\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -230,8 +236,8 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - addattribute with existing attributes\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -244,8 +250,8 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - addattribute null\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -261,8 +267,8 @@ main (int argc, char *argv[])
printf("ok %d - addattribute too big\n", testcount++);

free(value);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -277,8 +283,8 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - suppattrs non existing\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -295,8 +301,8 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - suppattrs existing\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -315,8 +321,8 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - suppattrs vendor\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -340,8 +346,9 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - modify attribute no match\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
regfree(&regex);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -365,8 +372,9 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - modify attribute match full replace\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
regfree(&regex);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -390,8 +398,9 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - modify attribute match full replace\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
regfree(&regex);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand Down Expand Up @@ -421,8 +430,9 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - modify attribute max length\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
regfree(&regex);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -444,8 +454,9 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - modify attribute too long\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
regfree(&regex);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -468,8 +479,9 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - modify attribute regex replace\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
regfree(&regex);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -494,8 +506,9 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - modify vendor\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
regfree(&regex);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand Down Expand Up @@ -526,8 +539,9 @@ main (int argc, char *argv[])
printf("not ");
printf("ok %d - modify vendor too long\n", testcount++);

_list_clear(origattrs);
_list_clear(expectedattrs);
regfree(&regex);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -546,8 +560,8 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - whitelistattrs\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);

}
Expand All @@ -571,8 +585,8 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - whitelistvendorattrs\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -598,8 +612,8 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - whitelistvendorattrs\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

Expand All @@ -624,11 +638,17 @@ main (int argc, char *argv[])
if (_check_rewrite(origattrs, &rewrite, expectedattrs, 0))
printf("not ");
printf("ok %d - whitelistvendorattrs\n", testcount++);
_list_clear(origattrs);
_list_clear(expectedattrs);
_tlv_list_clear(origattrs);
_tlv_list_clear(expectedattrs);
_reset_rewrite(&rewrite);
}

list_destroy(origattrs);
list_destroy(expectedattrs);
list_destroy(rewrite.addattrs);
list_destroy(rewrite.modattrs);
list_destroy(rewrite.modvattrs);
list_destroy(rewrite.supattrs);

return 0;
}
1 change: 1 addition & 0 deletions tests/t_rewrite_config.c
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,7 @@ main (int argc, char *argv[])
printf("not ok %d - rewrite ocnfig\n", numtests++);
}

freetlv(expected);

return 0;
}

0 comments on commit 964038e

Please sign in to comment.