Skip to content

Commit

Permalink
Add valgrind coverage of unit tests
Browse files Browse the repository at this point in the history
  • Loading branch information
rescrv committed Aug 8, 2016
1 parent cc3b0f9 commit 6d125f7
Show file tree
Hide file tree
Showing 25 changed files with 225 additions and 24 deletions.
20 changes: 20 additions & 0 deletions Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -118,24 +118,43 @@ macaroon_test_serialization_CFLAGS = $(AM_CFLAGS) $(CFLAGS)

macaroon_unit_test_stubs =
macaroon_unit_test_stubs += test/unit/caveat_v1_1.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_1.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_2.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_2.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_3.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_3.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_4.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_4.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_5.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_5.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_6.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v1_6.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_1.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_1.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_2.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_2.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_3.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_3.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_4.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_4.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_5.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_5.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_6.vtest.sh
macaroon_unit_test_stubs += test/unit/caveat_v2_6.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/root_v1_1.vtest.sh
macaroon_unit_test_stubs += test/unit/root_v1_1.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/root_v1_2.vtest.sh
macaroon_unit_test_stubs += test/unit/root_v1_2.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/root_v2_1.vtest.sh
macaroon_unit_test_stubs += test/unit/root_v2_1.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/root_v2_2.vtest.sh
macaroon_unit_test_stubs += test/unit/root_v2_2.vtest.valgrind.sh
macaroon_unit_test_stubs += test/unit/serialization_1.sh
macaroon_unit_test_stubs += test/unit/serialization_1.valgrind.sh
macaroon_unit_test_stubs += test/unit/serialization_2.sh
macaroon_unit_test_stubs += test/unit/serialization_2.valgrind.sh
macaroon_unit_test_stubs += test/unit/serialization_3.sh
macaroon_unit_test_stubs += test/unit/serialization_3.valgrind.sh

TESTS += $(macaroon_unit_test_stubs)
check_SCRIPTS += $(macaroon_unit_test_stubs)
Expand Down Expand Up @@ -193,3 +212,4 @@ EXTRA_DIST += maint/generate-shell-stubs

maintainer-generate:
maint/generate-shell-stubs
maint/valgrind-stubs
57 changes: 46 additions & 11 deletions macaroon-test-serialization.c
Original file line number Diff line number Diff line change
Expand Up @@ -44,19 +44,22 @@

struct parsed_macaroon
{
enum macaroon_format F;
unsigned char* B;
struct macaroon* M;
enum macaroon_format F;
};

int
main(int argc, const char* argv[])
{
char* line = NULL;
size_t line_sz = 0;
struct parsed_macaroon* tmp = NULL;
struct parsed_macaroon* macaroons = NULL;
size_t macaroons_sz = 0;
size_t i;
size_t j;
int ret = EXIT_SUCCESS;

while (1)
{
Expand All @@ -70,7 +73,7 @@ main(int argc, const char* argv[])
}

fprintf(stderr, "could not read from stdin: %s\n", strerror(ferror(stdin)));
return EXIT_FAILURE;
goto fail;
}

if (!line || amt == 0 || *line == '\n' || *line == '#')
Expand All @@ -85,7 +88,7 @@ main(int argc, const char* argv[])
if (!space)
{
fprintf(stderr, "space missing on line %lu\n", macaroons_sz + 1);
return EXIT_FAILURE;
goto fail;
}

assert(space < end);
Expand All @@ -112,23 +115,24 @@ main(int argc, const char* argv[])
else
{
fprintf(stderr, "version %s not supported\n", line);
return EXIT_FAILURE;
goto fail;
}

size_t buf_sz = strlen(space + 1);
unsigned char* buf = malloc(buf_sz);

if (!buf)
{
return EXIT_FAILURE;
goto fail;
}

memset(buf, 0, sizeof(buf));
int rc = b64_pton(space + 1, buf, buf_sz);

if (rc < 0)
{
fprintf(stderr, "could not unwrap serialized macaroon\n");
return EXIT_FAILURE;
goto fail;
}

enum macaroon_returncode err;
Expand All @@ -137,22 +141,24 @@ main(int argc, const char* argv[])
if (!M)
{
fprintf(stderr, "could not deserialize macaroon: %s\n", macaroon_error(err));
return EXIT_FAILURE;
goto fail;
}

++macaroons_sz;
macaroons = realloc(macaroons, macaroons_sz * sizeof(struct parsed_macaroon));
tmp = realloc(macaroons, macaroons_sz * sizeof(struct parsed_macaroon));

if (!macaroons)
if (!tmp)
{
return EXIT_FAILURE;
goto fail;
}

macaroons = tmp;
macaroons[macaroons_sz - 1].F = format;
macaroons[macaroons_sz - 1].M = M;
macaroons[macaroons_sz - 1].B = buf;
}

int ret = EXIT_SUCCESS;
ret = EXIT_SUCCESS;

for (i = 0; i < macaroons_sz; ++i)
{
Expand All @@ -166,6 +172,35 @@ main(int argc, const char* argv[])
}
}

goto exit;

fail:
ret = EXIT_FAILURE;

exit:
if (line)
{
free(line);
}

for (i = 0; i < macaroons_sz; ++i)
{
if (macaroons[i].B)
{
free(macaroons[i].B);
}

if (macaroons[i].M)
{
macaroon_destroy(macaroons[i].M);
}
}

