tomato/auto_tests/send_message_test.c
Green Sky b2ae9530a4 Squashed 'external/toxcore/c-toxcore/' changes from e29e185c03..f1df709b87
f1df709b87 feat: add ngc events
1b6c907235 refactor: Make event dispatch ordered by receive time.
b7f9367f6f test: Upgrade cppcheck, fix some warnings.
766e62bc89 chore: Use `pkg_search_module` directly in cmake.
00ff078f91 cleanup: Use target_link_libraries directly in cmake.
c58928cc89 chore: Add `IMPORTED_TARGET` to pkg-config packages.
895a6af122 cleanup: Remove NaCl support.
41dfb1c1c0 fix: unpack enum function names in event impl generator
447666d1a1 chore: Disable targets for cross-compilation.
572924e924 chore: Build a docker image with coverage info in it.
415cb78f5e cleanup: Some portability/warning fixes for Windows builds.
425216d9ec fix: Correct a use-after-free and fix some memory leaks.
4b1cfa3e08 refactor: Change all enum-like `#define` sequences into enums.
d3c2704fa9 chore: Fix make_single_file to support core-only.
0ce46b644e refactor: Change the `TCP_PACKET_*` defines into an enum.
22cd38ad50 adopt event impl generation tool to #2392
f31ea1088a add the event impl generation tool
4e603bb613 refactor: Use `enum-from-int` rule from tokstyle.
19d8f180d6 chore: Update github actions `uses`.
6a895be0c7 test: Make esp32 build actually try to instantiate tox.
65d09c9bfb cleanup: Remove test net support.
REVERT: e29e185c03 feat: add ngc events

git-subtree-dir: external/toxcore/c-toxcore
git-subtree-split: f1df709b8792da4c0e946d826b11df77d565064d
2023-12-27 12:37:22 +01:00

81 lines
2.4 KiB
C

/* Tests that we can send messages to friends.
*/
#include <stdbool.h>
#include <stdint.h>
#include <string.h>
typedef struct State {
bool message_received;
} State;
#include "auto_test_support.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 *user_data)
{
const AutoTox *autotox = (AutoTox *)user_data;
State *state = (State *)autotox->state;
if (type != TOX_MESSAGE_TYPE_NORMAL) {
ck_abort_msg("Bad type");
}
const size_t cmp_msg_len = tox_max_message_length();
uint8_t *cmp_msg = (uint8_t *)malloc(cmp_msg_len);
ck_assert(cmp_msg != nullptr);
memset(cmp_msg, MESSAGE_FILLER, cmp_msg_len);
if (length == tox_max_message_length() && memcmp(string, cmp_msg, cmp_msg_len) == 0) {
state->message_received = true;
}
free(cmp_msg);
}
static void send_message_test(AutoTox *autotoxes)
{
tox_callback_friend_message(autotoxes[1].tox, &message_callback);
const size_t msgs_len = tox_max_message_length() + 1;
uint8_t *msgs = (uint8_t *)malloc(msgs_len);
ck_assert(msgs != nullptr);
memset(msgs, MESSAGE_FILLER, msgs_len);
Tox_Err_Friend_Send_Message errm;
tox_friend_send_message(autotoxes[0].tox, 0, TOX_MESSAGE_TYPE_NORMAL, msgs, msgs_len, &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(autotoxes[0].tox, 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);
free(msgs);
do {
iterate_all_wait(autotoxes, 2, ITERATION_INTERVAL);
} while (!((State *)autotoxes[1].state)->message_received);
}
int main(void)
{
setvbuf(stdout, nullptr, _IONBF, 0);
struct Tox_Options *tox_options = tox_options_new(nullptr);
ck_assert(tox_options != nullptr);
Run_Auto_Options options = default_run_auto_options();
options.graph = GRAPH_LINEAR;
tox_options_set_ipv6_enabled(tox_options, true);
run_auto_test(tox_options, 2, send_message_test, sizeof(State), &options);
tox_options_set_ipv6_enabled(tox_options, false);
run_auto_test(tox_options, 2, send_message_test, sizeof(State), &options);
tox_options_free(tox_options);
return 0;
}