tomato-testing/toxcore/tox_events_test.cc
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

70 lines
2.1 KiB
C++

#include "tox_events.h"
#include <gtest/gtest.h>
#include <array>
#include <vector>
#include "crypto_core.h"
#include "tox_private.h"
namespace {
TEST(ToxEvents, UnpackRandomDataDoesntCrash)
{
const Tox_System sys = tox_default_system();
ASSERT_NE(sys.rng, nullptr);
std::array<uint8_t, 128> data;
random_bytes(sys.rng, data.data(), data.size());
tox_events_free(tox_events_load(&sys, data.data(), data.size()));
}
TEST(ToxEvents, UnpackEmptyDataFails)
{
const Tox_System sys = tox_default_system();
std::array<uint8_t, 1> data;
Tox_Events *events = tox_events_load(&sys, data.end(), 0);
EXPECT_EQ(events, nullptr);
}
TEST(ToxEvents, UnpackEmptyArrayCreatesEmptyEvents)
{
const Tox_System sys = tox_default_system();
std::array<uint8_t, 1> data{0x90}; // empty msgpack array
Tox_Events *events = tox_events_load(&sys, data.data(), data.size());
ASSERT_NE(events, nullptr);
EXPECT_EQ(tox_events_get_conference_connected_size(events), 0);
tox_events_free(events);
}
TEST(ToxEvents, NullEventsPacksToEmptyArray)
{
std::array<uint8_t, 1> bytes;
ASSERT_EQ(tox_events_bytes_size(nullptr), bytes.size());
tox_events_get_bytes(nullptr, bytes.data());
EXPECT_EQ(bytes, (std::array<uint8_t, 1>{0x90}));
}
TEST(ToxEvents, PackedEventsCanBeUnpacked)
{
const Tox_System sys = tox_default_system();
// [[1, 1]] == Tox_Self_Connection_Status { .connection_status = TOX_CONNECTION_TCP }
std::array<uint8_t, 6> packed{0x91, 0x92, 0xcc, 0x01, 0xcc, 0x01};
Tox_Events *events = tox_events_load(&sys, packed.data(), packed.size());
ASSERT_NE(events, nullptr);
std::array<uint8_t, 4> bytes;
ASSERT_EQ(tox_events_bytes_size(events), bytes.size());
tox_events_get_bytes(events, bytes.data());
EXPECT_EQ(bytes, (std::array<uint8_t, 4>{0x91, 0x92, 0x01, 0x01}));
tox_events_free(events);
}
TEST(ToxEvents, DealsWithHugeMsgpackArrays)
{
const Tox_System sys = tox_default_system();
std::vector<uint8_t> data{0xdd, 0xff, 0xff, 0xff, 0xff};
EXPECT_EQ(tox_events_load(&sys, data.data(), data.size()), nullptr);
}
} // namespace