mirror of
https://github.com/qTox/qTox.git
synced 2024-03-22 14:00:36 +08:00
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.
This commit is contained in:
parent
813643adbf
commit
1206da2b05
|
@ -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);
|
||||
|
|
|
@ -18,7 +18,6 @@
|
|||
*/
|
||||
|
||||
#include "groupid.h"
|
||||
#include <tox/tox.h>
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
|
@ -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<const char*>(rawId), TOX_CONFERENCE_UID_SIZE))
|
||||
: ContactId(QByteArray(reinterpret_cast<const char*>(rawId), size))
|
||||
{
|
||||
}
|
||||
|
||||
|
@ -66,5 +65,5 @@ GroupId::GroupId(const uint8_t* rawId)
|
|||
*/
|
||||
int GroupId::getSize() const
|
||||
{
|
||||
return TOX_CONFERENCE_UID_SIZE;
|
||||
return size;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -22,22 +22,12 @@
|
|||
#include "toxid.h"
|
||||
#include "toxpk.h"
|
||||
|
||||
#include <tox/tox.h>
|
||||
|
||||
#include <QRegularExpression>
|
||||
|
||||
#include <cassert>
|
||||
#include <cstdint>
|
||||
|
||||
// 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];
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -20,15 +20,11 @@
|
|||
#include "contactid.h"
|
||||
#include "toxpk.h"
|
||||
|
||||
#include <tox/tox.h>
|
||||
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
#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<const char*>(rawId), TOX_PUBLIC_KEY_SIZE))
|
||||
: ContactId(QByteArray(reinterpret_cast<const char*>(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;
|
||||
}
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -22,8 +22,6 @@
|
|||
#include "src/core/toxid.h"
|
||||
#include "src/core/groupid.h"
|
||||
|
||||
#include <tox/tox.h>
|
||||
|
||||
#include <QtTest/QtTest>
|
||||
#include <QByteArray>
|
||||
#include <QString>
|
||||
|
@ -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()
|
||||
|
|
|
@ -21,8 +21,6 @@
|
|||
|
||||
#include "src/core/icoreidhandler.h"
|
||||
|
||||
#include <tox/tox.h>
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
|
|
|
@ -21,8 +21,6 @@
|
|||
|
||||
#include "src/core/icoregroupquery.h"
|
||||
|
||||
#include <tox/tox.h>
|
||||
|
||||
/**
|
||||
* 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<uint8_t>(peerId)};
|
||||
uint8_t id[ToxPk::size] = {static_cast<uint8_t>(peerId)};
|
||||
return ToxPk(id);
|
||||
}
|
||||
|
||||
|
|
|
@ -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);
|
||||
|
|
|
@ -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);
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in New Issue
Block a user