From d351f9e5ec80ca4505f6e7a850076fb4bb302b7c Mon Sep 17 00:00:00 2001 From: Diadlo Date: Tue, 31 Jan 2017 00:06:42 +0300 Subject: [PATCH] test(toxpk): Add TestPk and TestId tests --- cmake/Testing.cmake | 8 +++- test/core/toxid_test.cpp | 91 +++++++++++++++++++++++++++++++++++++++ test/core/toxpk_test.cpp | 92 ++++++++++++++++++++++++++++++++++++++++ test/core/utils.h | 13 ++++++ 4 files changed, 203 insertions(+), 1 deletion(-) create mode 100644 test/core/toxid_test.cpp create mode 100644 test/core/toxpk_test.cpp create mode 100644 test/core/utils.h diff --git a/cmake/Testing.cmake b/cmake/Testing.cmake index c39f6b703..b30cb2253 100644 --- a/cmake/Testing.cmake +++ b/cmake/Testing.cmake @@ -14,9 +14,15 @@ function(auto_test subsystem module) test/${subsystem}/${module}_test.cpp) target_link_libraries(test_${module} ${PROJECT_NAME}_static - ${ALL_LIBRARIES} + ${CHECK_LIBRARIES} Qt5::Test) add_test( NAME test_${module} COMMAND test_${module}) endfunction() + +search_dependency(CHECK PACKAGE check OPTIONAL) +if (CHECK_FOUND) + auto_test(core toxpk) + auto_test(core toxid) +endif() diff --git a/test/core/toxid_test.cpp b/test/core/toxid_test.cpp new file mode 100644 index 000000000..24574084f --- /dev/null +++ b/test/core/toxid_test.cpp @@ -0,0 +1,91 @@ +#include "src/core/toxid.h" + +#include "test/common.h" + +#include +#include + +const QString corrupted = QStringLiteral("C7719C6808C14B77348004956D1D98046CE09A34370E7608150EAD74C3815D30C8BA3AB9BEBA"); +const QString testToxId = QStringLiteral("C7719C6808C14B77348004956D1D98046CE09A34370E7608150EAD74C3815D30C8BA3AB9BEB9"); +const QString publicKey = QStringLiteral("C7719C6808C14B77348004956D1D98046CE09A34370E7608150EAD74C3815D30"); +const QString echoToxId = QStringLiteral("76518406F6A9F2217E8DC487CC783C25CC16A15EB36FF32E335A235342C48A39218F515C39A6"); + +START_TEST(toStringTest) +{ + ToxId toxId(testToxId); + ck_assert(testToxId == toxId.toString()); +} +END_TEST + +START_TEST(equalTest) +{ + ToxId toxId1(testToxId); + ToxId toxId2(publicKey); + ck_assert(toxId1 == toxId2); + ck_assert(!(toxId1 != toxId2)); +} +END_TEST + +START_TEST(notEqualTest) +{ + ToxId toxId1(testToxId); + ToxId toxId2(echoToxId); + ck_assert(toxId1 != toxId2); +} +END_TEST + +START_TEST(clearTest) +{ + ToxId empty; + ToxId test(testToxId); + ck_assert(empty != test); + test.clear(); + ck_assert(empty == test); +} +END_TEST + +START_TEST(copyTest) +{ + ToxId src(testToxId); + ToxId copy = src; + ck_assert(copy == src); +} +END_TEST + +START_TEST(validationTest) +{ + ck_assert(ToxId(testToxId).isValid()); + ck_assert(!ToxId(publicKey).isValid()); + ck_assert(!ToxId(corrupted).isValid()); + QString deadbeef = "DEADBEEF"; + ck_assert(!ToxId(deadbeef).isValid()); +} +END_TEST + +static Suite *toxIdSuite(void) +{ + Suite *s = suite_create("ToxId"); + + DEFTESTCASE(toString); + DEFTESTCASE(equal); + DEFTESTCASE(notEqual); + DEFTESTCASE(clear); + DEFTESTCASE(copy); + DEFTESTCASE(validation); + + return s; +} + +int main(int argc, char *argv[]) +{ + srand((unsigned int) time(NULL)); + + Suite *toxId = toxIdSuite(); + SRunner *runner = srunner_create(toxId); + srunner_run_all(runner, CK_NORMAL); + + int res = srunner_ntests_failed(runner); + srunner_free(runner); + + return res; +} diff --git a/test/core/toxpk_test.cpp b/test/core/toxpk_test.cpp new file mode 100644 index 000000000..415e09d19 --- /dev/null +++ b/test/core/toxpk_test.cpp @@ -0,0 +1,92 @@ +#include "src/core/toxid.h" + +#include "test/common.h" + +#include +#include + +#include + +const uint8_t testPkArray[32] = { + 0xC7, 0x71, 0x9C, 0x68, 0x08, 0xC1, 0x4B, 0x77, 0x34, 0x80, 0x04, + 0x95, 0x6D, 0x1D, 0x98, 0x04, 0x6C, 0xE0, 0x9A, 0x34, 0x37, 0x0E, + 0x76, 0x08, 0x15, 0x0E, 0xAD, 0x74, 0xC3, 0x81, 0x5D, 0x30 +}; + +const QString testStr = QStringLiteral("C7719C6808C14B77348004956D1D98046CE09A34370E7608150EAD74C3815D30"); +const QByteArray testPk = QByteArray::fromHex(testStr.toLatin1()); + +const QString echoStr = QStringLiteral("76518406F6A9F2217E8DC487CC783C25CC16A15EB36FF32E335A235342C48A39"); +const QByteArray echoPk = QByteArray::fromHex(echoStr.toLatin1()); + +START_TEST(toStringTest) +{ + ToxPk pk(testPk); + ck_assert(testStr == pk.toString()); +} +END_TEST + +START_TEST(equalTest) +{ + ToxPk pk1(testPk); + ToxPk pk2(testPk); + ToxPk pk3(echoPk); + ck_assert(pk1 == pk2); + ck_assert(pk1 != pk3); + ck_assert(!(pk1 != pk2)); +} +END_TEST + +START_TEST(clearTest) +{ + ToxPk empty; + ToxPk pk(testPk); + ck_assert(empty.isEmpty()); + ck_assert(!pk.isEmpty()); +} +END_TEST + +START_TEST(copyTest) +{ + ToxPk src(testPk); + ToxPk copy = src; + ck_assert(copy == src); +} +END_TEST + +START_TEST(publicKeyTest) +{ + ToxPk pk(testPk); + ck_assert(testPk == pk.getKey()); + for (int i = 0; i < 32; i++) { + ck_assert(testPkArray[i] == pk.getBytes()[i]); + } +} +END_TEST + +static Suite *toxPkSuite(void) +{ + Suite *s = suite_create("ToxPk"); + + DEFTESTCASE(toString); + DEFTESTCASE(equal); + DEFTESTCASE(clear); + DEFTESTCASE(publicKey); + DEFTESTCASE(copy); + + return s; +} + +int main(int argc, char *argv[]) +{ + srand((unsigned int) time(NULL)); + + Suite *toxPk = toxPkSuite(); + SRunner *runner = srunner_create(toxPk); + srunner_run_all(runner, CK_NORMAL); + + int res = srunner_ntests_failed(runner); + srunner_free(runner); + + return res; +} diff --git a/test/core/utils.h b/test/core/utils.h new file mode 100644 index 000000000..f18d1192d --- /dev/null +++ b/test/core/utils.h @@ -0,0 +1,13 @@ +#ifndef _TEST_UTILS_H_ +#define _TEST_UTILS_H_ + +#define DEFTESTCASE(NAME) \ + TCase *tc_##NAME = tcase_create(#NAME); \ + tcase_add_test(tc_##NAME, NAME##Test); \ + suite_add_tcase(s, tc_##NAME); + +#define DEFTESTCASE_SLOW(NAME, TIMEOUT) \ + DEFTESTCASE(NAME) \ + tcase_set_timeout(tc_##NAME, TIMEOUT); + +#endif // _TEST_UTILS_H_