-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathmnemonic_tests.cpp
139 lines (101 loc) · 4.91 KB
/
mnemonic_tests.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
// Copyright (c) 2017-2020 The Particl Core developers
// Distributed under the MIT software license, see the accompanying
// file COPYING or http://www.opensource.org/licenses/mit-license.php.
#include <test/util/setup_common.h>
#include <test/data/bip39_vectors_english.json.h>
#include <test/data/bip39_vectors_japanese.json.h>
#include <key/mnemonic.h>
#include <key/extkey.h>
#include <key_io.h>
#include <util/system.h>
#include <util/strencodings.h>
#include <string>
#include <univalue.h>
#include <boost/test/unit_test.hpp>
extern UniValue read_json(const std::string& jsondata);
BOOST_FIXTURE_TEST_SUITE(mnemonic_tests, BasicTestingSetup)
BOOST_AUTO_TEST_CASE(mnemonic_test)
{
std::string words = "deer clever bitter bonus unable menu satoshi chaos dwarf inmate robot drama exist nuclear raise";
std::string expect_seed = "1da563986981b82c17a76160934f4b532eac77e14b632c6adcf31ba4166913e063ce158164c512cdce0672cbc9256dd81e7be23a8d8eb331de1a497493c382b1";
std::vector<uint8_t> vSeed;
std::string password = "";
BOOST_CHECK(0 == mnemonic::ToSeed(words, password, vSeed));
BOOST_CHECK(HexStr(vSeed) == expect_seed);
}
BOOST_AUTO_TEST_CASE(mnemonic_test_fails)
{
// Fail tests
int nLanguage = -1;
std::string sError;
std::vector<uint8_t> vEntropy;
std::string sWords = "legals winner thank year wave sausage worth useful legal winner thank yellow";
BOOST_CHECK_MESSAGE(3 == mnemonic::Decode(nLanguage, sWords, vEntropy, sError), "MnemonicDecode: " << sError);
sWords = "winner legal thank year wave sausage worth useful legal winner thank yellow";
BOOST_CHECK_MESSAGE(5 == mnemonic::Decode(nLanguage, sWords, vEntropy, sError), "MnemonicDecode: " << sError);
}
BOOST_AUTO_TEST_CASE(mnemonic_addchecksum)
{
std::string sError;
std::string sWordsIn = "abandon baby cabbage dad eager fabric gadget habit ice kangaroo lab";
std::string sWordsOut;
BOOST_CHECK_MESSAGE(0 == mnemonic::AddChecksum(-1, sWordsIn, sWordsOut, sError), "MnemonicAddChecksum: " << sError);
BOOST_CHECK_MESSAGE(sWordsOut == "abandon baby cabbage dad eager fabric gadget habit ice kangaroo lab absorb", "sWordsOut: " << sWordsOut);
// Must fail, len % 3 != 0
std::string sWordsInFail = "abandon baby cabbage dad eager fabric gadget habit ice kangaroo";
BOOST_CHECK_MESSAGE(4 == mnemonic::AddChecksum(-1, sWordsInFail, sWordsOut, sError), "MnemonicAddChecksum: " << sError);
std::string sWordsInFrench = "zoologie ficeler xénon voyelle village viande vignette sécréter séduire torpille remède";
BOOST_CHECK(0 == mnemonic::AddChecksum(-1, sWordsInFrench, sWordsOut, sError));
BOOST_CHECK(sWordsOut == "zoologie ficeler xénon voyelle village viande vignette sécréter séduire torpille remède abolir");
}
static void runTests(int nLanguage, UniValue &tests)
{
std::string sError;
for (unsigned int idx = 0; idx < tests.size(); idx++) {
const UniValue &test = tests[idx];
assert(test.size() > 2);
std::string sEntropy = test[0].get_str();
std::string sWords = test[1].get_str();
std::string sSeed;
std::string sPassphrase;
if (test.size() > 3) {
sPassphrase = test[2].get_str();
sSeed = test[3].get_str();
} else {
sPassphrase = "TREZOR";
sSeed = test[2].get_str();
}
std::vector<uint8_t> vEntropy = ParseHex(sEntropy);
std::vector<uint8_t> vEntropyTest;
std::string sWordsTest;
BOOST_CHECK_MESSAGE(0 == mnemonic::Encode(nLanguage, vEntropy, sWordsTest, sError), "MnemonicEncode: " << sError);
BOOST_CHECK(sWords == sWordsTest);
int nLanguage = -1;
BOOST_CHECK_MESSAGE(0 == mnemonic::Decode(nLanguage, sWords, vEntropyTest, sError), "MnemonicDecode: " << sError);
BOOST_CHECK(vEntropy == vEntropyTest);
std::vector<uint8_t> vSeed = ParseHex(sSeed);
std::vector<uint8_t> vSeedTest;
BOOST_CHECK(0 == mnemonic::ToSeed(sWords, sPassphrase, vSeedTest));
BOOST_CHECK(vSeed == vSeedTest);
if (test.size() > 4) {
CExtKey58 eKey58;
std::string sExtKey = test[4].get_str();
CExtKey ekTest;
ekTest.SetSeed(&vSeed[0], vSeed.size());
eKey58.SetKey(CExtKeyPair(ekTest), CChainParams::EXT_SECRET_KEY_BTC);
BOOST_CHECK(eKey58.ToString() == sExtKey);
}
}
}
BOOST_AUTO_TEST_CASE(mnemonic_test_json)
{
UniValue tests_english = read_json(
std::string(json_tests::bip39_vectors_english,
json_tests::bip39_vectors_english + sizeof(json_tests::bip39_vectors_english)));
runTests(mnemonic::WLL_ENGLISH, tests_english);
UniValue tests_japanese = read_json(
std::string(json_tests::bip39_vectors_japanese,
json_tests::bip39_vectors_japanese + sizeof(json_tests::bip39_vectors_japanese)));
runTests(mnemonic::WLL_JAPANESE, tests_japanese);
}
BOOST_AUTO_TEST_SUITE_END()