if (macaroons)
{
free(macaroons);
}

(void) argc;
(void) argv;
return ret;
Expand Down
56 changes: 43 additions & 13 deletions macaroon-test-verifier.c
Original file line number Diff line number Diff line change
Expand Up @@ -109,6 +109,7 @@ parse_macaroon(const char* line, struct macaroon*** macaroons, size_t* macaroons
{
size_t buf_sz = strlen(line);
unsigned char* buf = malloc(buf_sz);
struct macaroon** tmp;

if (!buf)
{
Expand All @@ -120,11 +121,13 @@ parse_macaroon(const char* line, struct macaroon*** macaroons, size_t* macaroons
if (rc < 0)
{
fprintf(stderr, "could not unwrap serialized macaroon\n");
free(buf);
return -1;
}

enum macaroon_returncode err;
struct macaroon* M = macaroon_deserialize(buf, rc, &err);
free(buf);

if (!M)
{
Expand All @@ -133,13 +136,14 @@ parse_macaroon(const char* line, struct macaroon*** macaroons, size_t* macaroons
}

++*macaroons_sz;
*macaroons = realloc(*macaroons, *macaroons_sz * sizeof(struct macaroon*));
tmp = realloc(*macaroons, *macaroons_sz * sizeof(struct macaroon*));

if (!*macaroons)
if (!tmp)
{
return -1;
}

*macaroons = tmp;
(*macaroons)[*macaroons_sz - 1] = M;
return 0;
}
Expand All @@ -155,11 +159,13 @@ main(int argc, const char* argv[])
struct macaroon_verifier* V = NULL;
struct macaroon** macaroons = NULL;
size_t macaroons_sz = 0;
size_t i = 0;
int ret = EXIT_SUCCESS;

if (!(V = macaroon_verifier_create()))
{
fprintf(stderr, "could not create verifier: %s\n", strerror(ferror(stdin)));
return EXIT_FAILURE;
goto fail;
}

while (1)
Expand All @@ -174,7 +180,7 @@ main(int argc, const char* argv[])
}

fprintf(stderr, "could not read from stdin: %s\n", strerror(ferror(stdin)));
return EXIT_FAILURE;
goto fail;
}

if (!line || amt == 0 || *line == '\n' || *line == '#')
Expand All @@ -188,7 +194,7 @@ main(int argc, const char* argv[])
{
if (parse_version(line) < 0)
{
return EXIT_FAILURE;
goto fail;
}
}
else if (strcmp(line, AUTHORIZED) == 0)
Expand All @@ -203,14 +209,14 @@ main(int argc, const char* argv[])
{
if (parse_key(line, &key, &key_sz) < 0)
{
return EXIT_FAILURE;
goto fail;
}
}
else if (strncmp(line, EXACT, STRLENOF(EXACT)) == 0)
{
if (parse_exact_caveat(line, V) < 0)
{
return EXIT_FAILURE;
goto fail;
}
}
else if (strncmp(line, GENERAL, STRLENOF(GENERAL)) == 0)
Expand All @@ -219,14 +225,14 @@ main(int argc, const char* argv[])
}
else if (parse_macaroon(line, &macaroons, &macaroons_sz) < 0)
{
return EXIT_FAILURE;
goto fail;
}
}

if (macaroons_sz == 0)
{
fprintf(stderr, "no macaroons provided\n");
return EXIT_FAILURE;
goto fail;
}

enum macaroon_returncode err;
Expand All @@ -236,21 +242,27 @@ main(int argc, const char* argv[])
if (verify != 0 && err != MACAROON_NOT_AUTHORIZED)
{
fprintf(stderr, "verification encountered exceptional error: %s\n", macaroon_error(err));
return -1;
goto fail;
}

if (verify == 0 && !authorized)
{
fprintf(stderr, "verification passed for \"unauthorized\" scenario\n");
return -1;
goto fail;
}

if (verify != 0 && err == MACAROON_NOT_AUTHORIZED && authorized)
{
fprintf(stderr, "verification failed for \"authorized\" scenario\n");
return -1;
goto fail;
}

goto exit;

fail:
ret = EXIT_FAILURE;

exit:
if (line)
{
free(line);
Expand All @@ -261,7 +273,25 @@ main(int argc, const char* argv[])
free(key);
}

for (i = 0; i < macaroons_sz; ++i)
{
if (macaroons[i])
{
macaroon_destroy(macaroons[i]);
}
}

if (macaroons)
{
free(macaroons);
}

if (V)
{
macaroon_verifier_destroy(V);
}

(void) argc;
(void) argv;
return EXIT_SUCCESS;
return ret;
}
41 changes: 41 additions & 0 deletions macaroons.supp
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
{
<bash>
Memcheck:Leak
...
obj:*/bash
...
}
{
<sed>
Memcheck:Leak
...
obj:*/sed
...
}
{
<gcc>
Memcheck:Leak
...
obj:*/gcc
...
}
{
<ld>
Memcheck:Leak
...
obj:*/ld
...
}
{
<ld>
Memcheck:Leak
...
obj:*/collect2
...
}
{
<base64>
Memcheck:Cond
fun:b64_pton
...
}
Loading

0 comments on commit 6d125f7

Please sign in to comment.