Fabcoin Core  0.16.2
P2P Digital Currency
whisperMessage.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 */
22 #include <boost/test/unit_test.hpp>
23 #include <libwhisper/Message.h>
25 
26 using namespace std;
27 using namespace dev;
28 using namespace dev::test;
29 using namespace dev::shh;
30 
31 Topics createRandomTopics(unsigned int i)
32 {
33  Topics ret;
34  h256 t(i);
35 
36  for (int j = 0; j < 8; ++j)
37  {
38  t = sha3(t);
39  ret.push_back(t);
40  }
41 
42  return ret;
43 }
44 
45 bytes createRandomPayload(unsigned int i)
46 {
47  bytes ret;
48  srand(i);
49  int const sz = rand() % 1024;
50  for (int j = 0; j < sz; ++j)
51  ret.push_back(rand() % 256);
52 
53  return ret;
54 }
55 
56 void comparePayloads(Message const& m1, Message const& m2)
57 {
58  bytes const& p1 = m1.payload();
59  bytes const& p2 = m2.payload();
60  BOOST_REQUIRE_EQUAL(p1.size(), p2.size());
61 
62  for (size_t i = 0; i < p1.size(); ++i)
63  BOOST_REQUIRE_EQUAL(p1[i], p2[i]);
64 }
65 
66 void sealAndOpenSingleMessage(unsigned int i)
67 {
68  Secret zero;
69  Topics topics = createRandomTopics(i);
70  bytes const payload = createRandomPayload(i);
71  Message m1(payload);
72  Envelope e = m1.seal(zero, topics, 1, 1);
73 
74  for (auto const& t: topics)
75  {
76  Topics singleTopic;
77  singleTopic.push_back(t);
78  Message m2(e, singleTopic, zero);
79  comparePayloads(m1, m2);
80  }
81 }
82 
84 
85 //
86 // Disabled tests as they are unstable and tend to stall the test suite.
87 //
88 
89 //BOOST_AUTO_TEST_CASE(seal)
90 //{
91 // VerbosityHolder setTemporaryLevel(10);
92 
93 // cnote << "Testing Envelope encryption...";
94 
95 // for (unsigned int i = 1; i < 10; ++i)
96 // sealAndOpenSingleMessage(i);
97 //}
98 
99 //BOOST_AUTO_TEST_CASE(work)
100 //{
101 // VerbosityHolder setTemporaryLevel(10);
102 // cnote << "Testing proof of work...";
103 
104 // Secret zero;
105 // unsigned r = 0xC0DEFEED;
106 
107 // for (int i = 0; i < 20; ++i)
108 // {
109 // Topics topics = createRandomTopics(++r);
110 // bytes const payload = createRandomPayload(++r);
111 // Message m(payload);
112 // Envelope e = m.seal(zero, topics, 1, 50);
113 // unsigned x = e.workProved();
114 // //cnote << x;
115 // BOOST_REQUIRE(x > 4);
116 // }
117 //}
118 
void sealAndOpenSingleMessage(unsigned int i)
Adapted from code found on http://stackoverflow.com/questions/180947/base64-decode-snippet-in-c Origi...
Definition: Arith256.cpp:15
bytes createRandomPayload(unsigned int i)
Topics createRandomTopics(unsigned int i)
h256s Topics
Definition: Common.h:69
std::hash for asio::adress
Definition: Common.h:323
Definition: Eth.h:45
bytes const & payload() const
Definition: Message.h:111
std::vector< byte > bytes
Definition: Common.h:75
#define BOOST_FIXTURE_TEST_SUITE(a, b)
Definition: object.cpp:14
An (unencrypted) message, constructed from the combination of an Envelope, and, potentially, a Secret key to decrypt the Message.
Definition: Message.h:100
bool sha3(bytesConstRef _input, bytesRef o_output)
Calculate SHA3-256 hash of the given input and load it into the given output.
Definition: SHA3.cpp:214
#define BOOST_AUTO_TEST_SUITE_END()
Definition: object.cpp:16
#define e(i)
Definition: sha.cpp:733
Envelope seal(Secret const &_from, Topics const &_topics, unsigned _ttl=50, unsigned _workToProve=50) const
Turn this message into a ditributable Envelope.
Definition: Message.cpp:100
Helper functions to work with json::spirit and test files.
void comparePayloads(Message const &m1, Message const &m2)