Fabcoin Core  0.16.2
P2P Digital Currency
keymanager.cpp
Go to the documentation of this file.
1 /*
2  This file is part of cpp-ethereum.
3 
4  cpp-ethereum is free software: you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation, either version 3 of the License, or
7  (at your option) any later version.
8 
9  cpp-ethereum is distributed in the hope that it will be useful,
10  but WITHOUT ANY WARRANTY; without even the implied warranty of
11  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12  GNU General Public License for more details.
13 
14  You should have received a copy of the GNU General Public License
15  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
16 */
23 #include <boost/test/unit_test.hpp>
26 #include <libethcore/KeyManager.h>
27 
28 using namespace std;
29 using namespace dev;
30 using namespace dev::eth;
31 using namespace dev::test;
32 
34 
35 BOOST_AUTO_TEST_CASE(KeyInfoDefaultConstructor)
36 {
37  KeyInfo kiDefault;
38  BOOST_CHECK_EQUAL(kiDefault.accountName, "");
39  BOOST_CHECK(kiDefault.passHash == h256());
40 }
41 
42 BOOST_AUTO_TEST_CASE(KeyInfoConstructor)
43 {
44  h256 passHash("0x2a");
45  string accountName = "myAccount";
46  KeyInfo ki(passHash, accountName);
47  BOOST_CHECK_EQUAL(ki.accountName, "myAccount");
48  BOOST_CHECK(ki.passHash == h256("0x2a"));
49 }
50 
51 BOOST_AUTO_TEST_CASE(KeyManagerConstructor)
52 {
53  KeyManager km;
55  BOOST_CHECK_EQUAL(km.defaultPath(), getDataDir("ethereum") + "/keys.info");
56  BOOST_CHECK(km.store().keys() == SecretStore(SecretStore::defaultPath()).keys());
57  for (auto a: km.accounts())
58  km.kill(a);
59 }
60 
61 BOOST_AUTO_TEST_CASE(KeyManagerKeysFile)
62 {
63  KeyManager km;
64  string password = "hardPassword";
65  BOOST_CHECK(!km.load(password));
66 
67  // set to valid path
68  TransientDirectory tmpDir;
69  km.setKeysFile(tmpDir.path());
70  BOOST_CHECK(!km.exists());
71  BOOST_CHECK_THROW(km.create(password), boost::filesystem::filesystem_error);
72  km.setKeysFile(tmpDir.path() + "/notExistingDir/keysFile.json");
73  BOOST_CHECK_NO_THROW(km.create(password));
74  BOOST_CHECK(km.exists());
75  km.setKeysFile(tmpDir.path() + "keysFile.json");
76  BOOST_CHECK_NO_THROW(km.create(password));
77  km.save(password);
78  BOOST_CHECK(km.load(password));
79 
80  for (auto a: km.accounts())
81  km.kill(a);
82 }
83 
84 BOOST_AUTO_TEST_CASE(KeyManagerHints)
85 {
86  KeyManager km;
87  string password = "hardPassword";
88 
89  // set to valid path
90  TransientDirectory tmpDir;
91  km.setKeysFile(tmpDir.path() + "keysFile.json");
92  km.create(password);
93  km.save(password);
94 
95  BOOST_CHECK(!km.haveHint(password + "2"));
96  km.notePassword(password);
97  BOOST_CHECK(km.haveHint(password));
98 
99  for (auto a: km.accounts())
100  km.kill(a);
101 }
102 
103 BOOST_AUTO_TEST_CASE(KeyManagerAccounts)
104 {
105  string password = "hardPassword";
106 
107  TransientDirectory tmpDir;
108  KeyManager km(tmpDir.path()+ "keysFile.json", tmpDir.path());
109 
110  BOOST_CHECK_NO_THROW(km.create(password));
111  BOOST_CHECK(km.accounts().empty());
112  BOOST_CHECK(km.load(password));
113 
114  for (auto a: km.accounts())
115  km.kill(a);
116 }
117 
118 BOOST_AUTO_TEST_CASE(KeyManagerKill)
119 {
120  string password = "hardPassword";
121  TransientDirectory tmpDir;
122  KeyPair kp = KeyPair::create();
123 
124  {
125  KeyManager km(tmpDir.path() + "keysFile.json", tmpDir.path());
126  BOOST_CHECK_NO_THROW(km.create(password));
127  BOOST_CHECK(km.accounts().empty());
128  BOOST_CHECK(km.load(password));
129  BOOST_CHECK(km.import(kp.secret(), "test"));
130  }
131  {
132  KeyManager km(tmpDir.path() + "keysFile.json", tmpDir.path());
133  BOOST_CHECK(km.load(password));
134  Addresses addresses = km.accounts();
135  BOOST_CHECK(addresses.size() == 1 && addresses[0] == kp.address());
136  km.kill(addresses[0]);
137  }
138  {
139  KeyManager km(tmpDir.path() + "keysFile.json", tmpDir.path());
140  BOOST_CHECK(km.load(password));
141  BOOST_CHECK(km.accounts().empty());
142  }
143 }
144 
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
#define BOOST_CHECK_THROW(stmt, excMatch)
Definition: object.cpp:19
void notePassword(std::string const &_pass)
Definition: KeyManager.h:89
Simple class that represents a "key pair".
Definition: Common.h:150
std::vector< h128 > keys() const
Returns the uuids of all stored keys.
Definition: SecretStore.h:93
std::string const & keysFile() const
Definition: KeyManager.h:82
std::hash for asio::adress
Definition: Common.h:323
std::string getDataDir(std::string _prefix="ethereum")
Secret const & secret() const
Definition: Common.h:167
std::string accountName
Name of the key, or JSON key info if begins with &#39;{&#39;.
Definition: KeyManager.h:44
void setKeysFile(std::string const &_keysFile)
Definition: KeyManager.h:81
#define a(i)
High-level manager of password-encrypted keys for Ethereum.
Definition: KeyManager.h:72
void create(std::string const &_pass)
Definition: KeyManager.cpp:56
void kill(h128 const &_id)
Definition: KeyManager.h:133
static std::string defaultPath()
Definition: KeyManager.h:136
h160s Addresses
A vector of Ethereum addresses.
Definition: Common.h:68
void save(std::string const &_pass) const
Definition: KeyManager.h:87
Address const & address() const
Retrieve the associated address of the public key.
Definition: Common.h:173
h256 passHash
Hash of the password or h256() / UnknownPassword if unknown.
Definition: KeyManager.h:42
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
std::string const & path() const
#define BOOST_CHECK_EQUAL(v1, v2)
Definition: object.cpp:18
bool load(std::string const &_pass)
Definition: KeyManager.cpp:88
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
Manages encrypted keys stored in a certain directory on disk.
Definition: SecretStore.h:46
bool exists() const
Definition: KeyManager.cpp:51
#define BOOST_CHECK_NO_THROW(stmt)
Definition: object.cpp:28
bool haveHint(std::string const &_pass) const
Definition: KeyManager.h:91
Addresses accounts() const
Definition: KeyManager.cpp:328
temporary directory implementation It creates temporary directory in the given path.
SecretStore & store()
Definition: KeyManager.h:118
BOOST_AUTO_TEST_CASE(KeyInfoDefaultConstructor)
Definition: keymanager.cpp:35
Helper functions to work with json::spirit and test files.
#define BOOST_CHECK(expr)
Definition: object.cpp:17