From 1206da2b0575bce98c731d8f952aafca8d982a5a Mon Sep 17 00:00:00 2001 From: Anthony Bilinski Date: Sun, 13 Mar 2022 04:59:40 -0700 Subject: [PATCH] refactor(ToxPk): Don't define model's ToxPk, ToxId, GroupId from tox.h Since they exist in saved settings and the chat database, they are their own size. assert that it matches tox.h's size in Core constructor, though. --- src/core/core.cpp | 4 +++ src/core/groupid.cpp | 11 +++---- src/core/groupid.h | 1 + src/core/toxid.cpp | 38 ++++++++-------------- src/core/toxid.h | 7 ++++ src/core/toxpk.cpp | 16 ++++----- src/core/toxpk.h | 2 ++ test/core/contactid_test.cpp | 6 ++-- test/mock/include/mock/mockcoreidhandler.h | 4 +-- test/mock/include/mock/mockgroupquery.h | 4 +-- test/model/groupmessagedispatcher_test.cpp | 6 ++-- test/model/sessionchatlog_test.cpp | 2 +- 12 files changed, 47 insertions(+), 54 deletions(-) diff --git a/src/core/core.cpp b/src/core/core.cpp index ea549c131..015793f5b 100644 --- a/src/core/core.cpp +++ b/src/core/core.cpp @@ -509,6 +509,10 @@ Core::Core(QThread* coreThread_, IBootstrapListGenerator& bootstrapListGenerator , settings(settings_) { assert(toxTimer); + // need to migrate Settings and History if this changes + assert(ToxPk::size == tox_public_key_size()); + assert(GroupId::size == tox_conference_id_size()); + assert(ToxId::size == tox_address_size()); toxTimer->setSingleShot(true); connect(toxTimer, &QTimer::timeout, this, &Core::process); connect(coreThread_, &QThread::finished, toxTimer, &QTimer::stop); diff --git a/src/core/groupid.cpp b/src/core/groupid.cpp index f0d511b5e..90a3c07cb 100644 --- a/src/core/groupid.cpp +++ b/src/core/groupid.cpp @@ -18,7 +18,6 @@ */ #include "groupid.h" -#include #include #include @@ -41,11 +40,11 @@ GroupId::GroupId() /** * @brief Constructs a GroupId from bytes. * @param rawId The bytes to construct the GroupId from. The lenght must be exactly - * TOX_CONFERENCE_UID_SIZE, else the GroupId will be empty. + * GroupId::size, else the GroupId will be empty. */ GroupId::GroupId(const QByteArray& rawId) : ContactId([rawId](){ - assert(rawId.length() == TOX_CONFERENCE_UID_SIZE); + assert(rawId.length() == size); return rawId;}()) { } @@ -53,10 +52,10 @@ GroupId::GroupId(const QByteArray& rawId) /** * @brief Constructs a GroupId from bytes. * @param rawId The bytes to construct the GroupId from, will read exactly - * TOX_CONFERENCE_UID_SIZE from the specified buffer. + * GroupId::size from the specified buffer. */ GroupId::GroupId(const uint8_t* rawId) - : ContactId(QByteArray(reinterpret_cast(rawId), TOX_CONFERENCE_UID_SIZE)) + : ContactId(QByteArray(reinterpret_cast(rawId), size)) { } @@ -66,5 +65,5 @@ GroupId::GroupId(const uint8_t* rawId) */ int GroupId::getSize() const { - return TOX_CONFERENCE_UID_SIZE; + return size; } diff --git a/src/core/groupid.h b/src/core/groupid.h index 98e7bb1dd..af2338af4 100644 --- a/src/core/groupid.h +++ b/src/core/groupid.h @@ -26,6 +26,7 @@ class GroupId : public ContactId { public: + static constexpr int size = 32; GroupId(); explicit GroupId(const QByteArray& rawId); explicit GroupId(const uint8_t* rawId); diff --git a/src/core/toxid.cpp b/src/core/toxid.cpp index b1adacf66..93210f0c0 100644 --- a/src/core/toxid.cpp +++ b/src/core/toxid.cpp @@ -22,22 +22,12 @@ #include "toxid.h" #include "toxpk.h" -#include - #include #include #include -// Tox doesn't publicly define these -#define NOSPAM_BYTES 4 -#define CHECKSUM_BYTES 2 - -#define NOSPAM_HEX_CHARS (2 * NOSPAM_BYTES) -#define CHECKSUM_HEX_CHARS (2 * CHECKSUM_BYTES) -#define TOXID_HEX_CHARS (2 * TOX_ADDRESS_SIZE) - -const QRegularExpression ToxId::ToxIdRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(TOXID_HEX_CHARS)); +const QRegularExpression ToxId::ToxIdRegEx(QString("(^|\\s)[A-Fa-f0-9]{%1}($|\\s)").arg(ToxId::numHexChars)); /** * @class ToxId @@ -112,8 +102,8 @@ ToxId::ToxId(const QByteArray& rawId) * If the given rawId isn't a valid Public Key or Tox ID a ToxId with all zero bytes is created. * * @param rawId Pointer to bytes to convert to ToxId object - * @param len Number of bytes to read. Must be TOX_PUBLIC_KEY_SIZE for a Public Key or - * TOX_ADDRESS_SIZE for a Tox ID. + * @param len Number of bytes to read. Must be ToxPk::size for a Public Key or + * ToxId::size for a Tox ID. */ ToxId::ToxId(const uint8_t* rawId, int len) { @@ -124,7 +114,7 @@ ToxId::ToxId(const uint8_t* rawId, int len) void ToxId::constructToxId(const QByteArray& rawId) { - if (rawId.length() == TOX_ADDRESS_SIZE && isToxId(rawId.toHex().toUpper())) { + if (rawId.length() == ToxId::size && isToxId(rawId.toHex().toUpper())) { toxId = QByteArray(rawId); // construct from full toxid } else { assert(!"ToxId constructed with invalid input"); @@ -189,7 +179,7 @@ const uint8_t* ToxId::getBytes() const */ ToxPk ToxId::getPublicKey() const { - auto const pkBytes = toxId.left(TOX_PUBLIC_KEY_SIZE); + auto const pkBytes = toxId.left(ToxPk::size); if (pkBytes.isEmpty()) { return ToxPk{}; } else { @@ -203,8 +193,8 @@ ToxPk ToxId::getPublicKey() const */ QString ToxId::getNoSpamString() const { - if (toxId.length() == TOX_ADDRESS_SIZE) { - return toxId.mid(TOX_PUBLIC_KEY_SIZE, NOSPAM_BYTES).toHex().toUpper(); + if (toxId.length() == ToxId::size) { + return toxId.mid(ToxPk::size, ToxId::nospamSize).toHex().toUpper(); } return {}; @@ -229,7 +219,7 @@ bool ToxId::isValidToxId(const QString& id) */ bool ToxId::isToxId(const QString& id) { - return id.length() == TOXID_HEX_CHARS && id.contains(ToxIdRegEx); + return id.length() == ToxId::numHexChars && id.contains(ToxIdRegEx); } /** @@ -238,17 +228,17 @@ bool ToxId::isToxId(const QString& id) */ bool ToxId::isValid() const { - if (toxId.length() != TOX_ADDRESS_SIZE) { + if (toxId.length() != ToxId::size) { return false; } - const int size = TOX_PUBLIC_KEY_SIZE + NOSPAM_BYTES; + const int pkAndChecksum = ToxPk::size + ToxId::nospamSize; - QByteArray data = toxId.left(size); - QByteArray checksum = toxId.right(CHECKSUM_BYTES); - QByteArray calculated(CHECKSUM_BYTES, 0x00); + QByteArray data = toxId.left(pkAndChecksum); + QByteArray checksum = toxId.right(ToxId::checksumSize); + QByteArray calculated(ToxId::checksumSize, 0x00); - for (int i = 0; i < size; i++) { + for (int i = 0; i < pkAndChecksum; i++) { calculated[i % 2] = calculated[i % 2] ^ data[i]; } diff --git a/src/core/toxid.h b/src/core/toxid.h index 4972b5f1d..8f21f599b 100644 --- a/src/core/toxid.h +++ b/src/core/toxid.h @@ -29,6 +29,13 @@ class ToxId { public: + static constexpr int nospamSize = 4; + static constexpr int nospamNumHexChars = nospamSize*2; + static constexpr int checksumSize = 2; + static constexpr int checksumNumHexChars = checksumSize*2; + static constexpr int size = 38; + static constexpr int numHexChars = size*2; + ToxId(); ToxId(const ToxId& other); explicit ToxId(const QString& id); diff --git a/src/core/toxpk.cpp b/src/core/toxpk.cpp index 1e47d85a6..afbfb52c3 100644 --- a/src/core/toxpk.cpp +++ b/src/core/toxpk.cpp @@ -20,15 +20,11 @@ #include "contactid.h" #include "toxpk.h" -#include - #include #include #include -#define PUBLIC_KEY_HEX_CHARS (2 * TOX_PUBLIC_KEY_SIZE) - /** * @class ToxPk * @brief This class represents a Tox Public Key, which is a part of Tox ID. @@ -45,11 +41,11 @@ ToxPk::ToxPk() /** * @brief Constructs a ToxPk from bytes. * @param rawId The bytes to construct the ToxPk from. The lenght must be exactly - * TOX_PUBLIC_KEY_SIZE, else the ToxPk will be empty. + * ToxPk::size, else the ToxPk will be empty. */ ToxPk::ToxPk(const QByteArray& rawId) : ContactId([&rawId](){ - assert(rawId.length() == TOX_PUBLIC_KEY_SIZE); + assert(rawId.length() == size); return rawId;}()) { } @@ -57,10 +53,10 @@ ToxPk::ToxPk(const QByteArray& rawId) /** * @brief Constructs a ToxPk from bytes. * @param rawId The bytes to construct the ToxPk from, will read exactly - * TOX_PUBLIC_KEY_SIZE from the specified buffer. + * ToxPk::size from the specified buffer. */ ToxPk::ToxPk(const uint8_t* rawId) - : ContactId(QByteArray(reinterpret_cast(rawId), TOX_PUBLIC_KEY_SIZE)) + : ContactId(QByteArray(reinterpret_cast(rawId), size)) { } @@ -73,7 +69,7 @@ ToxPk::ToxPk(const uint8_t* rawId) */ ToxPk::ToxPk(const QString& pk) : ContactId([&pk](){ - if (pk.length() == PUBLIC_KEY_HEX_CHARS) { + if (pk.length() == numHexChars) { return QByteArray::fromHex(pk.toLatin1()); } else { assert(!"ToxPk constructed with invalid length string"); @@ -89,5 +85,5 @@ ToxPk::ToxPk(const QString& pk) */ int ToxPk::getSize() const { - return TOX_PUBLIC_KEY_SIZE; + return size; } diff --git a/src/core/toxpk.h b/src/core/toxpk.h index 8c6f24588..7c3082177 100644 --- a/src/core/toxpk.h +++ b/src/core/toxpk.h @@ -26,6 +26,8 @@ class ToxPk : public ContactId { public: + static constexpr int size = 32; + static constexpr int numHexChars = 64; ToxPk(); explicit ToxPk(const QByteArray& rawId); explicit ToxPk(const uint8_t* rawId); diff --git a/test/core/contactid_test.cpp b/test/core/contactid_test.cpp index b93dd5aeb..edc45aa95 100644 --- a/test/core/contactid_test.cpp +++ b/test/core/contactid_test.cpp @@ -22,8 +22,6 @@ #include "src/core/toxid.h" #include "src/core/groupid.h" -#include - #include #include #include @@ -97,8 +95,8 @@ void TestContactId::sizeTest() { ToxPk pk; GroupId id; - QVERIFY(pk.getSize() == TOX_PUBLIC_KEY_SIZE); - QVERIFY(id.getSize() == TOX_CONFERENCE_UID_SIZE); + QVERIFY(pk.getSize() == ToxPk::size); + QVERIFY(id.getSize() == GroupId::size); } void TestContactId::hashableTest() diff --git a/test/mock/include/mock/mockcoreidhandler.h b/test/mock/include/mock/mockcoreidhandler.h index 477d84a47..5d6d922fa 100644 --- a/test/mock/include/mock/mockcoreidhandler.h +++ b/test/mock/include/mock/mockcoreidhandler.h @@ -21,8 +21,6 @@ #include "src/core/icoreidhandler.h" -#include - class MockCoreIdHandler : public ICoreIdHandler { public: @@ -41,7 +39,7 @@ public: ToxPk getSelfPublicKey() const override { - static uint8_t id[TOX_PUBLIC_KEY_SIZE] = {0}; + static uint8_t id[ToxPk::size] = {0}; return ToxPk(id); } diff --git a/test/mock/include/mock/mockgroupquery.h b/test/mock/include/mock/mockgroupquery.h index c29c6d2e6..6841e2dd1 100644 --- a/test/mock/include/mock/mockgroupquery.h +++ b/test/mock/include/mock/mockgroupquery.h @@ -21,8 +21,6 @@ #include "src/core/icoregroupquery.h" -#include - /** * Mock 1 peer at group number 0 */ @@ -61,7 +59,7 @@ public: ToxPk getGroupPeerPk(int groupId, int peerId) const override { std::ignore = groupId; - uint8_t id[TOX_PUBLIC_KEY_SIZE] = {static_cast(peerId)}; + uint8_t id[ToxPk::size] = {static_cast(peerId)}; return ToxPk(id); } diff --git a/test/model/groupmessagedispatcher_test.cpp b/test/model/groupmessagedispatcher_test.cpp index 12ce0f7ea..16ca28d99 100644 --- a/test/model/groupmessagedispatcher_test.cpp +++ b/test/model/groupmessagedispatcher_test.cpp @@ -229,11 +229,11 @@ void TestGroupMessageDispatcher::testEmptyGroup() */ void TestGroupMessageDispatcher::testSelfReceive() { - uint8_t selfId[TOX_PUBLIC_KEY_SIZE] = {0}; + uint8_t selfId[ToxPk::size] = {0}; groupMessageDispatcher->onMessageReceived(ToxPk(selfId), false, "Test"); QVERIFY(receivedMessages.size() == 0); - uint8_t id[TOX_PUBLIC_KEY_SIZE] = {1}; + uint8_t id[ToxPk::size] = {1}; groupMessageDispatcher->onMessageReceived(ToxPk(id), false, "Test"); QVERIFY(receivedMessages.size() == 1); } @@ -243,7 +243,7 @@ void TestGroupMessageDispatcher::testSelfReceive() */ void TestGroupMessageDispatcher::testBlacklist() { - uint8_t id[TOX_PUBLIC_KEY_SIZE] = {1}; + uint8_t id[ToxPk::size] = {1}; auto otherPk = ToxPk(id); groupMessageDispatcher->onMessageReceived(otherPk, false, "Test"); QVERIFY(receivedMessages.size() == 1); diff --git a/test/model/sessionchatlog_test.cpp b/test/model/sessionchatlog_test.cpp index 23b8e2642..28eefa7dc 100644 --- a/test/model/sessionchatlog_test.cpp +++ b/test/model/sessionchatlog_test.cpp @@ -46,7 +46,7 @@ public: ToxPk getSelfPublicKey() const override { - static uint8_t id[TOX_PUBLIC_KEY_SIZE] = {5}; + static uint8_t id[ToxPk::size] = {5}; return ToxPk(id); }