#include <gtest/gtest.h>
#include <iostream>
#include <fstream>
using std::cout;
using std::endl;
using std::system_error;
using std::runtime_error;
using std::fstream;
using std::ofstream;
using std::ifstream;
using std::vector;
using std::string;
using std::ios;
TEST(secure_vector_test, char_sanity)
{
SecVecChar v;
ASSERT_EQ(v.size(), 0);
SecVecChar v2(10);
ASSERT_EQ(v2.size(), 10);
SecVecChar v3(10, '\1');
ASSERT_EQ(v3.size(), 10);
vector<char> t;
for (unsigned i = 0; i < v3.size(); i++) t.push_back('\1');
for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
v3.resize(5);
ASSERT_EQ(v3.size(), 5);
t.resize(5);
for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
v3.resize(10, '\2');
ASSERT_EQ(v3.size(), 10);
t.resize(10, '\2');
for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
v3.clear();
ASSERT_EQ(v3.size(), 0);
}
TEST(secure_vector_test, uchar_sanity)
{
SecVecUchar v;
ASSERT_EQ(v.size(), 0);
SecVecUchar v2(10);
ASSERT_EQ(v2.size(), 10);
SecVecUchar v3(10, '\1');
ASSERT_EQ(v3.size(), 10);
vector<unsigned char> t;
for (unsigned i = 0; i < v3.size(); i++) t.push_back('\1');
for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
v3.resize(5);
ASSERT_EQ(v3.size(), 5);
t.resize(5);
for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
v3.resize(10, '\2');
ASSERT_EQ(v3.size(), 10);
t.resize(10, '\2');
for (unsigned i = 0; i < v3.size(); i++) ASSERT_EQ(v3[i], t[i]);
v3.clear();
ASSERT_EQ(v3.size(), 0);
}
static string plaintext = "\
To be, or not to be, that is the question: \n\
Whether 'tis nobler in the mind to suffer \n\
The slings and arrows of outrageous fortune, \n\
Or to take Arms against a Sea of troubles, \n\
And by opposing end them: to die, to sleep; \n\
No more; and by a sleep, to say we end \n\
The heart-ache, and the thousand natural shocks \n\
That Flesh is heir to? 'Tis a consummation \n\
Devoutly to be wished. To die, to sleep, \n\
perchance to Dream; aye, there's the rub...\n";
struct SecVecCharTest : public testing::Test
{
string fname;
string tname;
SecVecChar plainsv;
SecVecCharTest() : fname("testfile.txt"), tname("testfile2.txt")
{
ofstream file(fname, ios::out|ios::trunc);
if (!file.is_open())
{
throw runtime_error("could not open for writing");
}
file << plaintext;
file.flush();
file.close();
for (auto ch : plaintext)
{
plainsv.push_back(ch);
}
}
virtual ~SecVecCharTest()
{
system_error err;
}
};
TEST_F(SecVecCharTest, read_from_file)
{
SecVecChar sv;
ifstream f(fname);
f >> sv;
string ver(sv.begin(), sv.end());
cout << "<start plaintext>";
cout << ver;
cout << "<end plaintext>" << endl;
ASSERT_EQ(plaintext, ver);
ASSERT_EQ(plainsv, sv);
}
TEST_F(SecVecCharTest, write_to_file)
{
ofstream file(tname);
file << plainsv;
file.close();
ifstream t(tname);
string s;
char ch;
while (t.get(ch))
{
s.push_back(ch);
}
ASSERT_EQ(s, plaintext);
}
TEST_F(SecVecCharTest, bad_read)
{
ifstream f("nothere");
SecVecChar sv;
ASSERT_THROW(f >> sv, runtime_error);
}
TEST_F(SecVecCharTest, bad_write)
{
fstream file(fname, ios::in);
if (!file.is_open())
{
throw runtime_error("could not open test file for reading");
}
ASSERT_THROW(file << plainsv, runtime_error);
}
Common file system utilities.
static void remove(const std::string &, std::system_error *=nullptr)
Remove the file or directory.
Common file system utilities.
TEST(inet_example, client_server_stream_test)
[Inet client server]