2018-02-18 08:57:45 +08:00
|
|
|
/* Tests that we can send messages to friends.
|
|
|
|
*/
|
|
|
|
|
2018-07-04 22:42:02 +08:00
|
|
|
#include <stdbool.h>
|
|
|
|
#include <stdint.h>
|
2018-07-17 06:46:02 +08:00
|
|
|
#include <string.h>
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2018-07-04 22:42:02 +08:00
|
|
|
typedef struct State {
|
|
|
|
bool message_received;
|
|
|
|
} State;
|
2018-02-18 08:57:45 +08:00
|
|
|
|
2022-01-26 03:25:36 +08:00
|
|
|
#include "auto_test_support.h"
|
2018-02-18 08:57:45 +08:00
|
|
|
|
|
|
|
#define MESSAGE_FILLER 'G'
|
|
|
|
|
2018-07-04 22:42:02 +08:00
|
|
|
static void message_callback(
|
2018-10-09 05:05:14 +08:00
|
|
|
Tox *m, uint32_t friendnumber, Tox_Message_Type type,
|
2022-01-26 03:25:36 +08:00
|
|
|
const uint8_t *string, size_t length, void *user_data)
|
2018-02-18 08:57:45 +08:00
|
|
|
{
|
2022-01-26 03:25:36 +08:00
|
|
|
const AutoTox *autotox = (AutoTox *)user_data;
|
|
|
|
State *state = (State *)autotox->state;
|
2018-07-04 22:42:02 +08:00
|
|
|
|
2018-02-18 08:57:45 +08:00
|
|
|
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) {
|
2018-07-04 22:42:02 +08:00
|
|
|
state->message_received = true;
|
2018-02-18 08:57:45 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-01-26 03:25:36 +08:00
|
|
|
static void send_message_test(AutoTox *autotoxes)
|
2018-02-18 08:57:45 +08:00
|
|
|
{
|
2022-01-26 03:25:36 +08:00
|
|
|
tox_callback_friend_message(autotoxes[1].tox, &message_callback);
|
2018-02-18 08:57:45 +08:00
|
|
|
|
|
|
|
uint8_t msgs[TOX_MAX_MESSAGE_LENGTH + 1];
|
|
|
|
memset(msgs, MESSAGE_FILLER, sizeof(msgs));
|
|
|
|
|
2018-10-09 05:05:14 +08:00
|
|
|
Tox_Err_Friend_Send_Message errm;
|
2022-01-26 03:25:36 +08:00
|
|
|
tox_friend_send_message(autotoxes[0].tox, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH + 1, &errm);
|
2018-02-18 08:57:45 +08:00
|
|
|
ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_TOO_LONG, "TOX_MAX_MESSAGE_LENGTH is too small? error=%d", errm);
|
|
|
|
|
2022-01-26 03:25:36 +08:00
|
|
|
tox_friend_send_message(autotoxes[0].tox, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, TOX_MAX_MESSAGE_LENGTH, &errm);
|
2018-02-18 08:57:45 +08:00
|
|
|
ck_assert_msg(errm == TOX_ERR_FRIEND_SEND_MESSAGE_OK, "TOX_MAX_MESSAGE_LENGTH is too big? error=%d", errm);
|
|
|
|
|
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)->message_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, send_message_test, sizeof(State), &options);
|
|
|
|
|
2018-02-18 08:57:45 +08:00
|
|
|
return 0;
|
|
|
|
}
|