2018-02-18 08:57:45 +08:00
|
|
|
/* Tests that we can send lossless 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/ccompat.h"
|
|
|
|
#include "../toxcore/tox.h"
|
|
|
|
#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 {
|
|
|
|
uint32_t index;
|
2018-08-18 02:34:25 +08:00
|
|
|
uint64_t clock;
|
|
|
|
|
2018-07-26 07:19:05 +08:00
|
|
|
bool custom_packet_received;
|
|
|
|
} State;
|
|
|
|
|
|
|
|
#include "run_auto_test.h"
|
|
|
|
|
2018-02-18 08:57:45 +08:00
|
|
|
#define LOSSLESS_PACKET_FILLER 160
|
|
|
|
|
|
|
|
static void handle_lossless_packet(Tox *tox, uint32_t friend_number, const uint8_t *data, size_t length,
|
|
|
|
void *user_data)
|
|
|
|
{
|
2018-07-26 07:19:05 +08:00
|
|
|
State *state = (State *)user_data;
|
|
|
|
|
2018-02-18 08:57:45 +08:00
|
|
|
uint8_t cmp_packet[TOX_MAX_CUSTOM_PACKET_SIZE];
|
|
|
|
memset(cmp_packet, LOSSLESS_PACKET_FILLER, sizeof(cmp_packet));
|
|
|
|
|
|
|
|
if (length == TOX_MAX_CUSTOM_PACKET_SIZE && memcmp(data, cmp_packet, sizeof(cmp_packet)) == 0) {
|
2018-07-26 07:19:05 +08:00
|
|
|
state->custom_packet_received = true;
|
2018-02-18 08:57:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-07-26 07:19:05 +08:00
|
|
|
static void test_lossless_packet(Tox **toxes, State *state)
|
2018-02-18 08:57:45 +08:00
|
|
|
{
|
2018-07-26 07:19:05 +08:00
|
|
|
tox_callback_friend_lossless_packet(toxes[1], &handle_lossless_packet);
|
2018-02-18 08:57:45 +08:00
|
|
|
uint8_t packet[TOX_MAX_CUSTOM_PACKET_SIZE + 1];
|
|
|
|
memset(packet, LOSSLESS_PACKET_FILLER, sizeof(packet));
|
|
|
|
|
2018-07-26 07:19:05 +08:00
|
|
|
bool ret = tox_friend_send_lossless_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);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2018-07-26 07:19:05 +08:00
|
|
|
ret = tox_friend_send_lossless_packet(toxes[0], 0, packet, TOX_MAX_CUSTOM_PACKET_SIZE, nullptr);
|
|
|
|
ck_assert_msg(ret == true, "tox_friend_send_lossless_packet fail %i", ret);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2018-08-25 20:16:54 +08:00
|
|
|
do {
|
2018-08-18 02:34:25 +08:00
|
|
|
iterate_all_wait(2, toxes, state, ITERATION_INTERVAL);
|
2018-08-25 20:16:54 +08:00
|
|
|
} while (!state[1].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);
|
|
|
|
|
2018-07-25 15:43:48 +08:00
|
|
|
run_auto_test(2, test_lossless_packet, false);
|
2018-02-18 08:57:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|