Electroneum
wipeable_string.cpp File Reference
#include <boost/optional/optional.hpp>
#include <string.h>
#include "gtest/gtest.h"
#include "misc_log_ex.h"
#include "wipeable_string.h"
#include "hex.h"
Include dependency graph for wipeable_string.cpp:

Go to the source code of this file.

Functions

 TEST (wipeable_string, ctor)
 
 TEST (wipeable_string, wipe)
 
 TEST (wipeable_string, clear)
 
 TEST (wipeable_string, push_back)
 
 TEST (wipeable_string, append_char)
 
 TEST (wipeable_string, append_string)
 
 TEST (wipeable_string, empty)
 
 TEST (wipeable_string, pop_back)
 
 TEST (wipeable_string, equal)
 
 TEST (wipeable_string, not_equal)
 
 TEST (wipeable_string, trim)
 
 TEST (wipeable_string, split)
 
 TEST (wipeable_string, parse_hexstr)
 
 TEST (wipeable_string, to_hex)
 

Function Documentation

◆ TEST() [1/14]

TEST ( wipeable_string  ,
ctor   
)

Definition at line 37 of file wipeable_string.cpp.

38 {
40  ASSERT_EQ(s0.size(), 0);
41 
43  ASSERT_EQ(s1.size(), 3);
44  ASSERT_TRUE(!memcmp(s1.data(), "foo", s1.size()));
45 
47  ASSERT_EQ(s2.size(), 3);
48  ASSERT_TRUE(!memcmp(s2.data(), "bar", s2.size()));
49 
51  ASSERT_EQ(s3.size(), 4);
52  ASSERT_TRUE(!memcmp(s3.data(), "quux", s3.size()));
53 }
size_t size() const noexcept
::std::string string
Definition: gtest-port.h:1097
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [2/14]

TEST ( wipeable_string  ,
wipe   
)

Definition at line 55 of file wipeable_string.cpp.

56 {
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 }
::std::string string
Definition: gtest-port.h:1097
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [3/14]

TEST ( wipeable_string  ,
clear   
)

Definition at line 64 of file wipeable_string.cpp.

65 {
67  ASSERT_EQ(s0.size(), 3);
68  s0.clear();
69  ASSERT_EQ(s0.size(), 0);
70 }
::std::string string
Definition: gtest-port.h:1097
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
Here is the call graph for this function:

◆ TEST() [4/14]

TEST ( wipeable_string  ,
push_back   
)

Definition at line 72 of file wipeable_string.cpp.

73 {
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 }
::std::string string
Definition: gtest-port.h:1097
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [5/14]

TEST ( wipeable_string  ,
append_char   
)

Definition at line 81 of file wipeable_string.cpp.

82 {
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 }
::std::string string
Definition: gtest-port.h:1097
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [6/14]

TEST ( wipeable_string  ,
append_string   
)

Definition at line 90 of file wipeable_string.cpp.

91 {
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 }
::std::string string
Definition: gtest-port.h:1097
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [7/14]

TEST ( wipeable_string  ,
empty   
)

Definition at line 99 of file wipeable_string.cpp.

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 }
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
bool empty() const noexcept
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function:

◆ TEST() [8/14]

TEST ( wipeable_string  ,
pop_back   
)

Definition at line 109 of file wipeable_string.cpp.

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 }
size_t size() const noexcept
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
const char * data() const noexcept
Here is the call graph for this function:

◆ TEST() [9/14]

TEST ( wipeable_string  ,
equal   
)

Definition at line 118 of file wipeable_string.cpp.

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 }
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865

◆ TEST() [10/14]

TEST ( wipeable_string  ,
not_equal   
)

Definition at line 130 of file wipeable_string.cpp.

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 }
#define ASSERT_FALSE(condition)
Definition: gtest.h:1868
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865

◆ TEST() [11/14]

TEST ( wipeable_string  ,
trim   
)

Definition at line 149 of file wipeable_string.cpp.

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 }
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865

◆ TEST() [12/14]

TEST ( wipeable_string  ,
split   
)

Definition at line 174 of file wipeable_string.cpp.

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 }
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865

◆ TEST() [13/14]

TEST ( wipeable_string  ,
parse_hexstr   
)

Definition at line 187 of file wipeable_string.cpp.

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);
200  ASSERT_EQ(*s, epee::wipeable_string("", 1));
201  ASSERT_TRUE((s = epee::wipeable_string("41").parse_hexstr()) != boost::none);
203  ASSERT_TRUE((s = epee::wipeable_string("414243").parse_hexstr()) != boost::none);
204  ASSERT_EQ(*s, epee::wipeable_string("ABC"));
205 }
#define ASSERT_EQ(val1, val2)
Definition: gtest.h:1956
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865

◆ TEST() [14/14]

TEST ( wipeable_string  ,
to_hex   
)

Definition at line 207 of file wipeable_string.cpp.

208 {
211 }
Non-owning sequence of data. Does not deep copy.
Definition: span.h:56
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
#define ASSERT_TRUE(condition)
Definition: gtest.h:1865
Here is the call graph for this function: