1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
|
#include <gtest/gtest.h>
#include "file.h"
#include <filesystem>
#include <fstream>
#include <iostream>
namespace fs = std::filesystem;
namespace {
const fs::path testFilename{"testfile.txt"};
} // namespace
class FileTest: public ::testing::Test
{
protected:
FileTest(){
}
~FileTest() override{
}
void SetUp() override
{
std::error_code ec;
fs::remove(testFilename, ec);
}
void TearDown() override
{
std::error_code ec;
fs::remove(testFilename, ec);
}
};
TEST_F(FileTest, getFile)
{
{
std::ofstream of(testFilename, std::ios::binary);
of << "abc";
}
std::string s{Reichwein::File::getFile(testFilename)};
EXPECT_EQ(s, "abc");
}
TEST_F(FileTest, getFile_proc)
{
std::string s{Reichwein::File::getFile("/proc/cmdline")};
EXPECT_GT(s.size(), 0);
}
TEST_F(FileTest, getFile_not_found)
{
EXPECT_THROW({Reichwein::File::getFile(testFilename);}, std::runtime_error);
}
TEST_F(FileTest, setFile)
{
Reichwein::File::setFile(testFilename, std::string("abc"));
std::ifstream f(testFilename, std::ios::binary);
std::string s{" "};
EXPECT_EQ(f.readsome(s.data(), s.size()), static_cast<std::streamsize>(3));
EXPECT_EQ(s, "abc ");
}
TEST_F(FileTest, setFile_ptr)
{
std::string s{"abc"};
Reichwein::File::setFile(testFilename, s.data(), s.size());
EXPECT_EQ(Reichwein::File::getFile(testFilename), "abc");
}
TEST_F(FileTest, setFile_vector)
{
std::vector<uint8_t> v{1, 2, 3, 4};
Reichwein::File::setFile(testFilename, v);
EXPECT_EQ(Reichwein::File::getFile(testFilename), "\x01\x02\x03\x04");
}
TEST_F(FileTest, setFile_overwrite)
{
Reichwein::File::setFile(testFilename, std::string("abc"));
Reichwein::File::setFile(testFilename, std::string("def"));
EXPECT_EQ(Reichwein::File::getFile(testFilename), "def");
}
TEST_F(FileTest, setFile_nonexisting_dir)
{
EXPECT_THROW({Reichwein::File::setFile(testFilename/testFilename, "abc");}, std::runtime_error);
}
|