Electroneum
Loading...
Searching...
No Matches
wipeable_string.cpp
Go to the documentation of this file.
1// Copyright (c) 2018, The Monero Project
2//
3// All rights reserved.
4//
5// Redistribution and use in source and binary forms, with or without modification, are
6// permitted provided that the following conditions are met:
7//
8// 1. Redistributions of source code must retain the above copyright notice, this list of
9// conditions and the following disclaimer.
10//
11// 2. Redistributions in binary form must reproduce the above copyright notice, this list
12// of conditions and the following disclaimer in the documentation and/or other
13// materials provided with the distribution.
14//
15// 3. Neither the name of the copyright holder nor the names of its contributors may be
16// used to endorse or promote products derived from this software without specific
17// prior written permission.
18//
19// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY
20// EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
21// MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL
22// THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23// SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25// INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
26// STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
27// THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
28
29#include <boost/optional/optional.hpp>
30#include <string.h>
31#include "gtest/gtest.h"
32
33#include "misc_log_ex.h"
34#include "wipeable_string.h"
35#include "hex.h"
36
37TEST(wipeable_string, ctor)
38{
40 ASSERT_EQ(s0.size(), 0);
41
42 epee::wipeable_string s1(std::string("foo"));
43 ASSERT_EQ(s1.size(), 3);
44 ASSERT_TRUE(!memcmp(s1.data(), "foo", s1.size()));
45
46 epee::wipeable_string s2(std::string("bar"));
47 ASSERT_EQ(s2.size(), 3);
48 ASSERT_TRUE(!memcmp(s2.data(), "bar", s2.size()));
49
50 epee::wipeable_string s3(std::string("quux"));
51 ASSERT_EQ(s3.size(), 4);
52 ASSERT_TRUE(!memcmp(s3.data(), "quux", s3.size()));
53}
54
55TEST(wipeable_string, wipe)
56{
57 epee::wipeable_string s0(std::string("foo"));
58 ASSERT_EQ(s0.size(), 3);
59 s0.wipe();
60 ASSERT_EQ(s0.size(), 3);
61 ASSERT_TRUE(!memcmp(s0.data(), "\0\0\0", 3));
62}
63
64TEST(wipeable_string, clear)
65{
66 epee::wipeable_string s0(std::string("foo"));
67 ASSERT_EQ(s0.size(), 3);
68 s0.clear();
69 ASSERT_EQ(s0.size(), 0);
70}
71
72TEST(wipeable_string, push_back)
73{
74 epee::wipeable_string s0(std::string("fo"));
75 ASSERT_EQ(s0.size(), 2);
76 s0.push_back('o');
77 ASSERT_EQ(s0.size(), 3);
78 ASSERT_TRUE(!memcmp(s0.data(), "foo", s0.size()));
79}
80
81TEST(wipeable_string, append_char)
82{
83 epee::wipeable_string s0(std::string("fo"));
84 ASSERT_EQ(s0.size(), 2);
85 s0 += 'o';
86 ASSERT_EQ(s0.size(), 3);
87 ASSERT_TRUE(!memcmp(s0.data(), "foo", s0.size()));
88}
89
90TEST(wipeable_string, append_string)
91{
92 epee::wipeable_string s0(std::string("foo"));
93 ASSERT_EQ(s0.size(), 3);
94 s0 += "bar";
95 ASSERT_EQ(s0.size(), 6);
96 ASSERT_TRUE(!memcmp(s0.data(), "foobar", s0.size()));
97}
98
99TEST(wipeable_string, empty)
100{
102 ASSERT_TRUE(s0.empty());
103 s0.push_back(' ');
104 ASSERT_FALSE(s0.empty());
105 ASSERT_EQ(s0.pop_back(), ' ');
106 ASSERT_TRUE(s0.empty());
107}
108
109TEST(wipeable_string, pop_back)
110{
111 epee::wipeable_string s = "test";
112 ASSERT_EQ(s.size(), 4);
113 ASSERT_EQ(s.pop_back(), 't');
114 ASSERT_EQ(s.size(), 3);
115 ASSERT_TRUE(!memcmp(s.data(), "tes", s.size()));
116}
117
118TEST(wipeable_string, equal)
119{
120 epee::wipeable_string s0 = "foo";
121 epee::wipeable_string s1 = "bar";
122 epee::wipeable_string s0_2 = "foo";
123 ASSERT_TRUE(s0 == s0);
124 ASSERT_TRUE(s0 == s0_2);
125 ASSERT_TRUE(s1 == s1);
126 ASSERT_FALSE(s1 == s0);
127 ASSERT_FALSE(s1 == s0_2);
128}
129
130TEST(wipeable_string, not_equal)
131{
132 epee::wipeable_string s0 = "foo";
133 epee::wipeable_string s1 = "bar";
134 epee::wipeable_string s0_2 = "foo";
135 ASSERT_FALSE(s0 != s0);
136 ASSERT_FALSE(s0 != s0_2);
137 ASSERT_FALSE(s1 != s1);
138 ASSERT_TRUE(s1 != s0);
139 ASSERT_TRUE(s1 != s0_2);
140}
141
142static epee::wipeable_string trimmed(const char *s)
143{
145 str.trim();
146 return str;
147}
148
149TEST(wipeable_string, trim)
150{
151 ASSERT_TRUE(trimmed("") == "");
152 ASSERT_TRUE(trimmed(" ") == "");
153 ASSERT_TRUE(trimmed(" ") == "");
154 ASSERT_TRUE(trimmed("a") == "a");
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(" ab ") == "ab");
162 ASSERT_TRUE(trimmed(" a b ") == "a b");
163 ASSERT_TRUE(trimmed(" a b ") == "a b");
164}
165
166static bool check_split(const char *s, const std::vector<epee::wipeable_string> &v)
167{
169 std::vector<epee::wipeable_string> fields;
170 str.split(fields);
171 return v == fields;
172}
173
174TEST(wipeable_string, split)
175{
176 ASSERT_TRUE(check_split("", {}));
177 ASSERT_TRUE(check_split("foo", {"foo"}));
178 ASSERT_TRUE(check_split(" foo ", {"foo"}));
179 ASSERT_TRUE(check_split("foo bar", {"foo", "bar"}));
180 ASSERT_TRUE(check_split("foo bar", {"foo", "bar"}));
181 ASSERT_TRUE(check_split("foo bar baz", {"foo", "bar", "baz"}));
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}
186
187TEST(wipeable_string, parse_hexstr)
188{
189 boost::optional<epee::wipeable_string> s;
190
191 ASSERT_EQ(boost::none, epee::wipeable_string("x").parse_hexstr());
192 ASSERT_EQ(boost::none, epee::wipeable_string("x0000000000000000").parse_hexstr());
193 ASSERT_EQ(boost::none, epee::wipeable_string("0000000000000000x").parse_hexstr());
194 ASSERT_EQ(boost::none, epee::wipeable_string("0").parse_hexstr());
195 ASSERT_EQ(boost::none, epee::wipeable_string("000").parse_hexstr());
196
197 ASSERT_TRUE((s = epee::wipeable_string("").parse_hexstr()) != boost::none);
198 ASSERT_EQ(*s, "");
199 ASSERT_TRUE((s = epee::wipeable_string("00").parse_hexstr()) != boost::none);
201 ASSERT_TRUE((s = epee::wipeable_string("41").parse_hexstr()) != boost::none);
203 ASSERT_TRUE((s = epee::wipeable_string("414243").parse_hexstr()) != boost::none);
205}
206
Non-owning sequence of data. Does not deep copy.
Definition span.h:57
const char * data() const noexcept
bool empty() const noexcept
size_t size() const noexcept
#define ASSERT_EQ(val1, val2)
Definition gtest.h:1956
#define ASSERT_FALSE(condition)
Definition gtest.h:1868
#define TEST(test_case_name, test_name)
Definition gtest.h:2187
#define ASSERT_TRUE(condition)
Definition gtest.h:1865
unsigned char uint8_t
Definition stdint.h:124
static epee::wipeable_string wipeable_string(const span< const std::uint8_t > src)
Definition hex.cpp:69