Browse Source

Merge pull request #2266 from CJentzsch/KeyManagerTests

Key manager tests
cl-refactor
Gav Wood 10 years ago
parent
commit
b342f54cbc
  1. 11
      ethkey/KeyAux.h
  2. 2
      libethcore/KeyManager.h
  3. 101
      test/libethcore/keymanager.cpp

11
ethkey/KeyAux.h

@ -198,7 +198,16 @@ public:
if (m_masterPassword.empty())
cerr << "Aborted (empty password not allowed)." << endl;
else
wallet.create(m_masterPassword);
{
try
{
wallet.create(m_masterPassword);
}
catch (Exception const& _e)
{
cerr << "unable to create wallet" << endl << boost::diagnostic_information(_e);
}
}
}
else if (m_mode < OperationMode::CreateWallet)
{

2
libethcore/KeyManager.h

@ -46,7 +46,7 @@ struct KeyInfo
static h256 const UnknownPassword;
/// Password query function that never returns a password.
static auto const DontKnowThrow = [](){ throw PasswordUnknown(); return std::string(); };
static auto const DontKnowThrow = [](){ BOOST_THROW_EXCEPTION(PasswordUnknown()); return std::string(); };
enum class SemanticPassword
{

101
test/libethcore/keymanager.cpp

@ -0,0 +1,101 @@
/*
This file is part of cpp-ethereum.
cpp-ethereum is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
cpp-ethereum is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
*/
/** @file keymanager.cpp
* @author Christoph Jentzsch <cj@ethdev.com>
* @date 2015
* Keymanager test functions.
*/
#include <boost/test/unit_test.hpp>
#include <test/TestHelper.h>
#include <libdevcore/TransientDirectory.h>
#include <libethcore/KeyManager.h>
using namespace std;
using namespace dev;
using namespace dev::eth;
BOOST_AUTO_TEST_SUITE(KeyManagerTests)
BOOST_AUTO_TEST_CASE(KeyInfoDefaultConstructor)
{
cnote << "KeyInfoDefaultConstructor";
KeyInfo kiDefault;
BOOST_CHECK_EQUAL(kiDefault.accountName, "");
BOOST_CHECK(kiDefault.passHash == h256());
}
BOOST_AUTO_TEST_CASE(KeyInfoConstructor)
{
cnote << "KeyInfoConstructor";
h256 passHash("0x2a");
string accountName = "myAccount";
KeyInfo ki(passHash, accountName);
BOOST_CHECK_EQUAL(ki.accountName, "myAccount");
BOOST_CHECK(ki.passHash == h256("0x2a"));
}
BOOST_AUTO_TEST_CASE(KeyManagerConstructor)
{
cnote << "KeyManagerConstructor";
KeyManager km;
BOOST_CHECK_EQUAL(km.keysFile(), km.defaultPath());
BOOST_CHECK_EQUAL(km.defaultPath(), getDataDir("ethereum") + "/keys.info");
BOOST_CHECK(km.store().keys() == SecretStore().keys());
}
BOOST_AUTO_TEST_CASE(KeyManagerKeysFile)
{
cnote << "KeyManagerKeysFile";
KeyManager km;
string password = "hardPassword";
BOOST_CHECK(!km.load(password));
// set to valid path
TransientDirectory tmpDir;
km.setKeysFile(tmpDir.path());
BOOST_CHECK(!km.exists());
BOOST_CHECK_THROW(km.create(password), FileError);
km.setKeysFile(tmpDir.path() + "/notExistingDir/keysFile.json");
BOOST_CHECK_THROW(km.create(password), FileError);
BOOST_CHECK(!km.exists());
km.setKeysFile(tmpDir.path() + "keysFile.json");
BOOST_CHECK_NO_THROW(km.create(password));
km.save(password);
BOOST_CHECK(km.load(password));
}
BOOST_AUTO_TEST_CASE(KeyManagerHints)
{
cnote << "KeyManagerHints";
KeyManager km;
string password = "hardPassword";
// set to valid path
TransientDirectory tmpDir;
km.setKeysFile(tmpDir.path() + "keysFile.json");
km.create(password);
km.save(password);
BOOST_CHECK(!km.haveHint(password + "2"));
km.notePassword(password);
BOOST_CHECK(km.haveHint(password));
}
BOOST_AUTO_TEST_SUITE_END()
Loading…
Cancel
Save