rippled
Basics_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/nodestore/DummyScheduler.h>
21 #include <ripple/nodestore/Manager.h>
22 #include <ripple/nodestore/impl/DecodedBlob.h>
23 #include <ripple/nodestore/impl/EncodedBlob.h>
24 #include <test/nodestore/TestBase.h>
25 
26 namespace ripple {
27 namespace NodeStore {
28 
29 // Tests predictable batches, and NodeObject blob encoding
30 //
32 {
33 public:
34  // Make sure predictable object generation works!
35  void
36  testBatches(std::uint64_t const seedValue)
37  {
38  testcase("batch");
39 
40  auto batch1 = createPredictableBatch(numObjectsToTest, seedValue);
41 
42  auto batch2 = createPredictableBatch(numObjectsToTest, seedValue);
43 
44  BEAST_EXPECT(areBatchesEqual(batch1, batch2));
45 
46  auto batch3 = createPredictableBatch(numObjectsToTest, seedValue + 1);
47 
48  BEAST_EXPECT(!areBatchesEqual(batch1, batch3));
49  }
50 
51  // Checks encoding/decoding blobs
52  void
53  testBlobs(std::uint64_t const seedValue)
54  {
55  testcase("encoding");
56 
57  auto batch = createPredictableBatch(numObjectsToTest, seedValue);
58 
59  for (int i = 0; i < batch.size(); ++i)
60  {
61  EncodedBlob encoded(batch[i]);
62 
63  DecodedBlob decoded(
64  encoded.getKey(), encoded.getData(), encoded.getSize());
65 
66  BEAST_EXPECT(decoded.wasOk());
67 
68  if (decoded.wasOk())
69  {
70  std::shared_ptr<NodeObject> const object(
71  decoded.createObject());
72 
73  BEAST_EXPECT(isSame(batch[i], object));
74  }
75  }
76  }
77 
78  void
79  run() override
80  {
81  std::uint64_t const seedValue = 50;
82 
83  testBatches(seedValue);
84 
85  testBlobs(seedValue);
86  }
87 };
88 
89 BEAST_DEFINE_TESTSUITE(NodeStoreBasic, ripple_core, ripple);
90 
91 } // namespace NodeStore
92 } // namespace ripple
ripple::NodeStore::DecodedBlob::wasOk
bool wasOk() const noexcept
Determine if the decoding was successful.
Definition: DecodedBlob.h:46
ripple::NodeStore::NodeStoreBasic_test::testBatches
void testBatches(std::uint64_t const seedValue)
Definition: Basics_test.cpp:36
ripple::NodeStore::DecodedBlob
Parsed key/value blob into NodeObject components.
Definition: DecodedBlob.h:38
ripple::NodeStore::TestBase
Definition: TestBase.h:68
std::shared_ptr< NodeObject >
ripple::NodeStore::TestBase::areBatchesEqual
static bool areBatchesEqual(Batch const &lhs, Batch const &rhs)
Definition: TestBase.h:120
ripple::NodeStore::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(Backend, ripple_core, ripple)
ripple::NodeStore::DecodedBlob::createObject
std::shared_ptr< NodeObject > createObject()
Create a NodeObject from this data.
Definition: DecodedBlob.cpp:73
ripple::NodeStore::NodeStoreBasic_test::testBlobs
void testBlobs(std::uint64_t const seedValue)
Definition: Basics_test.cpp:53
ripple::NodeStore::EncodedBlob::getData
void const * getData() const noexcept
Definition: EncodedBlob.h:124
ripple::NodeStore::NodeStoreBasic_test
Definition: Basics_test.cpp:31
std::uint64_t
ripple::NodeStore::TestBase::numObjectsToTest
static const int numObjectsToTest
Definition: TestBase.h:75
ripple::NodeStore::NodeStoreBasic_test::run
void run() override
Definition: Basics_test.cpp:79
ripple::NodeStore::TestBase::createPredictableBatch
static Batch createPredictableBatch(int numObjects, std::uint64_t seed)
Definition: TestBase.h:80
ripple
Use hash_* containers for keys that do not need a cryptographically secure hashing algorithm.
Definition: RCLCensorshipDetector.h:29
ripple::NodeStore::EncodedBlob::getSize
std::size_t getSize() const noexcept
Definition: EncodedBlob.h:118
ripple::NodeStore::EncodedBlob
Convert a NodeObject from in-memory to database format.
Definition: EncodedBlob.h:54
ripple::NodeStore::isSame
bool isSame(std::shared_ptr< NodeObject > const &lhs, std::shared_ptr< NodeObject > const &rhs)
Returns true if objects are identical.
Definition: TestBase.h:57
ripple::NodeStore::EncodedBlob::getKey
void const * getKey() const noexcept
Definition: EncodedBlob.h:112