Skip to content

Commit 37ae687

Browse files
committed
Add tests for CPubKey serialization/unserialization
1 parent 9b8907f commit 37ae687

File tree

1 file changed

+44
-0
lines changed

1 file changed

+44
-0
lines changed

src/test/key_tests.cpp

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <key.h>
66

77
#include <key_io.h>
8+
#include <streams.h>
89
#include <test/util/setup_common.h>
910
#include <uint256.h>
1011
#include <util/strencodings.h>
@@ -220,4 +221,47 @@ BOOST_AUTO_TEST_CASE(key_key_negation)
220221
BOOST_CHECK(key.GetPubKey().data()[0] == 0x03);
221222
}
222223

224+
static CPubKey UnserializePubkey(const std::vector<uint8_t>& data)
225+
{
226+
CDataStream stream{SER_NETWORK, INIT_PROTO_VERSION};
227+
stream << data;
228+
CPubKey pubkey;
229+
stream >> pubkey;
230+
return pubkey;
231+
}
232+
233+
static unsigned int GetLen(unsigned char chHeader)
234+
{
235+
if (chHeader == 2 || chHeader == 3)
236+
return CPubKey::COMPRESSED_SIZE;
237+
if (chHeader == 4 || chHeader == 6 || chHeader == 7)
238+
return CPubKey::SIZE;
239+
return 0;
240+
}
241+
242+
static void CmpSerializationPubkey(const CPubKey& pubkey)
243+
{
244+
CDataStream stream{SER_NETWORK, INIT_PROTO_VERSION};
245+
stream << pubkey;
246+
CPubKey pubkey2;
247+
stream >> pubkey2;
248+
BOOST_CHECK(pubkey == pubkey2);
249+
}
250+
251+
BOOST_AUTO_TEST_CASE(pubkey_unserialize)
252+
{
253+
for (uint8_t i = 2; i <= 7; ++i) {
254+
CPubKey key = UnserializePubkey({0x02});
255+
BOOST_CHECK(!key.IsValid());
256+
CmpSerializationPubkey(key);
257+
key = UnserializePubkey(std::vector<uint8_t>(GetLen(i), i));
258+
CmpSerializationPubkey(key);
259+
if (i == 5) {
260+
BOOST_CHECK(!key.IsValid());
261+
} else {
262+
BOOST_CHECK(key.IsValid());
263+
}
264+
}
265+
}
266+
223267
BOOST_AUTO_TEST_SUITE_END()

0 commit comments

Comments
 (0)