mirror of
https://github.com/irungentoo/toxcore.git
synced 2024-03-22 13:30:51 +08:00
94b06818fb
This forces all the loop bodies to be executed at least once, which is harmless since it just means one more tox event loop iteration. This reduces the jitter we see in coverage measurements, which is partially caused by loops sometimes being entered and sometimes not (because their condition happens to randomly already be true).
63 lines
1.7 KiB
C
63 lines
1.7 KiB
C
/* Tests that we can send lossy packets.
|
|
*/
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
#include "config.h"
|
|
#endif
|
|
|
|
#include <stdio.h>
|
|
#include <stdlib.h>
|
|
#include <string.h>
|
|
#include <time.h>
|
|
|
|
#include "../testing/misc_tools.h"
|
|
#include "../toxcore/util.h"
|
|
#include "check_compat.h"
|
|
|
|
typedef struct State {
|
|
uint32_t index;
|
|
bool custom_packet_received;
|
|
} State;
|
|
|
|
#include "run_auto_test.h"
|
|
|
|
#define LOSSY_PACKET_FILLER 200
|
|
|
|
static void handle_lossy_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length, void *user_data)
|
|
{
|
|
uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE];
|
|
memset(cmp_packet, LOSSY_PACKET_FILLER, sizeof(cmp_packet));
|
|
|
|
if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) {
|
|
State *state = (State *)user_data;
|
|
state->custom_packet_received = true;
|
|
}
|
|
}
|
|
|
|
static void test_lossy_packet(Tox **toxes, State *state)
|
|
{
|
|
tox_callback_friend_lossy_packet(toxes[1], &handle_lossy_packet);
|
|
uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1];
|
|
memset(packet, LOSSY_PACKET_FILLER, sizeof(packet));
|
|
|
|
bool ret = tox_friend_send_lossy_packet(toxes[0], 0, packet, sizeof(packet), nullptr);
|
|
ck_assert_msg(ret == false, "should not be able to send custom packets this big %i", ret);
|
|
|
|
ret = tox_friend_send_lossy_packet(toxes[0], 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
|
|
ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret);
|
|
|
|
do {
|
|
tox_iterate(toxes[0], nullptr);
|
|
tox_iterate(toxes[1], &state[1]);
|
|
c_sleep(ITERATION_INTERVAL);
|
|
} while (!state[1].custom_packet_received);
|
|
}
|
|
|
|
int main(void)
|
|
{
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
|
|
|
run_auto_test(2, test_lossy_packet);
|
|
return 0;
|
|
}
|