Debian Codegraph @

tests/unit_tests/wipeable_string.cpp

raw symbols SCIP
1 // Copyright (c) 2018-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 <boost/optional/optional.hpp>
31 #include <string.h>
32 #include "gtest/gtest.h"
33
34 #include "misc_log_ex.h"
35 #include "wipeable_string.h"
36 #include "hex.h"
37
38 TEST(wipeable_string, ctor)
39 {
40 epee::wipeable_string s0;
41 ASSERT_EQ(s0.size(), 0);
42
43 epee::wipeable_string s1(std::string("foo"));
44 ASSERT_EQ(s1.size(), 3);
45 ASSERT_TRUE(!memcmp(s1.data(), "foo", s1.size()));
46
47 epee::wipeable_string s2(std::string("bar"));
48 ASSERT_EQ(s2.size(), 3);
49 ASSERT_TRUE(!memcmp(s2.data(), "bar", s2.size()));
50
51 epee::wipeable_string s3(std::string("quux"));
52 ASSERT_EQ(s3.size(), 4);
53 ASSERT_TRUE(!memcmp(s3.data(), "quux", s3.size()));
54 }
55
56 TEST(wipeable_string, wipe)
57 {
58 epee::wipeable_string s0(std::string("foo"));
59 ASSERT_EQ(s0.size(), 3);
60 s0.wipe();
61 ASSERT_EQ(s0.size(), 3);
62 ASSERT_TRUE(!memcmp(s0.data(), "\0\0\0", 3));
63 }
64
65 TEST(wipeable_string, clear)
66 {
67 epee::wipeable_string s0(std::string("foo"));
68 ASSERT_EQ(s0.size(), 3);
69 s0.clear();
70 ASSERT_EQ(s0.size(), 0);
71 }
72
73 TEST(wipeable_string, push_back)
74 {
75 epee::wipeable_string s0(std::string("fo"));
76 ASSERT_EQ(s0.size(), 2);
77 s0.push_back('o');
78 ASSERT_EQ(s0.size(), 3);
79 ASSERT_TRUE(!memcmp(s0.data(), "foo", s0.size()));
80 }
81
82 TEST(wipeable_string, append_char)
83 {
84 epee::wipeable_string s0(std::string("fo"));
85 ASSERT_EQ(s0.size(), 2);
86 s0 += 'o';
87 ASSERT_EQ(s0.size(), 3);
88 ASSERT_TRUE(!memcmp(s0.data(), "foo", s0.size()));
89 }
90
91 TEST(wipeable_string, append_string)
92 {
93 epee::wipeable_string s0(std::string("foo"));
94 ASSERT_EQ(s0.size(), 3);
95 s0 += "bar";
96 ASSERT_EQ(s0.size(), 6);
97 ASSERT_TRUE(!memcmp(s0.data(), "foobar", s0.size()));
98 }
99
100 TEST(wipeable_string, empty)
101 {
102 epee::wipeable_string s0;
103 ASSERT_TRUE(s0.empty());
104 s0.push_back(' ');
105 ASSERT_FALSE(s0.empty());
106 ASSERT_EQ(s0.pop_back(), ' ');
107 ASSERT_TRUE(s0.empty());
108 }
109
110 TEST(wipeable_string, pop_back)
111 {
112 epee::wipeable_string s = "test";
113 ASSERT_EQ(s.size(), 4);
114 ASSERT_EQ(s.pop_back(), 't');
115 ASSERT_EQ(s.size(), 3);
116 ASSERT_TRUE(!memcmp(s.data(), "tes", s.size()));
117 }
118
119 TEST(wipeable_string, equal)
120 {
121 epee::wipeable_string s0 = "foo";
122 epee::wipeable_string s1 = "bar";
123 epee::wipeable_string s0_2 = "foo";
124 ASSERT_TRUE(s0 == s0);
125 ASSERT_TRUE(s0 == s0_2);
126 ASSERT_TRUE(s1 == s1);
127 ASSERT_FALSE(s1 == s0);
128 ASSERT_FALSE(s1 == s0_2);
129 }
130
131 TEST(wipeable_string, not_equal)
132 {
133 epee::wipeable_string s0 = "foo";
134 epee::wipeable_string s1 = "bar";
135 epee::wipeable_string s0_2 = "foo";
136 ASSERT_FALSE(s0 != s0);
137 ASSERT_FALSE(s0 != s0_2);
138 ASSERT_FALSE(s1 != s1);
139 ASSERT_TRUE(s1 != s0);
140 ASSERT_TRUE(s1 != s0_2);
141 }
142
143 static epee::wipeable_string trimmed(const char *s)
144 {
145 epee::wipeable_string str(s);
146 str.trim();
147 return str;
148 }
149
150 TEST(wipeable_string, trim)
151 {
152 ASSERT_TRUE(trimmed("") == "");
153 ASSERT_TRUE(trimmed(" ") == "");
154 ASSERT_TRUE(trimmed(" ") == "");
155 ASSERT_TRUE(trimmed("a") == "a");
156 ASSERT_TRUE(trimmed(" a") == "a");
157 ASSERT_TRUE(trimmed(" a") == "a");
158 ASSERT_TRUE(trimmed("a ") == "a");
159 ASSERT_TRUE(trimmed("a ") == "a");
160 ASSERT_TRUE(trimmed(" a ") == "a");
161 ASSERT_TRUE(trimmed(" a ") == "a");
162 ASSERT_TRUE(trimmed(" ab ") == "ab");
163 ASSERT_TRUE(trimmed(" a b ") == "a b");
164 ASSERT_TRUE(trimmed(" a b ") == "a b");
165 }
166
167 static bool check_split(const char *s, const std::vector<epee::wipeable_string> &v)
168 {
169 epee::wipeable_string str(s);
170 std::vector<epee::wipeable_string> fields;
171 str.split(fields);
172 return v == fields;
173 }
174
175 TEST(wipeable_string, split)
176 {
177 ASSERT_TRUE(check_split("", {}));
178 ASSERT_TRUE(check_split("foo", {"foo"}));
179 ASSERT_TRUE(check_split(" foo ", {"foo"}));
180 ASSERT_TRUE(check_split("foo bar", {"foo", "bar"}));
181 ASSERT_TRUE(check_split("foo bar", {"foo", "bar"}));
182 ASSERT_TRUE(check_split("foo bar baz", {"foo", "bar", "baz"}));
183 ASSERT_TRUE(check_split(" foo bar baz ", {"foo", "bar", "baz"}));
184 ASSERT_TRUE(check_split(" foo bar baz", {"foo", "bar", "baz"}));
185 ASSERT_TRUE(check_split("foo bar baz ", {"foo", "bar", "baz"}));
186 ASSERT_TRUE(check_split("\tfoo\n bar\r\nbaz", {"foo", "bar", "baz"}));
187 }
188
189 TEST(wipeable_string, parse_hexstr)
190 {
191 boost::optional<epee::wipeable_string> s;
192
193 ASSERT_TRUE(boost::none == epee::wipeable_string("x").parse_hexstr());
194 ASSERT_TRUE(boost::none == epee::wipeable_string("x0000000000000000").parse_hexstr());
195 ASSERT_TRUE(boost::none == epee::wipeable_string("0000000000000000x").parse_hexstr());
196 ASSERT_TRUE(boost::none == epee::wipeable_string("0").parse_hexstr());
197 ASSERT_TRUE(boost::none == epee::wipeable_string("000").parse_hexstr());
198
199 ASSERT_TRUE((s = epee::wipeable_string("").parse_hexstr()) != boost::none);
200 ASSERT_TRUE(*s == "");
201 ASSERT_TRUE((s = epee::wipeable_string("00").parse_hexstr()) != boost::none);
202 ASSERT_TRUE(*s == epee::wipeable_string("", 1));
203 ASSERT_TRUE((s = epee::wipeable_string("41").parse_hexstr()) != boost::none);
204 ASSERT_TRUE(*s == epee::wipeable_string("A"));
205 ASSERT_TRUE((s = epee::wipeable_string("414243").parse_hexstr()) != boost::none);
206 ASSERT_TRUE(*s == epee::wipeable_string("ABC"));
207 }
208
209 TEST(wipeable_string, to_hex)
210 {
211 ASSERT_TRUE(epee::to_hex::wipeable_string(epee::span<const uint8_t>((const uint8_t*)"", 0)) == epee::wipeable_string(""));
212 ASSERT_TRUE(epee::to_hex::wipeable_string(epee::span<const uint8_t>((const uint8_t*)"abc", 3)) == epee::wipeable_string("616263"));
213 }
214
215 TEST(wipeable_string, to_string)
216 {
217 // Converting a wipeable_string to a string defeats the purpose of wipeable_string,
218 // but nice to know this works
219 std::string str;
220 {
221 epee::wipeable_string wipeable_str("foo");
222 str = std::string(wipeable_str.data(), wipeable_str.size());
223 }
224 ASSERT_TRUE(str == std::string("foo"));
225 }