diff options
author | Thomas Habets <thomas@habets.se> | 2015-11-25 14:53:52 +0000 |
---|---|---|
committer | Thomas Habets <thomas@habets.se> | 2015-11-25 14:56:42 +0000 |
commit | f1fe8ab3733222a42c46e7ae812fcead52088cf1 (patch) | |
tree | 9487b9dd176c96c2baa1e10d704a1dbb01e4e601 /src/arping_test.c | |
parent | d14a4ac41cf005b66698fd308f836fd2eb0759a9 (diff) | |
download | arping-f1fe8ab3733222a42c46e7ae812fcead52088cf1.tar.gz |
Correctly check if libnet_init fails.
If `libnet_init()` failed (e.g. not root) AND IP parsing failed, then
arping would SEGV when it tried to construct the error message. This fixes that.
Diffstat (limited to 'src/arping_test.c')
-rw-r--r-- | src/arping_test.c | 52 |
1 files changed, 51 insertions, 1 deletions
diff --git a/src/arping_test.c b/src/arping_test.c index 2c21ae0..50e2367 100644 --- a/src/arping_test.c +++ b/src/arping_test.c @@ -31,12 +31,21 @@ #include"arping.h" -static int numtests = 0; +extern libnet_t* libnet; +extern int mock_libnet_lo_ok; +extern int mock_libnet_null_ok; + struct registered_test { void* fn; const char* name; }; + +static int numtests = 0; static struct registered_test test_registry[1024]; + +static int num_exit_tests = 0; +static struct registered_test test_exit_registry[1024]; + int get_mac_addr(const char *in, char *out); void strip_newline(char* s); @@ -48,6 +57,13 @@ static void cons_##a() { \ numtests++; \ } START_TEST(a) +#define MY_EXIT_TEST(a) static void a(int);__attribute__((constructor)) \ +static void cons_##a() { \ + test_exit_registry[num_exit_tests].fn = a; \ + test_exit_registry[num_exit_tests].name = #a; \ + num_exit_tests++; \ +} START_TEST(a) + /** * */ @@ -492,6 +508,35 @@ MYTEST(get_mac_addr_fail) } } END_TEST +MY_EXIT_TEST(libnet_init_bad_nolo) +{ + // It'll only try lo if named interface fails. + // So by accepting lo, we make sure it doesn't try lo. + mock_libnet_lo_ok = 1; + do_libnet_init("bad", 0); +} END_TEST + +MY_EXIT_TEST(libnet_init_null_nolo_nonull) +{ + mock_libnet_lo_ok = 0; + mock_libnet_null_ok = 0; + do_libnet_init(NULL, 0); +} END_TEST + +MYTEST(libnet_init_good) +{ + mock_libnet_lo_ok = 0; // Don't even try falling back to lo. + do_libnet_init("good", 0); + fail_if(libnet == NULL); +} END_TEST + +MYTEST(libnet_init_null_nolo) +{ + mock_libnet_lo_ok = 0; + mock_libnet_null_ok = 1; + do_libnet_init(NULL, 0); + fail_if(libnet == NULL); +} END_TEST static Suite* arping_suite(void) @@ -505,6 +550,11 @@ arping_suite(void) tcase_add_test(tc_core, test_registry[c].fn); suite_add_tcase(s, tc_core); } + for (c = 0; c < num_exit_tests; c++) { + TCase *tc_core = tcase_create(test_exit_registry[c].name); + tcase_add_exit_test(tc_core, test_exit_registry[c].fn, 1); + suite_add_tcase(s, tc_core); + } return s; } |