rippled
PreimageSha256_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2016 Ripple Labs Inc.
5 
6  Permission to use, copy, modify, and/or distribute this software for any
7  purpose with or without fee is hereby granted, provided that the above
8  copyright notice and this permission notice appear in all copies.
9 
10  THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11  WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12  MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13  ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14  WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15  ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16  OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17 */
18 //==============================================================================
19 
20 #include <ripple/basics/Buffer.h>
21 #include <ripple/basics/strHex.h>
22 #include <ripple/basics/Slice.h>
23 #include <ripple/beast/unit_test.h>
24 #include <ripple/conditions/Condition.h>
25 #include <ripple/conditions/Fulfillment.h>
26 #include <ripple/conditions/impl/PreimageSha256.h>
27 #include <algorithm>
28 #include <string>
29 #include <utility>
30 #include <vector>
31 
32 namespace ripple {
33 namespace cryptoconditions {
34 
35 class PreimageSha256_test : public beast::unit_test::suite
36 {
37  inline
38  Buffer
39  hexblob(std::string const& s)
40  {
42  x.reserve(s.size() / 2);
43 
44  auto iter = s.cbegin();
45 
46  while (iter != s.cend())
47  {
48  int cHigh = charUnHex(*iter++);
49 
50  if (cHigh < 0)
51  return {};
52 
53  int cLow = charUnHex(*iter++);
54 
55  if (cLow < 0)
56  return {};
57 
58  x.push_back(
59  static_cast<std::uint8_t>(cHigh << 4) |
60  static_cast<std::uint8_t>(cLow));
61  }
62 
63  return { x.data(), x.size() };
64  }
65 
66  void
68  {
69  testcase("Known Vectors");
70 
72  {
73  { "A0028000",
74  "A0258020E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7852B855810100" },
75  { "A0058003616161",
76  "A02580209834876DCFB05CB167A5C24953EBA58C4AC89B1ADF57F28F2F9D09AF107EE8F0810103" },
77  };
78 
79  std::error_code ec;
80 
81  auto f1 = Fulfillment::deserialize (hexblob(known[0].first), ec);
82  BEAST_EXPECT (f1);
83  BEAST_EXPECT (!ec);
84 
85  auto c1 = Condition::deserialize (hexblob(known[0].second), ec);
86  BEAST_EXPECT (c1);
87  BEAST_EXPECT (!ec);
88 
89  auto f2 = Fulfillment::deserialize(hexblob(known[1].first), ec);
90  BEAST_EXPECT(f2);
91  BEAST_EXPECT(!ec);
92 
93  auto c2 = Condition::deserialize(hexblob(known[1].second), ec);
94  BEAST_EXPECT(c2);
95  BEAST_EXPECT(!ec);
96 
97  // Check equality and inequality
98  BEAST_EXPECT (f1->condition() == *c1);
99  BEAST_EXPECT (f1->condition() != *c2);
100  BEAST_EXPECT (f2->condition() == *c2);
101  BEAST_EXPECT (f2->condition() != *c1);
102  BEAST_EXPECT (*c1 != *c2);
103  BEAST_EXPECT (*c1 == *c1);
104  BEAST_EXPECT (f1->condition() == f1->condition());
105 
106  // Should validate with the empty string
107  BEAST_EXPECT (validate (*f1, *c1));
108  BEAST_EXPECT (validate(*f2, *c2));
109 
110  // And with any string - the message doesn't matter for PrefixSha256
111  BEAST_EXPECT (validate (*f1, *c1, makeSlice(known[0].first)));
112  BEAST_EXPECT (validate(*f1, *c1, makeSlice(known[0].second)));
113  BEAST_EXPECT (validate(*f2, *c2, makeSlice(known[0].first)));
114  BEAST_EXPECT (validate(*f2, *c2, makeSlice(known[0].second)));
115 
116  // Shouldn't validate if the fulfillment & condition don't match
117  // regardless of the message.
118  BEAST_EXPECT (! validate(*f2, *c1));
119  BEAST_EXPECT (! validate(*f2, *c1, makeSlice(known[0].first)));
120  BEAST_EXPECT (! validate(*f2, *c1, makeSlice(known[0].second)));
121  BEAST_EXPECT (! validate(*f1, *c2));
122  BEAST_EXPECT (! validate(*f1, *c2, makeSlice(known[0].first)));
123  BEAST_EXPECT (! validate(*f1, *c2, makeSlice(known[0].second)));
124  }
125 
127  {
128  testcase ("Other Types");
129 
130  std::pair<std::string, std::string> const others[] =
131  {
132  // PREFIX + PREIMAGE:
133  { "A10B8000810100A204A0028000",
134 
135  "A12A8020BB1AC5260C0141B7E54B26EC2330637C5597BF811951AC09E744AD20FF77E287810204"
136  "0082020780" },
137 
138  // THRESHOLD:
139  { "A208A004A0028000A100",
140 
141  "A22A8020B4B84136DF48A71D73F4985C04C6767A778ECB65BA7023B4506823BEEE7631B9810204"
142  "0082020780" },
143 
144  // RSA:
145  { "A382020880820100E1EF8B24D6F76B09C81ED7752AA262F044F04A874D43809D31CEA612F99B0C97"
146  "A8B4374153E3EEF3D66616843E0E41C293264B71B6173DB1CF0D6CD558C58657706FCF097F704C48"
147  "3E59CBFDFD5B3EE7BC80D740C5E0F047F3E85FC0D75815776A6F3F23C5DC5E797139A6882E38336A"
148  "4A5FB36137620FF3663DBAE328472801862F72F2F87B202B9C89ADD7CD5B0A076F7C53E35039F67E"
149  "D17EC815E5B4305CC63197068D5E6E579BA6DE5F4E3E57DF5E4E072FF2CE4C66EB45233973875275"
150  "9639F0257BF57DBD5C443FB5158CCE0A3D36ADC7BA01F33A0BB6DBB2BF989D607112F2344D993E77"
151  "E563C1D361DEDF57DA96EF2CFC685F002B638246A5B309B981820100BD42D6569F6599AED455F96B"
152  "C0ED08ED1480BF36CD9E1467F9C6F74461C9E3A749334B2F6404AA5F9F6BAFE76C347D069250B35D"
153  "1C970C793059EE733A8193F30FA78FEC7CAE459E3DDFD7633805D476940D0CB53D7FB389DCDAEAF6"
154  "E8CF48C4B5635430E4F2BCDFE505C2C0FC17B40D93C7EDB7C261EBF43895A705E024AA0549A660F7"
155  "0A32150647522DBE6B63520497CFF8F8D5D74768A27C5B86E580BE3FCDC96F1976293CBA0D58DFC6"
156  "0B518B632A6DC1E950C43E231FE1A379AA6DDCC52C70EDF851C6C0123A964261CFDB3857CD6CD5AD"
157  "C37D8DA2CC924EDAE1D84CF6124587F274C1FA3697DA2901F0269F03B243C03B614E0385E1961FAC"
158  "5000F9BB",
159 
160  "A32580204849505152535455484950515253545548495051525354554849505152535455810101" },
161 
162  // ED25519:
163  { "A4648020D75A980182B10AB7D54BFED3C964073A0EE172F3DAA62325AF021A68F707511A8140E556"
164  "4300C360AC729086E2CC806E828A84877F1EB8E5D974D873E065224901555FB8821590A33BACC61E"
165  "39701CF9B46BD25BF5F0595BBE24655141438E7A100B",
166 
167  "A4278020799239ABA8FC4FF7EABFBC4C44E69E8BDFED993324E12ED64792ABE289CF1D5F810302"
168  "0000" }
169  };
170 
171  for (auto x : others)
172  {
173  std::error_code ec;
174 
175  BEAST_EXPECT (!Fulfillment::deserialize(hexblob(x.first), ec));
176  BEAST_EXPECT (!Condition::deserialize (hexblob(x.second), ec));
177  }
178 
179 
180  }
181 
182  void run () override
183  {
185  testOtherTypes();
186  }
187 };
188 
189 BEAST_DEFINE_TESTSUITE (PreimageSha256, conditions, ripple);
190 
191 }
192 
193 }
ripple::makeSlice
std::enable_if_t< std::is_same< T, char >::value||std::is_same< T, unsigned char >::value, Slice > makeSlice(std::array< T, N > const &a)
Definition: Slice.h:199
std::string
STL class.
ripple::cryptoconditions::Fulfillment::deserialize
static std::unique_ptr< Fulfillment > deserialize(Slice s, std::error_code &ec)
Load a fulfillment from its binary form.
Definition: Fulfillment.cpp:66
utility
ripple::charUnHex
int charUnHex(unsigned char c)
Converts a hex digit to the corresponding integer.
Definition: strHex.cpp:26
std::pair
std::vector::reserve
T reserve(T... args)
vector
std::string::size
T size(T... args)
ripple::cryptoconditions::PreimageSha256_test::testOtherTypes
void testOtherTypes()
Definition: PreimageSha256_test.cpp:126
ripple::cryptoconditions::PreimageSha256_test::run
void run() override
Definition: PreimageSha256_test.cpp:182
ripple::Buffer
Like std::vector<char> but better.
Definition: Buffer.h:35
algorithm
std::error_code
STL class.
ripple::cryptoconditions::PreimageSha256_test::testKnownVectors
void testKnownVectors()
Definition: PreimageSha256_test.cpp:67
std::vector::push_back
T push_back(T... args)
ripple::cryptoconditions::PreimageSha256_test
Definition: PreimageSha256_test.cpp:35
std::uint8_t
ripple::cryptoconditions::validate
bool validate(Fulfillment const &f, Condition const &c, Slice m)
Verify if the given message satisfies the fulfillment.
Definition: Fulfillment.cpp:49
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
std::string::cbegin
T cbegin(T... args)
std::string::cend
T cend(T... args)
ripple::cryptoconditions::PreimageSha256_test::hexblob
Buffer hexblob(std::string const &s)
Definition: PreimageSha256_test.cpp:39
ripple::cryptoconditions::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(PreimageSha256, conditions, ripple)
std::vector::data
T data(T... args)
ripple::cryptoconditions::Condition::deserialize
static std::unique_ptr< Condition > deserialize(Slice s, std::error_code &ec)
Load a condition from its binary form.
Definition: Condition.cpp:154
string