|
| 1 | +// Copyright (c) 2026 The Bitcoin Core developers |
| 2 | +// Distributed under the MIT software license, see the accompanying |
| 3 | +// file COPYING or http://www.opensource.org/licenses/mit-license.php. |
| 4 | + |
| 5 | +#include <qml/models/psbtoperationsadapter.h> |
| 6 | + |
| 7 | +#include <primitives/transaction.h> |
| 8 | +#include <script/script.h> |
| 9 | + |
| 10 | +#include <QtTest/QtTest> |
| 11 | + |
| 12 | +namespace { |
| 13 | +PartiallySignedTransaction BuildSamplePsbt() |
| 14 | +{ |
| 15 | + CMutableTransaction tx; |
| 16 | + tx.version = 2; |
| 17 | + tx.vin.emplace_back(COutPoint{Txid{}, 0}, CScript{}, 0xFFFFFFFFU); |
| 18 | + tx.vout.emplace_back(CAmount{25000}, CScript{}); |
| 19 | + return PartiallySignedTransaction{tx}; |
| 20 | +} |
| 21 | +} // namespace |
| 22 | + |
| 23 | +class PsbtOperationsAdapterTests : public QObject |
| 24 | +{ |
| 25 | + Q_OBJECT |
| 26 | + |
| 27 | +private Q_SLOTS: |
| 28 | + void decode_clipboard_requires_valid_base64() |
| 29 | + { |
| 30 | + const PsbtDecodeResult result = PsbtOperationsAdapter::DecodeClipboardBase64(QStringLiteral("not_base64_psbt")); |
| 31 | + QVERIFY(!result.success); |
| 32 | + QCOMPARE(result.message, QStringLiteral("Unable to decode PSBT from clipboard (invalid base64)")); |
| 33 | + QVERIFY(!result.psbt.has_value()); |
| 34 | + } |
| 35 | + |
| 36 | + void decode_clipboard_roundtrip_success() |
| 37 | + { |
| 38 | + const PartiallySignedTransaction source = BuildSamplePsbt(); |
| 39 | + const QString encoded = PsbtOperationsAdapter::EncodeBase64(source); |
| 40 | + |
| 41 | + const PsbtDecodeResult result = PsbtOperationsAdapter::DecodeClipboardBase64(encoded); |
| 42 | + QVERIFY(result.success); |
| 43 | + QVERIFY(result.psbt.has_value()); |
| 44 | + QCOMPARE(PsbtOperationsAdapter::EncodeBase64(*result.psbt), encoded); |
| 45 | + } |
| 46 | + |
| 47 | + void decode_file_binary_roundtrip_success() |
| 48 | + { |
| 49 | + const PartiallySignedTransaction source = BuildSamplePsbt(); |
| 50 | + const QByteArray raw = PsbtOperationsAdapter::EncodeRaw(source); |
| 51 | + |
| 52 | + const PsbtDecodeResult result = PsbtOperationsAdapter::DecodeFilePayload(raw); |
| 53 | + QVERIFY(result.success); |
| 54 | + QVERIFY(result.psbt.has_value()); |
| 55 | + QCOMPARE(PsbtOperationsAdapter::EncodeRaw(*result.psbt), raw); |
| 56 | + } |
| 57 | + |
| 58 | + void decode_file_base64_payload_success() |
| 59 | + { |
| 60 | + const PartiallySignedTransaction source = BuildSamplePsbt(); |
| 61 | + QByteArray payload = PsbtOperationsAdapter::EncodeBase64(source).toUtf8(); |
| 62 | + payload.append("\n\t"); |
| 63 | + |
| 64 | + const PsbtDecodeResult result = PsbtOperationsAdapter::DecodeFilePayload(payload); |
| 65 | + QVERIFY(result.success); |
| 66 | + QVERIFY(result.psbt.has_value()); |
| 67 | + QCOMPARE(PsbtOperationsAdapter::EncodeBase64(*result.psbt), PsbtOperationsAdapter::EncodeBase64(source)); |
| 68 | + } |
| 69 | + |
| 70 | + void decode_file_rejects_empty_payload() |
| 71 | + { |
| 72 | + const PsbtDecodeResult result = PsbtOperationsAdapter::DecodeFilePayload(QByteArray{}); |
| 73 | + QVERIFY(!result.success); |
| 74 | + QCOMPARE(result.message, QStringLiteral("PSBT file is empty.")); |
| 75 | + } |
| 76 | + |
| 77 | + void decode_file_rejects_large_payload() |
| 78 | + { |
| 79 | + const PsbtDecodeResult result = PsbtOperationsAdapter::DecodeFilePayload(QByteArrayLiteral("abc"), |
| 80 | + PsbtOperationsAdapter::MAX_PSBT_FILE_SIZE_BYTES + 1); |
| 81 | + QVERIFY(!result.success); |
| 82 | + QCOMPARE(result.message, QStringLiteral("PSBT file must be smaller than 100 MiB")); |
| 83 | + } |
| 84 | +}; |
| 85 | + |
| 86 | +int RunPsbtOperationsAdapterTests(int argc, char* argv[]) |
| 87 | +{ |
| 88 | + PsbtOperationsAdapterTests tests; |
| 89 | + return QTest::qExec(&tests, argc, argv); |
| 90 | +} |
| 91 | + |
| 92 | +#ifndef BITCOINQML_NO_TEST_MAIN |
| 93 | +QTEST_MAIN(PsbtOperationsAdapterTests) |
| 94 | +#endif |
| 95 | +#include "test_psbtoperationsadapter.moc" |
0 commit comments