2018-02-18 08:57:45 +08:00
|
|
|
/* Tests that we can send lossy packets.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
2018-07-17 06:46:02 +08:00
|
|
|
#include <string.h>
|
2018-02-18 08:57:45 +08:00
|
|
|
#include <time.h>
|
|
|
|
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "../testing/misc_tools.h"
|
2018-02-18 08:57:45 +08:00
|
|
|
#include "../toxcore/util.h"
|
2018-07-17 06:46:02 +08:00
|
|
|
#include "check_compat.h"
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2018-07-26 07:19:05 +08:00
|
|
|
typedef struct State {
|
|
|
|
bool custom_packet_received;
|
|
|
|
} State;
|
|
|
|
|
2022-01-26 03:25:36 +08:00
|
|
|
#include "auto_test_support.h"
|
2018-07-26 07:19:05 +08:00
|
|
|
|
2018-02-18 08:57:45 +08:00
|
|
|
#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) {
|
2022-01-26 03:25:36 +08:00
|
|
|
const AutoTox *autotox = (AutoTox *)user_data;
|
|
|
|
State *state = (State *)autotox->state;
|
2018-07-26 07:19:05 +08:00
|
|
|
state->custom_packet_received = true;
|
2018-02-18 08:57:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 03:25:36 +08:00
|
|
|
static void test_lossy_packet(AutoTox *autotoxes)
|
2018-02-18 08:57:45 +08:00
|
|
|
{
|
2022-01-26 03:25:36 +08:00
|
|
|
tox_callback_friend_lossy_packet(autotoxes[1].tox, &handle_lossy_packet);
|
2018-02-18 08:57:45 +08:00
|
|
|
uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1];
|
|
|
|
memset(packet, LOSSY_PACKET_FILLER, sizeof(packet));
|
|
|
|
|
2022-01-26 03:25:36 +08:00
|
|
|
bool ret = tox_friend_send_lossy_packet(autotoxes[0].tox, 0, packet, sizeof(packet), nullptr);
|
2018-07-26 07:19:05 +08:00
|
|
|
ck_assert_msg(ret == false, "should not be able to send custom packets this big %i", ret);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2022-01-26 03:25:36 +08:00
|
|
|
ret = tox_friend_send_lossy_packet(autotoxes[0].tox, 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
|
2018-07-26 07:19:05 +08:00
|
|
|
ck_assert_msg(ret == true, "tox_friend_send_lossy_packet fail %i", ret);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2022-01-26 11:18:05 +08:00
|
|
|
iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL);
|
2022-01-26 03:25:36 +08:00
|
|
|
} while (!((State *)autotoxes[1].state)->custom_packet_received);
|
2018-02-18 08:57:45 +08:00
|
|
|
}
|
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
int main(void)
|
2018-02-18 08:57:45 +08:00
|
|
|
{
|
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
|
|
|
|
2022-01-26 03:25:36 +08:00
|
|
|
Run_Auto_Options options = default_run_auto_options;
|
|
|
|
options.graph = GRAPH_LINEAR;
|
|
|
|
|
|
|
|
run_auto_test(nullptr, 2, test_lossy_packet, sizeof(State), &options);
|
|
|
|
|
2018-02-18 08:57:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|