| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
from unittest import mock |
| 17 |
|
| 18 |
import testtools |
| 19 |
|
| 20 |
from neutron_lib import constants |
| 21 |
|
| 22 |
from neutron_fwaas.privileged import netlink_constants as nl_constants |
| 23 |
from neutron_fwaas.privileged import netlink_lib as nl_lib |
| 24 |
from neutron_fwaas.tests import base |
| 25 |
|
| 26 |
|
| 27 |
FAKE_ENTRY = {'ipversion': 4, 'protocol': 'icmp', |
| 28 |
'type': '8', 'code': '0', 'id': 1234, |
| 29 |
'src': '1.1.1.1', 'dst': '2.2.2.2'} |
| 30 |
FAKE_TCP_ENTRY = {'ipversion': 4, 'protocol': 'tcp', |
| 31 |
'sport': 1, 'dport': 2, |
| 32 |
'src': '1.1.1.1', 'dst': '2.2.2.2'} |
| 33 |
FAKE_UDP_ENTRY = {'ipversion': 4, 'protocol': 'udp', |
| 34 |
'sport': 1, 'dport': 2, |
| 35 |
'src': '1.1.1.1', 'dst': '2.2.2.2'} |
| 36 |
FAKE_ICMPV6_ENTRY = {'ipversion': 6, 'protocol': 'ipv6-icmp', |
| 37 |
'sport': 1, 'dport': 2, 'type': '8', 'code': '0', |
| 38 |
'id': 3456, 'src': '10::10', 'dst': '20::20'} |
| 39 |
|
| 40 |
|
| 41 |
class NetlinkLibTestCase(base.BaseTestCase): |
| 42 |
def setUp(self): |
| 43 |
super().setUp() |
| 44 |
nl_lib.nfct = mock.Mock() |
| 45 |
nl_lib.libc = mock.Mock() |
| 46 |
|
| 47 |
def test_open_new_conntrack_handler_failed(self): |
| 48 |
nl_lib.nfct.nfct_open.return_value = None |
| 49 |
with testtools.ExpectedException(nl_lib.ConntrackOpenFailedExit): |
| 50 |
with nl_lib.ConntrackManager(): |
| 51 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 52 |
nl_lib.nfct.nfct_close.assert_not_called() |
| 53 |
|
| 54 |
def test_open_new_conntrack_handler_pass(self): |
| 55 |
with nl_lib.ConntrackManager(): |
| 56 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 57 |
nl_lib.nfct.nfct_close.assert_called_once() |
| 58 |
|
| 59 |
def test_conntrack_list_entries(self): |
| 60 |
with nl_lib.ConntrackManager() as conntrack: |
| 61 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 62 |
conntrack.list_entries() |
| 63 |
nl_lib.nfct.nfct_callback_register.assert_called_once() |
| 64 |
nl_lib.nfct.nfct_query.assert_called_once() |
| 65 |
nl_lib.nfct.nfct_close.assert_called_once() |
| 66 |
|
| 67 |
def test_conntrack_flush_entries(self): |
| 68 |
with nl_lib.ConntrackManager() as conntrack: |
| 69 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 70 |
conntrack.flush_entries() |
| 71 |
nl_lib.nfct.nfct_query.assert_called_once() |
| 72 |
nl_lib.nfct.nfct_close.assert_called_once() |
| 73 |
|
| 74 |
def test_conntrack_new_failed(self): |
| 75 |
nl_lib.nfct.nfct_new.return_value = None |
| 76 |
with nl_lib.ConntrackManager() as conntrack: |
| 77 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 78 |
conntrack.delete_entries([FAKE_ENTRY]) |
| 79 |
nl_lib.nfct.nfct_new.assert_called_once() |
| 80 |
nl_lib.nfct.nfct_destroy.assert_called_once() |
| 81 |
nl_lib.nfct.nfct_close.assert_called_once() |
| 82 |
|
| 83 |
def test_conntrack_delete_icmp_entry(self): |
| 84 |
conntrack_filter = mock.Mock() |
| 85 |
nl_lib.nfct.nfct_new.return_value = conntrack_filter |
| 86 |
with nl_lib.ConntrackManager() as conntrack: |
| 87 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 88 |
conntrack.delete_entries([FAKE_ENTRY]) |
| 89 |
calls = [ |
| 90 |
mock.call(conntrack_filter, |
| 91 |
nl_constants.ATTR_L3PROTO, |
| 92 |
nl_constants.IPVERSION_SOCKET[4]), |
| 93 |
mock.call(conntrack_filter, |
| 94 |
nl_constants.ATTR_L4PROTO, |
| 95 |
constants.IP_PROTOCOL_MAP['icmp']), |
| 96 |
mock.call(conntrack_filter, |
| 97 |
nl_constants.ATTR_ICMP_CODE, |
| 98 |
int(FAKE_ENTRY['code'])), |
| 99 |
mock.call(conntrack_filter, |
| 100 |
nl_constants.ATTR_ICMP_TYPE, |
| 101 |
int(FAKE_ENTRY['type'])) |
| 102 |
] |
| 103 |
nl_lib.nfct.nfct_set_attr_u8.assert_has_calls(calls, |
| 104 |
any_order=True) |
| 105 |
calls = [ |
| 106 |
mock.call(conntrack_filter, |
| 107 |
nl_constants.ATTR_ICMP_ID, |
| 108 |
nl_lib.libc.htons(FAKE_ENTRY['id'])), |
| 109 |
] |
| 110 |
nl_lib.nfct.nfct_set_attr_u16.assert_has_calls(calls) |
| 111 |
calls = [ |
| 112 |
mock.call(conntrack_filter, |
| 113 |
nl_constants.ATTR_IPV4_SRC, |
| 114 |
conntrack._convert_text_to_binary( |
| 115 |
FAKE_ENTRY['src'], 4)), |
| 116 |
mock.call(conntrack_filter, |
| 117 |
nl_constants.ATTR_IPV4_DST, |
| 118 |
conntrack._convert_text_to_binary( |
| 119 |
FAKE_ENTRY['dst'], 4)), |
| 120 |
] |
| 121 |
nl_lib.nfct.nfct_set_attr.assert_has_calls(calls, any_order=True) |
| 122 |
nl_lib.nfct.nfct_destroy.assert_called_once() |
| 123 |
nl_lib.nfct.nfct_close.assert_called_once() |
| 124 |
|
| 125 |
def test_conntrack_delete_icmpv6_entry(self): |
| 126 |
conntrack_filter = mock.Mock() |
| 127 |
nl_lib.nfct.nfct_new.return_value = conntrack_filter |
| 128 |
with nl_lib.ConntrackManager() as conntrack: |
| 129 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 130 |
conntrack.delete_entries([FAKE_ICMPV6_ENTRY]) |
| 131 |
calls = [ |
| 132 |
mock.call(conntrack_filter, |
| 133 |
nl_constants.ATTR_L3PROTO, |
| 134 |
nl_constants.IPVERSION_SOCKET[6]), |
| 135 |
mock.call(conntrack_filter, |
| 136 |
nl_constants.ATTR_L4PROTO, |
| 137 |
constants.IP_PROTOCOL_MAP['ipv6-icmp']), |
| 138 |
mock.call(conntrack_filter, |
| 139 |
nl_constants.ATTR_ICMP_CODE, |
| 140 |
int(FAKE_ICMPV6_ENTRY['code'])), |
| 141 |
mock.call(conntrack_filter, |
| 142 |
nl_constants.ATTR_ICMP_TYPE, |
| 143 |
int(FAKE_ICMPV6_ENTRY['type'])) |
| 144 |
] |
| 145 |
nl_lib.nfct.nfct_set_attr_u8.assert_has_calls(calls, |
| 146 |
any_order=True) |
| 147 |
calls = [ |
| 148 |
mock.call(conntrack_filter, |
| 149 |
nl_constants.ATTR_ICMP_ID, |
| 150 |
nl_lib.libc.htons(FAKE_ICMPV6_ENTRY['id'])), |
| 151 |
] |
| 152 |
nl_lib.nfct.nfct_set_attr_u16.assert_has_calls(calls) |
| 153 |
calls = [ |
| 154 |
mock.call(conntrack_filter, |
| 155 |
nl_constants.ATTR_IPV6_SRC, |
| 156 |
conntrack._convert_text_to_binary( |
| 157 |
FAKE_ENTRY['src'], 6)), |
| 158 |
mock.call(conntrack_filter, |
| 159 |
nl_constants.ATTR_IPV6_DST, |
| 160 |
conntrack._convert_text_to_binary( |
| 161 |
FAKE_ENTRY['dst'], 6)), |
| 162 |
] |
| 163 |
nl_lib.nfct.nfct_set_attr.assert_has_calls(calls, any_order=True) |
| 164 |
nl_lib.nfct.nfct_destroy.assert_called_once() |
| 165 |
nl_lib.nfct.nfct_close.assert_called_once() |
| 166 |
|
| 167 |
def test_conntrack_delete_udp_entry(self): |
| 168 |
conntrack_filter = mock.Mock() |
| 169 |
nl_lib.nfct.nfct_new.return_value = conntrack_filter |
| 170 |
with nl_lib.ConntrackManager() as conntrack: |
| 171 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 172 |
conntrack.delete_entries([FAKE_UDP_ENTRY]) |
| 173 |
calls = [ |
| 174 |
mock.call(conntrack_filter, |
| 175 |
nl_constants.ATTR_L3PROTO, |
| 176 |
nl_constants.IPVERSION_SOCKET[4]), |
| 177 |
mock.call(conntrack_filter, |
| 178 |
nl_constants.ATTR_L4PROTO, |
| 179 |
constants.IP_PROTOCOL_MAP['udp']) |
| 180 |
] |
| 181 |
nl_lib.nfct.nfct_set_attr_u8.assert_has_calls(calls, |
| 182 |
any_order=True) |
| 183 |
calls = [ |
| 184 |
mock.call(conntrack_filter, |
| 185 |
nl_constants.ATTR_PORT_SRC, |
| 186 |
nl_lib.libc.htons(FAKE_UDP_ENTRY['sport'])), |
| 187 |
mock.call(conntrack_filter, |
| 188 |
nl_constants.ATTR_PORT_DST, |
| 189 |
nl_lib.libc.htons(FAKE_UDP_ENTRY['dport'])) |
| 190 |
] |
| 191 |
nl_lib.nfct.nfct_set_attr_u16.assert_has_calls(calls, |
| 192 |
any_order=True) |
| 193 |
calls = [ |
| 194 |
mock.call(conntrack_filter, |
| 195 |
nl_constants.ATTR_IPV4_SRC, |
| 196 |
conntrack._convert_text_to_binary( |
| 197 |
FAKE_UDP_ENTRY['src'], 4)), |
| 198 |
mock.call(conntrack_filter, |
| 199 |
nl_constants.ATTR_IPV4_DST, |
| 200 |
conntrack._convert_text_to_binary( |
| 201 |
FAKE_UDP_ENTRY['dst'], 4)), |
| 202 |
] |
| 203 |
nl_lib.nfct.nfct_set_attr.assert_has_calls(calls, any_order=True) |
| 204 |
nl_lib.nfct.nfct_destroy.assert_called_once() |
| 205 |
nl_lib.nfct.nfct_close.assert_called_once() |
| 206 |
|
| 207 |
def test_conntrack_delete_tcp_entry(self): |
| 208 |
conntrack_filter = mock.Mock() |
| 209 |
nl_lib.nfct.nfct_new.return_value = conntrack_filter |
| 210 |
with nl_lib.ConntrackManager() as conntrack: |
| 211 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 212 |
conntrack.delete_entries([FAKE_TCP_ENTRY]) |
| 213 |
calls = [ |
| 214 |
mock.call(conntrack_filter, |
| 215 |
nl_constants.ATTR_L3PROTO, |
| 216 |
nl_constants.IPVERSION_SOCKET[4]), |
| 217 |
mock.call(conntrack_filter, |
| 218 |
nl_constants.ATTR_L4PROTO, |
| 219 |
constants.IP_PROTOCOL_MAP['tcp']) |
| 220 |
] |
| 221 |
nl_lib.nfct.nfct_set_attr_u8.assert_has_calls(calls, |
| 222 |
any_order=True) |
| 223 |
calls = [ |
| 224 |
mock.call(conntrack_filter, |
| 225 |
nl_constants.ATTR_PORT_SRC, |
| 226 |
nl_lib.libc.htons(FAKE_TCP_ENTRY['sport'])), |
| 227 |
mock.call(conntrack_filter, |
| 228 |
nl_constants.ATTR_PORT_DST, |
| 229 |
nl_lib.libc.htons(FAKE_TCP_ENTRY['dport'])) |
| 230 |
] |
| 231 |
nl_lib.nfct.nfct_set_attr_u16.assert_has_calls(calls, |
| 232 |
any_order=True) |
| 233 |
calls = [ |
| 234 |
mock.call(conntrack_filter, |
| 235 |
nl_constants.ATTR_IPV4_SRC, |
| 236 |
conntrack._convert_text_to_binary( |
| 237 |
FAKE_TCP_ENTRY['src'], 4)), |
| 238 |
mock.call(conntrack_filter, |
| 239 |
nl_constants.ATTR_IPV4_DST, |
| 240 |
conntrack._convert_text_to_binary( |
| 241 |
FAKE_TCP_ENTRY['dst'], 4)), |
| 242 |
] |
| 243 |
nl_lib.nfct.nfct_set_attr.assert_has_calls(calls, any_order=True) |
| 244 |
nl_lib.nfct.nfct_destroy.assert_called_once() |
| 245 |
nl_lib.nfct.nfct_close.assert_called_once() |
| 246 |
|
| 247 |
def test_conntrack_delete_entries(self): |
| 248 |
conntrack_filter = mock.Mock() |
| 249 |
nl_lib.nfct.nfct_new.return_value = conntrack_filter |
| 250 |
with nl_lib.ConntrackManager() as conntrack: |
| 251 |
nl_lib.nfct.nfct_open.assert_called_once() |
| 252 |
conntrack.delete_entries([FAKE_ENTRY, |
| 253 |
FAKE_TCP_ENTRY, |
| 254 |
FAKE_UDP_ENTRY]) |
| 255 |
calls = [ |
| 256 |
mock.call(conntrack_filter, |
| 257 |
nl_constants.ATTR_L3PROTO, |
| 258 |
nl_constants.IPVERSION_SOCKET[4]), |
| 259 |
mock.call(conntrack_filter, |
| 260 |
nl_constants.ATTR_L4PROTO, |
| 261 |
constants.IP_PROTOCOL_MAP['tcp']), |
| 262 |
mock.call(conntrack_filter, |
| 263 |
nl_constants.ATTR_L3PROTO, |
| 264 |
nl_constants.IPVERSION_SOCKET[4]), |
| 265 |
mock.call(conntrack_filter, |
| 266 |
nl_constants.ATTR_L4PROTO, |
| 267 |
constants.IP_PROTOCOL_MAP['udp']), |
| 268 |
mock.call(conntrack_filter, |
| 269 |
nl_constants.ATTR_L3PROTO, |
| 270 |
nl_constants.IPVERSION_SOCKET[4]), |
| 271 |
mock.call(conntrack_filter, |
| 272 |
nl_constants.ATTR_L4PROTO, |
| 273 |
constants.IP_PROTOCOL_MAP['icmp']), |
| 274 |
mock.call(conntrack_filter, |
| 275 |
nl_constants.ATTR_ICMP_CODE, |
| 276 |
int(FAKE_ENTRY['code'])), |
| 277 |
mock.call(conntrack_filter, |
| 278 |
nl_constants.ATTR_ICMP_TYPE, |
| 279 |
int(FAKE_ENTRY['type'])) |
| 280 |
] |
| 281 |
nl_lib.nfct.nfct_set_attr_u8.assert_has_calls(calls, |
| 282 |
any_order=True) |
| 283 |
calls = [ |
| 284 |
mock.call(conntrack_filter, |
| 285 |
nl_constants.ATTR_PORT_SRC, |
| 286 |
nl_lib.libc.htons(FAKE_TCP_ENTRY['sport'])), |
| 287 |
mock.call(conntrack_filter, |
| 288 |
nl_constants.ATTR_PORT_DST, |
| 289 |
nl_lib.libc.htons(FAKE_TCP_ENTRY['dport'])), |
| 290 |
mock.call(conntrack_filter, |
| 291 |
nl_constants.ATTR_PORT_SRC, |
| 292 |
nl_lib.libc.htons(FAKE_UDP_ENTRY['sport'])), |
| 293 |
mock.call(conntrack_filter, |
| 294 |
nl_constants.ATTR_PORT_DST, |
| 295 |
nl_lib.libc.htons(FAKE_UDP_ENTRY['dport'])), |
| 296 |
mock.call(conntrack_filter, |
| 297 |
nl_constants.ATTR_ICMP_ID, |
| 298 |
nl_lib.libc.htons(FAKE_ENTRY['id'])), |
| 299 |
] |
| 300 |
nl_lib.nfct.nfct_set_attr_u16.assert_has_calls(calls, |
| 301 |
any_order=True) |
| 302 |
calls = [ |
| 303 |
mock.call(conntrack_filter, |
| 304 |
nl_constants.ATTR_IPV4_SRC, |
| 305 |
conntrack._convert_text_to_binary( |
| 306 |
FAKE_TCP_ENTRY['src'], 4)), |
| 307 |
mock.call(conntrack_filter, |
| 308 |
nl_constants.ATTR_IPV4_DST, |
| 309 |
conntrack._convert_text_to_binary( |
| 310 |
FAKE_TCP_ENTRY['dst'], 4)), |
| 311 |
mock.call(conntrack_filter, |
| 312 |
nl_constants.ATTR_IPV4_SRC, |
| 313 |
conntrack._convert_text_to_binary( |
| 314 |
FAKE_UDP_ENTRY['src'], 4)), |
| 315 |
mock.call(conntrack_filter, |
| 316 |
nl_constants.ATTR_IPV4_DST, |
| 317 |
conntrack._convert_text_to_binary( |
| 318 |
FAKE_UDP_ENTRY['dst'], 4)), |
| 319 |
mock.call(conntrack_filter, |
| 320 |
nl_constants.ATTR_IPV4_SRC, |
| 321 |
conntrack._convert_text_to_binary( |
| 322 |
FAKE_ENTRY['src'], 4)), |
| 323 |
mock.call(conntrack_filter, |
| 324 |
nl_constants.ATTR_IPV4_DST, |
| 325 |
conntrack._convert_text_to_binary( |
| 326 |
FAKE_UDP_ENTRY['dst'], 4)), |
| 327 |
] |
| 328 |
nl_lib.nfct.nfct_set_attr.assert_has_calls(calls, any_order=True) |
| 329 |
nl_lib.nfct.nfct_destroy.assert_called_once() |
| 330 |
nl_lib.nfct.nfct_close.assert_called_once() |