test_serial_port_intel_x64.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>
24 
25 #include <map>
26 static std::map<uint16_t, uint8_t> g_ports;
27 
28 extern "C" uint8_t
29 __inb(uint16_t port) noexcept
30 {
31  return g_ports[port];
32 }
33 
34 extern "C" void
35 __outb(uint16_t port, uint8_t val) noexcept
36 {
37  g_ports[port] = val;
38 }
39 
40 void
41 serial_ut::test_serial_null_intrinsics()
42 {
43  this->expect_no_exception([&] { std::make_unique<serial_port_intel_x64>(); });
44 }
45 
46 void
47 serial_ut::test_serial_success()
48 {
54 
63 }
64 
65 void
66 serial_ut::test_serial_set_baud_rate_success()
67 {
68  auto serial = std::make_unique<serial_port_intel_x64>();
69 
70  serial->set_baud_rate(serial_port_intel_x64::baud_rate_50);
71  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_50);
72  serial->set_baud_rate(serial_port_intel_x64::baud_rate_75);
73  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_75);
74  serial->set_baud_rate(serial_port_intel_x64::baud_rate_110);
75  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_110);
76  serial->set_baud_rate(serial_port_intel_x64::baud_rate_150);
77  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_150);
78  serial->set_baud_rate(serial_port_intel_x64::baud_rate_300);
79  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_300);
80  serial->set_baud_rate(serial_port_intel_x64::baud_rate_600);
81  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_600);
82  serial->set_baud_rate(serial_port_intel_x64::baud_rate_1200);
83  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_1200);
84  serial->set_baud_rate(serial_port_intel_x64::baud_rate_1800);
85  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_1800);
86  serial->set_baud_rate(serial_port_intel_x64::baud_rate_2000);
87  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_2000);
88  serial->set_baud_rate(serial_port_intel_x64::baud_rate_2400);
89  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_2400);
90  serial->set_baud_rate(serial_port_intel_x64::baud_rate_3600);
91  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_3600);
92  serial->set_baud_rate(serial_port_intel_x64::baud_rate_4800);
93  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_4800);
94  serial->set_baud_rate(serial_port_intel_x64::baud_rate_7200);
95  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_7200);
96  serial->set_baud_rate(serial_port_intel_x64::baud_rate_9600);
97  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_9600);
98  serial->set_baud_rate(serial_port_intel_x64::baud_rate_19200);
99  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_19200);
100  serial->set_baud_rate(serial_port_intel_x64::baud_rate_38400);
101  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_38400);
102  serial->set_baud_rate(serial_port_intel_x64::baud_rate_57600);
103  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_57600);
104  serial->set_baud_rate(serial_port_intel_x64::baud_rate_115200);
105  this->expect_true(serial->baud_rate() == serial_port_intel_x64::baud_rate_115200);
106 }
107 
108 void
109 serial_ut::test_serial_set_data_bits_success()
110 {
111  auto serial = std::make_unique<serial_port_intel_x64>();
112 
113  serial->set_data_bits(serial_port_intel_x64::char_length_5);
114  this->expect_true(serial->data_bits() == serial_port_intel_x64::char_length_5);
115  serial->set_data_bits(serial_port_intel_x64::char_length_6);
116  this->expect_true(serial->data_bits() == serial_port_intel_x64::char_length_6);
117  serial->set_data_bits(serial_port_intel_x64::char_length_7);
118  this->expect_true(serial->data_bits() == serial_port_intel_x64::char_length_7);
119  serial->set_data_bits(serial_port_intel_x64::char_length_8);
120  this->expect_true(serial->data_bits() == serial_port_intel_x64::char_length_8);
121 }
122 
123 void
124 serial_ut::test_serial_set_data_bits_success_extra_bits()
125 {
126  auto serial = std::make_unique<serial_port_intel_x64>();
127 
129  serial->set_data_bits(static_cast<serial_port_intel_x64::data_bits_t>(bits));
130 
131  this->expect_true(serial->data_bits() == serial_port_intel_x64::DEFAULT_DATA_BITS);
132  this->expect_true(serial->stop_bits() == serial_port_intel_x64::DEFAULT_STOP_BITS);
133  this->expect_true(serial->parity_bits() == serial_port_intel_x64::DEFAULT_PARITY_BITS);
134 }
135 
136 void
137 serial_ut::test_serial_set_stop_bits_success()
138 {
139  auto serial = std::make_unique<serial_port_intel_x64>();
140 
141  serial->set_stop_bits(serial_port_intel_x64::stop_bits_1);
142  this->expect_true(serial->stop_bits() == serial_port_intel_x64::stop_bits_1);
143  serial->set_stop_bits(serial_port_intel_x64::stop_bits_2);
144  this->expect_true(serial->stop_bits() == serial_port_intel_x64::stop_bits_2);
145 }
146 
147 void
148 serial_ut::test_serial_set_stop_bits_success_extra_bits()
149 {
150  auto serial = std::make_unique<serial_port_intel_x64>();
151 
153  serial->set_stop_bits(static_cast<serial_port_intel_x64::stop_bits_t>(bits));
154 
155  this->expect_true(serial->data_bits() == serial_port_intel_x64::DEFAULT_DATA_BITS);
156  this->expect_true(serial->stop_bits() == serial_port_intel_x64::DEFAULT_STOP_BITS);
157  this->expect_true(serial->parity_bits() == serial_port_intel_x64::DEFAULT_PARITY_BITS);
158 }
159 
160 void
161 serial_ut::test_serial_set_parity_bits_success()
162 {
163  auto serial = std::make_unique<serial_port_intel_x64>();
164 
165  serial->set_parity_bits(serial_port_intel_x64::parity_none);
166  this->expect_true(serial->parity_bits() == serial_port_intel_x64::parity_none);
167  serial->set_parity_bits(serial_port_intel_x64::parity_odd);
168  this->expect_true(serial->parity_bits() == serial_port_intel_x64::parity_odd);
169  serial->set_parity_bits(serial_port_intel_x64::parity_even);
170  this->expect_true(serial->parity_bits() == serial_port_intel_x64::parity_even);
171  serial->set_parity_bits(serial_port_intel_x64::parity_mark);
172  this->expect_true(serial->parity_bits() == serial_port_intel_x64::parity_mark);
173  serial->set_parity_bits(serial_port_intel_x64::parity_space);
174  this->expect_true(serial->parity_bits() == serial_port_intel_x64::parity_space);
175 }
176 
177 void
178 serial_ut::test_serial_set_parity_bits_success_extra_bits()
179 {
180  auto serial = std::make_unique<serial_port_intel_x64>();
181 
183  serial->set_parity_bits(static_cast<serial_port_intel_x64::parity_bits_t>(bits));
184 
185  this->expect_true(serial->data_bits() == serial_port_intel_x64::DEFAULT_DATA_BITS);
186  this->expect_true(serial->stop_bits() == serial_port_intel_x64::DEFAULT_STOP_BITS);
187  this->expect_true(serial->parity_bits() == serial_port_intel_x64::DEFAULT_PARITY_BITS);
188 }
189 
190 void
191 serial_ut::test_serial_write_character()
192 {
194 
195  auto serial = std::make_unique<serial_port_intel_x64>();
196  serial->write('c');
197 }
198 
199 void
200 serial_ut::test_serial_write_string()
201 {
203 
204  auto serial = std::make_unique<serial_port_intel_x64>();
205  serial->write("hello world");
206 }
void __outb(uint16_t port, uint8_t val) noexcept
constexpr const x64::portio::port_8bit_type line_control_parity_mask
#define expect_no_exception(f)
Definition: unittest.h:198
constexpr const x64::portio::port_8bit_type line_control_stop_mask
#define DEFAULT_COM_PORT
Definition: constants.h:289
#define DEFAULT_BAUD_RATE
Definition: constants.h:298
constexpr const x64::portio::port_8bit_type fifo_control_clear_transmit_fifo
constexpr const x64::portio::port_addr_type line_status_reg
constexpr const x64::portio::port_8bit_type line_control_data_mask
void uint64_t uint64_t uint64_t *rdx noexcept
constexpr const x64::portio::port_addr_type baud_rate_lo_reg
std::map< portio::port_addr_type, portio::port_32bit_type > g_ports
static serial_port_intel_x64 * instance() noexcept
constexpr const x64::portio::port_addr_type line_control_reg
#define DEFAULT_STOP_BITS
Definition: constants.h:316
constexpr const x64::portio::port_addr_type baud_rate_hi_reg
#define DEFAULT_PARITY_BITS
Definition: constants.h:325
#define DEFAULT_DATA_BITS
Definition: constants.h:307
#define expect_true(a)
uint8_t __inb(uint16_t port) noexcept
constexpr const x64::portio::port_8bit_type fifo_control_enable_fifos
constexpr const x64::portio::port_addr_type fifo_control_reg
constexpr const x64::portio::port_8bit_type fifo_control_clear_recieve_fifo