-
Notifications
You must be signed in to change notification settings - Fork 63
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Check for malformed signature and schnorrKey fields in verifyTransac…
…tion (#46) * Check for malformed signature and schnorrKey fields in verifyTransaction We don't catch Json::Exceptions in verifyTransaction so we must check if those fields are not strings otherwise you can cause a crash * Write unit test for rejecting a malformed signature field
- Loading branch information
1 parent
3defa04
commit 16b2d76
Showing
7 changed files
with
125 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
#include "BlockchainTests.h" | ||
|
||
#include "blockchain.h" | ||
#include "consensus/regtest.h" | ||
|
||
CPPUNIT_TEST_SUITE_REGISTRATION(BlockchainTest); | ||
|
||
BlockchainTest::BlockchainTest() { | ||
log.reset(new CryptoKernel::Log("tests.log")); | ||
} | ||
|
||
BlockchainTest::~BlockchainTest() { | ||
CryptoKernel::Storage::destroy("./testblockdb"); | ||
} | ||
|
||
void BlockchainTest::setUp() { | ||
std::remove("genesistest.json"); | ||
CryptoKernel::Storage::destroy("./testblockdb"); | ||
|
||
blockchain.reset(new testChain(log.get())); | ||
consensus.reset(new CryptoKernel::Consensus::Regtest(blockchain.get())); | ||
blockchain->loadChain(consensus.get(), "genesistest.json"); | ||
consensus->start(); | ||
} | ||
|
||
void BlockchainTest::tearDown() {} | ||
|
||
BlockchainTest::testChain::testChain(CryptoKernel::Log* GlobalLog) : CryptoKernel::Blockchain(GlobalLog, "./testblockdb") {} | ||
|
||
BlockchainTest::testChain::~testChain() {} | ||
|
||
std::string BlockchainTest::testChain::getCoinbaseOwner(const std::string& publicKey) { | ||
return publicKey; | ||
} | ||
|
||
uint64_t BlockchainTest::testChain::getBlockReward(const uint64_t height) { | ||
return 100000000; | ||
} | ||
|
||
void BlockchainTest::testVerifyMalformedSignature() { | ||
consensus->mineBlock(true, "BL2AcSzFw2+rGgQwJ25r7v/misIvr3t4JzkH3U1CCknchfkncSneKLBo6tjnKDhDxZUSPXEKMDtTU/YsvkwxJR8="); | ||
|
||
const auto block = blockchain->getBlockByHeight(2); | ||
|
||
const auto output = *block.getCoinbaseTx().getOutputs().begin(); | ||
|
||
CryptoKernel::Blockchain::output outp(output.getValue() - 20000, 0, Json::nullValue); | ||
|
||
Json::Value spendData; | ||
Json::Value randomData; | ||
randomData["this is"] = "malformed"; | ||
|
||
// Something malformed (not a string) | ||
spendData["signature"] = randomData; | ||
|
||
CryptoKernel::Blockchain::input inp(output.getId(), spendData); | ||
|
||
CryptoKernel::Blockchain::transaction tx({inp}, {outp}, 1530888581); | ||
|
||
const auto res = blockchain->submitTransaction(tx); | ||
|
||
CPPUNIT_ASSERT(!std::get<0>(res)); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
#ifndef BLOCKCHAINTEST_H | ||
#define BLOCKCHAINTEST_H | ||
|
||
#include <cppunit/extensions/HelperMacros.h> | ||
|
||
#include "blockchain.h" | ||
|
||
class BlockchainTest : public CPPUNIT_NS::TestFixture { | ||
CPPUNIT_TEST_SUITE(BlockchainTest); | ||
|
||
CPPUNIT_TEST(testVerifyMalformedSignature); | ||
|
||
CPPUNIT_TEST_SUITE_END(); | ||
|
||
public: | ||
BlockchainTest(); | ||
virtual ~BlockchainTest(); | ||
void setUp(); | ||
void tearDown(); | ||
|
||
private: | ||
class testChain : public CryptoKernel::Blockchain { | ||
public: | ||
testChain(CryptoKernel::Log* GlobalLog); | ||
virtual ~testChain(); | ||
private: | ||
virtual std::string getCoinbaseOwner(const std::string& publicKey); | ||
virtual uint64_t getBlockReward(const uint64_t height); | ||
}; | ||
|
||
|
||
void testVerifyMalformedSignature(); | ||
|
||
std::unique_ptr<CryptoKernel::Blockchain> blockchain; | ||
std::unique_ptr<CryptoKernel::Log> log; | ||
std::unique_ptr<CryptoKernel::Consensus::Regtest> consensus; | ||
|
||
}; | ||
|
||
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters