Browse Source

Fix to correctly write nonce during tests.

cl-refactor
chriseth 10 years ago
parent
commit
b21b77d208
  1. 29
      libdevcrypto/Common.cpp
  2. 10
      libdevcrypto/Common.h
  3. 11
      test/TestUtils.cpp
  4. 9
      test/TestUtils.h
  5. 6
      test/libdevcrypto/SecretStore.cpp
  6. 5
      test/libdevcrypto/crypto.cpp

29
libdevcrypto/Common.cpp

@ -255,6 +255,7 @@ h256 crypto::kdf(Secret const& _priv, h256 const& _hash)
} }
mutex Nonce::s_x; mutex Nonce::s_x;
static string s_seedFile;
h256 Nonce::get() h256 Nonce::get()
{ {
@ -264,15 +265,23 @@ h256 Nonce::get()
return Nonce::singleton().next(); return Nonce::singleton().next();
} }
// get: Called for the first time: read seed hash from file (or generate) void Nonce::reset()
// before destruction: increment current value and store {
Guard l(Nonce::s_x);
Nonce::singleton().resetInternal();
}
void Nonce::setSeedFilePath(string const& _filePath)
{
s_seedFile = _filePath;
}
Nonce::~Nonce() Nonce::~Nonce()
{ {
Guard l(Nonce::s_x); Guard l(Nonce::s_x);
// These might throw. if (m_value)
next(); // this might throw
commit(); resetInternal();
} }
Nonce& Nonce::singleton() Nonce& Nonce::singleton()
@ -312,13 +321,17 @@ h256 Nonce::next()
return m_value; return m_value;
} }
void Nonce::commit() void Nonce::resetInternal()
{ {
// this might throw // this might throw
next();
writeFile(seedFile(), m_value.asBytes()); writeFile(seedFile(), m_value.asBytes());
m_value = h256();
} }
string Nonce::seedFile() string const& Nonce::seedFile()
{ {
return getDataDir() + "/seed"; if (s_seedFile.empty())
s_seedFile = getDataDir() + "/seed";
return s_seedFile;
} }

10
libdevcrypto/Common.h

@ -185,7 +185,13 @@ h256 kdf(Secret const& _priv, h256 const& _hash);
*/ */
struct Nonce struct Nonce
{ {
/// Returns the next nonce (might be read from a file).
static h256 get(); static h256 get();
/// Stores the current nonce in a file and resets Nonce to the uninitialised state.
static void reset();
/// Sets the location of the seed file to a non-default place. Used for testing.
static void setSeedFilePath(std::string const& _filePath);
private: private:
Nonce() {} Nonce() {}
~Nonce(); ~Nonce();
@ -196,9 +202,9 @@ private:
/// @returns the next nonce. /// @returns the next nonce.
h256 next(); h256 next();
/// Stores the current seed in the seed file. /// Stores the current seed in the seed file.
void commit(); void resetInternal();
/// @returns the path of the seed file. /// @returns the path of the seed file.
static std::string seedFile(); static std::string const& seedFile();
/// Mutex for the singleton object. /// Mutex for the singleton object.
/// @note Every access to any private function has to be guarded by this mutex. /// @note Every access to any private function has to be guarded by this mutex.

11
test/TestUtils.cpp

@ -22,6 +22,7 @@
#include <thread> #include <thread>
#include <boost/test/unit_test.hpp> #include <boost/test/unit_test.hpp>
#include <boost/filesystem.hpp> #include <boost/filesystem.hpp>
#include <libdevcrypto/Common.h>
#include <libtestutils/Common.h> #include <libtestutils/Common.h>
#include <libtestutils/BlockChainLoader.h> #include <libtestutils/BlockChainLoader.h>
#include <libtestutils/FixedClient.h> #include <libtestutils/FixedClient.h>
@ -116,3 +117,13 @@ void ParallelClientBaseFixture::enumerateClients(std::function<void(Json::Value
}); });
}); });
} }
MoveNonceToTempDir::MoveNonceToTempDir()
{
crypto::Nonce::setSeedFilePath(m_dir.path() + "/seed");
}
MoveNonceToTempDir::~MoveNonceToTempDir()
{
crypto::Nonce::reset();
}

9
test/TestUtils.h

@ -24,6 +24,7 @@
#include <functional> #include <functional>
#include <string> #include <string>
#include <json/json.h> #include <json/json.h>
#include <libdevcore/TransientDirectory.h>
#include <libethereum/BlockChain.h> #include <libethereum/BlockChain.h>
#include <libethereum/ClientBase.h> #include <libethereum/ClientBase.h>
@ -78,5 +79,13 @@ struct JsonRpcFixture: public ClientBaseFixture
}; };
struct MoveNonceToTempDir
{
MoveNonceToTempDir();
~MoveNonceToTempDir();
private:
TransientDirectory m_dir;
};
} }
} }

6
test/libdevcrypto/SecretStore.cpp

@ -29,12 +29,16 @@
#include <libdevcore/TrieDB.h> #include <libdevcore/TrieDB.h>
#include <libdevcore/TrieHash.h> #include <libdevcore/TrieHash.h>
#include "MemTrie.h" #include "MemTrie.h"
#include "../TestHelper.h" #include <test/TestHelper.h>
#include <test/TestUtils.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::test;
namespace js = json_spirit; namespace js = json_spirit;
BOOST_GLOBAL_FIXTURE( MoveNonceToTempDir );
BOOST_AUTO_TEST_SUITE(KeyStore) BOOST_AUTO_TEST_SUITE(KeyStore)
BOOST_AUTO_TEST_CASE(basic_tests) BOOST_AUTO_TEST_CASE(basic_tests)

5
test/libdevcrypto/crypto.cpp

@ -31,12 +31,16 @@
#include <libdevcore/SHA3.h> #include <libdevcore/SHA3.h>
#include <libdevcrypto/ECDHE.h> #include <libdevcrypto/ECDHE.h>
#include <libdevcrypto/CryptoPP.h> #include <libdevcrypto/CryptoPP.h>
#include <test/TestUtils.h>
using namespace std; using namespace std;
using namespace dev; using namespace dev;
using namespace dev::test;
using namespace dev::crypto; using namespace dev::crypto;
using namespace CryptoPP; using namespace CryptoPP;
BOOST_GLOBAL_FIXTURE( MoveNonceToTempDir );
BOOST_AUTO_TEST_SUITE(devcrypto) BOOST_AUTO_TEST_SUITE(devcrypto)
static Secp256k1 s_secp256k1; static Secp256k1 s_secp256k1;
@ -45,6 +49,7 @@ static CryptoPP::OID s_curveOID(CryptoPP::ASN1::secp256k1());
static CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP> s_params(s_curveOID); static CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP> s_params(s_curveOID);
static CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP>::EllipticCurve s_curve(s_params.GetCurve()); static CryptoPP::DL_GroupParameters_EC<CryptoPP::ECP>::EllipticCurve s_curve(s_params.GetCurve());
BOOST_AUTO_TEST_CASE(sha3general) BOOST_AUTO_TEST_CASE(sha3general)
{ {
BOOST_REQUIRE_EQUAL(sha3(""), h256("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470")); BOOST_REQUIRE_EQUAL(sha3(""), h256("c5d2460186f7233c927e7db2dcc703c0e500b653ca82273b7bfad8045d85a470"));

Loading…
Cancel
Save