rippled
STValidation_test.cpp
1 //------------------------------------------------------------------------------
2 /*
3  This file is part of rippled: https://github.com/ripple/rippled
4  Copyright (c) 2012, 2013 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/Log.h>
21 #include <ripple/protocol/jss.h>
22 #include <ripple/protocol/SecretKey.h>
23 #include <ripple/protocol/st.h>
24 #include <ripple/json/json_reader.h>
25 #include <ripple/json/to_string.h>
26 #include <ripple/beast/unit_test.h>
27 #include <test/jtx.h>
28 
29 #include <memory>
30 #include <type_traits>
31 
32 namespace ripple {
33 
34 class STValidation_test : public beast::unit_test::suite
35 {
36 public:
38  {
39  testcase ("Deserialization");
40 
41  constexpr std::uint8_t payload1[] =
42  {
43  // specifies an Ed25519 public key.
44  0x22, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x51,
45  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
46  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
47  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x73,
48  0x21, 0xed, 0x78, 0x00, 0xe6, 0x73, 0x00, 0x72, 0x00, 0x3c, 0x00,
49  0x00, 0x00, 0x88, 0x00, 0xe6, 0x73, 0x38, 0x00, 0x00, 0x8a, 0x00,
50  0x88, 0x4e, 0x31, 0x30, 0x5f, 0x5f, 0x63, 0x78, 0x78, 0x61, 0x62,
51  0x69
52  };
53 
54  constexpr std::uint8_t payload2[] =
55  {
56  0x22, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x51,
57  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
59  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a, 0x73,
60  0x21, 0xed, 0xff, 0x03, 0x1c, 0xbe, 0x65, 0x22, 0x61, 0x9c, 0x5e,
61  0x13, 0x12, 0x00, 0x3b, 0x43, 0x00, 0x00, 0x00, 0xf7, 0x3f, 0x3f,
62  0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x3f, 0x13, 0x13, 0x13, 0x3a, 0x27,
63  0xff
64  };
65 
66  constexpr std::uint8_t payload3[] =
67  {
68  // Has no public key at all
69  0x22, 0x00, 0x00, 0x00, 0x00, 0x29, 0x00, 0x00, 0x00, 0x00, 0x51,
70  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
71  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72  0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x2a
73  };
74 
75  try
76  {
77  SerialIter sit {payload1};
78  auto stx = std::make_shared<ripple::STValidation>(sit,
79  [](PublicKey const& pk) {
80  return calcNodeID(pk);
81  }, false);
82  fail("An exception should have been thrown");
83  }
84  catch (std::exception const& e)
85  {
86  BEAST_EXPECT(strcmp(e.what(),
87  "Invalid public key in validation") == 0);
88  }
89 
90  try
91  {
92  SerialIter sit {payload2};
93  auto stx = std::make_shared<ripple::STValidation>(sit,
94  [](PublicKey const& pk) {
95  return calcNodeID(pk);
96  }, false);
97  fail("An exception should have been thrown");
98  }
99  catch (std::exception const& e)
100  {
101  BEAST_EXPECT(strcmp(e.what(),
102  "Invalid public key in validation") == 0);
103  }
104 
105  try
106  {
107  SerialIter sit {payload3};
108  auto stx = std::make_shared<ripple::STValidation>(sit,
109  [](PublicKey const& pk) {
110  return calcNodeID(pk);
111  }, false);
112  fail("An exception should have been thrown");
113  }
114  catch (std::exception const& e)
115  {
116  BEAST_EXPECT(strcmp(e.what(),
117  "Field 'SigningPubKey' is required but missing.") == 0);
118  }
119  }
120 
121  void
122  run() override
123  {
125  }
126 };
127 
128 BEAST_DEFINE_TESTSUITE(STValidation,protocol,ripple);
129 
130 } // ripple
ripple::STValidation_test::testDeserialization
void testDeserialization()
Definition: STValidation_test.cpp:37
ripple::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(AccountTxPaging, app, ripple)
std::exception
STL class.
ripple::calcNodeID
NodeID calcNodeID(PublicKey const &pk)
Calculate the 160-bit node ID from a node public key.
Definition: PublicKey.cpp:307
ripple::STValidation_test
Definition: STValidation_test.cpp:34
ripple::PublicKey
A public key.
Definition: PublicKey.h:59
ripple::SerialIter
Definition: Serializer.h:311
std::uint8_t
memory
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::STValidation_test::run
void run() override
Definition: STValidation_test.cpp:122
type_traits
std::exception::what
T what(T... args)