Fabcoin Core  0.16.2
P2P Digital Currency
Client.cpp
Go to the documentation of this file.
1 
2 /*
3  This file is part of cpp-ethereum.
4 
5  cpp-ethereum is free software: you can redistribute it and/or modify
6  it under the terms of the GNU General Public License as published by
7  the Free Software Foundation, either version 3 of the License, or
8  (at your option) any later version.
9 
10  cpp-ethereum is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU General Public License for more details.
14 
15  You should have received a copy of the GNU General Public License
16  along with cpp-ethereum. If not, see <http://www.gnu.org/licenses/>.
17  */
23 #include <thread>
24 #include <boost/test/unit_test.hpp>
25 #include <boost/filesystem.hpp>
26 #include <jsonrpccpp/common/exception.h>
29 #include <libweb3jsonrpc/Eth.h>
31 #include <libdevcore/FileSystem.h>
32 #include <libethcore/KeyManager.h>
33 #include <libwebthree/WebThree.h>
34 #include <libp2p/Network.h>
36 
37 // This is defined by some weird windows header - workaround for now.
38 #undef GetMessage
39 
40 using namespace std;
41 using namespace dev;
42 using namespace dev::eth;
43 using namespace dev::p2p;
44 
45 namespace dev
46 {
47 namespace test
48 {
49 
50 BOOST_FIXTURE_TEST_SUITE(ClientTests, TestOutputHelper)
51 
53 {
54  TransientDirectory tempDir;
55  boost::filesystem::create_directories(tempDir.path() + "/keys");
56 
57  KeyManager keyManager(tempDir.path(), tempDir.path() + "/keys");
58  setDataDir(tempDir.path());
59 
61  WebThreeDirect::composeClientVersion("eth"),
62  getDataDir(),
63  ChainParams(),
64  WithExisting::Kill,
65  set<string>{"eth"},
67  );
68  web3.stopNetwork();
70 
71  bool userShouldEnterPassword = false;
72  string passwordUserWillEnter;
73 
75  [&](){ return web3.ethereum(); },
76  [&](Address) { if (!userShouldEnterPassword) BOOST_FAIL("Password input requested"); return passwordUserWillEnter; },
77  keyManager,
78  [](TransactionSkeleton const&, bool) -> bool {
79  return false; // user input goes here
80  }
81  );
82  rpc::Personal personal(keyManager, accountHolder, *web3.ethereum());
84 
85  // Create account
86 
87  string password = "12345";
88  string address = personal.personal_newAccount(password);
89 
90  // Try to send transaction
91 
92  Json::Value tx;
93  tx["from"] = address;
94  tx["to"] = string("0x0000000000000000000000000000000000000000");
95  tx["value"] = string("0x10000");
96  tx["value"] = string("0x5208");
97  auto sendingShouldFail = [&]() -> string
98  {
99  try
100  {
101  eth.eth_sendTransaction(tx);
102  BOOST_FAIL("Exception expected.");
103  }
104  catch (jsonrpc::JsonRpcException const& _e)
105  {
106  return _e.GetMessage();
107  }
108  return string();
109  };
110  auto sendingShouldSucceed = [&]()
111  {
112  BOOST_CHECK(!eth.eth_sendTransaction(tx).empty());
113  };
114 
115  BOOST_TEST_CHECKPOINT("Account is locked at the start.");
116  BOOST_CHECK_EQUAL(sendingShouldFail(), "Transaction rejected by user.");
117 
118  BOOST_TEST_CHECKPOINT("Unlocking without password should not work.");
119  BOOST_CHECK(!personal.personal_unlockAccount(address, string(), 2));
120  BOOST_CHECK_EQUAL(sendingShouldFail(), "Transaction rejected by user.");
121 
122  BOOST_TEST_CHECKPOINT("Unlocking with wrong password should not work.");
123  BOOST_CHECK(!personal.personal_unlockAccount(address, "abcd", 2));
124  BOOST_CHECK_EQUAL(sendingShouldFail(), "Transaction rejected by user.");
125 
126  BOOST_TEST_CHECKPOINT("Unlocking with correct password should work.");
127  BOOST_CHECK(personal.personal_unlockAccount(address, password, 2));
128  sendingShouldSucceed();
129  BOOST_TEST_CHECKPOINT("Transaction should be sendable multiple times in unlocked mode.");
130  sendingShouldSucceed();
131 
132  this_thread::sleep_for(chrono::seconds(2));
133  BOOST_TEST_CHECKPOINT("After unlock time, account should be locked again.");
134  BOOST_CHECK_EQUAL(sendingShouldFail(), "Transaction rejected by user.");
135 
136  BOOST_TEST_CHECKPOINT("Unlocking again with empty password should not work.");
137  BOOST_CHECK(!personal.personal_unlockAccount(address, string(), 2));
138  BOOST_CHECK_EQUAL(sendingShouldFail(), "Transaction rejected by user.");
139 }
140 
142 
143 }
144 }
void stopSealing() override
Stop sealing.
Definition: Client.h:161
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
void stopNetwork() override
Stop the network subsystem.
Definition: WebThree.h:207
unique_ptr< AccountHolder > accountHolder
Definition: shhrpc.cpp:64
eth::Client * ethereum() const
Definition: WebThree.h:140
h160 Address
An Ethereum address: 20 bytes.
Definition: Common.h:62
std::hash for asio::adress
Definition: Common.h:323
Main API hub for interfacing with Web 3 components.
Definition: WebThree.h:119
std::string getDataDir(std::string _prefix="ethereum")
Config::Value_type Value
High-level manager of password-encrypted keys for Ethereum.
Definition: KeyManager.h:72
void setDataDir(std::string const &_dir)
Sets the data dir for the default ("ethereum") prefix.
BOOST_AUTO_TEST_CASE(Personal)
Definition: Client.cpp:52
JSON-RPC api implementation.
Definition: Eth.h:64
WebThreeDirect * web3
Definition: shhrpc.cpp:54
#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
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
struct evm_uint160be address(struct evm_env *env)
Definition: capi.c:13
temporary directory implementation It creates temporary directory in the given path.
Helper functions to work with json::spirit and test files.
#define BOOST_CHECK(expr)
Definition: object.cpp:17