test_file.cpp
Go to the documentation of this file.
1 //
2 // Bareflank Hypervisor
3 //
4 // Copyright (C) 2015 Assured Information Security, Inc.
5 // Author: Rian Quinn <quinnr@ainfosec.com>
6 // Author: Brendan Kerrigan <kerriganb@ainfosec.com>
7 //
8 // This library is free software; you can redistribute it and/or
9 // modify it under the terms of the GNU Lesser General Public
10 // License as published by the Free Software Foundation; either
11 // version 2.1 of the License, or (at your option) any later version.
12 //
13 // This library is distributed in the hope that it will be useful,
14 // but WITHOUT ANY WARRANTY; without even the implied warranty of
15 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 // Lesser General Public License for more details.
17 //
18 // You should have received a copy of the GNU Lesser General Public
19 // License along with this library; if not, write to the Free Software
20 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 
22 #include <test.h>
23 
24 #include <file.h>
25 #include <exception.h>
26 
27 static auto operator"" _ife(const char *str, std::size_t len)
28 { (void)str; (void)len; return std::make_shared<bfn::invalid_file_error>(""); }
29 
30 void
31 bfm_ut::test_file_read_with_bad_filename()
32 {
33  auto &&f = file{};
34  auto &&filename = "/blah/bad_filename.txt"_s;
35 
36  this->expect_exception([&] { f.read_text(""); }, ""_ut_ffe);
37  this->expect_exception([&] { f.read_binary(""); }, ""_ut_ffe);
38 
39  this->expect_exception([&] { f.read_text(filename); }, ""_ife);
40  this->expect_exception([&] { f.read_binary(filename); }, ""_ife);
41 }
42 
43 void
44 bfm_ut::test_file_write_with_bad_filename()
45 {
46  auto &&f = file{};
47  auto &&filename = "/blah/bad_filename.txt"_s;
48 
49  auto &&text_data = "hello"_s;
50  auto &&binary_data = {'h', 'e', 'l', 'l', 'o'};
51 
52  this->expect_exception([&] { f.write_text("", text_data); }, ""_ut_ffe);
53  this->expect_exception([&] { f.write_binary("", binary_data); }, ""_ut_ffe);
54 
55  this->expect_exception([&] { f.write_text(filename, ""); }, ""_ut_ffe);
56  this->expect_exception([&] { f.write_binary(filename, {}); }, ""_ut_ffe);
57 
58  this->expect_exception([&] { f.write_text(filename, text_data); }, ""_ife);
59  this->expect_exception([&] { f.write_binary(filename, binary_data); }, ""_ife);
60 }
61 
62 void
63 bfm_ut::test_file_read_write_success()
64 {
65  auto &&f = file{};
66  auto &&filename = "/tmp/test_file.txt"_s;
67 
68  auto &&text_data = "hello"_s;
69  auto &&binary_data = {'h', 'e', 'l', 'l', 'o'};
70 
71  this->expect_no_exception([&] { f.write_text(filename, text_data); });
72  this->expect_true(f.read_text(filename) == text_data);
73 
74  this->expect_no_exception([&] { f.write_binary(filename, binary_data); });
75  this->expect_true(f.read_binary(filename) == file::binary_data(binary_data));
76 
77  auto &&ret = std::remove(filename.c_str());
78  (void) ret;
79 }
#define expect_exception(f, e)
Definition: unittest.h:162
Definition: file.h:35
#define expect_no_exception(f)
Definition: unittest.h:198
#define expect_true(a)
std::vector< char > binary_data
Definition: file.h:40