Debian Codegraph @

tests/unit_tests/epee_serialization.cpp

raw symbols SCIP
1 // Copyright (c) 2020-2022, The Monero Project
2
3 //
4 // All rights reserved.
5 //
6 // Redistribution and use in source and binary forms, with or without modification, are
7 // permitted provided that the following conditions are met:
8 //
9 // 1. Redistributions of source code must retain the above copyright notice, this list of
10 // conditions and the following disclaimer.
11 //
12 // 2. Redistributions in binary form must reproduce the above copyright notice, this list
13 // of conditions and the following disclaimer in the documentation and/or other
14 // materials provided with the distribution.
15 //
16 // 3. Neither the name of the copyright holder nor the names of its contributors may be
17 // used to endorse or promote products derived from this software without specific
18 // prior written permission.
19 //
20 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
21 // EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
22 // MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
23 // THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
24 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
25 // PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
27 // STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
28 // THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 #include <cstdint>
31 #include <gtest/gtest.h>
32 #include <vector>
33
34 #include "serialization/keyvalue_serialization.h"
35 #include "storages/portable_storage.h"
36 #include "storages/portable_storage_template_helper.h"
37 #include "span.h"
38
39 TEST(epee_binary, two_keys)
40 {
41 static constexpr const std::uint8_t data[] = {
42 0x01, 0x11, 0x01, 0x1, 0x01, 0x01, 0x02, 0x1, 0x1, 0x08, 0x01, 'a',
43 0x0B, 0x00, 0x01, 'b', 0x0B, 0x00
44 };
45
46 epee::serialization::portable_storage storage{};
47 EXPECT_TRUE(storage.load_from_binary(data));
48 }
49
50 TEST(epee_binary, duplicate_key)
51 {
52 static constexpr const std::uint8_t data[] = {
53 0x01, 0x11, 0x01, 0x1, 0x01, 0x01, 0x02, 0x1, 0x1, 0x08, 0x01, 'a',
54 0x0B, 0x00, 0x01, 'a', 0x0B, 0x00
55 };
56
57 epee::serialization::portable_storage storage{};
58 EXPECT_FALSE(storage.load_from_binary(data));
59 }
60
61 namespace
62 {
63
64 template<typename t_param>
65 struct ParentObjWithOptChild
66 {
67 t_param params;
68
69 ParentObjWithOptChild(): params{} {}
70
71 BEGIN_KV_SERIALIZE_MAP()
72 KV_SERIALIZE(params)
73 END_KV_SERIALIZE_MAP()
74 };
75
76 struct ObjWithOptChild
77 {
78 bool test_value;
79
80 BEGIN_KV_SERIALIZE_MAP()
81 KV_SERIALIZE_OPT(test_value, true);
82 END_KV_SERIALIZE_MAP()
83 };
84 }
85
86 TEST(epee_binary, serialize_deserialize)
87 {
88 ParentObjWithOptChild<ObjWithOptChild> o;
89 std::string o_json;
90 o.params.test_value = true;
91
92 EXPECT_TRUE(epee::serialization::store_t_to_json(o, o_json));
93 EXPECT_TRUE(o.params.test_value);
94
95 EXPECT_TRUE(epee::serialization::load_t_from_json(o, o_json));
96 EXPECT_TRUE(o.params.test_value);
97
98 ParentObjWithOptChild<ObjWithOptChild> o2;
99 std::string o2_json;
100 o.params.test_value = false;
101
102 EXPECT_TRUE(epee::serialization::store_t_to_json(o2, o2_json));
103 EXPECT_FALSE(o2.params.test_value);
104
105 EXPECT_TRUE(epee::serialization::load_t_from_json(o2, o2_json));
106 EXPECT_FALSE(o2.params.test_value);
107
108 // compiler sets default value of test_value to false
109 ParentObjWithOptChild<ObjWithOptChild> o3;
110 std::string o3_json;
111
112 EXPECT_TRUE(epee::serialization::store_t_to_json(o3, o3_json));
113 EXPECT_FALSE(o3.params.test_value);
114
115 EXPECT_TRUE(epee::serialization::load_t_from_json(o3, o3_json));
116 EXPECT_FALSE(o3.params.test_value);
117
118 // test optional field default initialization.
119 ParentObjWithOptChild<ObjWithOptChild> o4;
120 std::string o4_json = "{\"params\": {}}";
121
122 EXPECT_TRUE(epee::serialization::load_t_from_json(o4, o4_json));
123 EXPECT_TRUE(o4.params.test_value);
124 }