2018-02-18 08:57:45 +08:00
|
|
|
/* Tests that we can send messages to friends.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _XOPEN_SOURCE
|
|
|
|
#define _XOPEN_SOURCE 600
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#ifdef HAVE_CONFIG_H
|
|
|
|
#include "config.h"
|
|
|
|
#endif
|
|
|
|
|
|
|
|
#include "check_compat.h"
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <time.h>
|
|
|
|
|
|
|
|
#include "../toxcore/ccompat.h"
|
|
|
|
#include "../toxcore/tox.h"
|
|
|
|
#include "../toxcore/util.h"
|
|
|
|
|
|
|
|
#include "helpers.h"
|
|
|
|
|
|
|
|
#define MESSAGE_FILLER 'G'
|
|
|
|
|
|
|
|
static void message_callback(Tox *m, uint32_t friendnumber, TOX_MESSAGE_TYPE type, const uint8_t *string, size_t length,
|
|
|
|
void *userdata)
|
|
|
|
{
|
|
|
|
if (type != TOX_MESSAGE_TYPE_NORMAL) {
|
|
|
|
ck_abort_msg("Bad type");
|
|
|
|
}
|
|
|
|
|
|
|
|
uint8_t cmp_msg[TOX_MAX_MESSAGE_LENGTH];
|
|
|
|
memset(cmp_msg, MESSAGE_FILLER, sizeof(cmp_msg));
|
|
|
|
|
|
|
|
if (length == TOX_MAX_MESSAGE_LENGTH && memcmp(string, cmp_msg, sizeof(cmp_msg)) == 0) {
|
|
|
|
bool *message_received = (bool *)userdata;
|
|
|
|
*message_received = true;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void test_send_message(void)
|
|
|
|
{
|
|
|
|
printf("initialising 2 toxes\n");
|
|
|
|
uint32_t index[] = { 1, 2 };
|
|
|
|
const time_t cur_time = time(nullptr);
|
|
|
|
Tox *const tox1 = tox_new_log(nullptr, nullptr, &index[0]);
|
|
|
|
Tox *const tox2 = tox_new_log(nullptr, nullptr, &index[1]);
|
|
|
|
|
|
|
|
ck_assert_msg(tox1 && tox2, "failed to create 2 tox instances");
|
|
|
|
|
|
|
|
printf("tox1 adds tox2 as friend, tox2 adds tox1\n");
|
|
|
|
uint8_t public_key[TOX_PUBLIC_KEY_SIZE];
|
|
|
|
tox_self_get_public_key(tox2, public_key);
|
|
|
|
tox_friend_add_norequest(tox1, public_key, nullptr);
|
|
|
|
tox_self_get_public_key(tox1, public_key);
|
|
|
|
tox_friend_add_norequest(tox2, public_key, nullptr);
|
|
|
|
|
|
|
|
printf("bootstrapping tox2 off tox1\n");
|
|
|
|
uint8_t dht_key[TOX_PUBLIC_KEY_SIZE];
|
|
|
|
tox_self_get_dht_id(tox1, dht_key);
|
|
|
|
const uint16_t dht_port = tox_self_get_udp_port(tox1, nullptr);
|
|
|
|
|
|
|
|
tox_bootstrap(tox2, "localhost", dht_port, dht_key, nullptr);
|
|
|
|
|
|
|
|
while (tox_self_get_connection_status(tox1) == TOX_CONNECTION_NONE ||
|
|
|
|
tox_self_get_connection_status(tox2) == TOX_CONNECTION_NONE) {
|
|
|
|
tox_iterate(tox1, nullptr);
|
|
|
|
tox_iterate(tox2, nullptr);
|
|
|
|
|
|
|
|
c_sleep(ITERATION_INTERVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("toxes are online, took %ld seconds\n", time(nullptr) - cur_time);
|
|
|
|
const time_t con_time = time(nullptr);
|
|
|
|
|
|
|
|
while (tox_friend_get_connection_status(tox1, 0, nullptr) != TOX_CONNECTION_UDP ||
|
|
|
|
tox_friend_get_connection_status(tox2, 0, nullptr) != TOX_CONNECTION_UDP) {
|
|
|
|
tox_iterate(tox1, nullptr);
|
|
|
|
tox_iterate(tox2, nullptr);
|
|
|
|
|
|
|
|
c_sleep(ITERATION_INTERVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("tox clients connected took %ld seconds\n", time(nullptr) - con_time);
|
|
|
|
|
|
|
|
tox_callback_friend_message(tox2, &message_callback);
|
|
|
|
|
|
|
|
uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1];
|
|
|
|
memset(msgs, MESSAGE_FILLER, sizeof(msgs));
|
|
|
|
|
|
|
|
TOX_ERR_FRIEND_SEND_MESSAGE errm;
|
|
|
|
tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm);
|
|
|
|
ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "TOX_MAX_MESSAGE_LENGTH is too small? error=%d", errm);
|
|
|
|
|
|
|
|
tox_friend_send_message(tox1, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm);
|
|
|
|
ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm);
|
|
|
|
|
|
|
|
bool message_received = false;
|
|
|
|
|
|
|
|
while (!message_received) {
|
|
|
|
tox_iterate(tox1, nullptr);
|
|
|
|
tox_iterate(tox2, &message_received);
|
|
|
|
|
|
|
|
c_sleep(ITERATION_INTERVAL);
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("tox clients messaging succeeded\n");
|
|
|
|
|
|
|
|
tox_kill(tox1);
|
|
|
|
tox_kill(tox2);
|
|
|
|
}
|
|
|
|
|
2018-02-23 10:22:38 +08:00
|
|
|
int main(void)
|
2018-02-18 08:57:45 +08:00
|
|
|
{
|
|
|
|
setvbuf(stdout, nullptr, _IONBF, 0);
|
|
|
|
|
|
|
|
test_send_message();
|
|
|
|
return 0;
|
|
|
|
}
|