test_ioctl.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 #include <ioctl.h>
24 #include <debug_ring_interface.h>
25 
26 int g_ioctl_open = 0;
27 int g_send_ioctl = 0;
28 int g_read_ioctl = 0;
29 int g_write_ioctl = 0;
30 
31 int64_t bf_ioctl_open()
32 { return g_ioctl_open; }
33 
34 int64_t bf_send_ioctl(int fd, unsigned long request)
35 { (void) fd; (void) request; return g_send_ioctl; }
36 
37 int64_t bf_read_ioctl(int fd, unsigned long request, void *data)
38 { (void) fd; (void) request; (void) data; return g_read_ioctl; }
39 
40 int64_t bf_write_ioctl(int fd, unsigned long request, const void *data)
41 { (void) fd; (void) request; (void) data; return g_write_ioctl; }
42 
43 static auto operator"" _die(const char *str, std::size_t len)
44 { (void)str; (void)len; return std::make_shared<bfn::driver_inaccessible_error>(); }
45 
46 static auto operator"" _ife(const char *str, std::size_t len)
47 { (void)str; (void)len; return std::make_shared<bfn::ioctl_failed_error>(""); }
48 
49 void
50 bfm_ut::test_ioctl_driver_inaccessible()
51 {
52  auto &&ctl = ioctl{};
53 
54  g_ioctl_open = -1;
55  auto ___ = gsl::finally([&] { g_ioctl_open = 0; });
56 
57  this->expect_exception([&] { ctl.open(); }, ""_die);
58 }
59 
60 void
61 bfm_ut::test_ioctl_add_module_with_invalid_length()
62 {
63  auto &&ctl = ioctl{};
64  this->expect_exception([&] { ctl.call_ioctl_add_module({}); }, ""_ut_ffe);
65 }
66 
67 void
68 bfm_ut::test_ioctl_add_module_failed()
69 {
70  auto &&data = {'h', 'e', 'l', 'l', 'o'};
71  auto &&ctl = ioctl{};
72 
73  g_write_ioctl = -1;
74  auto ___ = gsl::finally([&] { g_write_ioctl = 0; });
75 
76  this->expect_exception([&] { ctl.call_ioctl_add_module(data); }, ""_ife);
77 }
78 
79 void
80 bfm_ut::test_ioctl_load_vmm_failed()
81 {
82  auto &&ctl = ioctl{};
83 
84  g_send_ioctl = -1;
85  auto ___ = gsl::finally([&] { g_send_ioctl = 0; });
86 
87  this->expect_exception([&] { ctl.call_ioctl_load_vmm(); }, ""_ife);
88 }
89 
90 void
91 bfm_ut::test_ioctl_unload_vmm_failed()
92 {
93  auto &&ctl = ioctl{};
94 
95  g_send_ioctl = -1;
96  auto ___ = gsl::finally([&] { g_send_ioctl = 0; });
97 
98  this->expect_exception([&] { ctl.call_ioctl_unload_vmm(); }, ""_ife);
99 }
100 
101 void
102 bfm_ut::test_ioctl_start_vmm_failed()
103 {
104  auto &&ctl = ioctl{};
105 
106  g_send_ioctl = -1;
107  auto ___ = gsl::finally([&] { g_send_ioctl = 0; });
108 
109  this->expect_exception([&] { ctl.call_ioctl_start_vmm(); }, ""_ife);
110 }
111 
112 void
113 bfm_ut::test_ioctl_stop_vmm_failed()
114 {
115  auto &&ctl = ioctl{};
116 
117  g_send_ioctl = -1;
118  auto ___ = gsl::finally([&] { g_send_ioctl = 0; });
119 
120  this->expect_exception([&] { ctl.call_ioctl_stop_vmm(); }, ""_ife);
121 }
122 
123 void
124 bfm_ut::test_ioctl_dump_vmm_with_invalid_drr()
125 {
126  auto &&drr = ioctl::drr_pointer{nullptr};
127  auto &&ctl = ioctl{};
128 
129  this->expect_exception([&] { ctl.call_ioctl_dump_vmm(drr, 0); }, ""_ut_ffe);
130 }
131 
132 void
133 bfm_ut::test_ioctl_dump_vmm_failed()
134 {
135  auto &&drr = ioctl::drr_type{};
136  auto &&ctl = ioctl{};
137 
138  g_read_ioctl = -1;
139  auto ___ = gsl::finally([&] { g_read_ioctl = 0; });
140 
141  this->expect_exception([&] { ctl.call_ioctl_dump_vmm(&drr, 0); }, ""_ife);
142 }
143 
144 void
145 bfm_ut::test_ioctl_vmm_status_with_invalid_status()
146 {
147  auto &&status = ioctl::status_pointer{nullptr};
148  auto &&ctl = ioctl{};
149 
150  this->expect_exception([&] { ctl.call_ioctl_vmm_status(status); }, ""_ut_ffe);
151 }
152 
153 void
154 bfm_ut::test_ioctl_vmm_status_failed()
155 {
156  auto &&status = ioctl::status_type{};
157  auto &&ctl = ioctl{};
158 
159  g_read_ioctl = -1;
160  auto ___ = gsl::finally([&] { g_read_ioctl = 0; });
161 
162  this->expect_exception([&] { ctl.call_ioctl_vmm_status(&status); }, ""_ife);
163 }
164 
165 void
166 bfm_ut::test_ioctl_vmm_vmcall_with_invalid_registers()
167 {
168  auto &&reigsters = ioctl::registers_pointer{nullptr};
169  auto &&ctl = ioctl{};
170 
171  this->expect_exception([&] { ctl.call_ioctl_vmcall(reigsters, 0); }, ""_ut_ffe);
172 }
173 
174 void
175 bfm_ut::test_ioctl_vmm_vmcall_failed()
176 {
177  auto &&registers = ioctl::registers_type{};
178  auto &&ctl = ioctl{};
179 
180  g_write_ioctl = -1;
181  auto ___ = gsl::finally([&] { g_write_ioctl = 0; });
182 
183  this->expect_exception([&] { ctl.call_ioctl_vmcall(&registers, 0); }, ""_ife);
184 }
#define expect_exception(f, e)
Definition: unittest.h:162
int g_send_ioctl
Definition: test_ioctl.cpp:27
int64_t unsigned long request
int64_t bf_write_ioctl(int fd, unsigned long request, const void *data)
Definition: test_ioctl.cpp:40
int64_t unsigned long void * data
int g_ioctl_open
Definition: test_ioctl.cpp:26
status_type * status_pointer
Definition: ioctl.h:58
debug_ring_resources_t * drr
int64_t status_type
Definition: ioctl.h:57
int g_write_ioctl
Definition: test_ioctl.cpp:29
int64_t bf_read_ioctl(int fd, unsigned long request, void *data)
Definition: test_ioctl.cpp:37
int64_t bf_ioctl_open()
Definition: test_ioctl.cpp:31
int g_read_ioctl
Definition: test_ioctl.cpp:28
int64_t bf_send_ioctl(int fd, unsigned long request)
Definition: test_ioctl.cpp:34
Definition: ioctl.h:48