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/Slice.h>
22 #include <ripple/basics/StringUtilities.h>
23 #include <ripple/basics/strHex.h>
24 #include <ripple/beast/unit_test.h>
25 #include <ripple/conditions/Condition.h>
26 #include <ripple/conditions/Fulfillment.h>
27 #include <ripple/conditions/impl/PreimageSha256.h>
28 #include <algorithm>
29 #include <string>
30 #include <utility>
31 #include <vector>
32 
33 namespace ripple {
34 namespace cryptoconditions {
35 
36 class PreimageSha256_test : public beast::unit_test::suite
37 {
38  inline Buffer
39  hexblob(std::string const& s)
40  {
41  auto blob = strUnHex(s);
42  BEAST_EXPECT(blob);
43  return {blob->data(), blob->size()};
44  }
45 
46  void
48  {
49  testcase("Known Vectors");
50 
51  std::pair<std::string, std::string> const known[] = {
52  {"A0028000",
53  "A0258020E3B0C44298FC1C149AFBF4C8996FB92427AE41E4649B934CA495991B7"
54  "852B855810100"},
55  {"A0058003616161",
56  "A02580209834876DCFB05CB167A5C24953EBA58C4AC89B1ADF57F28F2F9D09AF1"
57  "07EE8F0810103"},
58  };
59 
60  std::error_code ec;
61 
62  auto f1 = Fulfillment::deserialize(hexblob(known[0].first), ec);
63  BEAST_EXPECT(f1);
64  BEAST_EXPECT(!ec);
65 
66  auto c1 = Condition::deserialize(hexblob(known[0].second), ec);
67  BEAST_EXPECT(c1);
68  BEAST_EXPECT(!ec);
69 
70  auto f2 = Fulfillment::deserialize(hexblob(known[1].first), ec);
71  BEAST_EXPECT(f2);
72  BEAST_EXPECT(!ec);
73 
74  auto c2 = Condition::deserialize(hexblob(known[1].second), ec);
75  BEAST_EXPECT(c2);
76  BEAST_EXPECT(!ec);
77 
78  // Check equality and inequality
79  BEAST_EXPECT(f1->condition() == *c1);
80  BEAST_EXPECT(f1->condition() != *c2);
81  BEAST_EXPECT(f2->condition() == *c2);
82  BEAST_EXPECT(f2->condition() != *c1);
83  BEAST_EXPECT(*c1 != *c2);
84  BEAST_EXPECT(*c1 == *c1);
85  BEAST_EXPECT(f1->condition() == f1->condition());
86 
87  // Should validate with the empty string
88  BEAST_EXPECT(validate(*f1, *c1));
89  BEAST_EXPECT(validate(*f2, *c2));
90 
91  // And with any string - the message doesn't matter for PrefixSha256
92  BEAST_EXPECT(validate(*f1, *c1, makeSlice(known[0].first)));
93  BEAST_EXPECT(validate(*f1, *c1, makeSlice(known[0].second)));
94  BEAST_EXPECT(validate(*f2, *c2, makeSlice(known[0].first)));
95  BEAST_EXPECT(validate(*f2, *c2, makeSlice(known[0].second)));
96 
97  // Shouldn't validate if the fulfillment & condition don't match
98  // regardless of the message.
99  BEAST_EXPECT(!validate(*f2, *c1));
100  BEAST_EXPECT(!validate(*f2, *c1, makeSlice(known[0].first)));
101  BEAST_EXPECT(!validate(*f2, *c1, makeSlice(known[0].second)));
102  BEAST_EXPECT(!validate(*f1, *c2));
103  BEAST_EXPECT(!validate(*f1, *c2, makeSlice(known[0].first)));
104  BEAST_EXPECT(!validate(*f1, *c2, makeSlice(known[0].second)));
105  }
106 
107  void
109  {
110  testcase("Other Types");
111 
112  std::pair<std::string, std::string> const others[] = {
113  // PREFIX + PREIMAGE:
114  {"A10B8000810100A204A0028000",
115 
116  "A12A8020BB1AC5260C0141B7E54B26EC2330637C5597BF811951AC09E744AD20F"
117  "F77E287810204"
118  "0082020780"},
119 
120  // THRESHOLD:
121  {"A208A004A0028000A100",
122 
123  "A22A8020B4B84136DF48A71D73F4985C04C6767A778ECB65BA7023B4506823BEE"
124  "E7631B9810204"
125  "0082020780"},
126 
127  // RSA:
128  {"A382020880820100E1EF8B24D6F76B09C81ED7752AA262F044F04A874D43809D3"
129  "1CEA612F99B0C97"
130  "A8B4374153E3EEF3D66616843E0E41C293264B71B6173DB1CF0D6CD558C586577"
131  "06FCF097F704C48"
132  "3E59CBFDFD5B3EE7BC80D740C5E0F047F3E85FC0D75815776A6F3F23C5DC5E797"
133  "139A6882E38336A"
134  "4A5FB36137620FF3663DBAE328472801862F72F2F87B202B9C89ADD7CD5B0A076"
135  "F7C53E35039F67E"
136  "D17EC815E5B4305CC63197068D5E6E579BA6DE5F4E3E57DF5E4E072FF2CE4C66E"
137  "B45233973875275"
138  "9639F0257BF57DBD5C443FB5158CCE0A3D36ADC7BA01F33A0BB6DBB2BF989D607"
139  "112F2344D993E77"
140  "E563C1D361DEDF57DA96EF2CFC685F002B638246A5B309B981820100BD42D6569"
141  "F6599AED455F96B"
142  "C0ED08ED1480BF36CD9E1467F9C6F74461C9E3A749334B2F6404AA5F9F6BAFE76"
143  "C347D069250B35D"
144  "1C970C793059EE733A8193F30FA78FEC7CAE459E3DDFD7633805D476940D0CB53"
145  "D7FB389DCDAEAF6"
146  "E8CF48C4B5635430E4F2BCDFE505C2C0FC17B40D93C7EDB7C261EBF43895A705E"
147  "024AA0549A660F7"
148  "0A32150647522DBE6B63520497CFF8F8D5D74768A27C5B86E580BE3FCDC96F197"
149  "6293CBA0D58DFC6"
150  "0B518B632A6DC1E950C43E231FE1A379AA6DDCC52C70EDF851C6C0123A964261C"
151  "FDB3857CD6CD5AD"
152  "C37D8DA2CC924EDAE1D84CF6124587F274C1FA3697DA2901F0269F03B243C03B6"
153  "14E0385E1961FAC"
154  "5000F9BB",
155 
156  "A3258020484950515253545548495051525354554849505152535455484950515"
157  "2535455810101"},
158 
159  // ED25519:
160  {"A4648020D75A980182B10AB7D54BFED3C964073A0EE172F3DAA62325AF021A68F"
161  "707511A8140E556"
162  "4300C360AC729086E2CC806E828A84877F1EB8E5D974D873E065224901555FB88"
163  "21590A33BACC61E"
164  "39701CF9B46BD25BF5F0595BBE24655141438E7A100B",
165 
166  "A4278020799239ABA8FC4FF7EABFBC4C44E69E8BDFED993324E12ED64792ABE28"
167  "9CF1D5F810302"
168  "0000"}};
169 
170  for (auto x : others)
171  {
172  std::error_code ec;
173 
174  BEAST_EXPECT(!Fulfillment::deserialize(hexblob(x.first), ec));
175  BEAST_EXPECT(!Condition::deserialize(hexblob(x.second), ec));
176  }
177  }
178 
179  void
180  run() override
181  {
183  testOtherTypes();
184  }
185 };
186 
187 BEAST_DEFINE_TESTSUITE(PreimageSha256, conditions, ripple);
188 
189 } // namespace cryptoconditions
190 
191 } // namespace ripple
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:241
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:57
utility
std::pair
vector
ripple::cryptoconditions::PreimageSha256_test::testOtherTypes
void testOtherTypes()
Definition: PreimageSha256_test.cpp:108
ripple::cryptoconditions::PreimageSha256_test::run
void run() override
Definition: PreimageSha256_test.cpp:180
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:47
ripple::cryptoconditions::PreimageSha256_test
Definition: PreimageSha256_test.cpp:36
ripple::cryptoconditions::validate
bool validate(Fulfillment const &f, Condition const &c, Slice m)
Verify if the given message satisfies the fulfillment.
Definition: Fulfillment.cpp:45
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::cryptoconditions::PreimageSha256_test::hexblob
Buffer hexblob(std::string const &s)
Definition: PreimageSha256_test.cpp:39
ripple::strUnHex
std::optional< Blob > strUnHex(std::size_t strSize, Iterator begin, Iterator end)
Definition: StringUtilities.h:50
ripple::cryptoconditions::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(PreimageSha256, conditions, ripple)
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:152
string