diff options
author | iphydf <iphydf@users.noreply.github.com> | 2020-05-02 20:49:41 +0100 |
---|---|---|
committer | iphydf <iphydf@users.noreply.github.com> | 2020-05-02 21:47:08 +0100 |
commit | 2570ddcb17fdf5bea56c6bc1c5c2d04ba2068ee7 (patch) | |
tree | 621dd5a3953ad786650e50fdba2787009c78df95 /auto_tests | |
parent | e057bae563e133dbab7381ebbe1dc10f93d6eb4f (diff) |
Fix errors on error paths found by oomer.
* Use-after-free because we free network before dht in one case.
* Various unchecked allocs in tests (not so important).
* We used to not check whether ping arrays were actually allocated in DHT.
* `ping_kill` and `ping_array_kill` used to crash when passing NULL.
Also:
* Added an assert in all public API functions to ensure tox isn't NULL.
The error message you get from that is a bit nicer than "Segmentation
fault" when clients (or our tests) do things wrong.
* Decreased the sleep time in iterate_all_wait from 20ms to 5ms.
Everything seems to still work with 5ms, and this greatly decreases
the amount of time spent per test run, making oomer run much faster.
Diffstat (limited to 'auto_tests')
-rw-r--r-- | auto_tests/run_auto_test.h | 17 | ||||
-rw-r--r-- | auto_tests/tox_one_test.c | 5 |
2 files changed, 17 insertions, 5 deletions
diff --git a/auto_tests/run_auto_test.h b/auto_tests/run_auto_test.h index 47d9dc4a..8554a9be 100644 --- a/auto_tests/run_auto_test.h +++ b/auto_tests/run_auto_test.h | |||
@@ -39,7 +39,7 @@ static void iterate_all_wait(uint32_t tox_count, Tox **toxes, State *state, uint | |||
39 | } | 39 | } |
40 | 40 | ||
41 | /* Also actually sleep a little, to allow for local network processing */ | 41 | /* Also actually sleep a little, to allow for local network processing */ |
42 | c_sleep(20); | 42 | c_sleep(5); |
43 | } | 43 | } |
44 | 44 | ||
45 | static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data) | 45 | static uint64_t get_state_clock_callback(Mono_Time *mono_time, void *user_data) |
@@ -63,6 +63,9 @@ static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *stat | |||
63 | Tox **toxes = (Tox **)calloc(tox_count, sizeof(Tox *)); | 63 | Tox **toxes = (Tox **)calloc(tox_count, sizeof(Tox *)); |
64 | State *state = (State *)calloc(tox_count, sizeof(State)); | 64 | State *state = (State *)calloc(tox_count, sizeof(State)); |
65 | 65 | ||
66 | ck_assert(toxes != nullptr); | ||
67 | ck_assert(state != nullptr); | ||
68 | |||
66 | for (uint32_t i = 0; i < tox_count; i++) { | 69 | for (uint32_t i = 0; i < tox_count; i++) { |
67 | state[i].index = i; | 70 | state[i].index = i; |
68 | toxes[i] = tox_new_log(nullptr, nullptr, &state[i].index); | 71 | toxes[i] = tox_new_log(nullptr, nullptr, &state[i].index); |
@@ -82,7 +85,9 @@ static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *stat | |||
82 | 85 | ||
83 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; | 86 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; |
84 | tox_self_get_public_key(toxes[j], public_key); | 87 | tox_self_get_public_key(toxes[j], public_key); |
85 | tox_friend_add_norequest(toxes[i], public_key, nullptr); | 88 | Tox_Err_Friend_Add err; |
89 | tox_friend_add_norequest(toxes[i], public_key, &err); | ||
90 | ck_assert(err == TOX_ERR_FRIEND_ADD_OK); | ||
86 | } | 91 | } |
87 | } | 92 | } |
88 | } else { | 93 | } else { |
@@ -93,7 +98,9 @@ static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *stat | |||
93 | if (i != j) { | 98 | if (i != j) { |
94 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; | 99 | uint8_t public_key[TOX_PUBLIC_KEY_SIZE]; |
95 | tox_self_get_public_key(toxes[j], public_key); | 100 | tox_self_get_public_key(toxes[j], public_key); |
96 | tox_friend_add_norequest(toxes[i], public_key, nullptr); | 101 | Tox_Err_Friend_Add err; |
102 | tox_friend_add_norequest(toxes[i], public_key, &err); | ||
103 | ck_assert(err == TOX_ERR_FRIEND_ADD_OK); | ||
97 | } | 104 | } |
98 | } | 105 | } |
99 | } | 106 | } |
@@ -105,7 +112,9 @@ static void run_auto_test(uint32_t tox_count, void test(Tox **toxes, State *stat | |||
105 | const uint16_t dht_port = tox_self_get_udp_port(toxes[0], nullptr); | 112 | const uint16_t dht_port = tox_self_get_udp_port(toxes[0], nullptr); |
106 | 113 | ||
107 | for (uint32_t i = 1; i < tox_count; i++) { | 114 | for (uint32_t i = 1; i < tox_count; i++) { |
108 | tox_bootstrap(toxes[i], "localhost", dht_port, dht_key, nullptr); | 115 | Tox_Err_Bootstrap err; |
116 | tox_bootstrap(toxes[i], "localhost", dht_port, dht_key, &err); | ||
117 | ck_assert(err == TOX_ERR_BOOTSTRAP_OK); | ||
109 | } | 118 | } |
110 | 119 | ||
111 | do { | 120 | do { |
diff --git a/auto_tests/tox_one_test.c b/auto_tests/tox_one_test.c index 62486acb..2a280778 100644 --- a/auto_tests/tox_one_test.c +++ b/auto_tests/tox_one_test.c | |||
@@ -38,8 +38,10 @@ static void test_one(void) | |||
38 | 38 | ||
39 | uint32_t index[] = { 1, 2 }; | 39 | uint32_t index[] = { 1, 2 }; |
40 | Tox *tox1 = tox_new_log(nullptr, nullptr, &index[0]); | 40 | Tox *tox1 = tox_new_log(nullptr, nullptr, &index[0]); |
41 | ck_assert(tox1 != nullptr); | ||
41 | set_random_name_and_status_message(tox1, name, status_message); | 42 | set_random_name_and_status_message(tox1, name, status_message); |
42 | Tox *tox2 = tox_new_log(nullptr, nullptr, &index[1]); | 43 | Tox *tox2 = tox_new_log(nullptr, nullptr, &index[1]); |
44 | ck_assert(tox2 != nullptr); | ||
43 | set_random_name_and_status_message(tox2, name2, status_message2); | 45 | set_random_name_and_status_message(tox2, name2, status_message2); |
44 | 46 | ||
45 | uint8_t address[TOX_ADDRESS_SIZE]; | 47 | uint8_t address[TOX_ADDRESS_SIZE]; |
@@ -49,7 +51,7 @@ static void test_one(void) | |||
49 | ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_OWN_KEY, "Adding own address worked."); | 51 | ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_OWN_KEY, "Adding own address worked."); |
50 | 52 | ||
51 | tox_self_get_address(tox2, address); | 53 | tox_self_get_address(tox2, address); |
52 | uint8_t message[TOX_MAX_FRIEND_REQUEST_LENGTH + 1]; | 54 | uint8_t message[TOX_MAX_FRIEND_REQUEST_LENGTH + 1] = {0}; |
53 | ret = tox_friend_add(tox1, address, nullptr, 0, &error); | 55 | ret = tox_friend_add(tox1, address, nullptr, 0, &error); |
54 | ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_NULL, "Sending request with no message worked."); | 56 | ck_assert_msg(ret == UINT32_MAX && error == TOX_ERR_FRIEND_ADD_NULL, "Sending request with no message worked."); |
55 | ret = tox_friend_add(tox1, address, message, 0, &error); | 57 | ret = tox_friend_add(tox1, address, message, 0, &error); |
@@ -85,6 +87,7 @@ static void test_one(void) | |||
85 | Tox_Err_New err_n; | 87 | Tox_Err_New err_n; |
86 | 88 | ||
87 | struct Tox_Options *options = tox_options_new(nullptr); | 89 | struct Tox_Options *options = tox_options_new(nullptr); |
90 | ck_assert(options != nullptr); | ||
88 | tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); | 91 | tox_options_set_savedata_type(options, TOX_SAVEDATA_TYPE_TOX_SAVE); |
89 | tox_options_set_savedata_data(options, data, save_size); | 92 | tox_options_set_savedata_data(options, data, save_size); |
90 | tox2 = tox_new_log(options, &err_n, &index[1]); | 93 | tox2 = tox_new_log(options, &err_n, &index[1]); |