scclib
Stable Cloud Computing C++ Library
secvec.cc
Go to the documentation of this file.
1 /*
2 BSD 3-Clause License
3 
4 Copyright (c) 2022, Stable Cloud Computing, Inc.
5 
6 Redistribution and use in source and binary forms, with or without
7 modification, are permitted provided that the following conditions are met:
8 
9 1. Redistributions of source code must retain the above copyright notice, this
10  list of conditions and the following disclaimer.
11 
12 2. Redistributions in binary form must reproduce the above copyright notice,
13  this list of conditions and the following disclaimer in the documentation
14  and/or other materials provided with the distribution.
15 
16 3. Neither the name of the copyright holder nor the names of its
17  contributors may be used to endorse or promote products derived from
18  this software without specific prior written permission.
19 
20 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
21 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
23 DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
24 FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
26 SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
27 CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
28 OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
29 OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
30 */
31 #include <crypto/secvec.h>
32 #include <gtest/gtest.h>
33 #include <iostream>
34 #include <fstream>
35 #include <util/fs.h>
36 
43 using std::cout;
44 using std::endl;
45 using std::system_error;
46 using std::runtime_error;
47 using std::fstream;
48 using std::ofstream;
49 using std::ifstream;
50 using std::vector;
51 using std::string;
52 using std::ios;
56 
57 TEST(secure_vector_test, char_sanity)
58 {
59  SecVecChar v;
60  ASSERT_EQ(v.size(), 0);
61  SecVecChar v2(10);
62  ASSERT_EQ(v2.size(), 10);
63  SecVecChar v3(10, '\1');
64  ASSERT_EQ(v3.size(), 10);
65  vector<char> t;
66  for (unsigned i = 0; i < v3.size(); i++) t.push_back('\1');
67  for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
68 
69  v3.resize(5);
70  ASSERT_EQ(v3.size(), 5);
71  t.resize(5);
72  for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
73 
74  v3.resize(10, '\2');
75  ASSERT_EQ(v3.size(), 10);
76  t.resize(10, '\2');
77  for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
78 
79  v3.clear();
80  ASSERT_EQ(v3.size(), 0);
81 }
82 
83 TEST(secure_vector_test, uchar_sanity)
84 {
85  SecVecUchar v;
86  ASSERT_EQ(v.size(), 0);
87  SecVecUchar v2(10);
88  ASSERT_EQ(v2.size(), 10);
89  SecVecUchar v3(10, '\1');
90  ASSERT_EQ(v3.size(), 10);
91  vector<unsigned char> t;
92  for (unsigned i = 0; i < v3.size(); i++) t.push_back('\1');
93  for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
94 
95  v3.resize(5);
96  ASSERT_EQ(v3.size(), 5);
97  t.resize(5);
98  for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
99 
100  v3.resize(10, '\2');
101  ASSERT_EQ(v3.size(), 10);
102  t.resize(10, '\2');
103  for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
104 
105  v3.clear();
106  ASSERT_EQ(v3.size(), 0);
107 }
108 
109 static string plaintext = "\
110 To be, or not to be, that is the question: \n\
111 Whether 'tis nobler in the mind to suffer \n\
112 The slings and arrows of outrageous fortune, \n\
113 Or to take Arms against a Sea of troubles, \n\
114 And by opposing end them: to die, to sleep; \n\
115 No more; and by a sleep, to say we end \n\
116 The heart-ache, and the thousand natural shocks \n\
117 That Flesh is heir to? 'Tis a consummation \n\
118 Devoutly to be wished. To die, to sleep, \n\
119 perchance to Dream; aye, there's the rub...\n";
120 
121 struct SecVecCharTest : public testing::Test
122 {
123  string fname;
124  string tname;
125  SecVecChar plainsv;
126 
127  SecVecCharTest() : fname("testfile.txt"), tname("testfile2.txt")
128  {
129  ofstream file(fname, ios::out|ios::trunc);
130 
131  if (!file.is_open())
132  {
133  throw runtime_error("could not open for writing");
134  }
135 
136  file << plaintext;
137  file.flush();
138  file.close();
139 
140  for (auto ch : plaintext)
141  {
142  plainsv.push_back(ch);
143  }
144  }
145  virtual ~SecVecCharTest()
146  {
147  system_error err;
148  fs::remove(fname, &err);
149  fs::remove(tname, &err);
150  }
151 };
152 
153 TEST_F(SecVecCharTest, read_from_file)
154 {
155  SecVecChar sv;
156  ifstream f(fname);
157  f >> sv;
158 
159  string ver(sv.begin(), sv.end());
160 
161  cout << "<start plaintext>";
162  cout << ver;
163  cout << "<end plaintext>" << endl;
164 
165  ASSERT_EQ(plaintext, ver);
166  ASSERT_EQ(plainsv, sv);
167 }
168 
169 TEST_F(SecVecCharTest, write_to_file)
170 {
171  ofstream file(tname);
172 
173  file << plainsv;
174  file.close();
175 
176  ifstream t(tname);
177  string s;
178  char ch;
179  while (t.get(ch))
180  {
181  s.push_back(ch);
182  }
183  ASSERT_EQ(s, plaintext);
184 }
185 
186 TEST_F(SecVecCharTest, bad_read)
187 {
188  ifstream f("nothere");
189  SecVecChar sv;
190  ASSERT_THROW(f >> sv, runtime_error);
191 }
192 
193 TEST_F(SecVecCharTest, bad_write)
194 {
195  fstream file(fname, ios::in); // open read only
196  if (!file.is_open())
197  {
198  throw runtime_error("could not open test file for reading");
199  }
200 
201  ASSERT_THROW(file << plainsv, runtime_error);
202 }
Common file system utilities.
Definition: fs.h:103
static void remove(const std::string &, std::system_error *=nullptr)
Remove the file or directory.
Definition: fs.cc:233
Common file system utilities.
Secure vector.
TEST(inet_example, client_server_stream_test)
[Inet client server]
Definition: inet.cc:521