Files
pay/testing/utils/TestUtils.cpp
T

113 lines
4.7 KiB
C++
Raw Permalink Normal View History

/*
* This file is part of the Flowee project
* Copyright (C) 2025 Tom Zander <tom@flowee.org>
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#include "TestUtils.h"
#include <FloweePay.h>
2025-10-28 14:46:56 +01:00
#include <SeedsBackup.h>
#include <streaming/BufferPools.h>
#include <qtest.h>
#include <qtestcase.h>
#include <QLocale>
void TestUtils::testAmountToString()
{
QCOMPARE(FloweePay::amountToString(12345678, FloweePay::BCH), "0.12345678");
QCOMPARE(FloweePay::amountToString(512345678, FloweePay::BCH), "5.12345678");
QCOMPARE(FloweePay::amountToString(12345678, FloweePay::MilliBCH), "123.45678");
QCOMPARE(FloweePay::amountToString(512345678, FloweePay::MilliBCH), "5,123.45678");
QCOMPARE(FloweePay::amountToString(12345678, FloweePay::MicroBCH), "123,456.78");
QCOMPARE(FloweePay::amountToString(512345678, FloweePay::MicroBCH), "5,123,456.78");
QCOMPARE(FloweePay::amountToString(12345678, FloweePay::Bits), "123,456.78");
QCOMPARE(FloweePay::amountToString(512345678, FloweePay::Bits), "5,123,456.78");
QCOMPARE(FloweePay::amountToString(12345678, FloweePay::Satoshis), "12,345,678");
QCOMPARE(FloweePay::amountToString(512345678, FloweePay::Satoshis), "512,345,678");
QCOMPARE(FloweePay::amountToString(1, FloweePay::Satoshis), "1");
QCOMPARE(FloweePay::amountToString(10, FloweePay::Satoshis), "10");
QCOMPARE(FloweePay::amountToString(100, FloweePay::Satoshis), "100");
QCOMPARE(FloweePay::amountToString(1000, FloweePay::Satoshis), "1,000");
QCOMPARE(FloweePay::amountToString(10000, FloweePay::Satoshis), "10,000");
QCOMPARE(FloweePay::amountToString(100000, FloweePay::Satoshis), "100,000");
QCOMPARE(FloweePay::amountToString(0, FloweePay::BCH), "0.00000000");
QCOMPARE(FloweePay::amountToString(0, FloweePay::MilliBCH), "0.00000");
QCOMPARE(FloweePay::amountToString(1, FloweePay::MilliBCH), "0.00001");
QCOMPARE(FloweePay::amountToString(10, FloweePay::MilliBCH), "0.00010");
QCOMPARE(FloweePay::amountToString(100, FloweePay::MilliBCH), "0.00100");
QCOMPARE(FloweePay::amountToString(1000, FloweePay::MilliBCH), "0.01000");
QCOMPARE(FloweePay::amountToString(10000, FloweePay::MilliBCH), "0.10000");
QCOMPARE(FloweePay::amountToString(100000, FloweePay::MilliBCH), "1.00000");
}
2025-10-28 14:46:56 +01:00
namespace {
class MockSeedsBackup : public SeedsBackup
{
public:
MockSeedsBackup() {}
QString name() { return m_name; }
void setName(const QString &name) { m_name = name; }
void setSeed(const std::vector<uint8_t> &seed) { m_seed = seed; }
std::vector<uint8_t> seed() const { return m_seed; }
void setLanguageId(int id) { m_language = id; }
int languageId() const { return m_language; }
QList<Path> paths() const { return m_paths; }
void setPaths(const QList<Path> &paths) { m_paths = paths; }
};
}
void TestUtils::testSeedsBackup()
{
MockSeedsBackup bc;
bc.setName("some Name");
std::vector<uint8_t> seed { 10, 56, 48, 241, 189, 12, 46, 45, 126, 43, 61, 174, 216, 51, 181, 255, 61 };
bc.setSeed(seed);
SeedsBackup::Path onePath;
onePath.path = "m/0'/5";
onePath.name = "Another name";
onePath.startingHeight = 622812;
bc.setPaths({ onePath });
auto backup = bc.toBuffer();
QVERIFY(!backup.isEmpty());
auto *restored = SeedsBackup::fromData(backup);
QVERIFY(restored);
MockSeedsBackup *mockRestored = static_cast<MockSeedsBackup*>(restored);
QCOMPARE(mockRestored->name(), bc.name());
QCOMPARE(mockRestored->languageId(), bc.languageId());
QCOMPARE(mockRestored->paths().size(), bc.paths().size());
auto p = mockRestored->paths().first();
QCOMPARE(p.name, onePath.name);
QCOMPARE(p.path, onePath.path);
QCOMPARE(p.startingHeight, onePath.startingHeight);
QCOMPARE(mockRestored->seed(), seed);
// check if it doesn't choke on the input.
for (int l = 0; l < backup.size(); ++l) {
auto *dummy = SeedsBackup::fromData(backup.mid(0, l));
QVERIFY(dummy == nullptr);
if (l > 0) {
dummy = SeedsBackup::fromData(backup.mid(l, -1));
QVERIFY(dummy == nullptr);
}
}
}
QTEST_MAIN(TestUtils)