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 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678 679 680 681 682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751
|
#
# Copyright 2012-2018 Canonical Ltd.
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License version 3,
# as published by the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
import unittest
import tests.unit.support
import ufw.common
class CommonTestCase(unittest.TestCase):
def setUp(self):
self.rules = {
"any": ufw.common.UFWRule("allow", "any"),
"ipv6": ufw.common.UFWRule("deny", "ipv6"),
"tcp": ufw.common.UFWRule("limit", "tcp"),
"udp": ufw.common.UFWRule("allow", "udp"),
"reject-tcp": ufw.common.UFWRule("reject", "tcp"),
"reject-udp": ufw.common.UFWRule("reject", "udp"),
"full-any": ufw.common.UFWRule("allow", "any",
dport="123", dst="10.0.0.1", sport="124",
src="10.0.0.2", direction="in"),
"full-ipv6": ufw.common.UFWRule("deny", "ipv6",
dport="123", dst="10.0.0.1", sport="124",
src="10.0.0.2", direction="in"),
"full-tcp": ufw.common.UFWRule("limit", "tcp",
dport="123", dst="10.0.0.1", sport="124",
src="10.0.0.2", direction="out"),
"full-udp": ufw.common.UFWRule("reject", "udp",
dport="123", dst="10.0.0.1", sport="124",
src="10.0.0.2", direction="out"),
"dapp": ufw.common.UFWRule("allow", "any"),
"sapp": ufw.common.UFWRule("deny", "any"),
"app-both": ufw.common.UFWRule("deny", "any"),
"multi-dport": ufw.common.UFWRule("allow", "tcp",
dport="80,443,8080:8090"),
"multi-sport": ufw.common.UFWRule("allow", "tcp",
sport="80,443,8080:8090"),
"multi-both": ufw.common.UFWRule("allow", "tcp",
dport="80,443,8080:8090", sport="23"),
"log": ufw.common.UFWRule("allow", "tcp", dport="22"),
"log-all": ufw.common.UFWRule("allow", "tcp", dport="22"),
}
self.rules['dapp'].dapp = "Apache"
self.rules['dapp'].dport = "80"
self.rules['dapp'].proto = "tcp"
self.rules['sapp'].sapp = "Apache"
self.rules['sapp'].sport = "80"
self.rules['sapp'].proto = "tcp"
self.rules['app-both'].dapp = "Apache"
self.rules['app-both'].dport = "80"
self.rules['app-both'].proto = "tcp"
self.rules['app-both'].sapp = "Apache"
self.rules['app-both'].sport = "80"
self.rules['app-both'].proto = "tcp"
self.rules['log'].set_logtype("log")
self.rules['log-all'].set_logtype("log-all")
def tearDown(self):
self.rules = None
def test_ufwerror(self):
'''Test UFWError'''
try:
raise ufw.common.UFWError("test")
except ufw.common.UFWError as e:
self.assertEqual(e.value, "test", "'%s' != 'test'" % e.value)
return
self.assertTrue(False, "Did not raise an error")
def test_ufwerror_str(self):
'''Test UFWError.str()'''
e = ufw.common.UFWError("test")
search = repr("test")
self.assertEqual(str(e), search, "'%s' != 'test'" % search)
def test__init_(self):
'''Test UFWRule.__init__()'''
r = ufw.common.UFWRule("allow", "tcp", "22")
self.assertEqual(r.action, "allow")
self.assertEqual(r.protocol, "tcp")
self.assertEqual(r.dport, "22")
tests.unit.support.check_for_exception(self, ufw.common.UFWError,
ufw.common.UFWRule,
"allow",
"nonexistent",
"22")
def test__get_attrib(self):
'''Test _get_attrib()'''
res = self.rules["any"]._get_attrib()
search = "'-p all -j ACCEPT', action=allow, comment=, dapp=, " + \
"direction=in, dport=any, dst=0.0.0.0/0, forward=False, " + \
"interface_in=, interface_out=, logtype=, multi=False, " + \
"position=0, protocol=any, remove=False, sapp=, " + \
"sport=any, src=0.0.0.0/0, updated=False, v6=False"
self.assertEqual(res, search, "'%s' != '%s'" % (res, search))
def test_dup_rule(self):
'''Test dup_rule()'''
r = self.rules["any"].dup_rule()
self.assertEqual(ufw.common.UFWRule.match(r, self.rules["any"]), 0)
def test_format_rule(self):
'''Test format_rule()'''
s = str(self.rules["any"])
self.assertEqual(s, "-p all -j ACCEPT")
s = str(self.rules["app-both"])
self.assertEqual(s, "-p all --dport 80 --sport 80 -j DROP " + \
"-m comment --comment 'dapp_Apache,sapp_Apache'")
s = str(self.rules["dapp"])
self.assertEqual(s, "-p all --dport 80 -j ACCEPT " + \
"-m comment --comment 'dapp_Apache'")
s = str(self.rules["full-any"])
self.assertEqual(s, "-p all -d 10.0.0.1 --dport 123 " + \
"-s 10.0.0.2 --sport 124 -j ACCEPT")
s = str(self.rules["full-ipv6"])
self.assertEqual(s, "-p ipv6 -d 10.0.0.1 --dport 123 " + \
"-s 10.0.0.2 --sport 124 -j DROP")
s = str(self.rules["full-tcp"])
self.assertEqual(s, "-p tcp -d 10.0.0.1 --dport 123 " + \
"-s 10.0.0.2 --sport 124 -j LIMIT")
s = str(self.rules["full-udp"])
self.assertEqual(s, "-p udp -d 10.0.0.1 --dport 123 " + \
"-s 10.0.0.2 --sport 124 -j REJECT")
s = str(self.rules["ipv6"])
self.assertEqual(s, "-p ipv6 -j DROP")
s = str(self.rules["log"])
self.assertEqual(s, "-p tcp --dport 22 -j ACCEPT_log")
s = str(self.rules["log-all"])
self.assertEqual(s, "-p tcp --dport 22 -j ACCEPT_log-all")
r = self.rules["log-all"].dup_rule()
r.set_action("deny_log-all")
s = str(r)
self.assertEqual(s, "-p tcp --dport 22 -j DROP_log-all")
s = str(self.rules["multi-both"])
self.assertEqual(s, "-p tcp -m multiport " + \
"--dports 80,443,8080:8090 " + \
"-m multiport --sports 23 -j ACCEPT")
s = str(self.rules["multi-dport"])
self.assertEqual(s, "-p tcp -m multiport " + \
"--dports 80,443,8080:8090 -j ACCEPT")
s = str(self.rules["multi-sport"])
self.assertEqual(s, "-p tcp -m multiport " + \
"--sports 80,443,8080:8090 -j ACCEPT")
s = str(self.rules["reject-tcp"])
self.assertEqual(s, "-p tcp -j REJECT --reject-with tcp-reset")
s = str(self.rules["reject-udp"])
self.assertEqual(s, "-p udp -j REJECT")
s = str(self.rules["sapp"])
self.assertEqual(s, "-p all --sport 80 -j DROP " + \
"-m comment --comment 'sapp_Apache'")
s = str(self.rules["tcp"])
self.assertEqual(s, "-p tcp -j LIMIT")
s = str(self.rules["udp"])
self.assertEqual(s, "-p udp -j ACCEPT")
def test_set_action(self):
'''Test set_action()'''
r = self.rules["any"]
for action in ['allow', 'deny', 'reject', 'limit']:
r.set_action(action)
self.assertEqual(action, r.action, "%s != %s" %
(action, r.action))
def test_set_port(self):
'''Test set_port()'''
rule = self.rules["any"]
for loc in ['dst', 'src']:
for port in ['any',
'1',
'22',
'1023',
'1024',
'1025',
'65535',
'1,2,3,4,5,6,7,8,9,10,11,12,13,14,15',
'80,443,8080:8090',
'22:25']:
r = rule.dup_rule()
r.set_port(port, loc)
if loc == 'dst':
self.assertEqual(port, r.dport, "%s != %s" % (port,
r.dport))
else:
self.assertEqual(port, r.sport, "%s != %s" % (port,
r.sport))
r = self.rules["dapp"].dup_rule()
r.dapp = "Apache"
r.set_port("Apache", "dst")
self.assertEqual(r.dapp, r.dport, "%s != %s" % (r.dapp, r.dport))
r = self.rules["sapp"].dup_rule()
r.sapp = "Apache"
r.set_port("Apache", "src")
self.assertEqual(r.sapp, r.sport, "%s != %s" % (r.sapp, r.sport))
def test_set_port_bad(self):
'''Test set_port() - bad'''
rule = self.rules["any"]
for loc in ['dst', 'src']:
for port in ['an',
'0',
',',
'',
' ',
22,
'65536',
',443,8080:8090',
'443:8080:8090',
'0:65536',
'2:1',
'80,',
'80,443,8080:',
':8090',
'1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16',
]:
r = rule.dup_rule()
e = ufw.common.UFWError
if port == 22:
e = TypeError
tests.unit.support.check_for_exception(self,
e,
r.set_port,
port,
loc)
def test_set_protocol(self):
'''Test set_protocol()'''
r = self.rules["any"]
for proto in ['any', 'tcp', 'udp', 'ipv6', 'esp', 'ah', 'vrrp']:
r.set_protocol(proto)
self.assertEqual(proto, r.protocol, "%s != %s" %
(proto, r.protocol))
def test_set_protocol_bad(self):
'''Test set_protocol() - bad'''
r = self.rules["any"]
for proto in ['an', 'cp', 'up', 'nonexistent']:
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
r.set_protocol,
proto)
def test__fix_anywhere(self):
'''Test _fix_anywhere()'''
x = self.rules["any"].dup_rule()
x.set_v6(False)
x._fix_anywhere()
search = "0.0.0.0/0"
self.assertEqual(x.dst, search, "'%s' != '%s'" % (x.dst, search))
y = x.dup_rule()
y.set_v6(True)
y._fix_anywhere()
search = "::/0"
self.assertEqual(y.dst, search, "'%s' != '%s'" % (y.dst, search))
def test_set_v6(self):
'''Test set_v6()'''
r = self.rules["any"]
for ipv6 in [True, False]:
r.set_v6(ipv6)
self.assertEqual(ipv6, r.v6, "%s != %s" %
(ipv6, r.v6))
def test_set_src(self):
'''Test set_src()'''
r = self.rules["any"]
for src in ["10.0.0.3"]:
r.set_src(src)
self.assertEqual(src, r.src, "%s != %s" %
(src, r.src))
def test_set_src_bad(self):
'''Test set_src() - bad'''
r = self.rules["any"]
for src in ["10.0.0.", "10..0.0.3"]:
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
r.set_src,
src)
def test_set_dst(self):
'''Test set_dst()'''
r = self.rules["any"]
for dst in ["10.0.0.3"]:
r.set_dst(dst)
self.assertEqual(dst, r.dst, "%s != %s" %
(dst, r.dst))
def test_set_dst_bad(self):
'''Test set_dst() - bad'''
r = self.rules["any"]
for dst in ["10.0.0.", "10..0.0.3"]:
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
r.set_dst,
dst)
def test_set_interface(self):
'''Test set_interface()'''
r = self.rules["any"]
for if_type in ["in", "out"]:
for interface in ["eth0", "wlan1", "br_lan", "virbr0-nic", "0eth",
"eth0_1", "eth0.1", "foo%bar", "foo@Bar",
"=foo", "vethQNIAKF@if18", "lo"]:
r.set_interface(if_type, interface)
if if_type == "in":
self.assertEqual(interface, r.interface_in, "%s != %s" %
(interface, r.interface_in))
else:
self.assertEqual(interface, r.interface_out, "%s != %s" %
(interface, r.interface_out))
def test_set_interface_bad(self):
'''Test set_interface() - bad'''
r = self.rules["any"]
interface = "eth0"
for if_type in ["ina", "ot"]:
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
r.set_interface,
if_type,
interface)
for if_type in ["in", "out"]:
for interface in ["\tfoo", "<bad", "also/bad", "eth0:0", "!eth0",
"", ".", "..", "$foo", "`uname`", "a" * 16]:
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
r.set_interface,
if_type,
interface)
def test_set_position(self):
'''Test set_position()'''
r = self.rules["any"]
r.set_position(2)
self.assertEqual(2, r.position)
def test_set_position_bad(self):
'''Test set_position() - bad'''
r = self.rules["any"]
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
r.set_position,
'a')
def test_set_logtype(self):
'''Test set_logtype()'''
r = self.rules["any"]
for logtype in ["", "log", "log-all"]:
r.set_logtype(logtype)
self.assertEqual(logtype, r.logtype, "%s != %s" %
(logtype, r.logtype))
def test_set_logtype_bad(self):
'''Test set_logtype() - bad'''
r = self.rules["any"]
for logtype in ["a", "loga", "d"]:
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
r.set_logtype,
logtype)
def test_set_direction(self):
'''Test set_direction()'''
r = self.rules["any"]
for direction in ["in", "out"]:
r.set_direction(direction)
self.assertEqual(direction, r.direction, "%s != %s" %
(direction, r.direction))
def test_set_direction_bad(self):
'''Test set_direction() - bad'''
r = self.rules["any"]
for direction in ["", "ina", "outta"]:
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
r.set_direction,
direction)
def test_normalize(self):
'''Test normalize()'''
# Test the pre-canned rules above-- none of them are normalized, so
# UFWRule.match() should always be 0
keys = list(self.rules.keys())
keys.sort()
for rule in keys:
r = self.rules[rule].dup_rule()
r.normalize()
self.assertEqual(ufw.common.UFWRule.match(self.rules[rule], r), 0,
"'%s' != '%s'" % (self.rules[rule], r))
# Bad rules
bad = ufw.common.UFWRule("allow", "any")
bad.src = "1000.0.0.1"
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
bad.normalize)
bad = None
bad = ufw.common.UFWRule("allow", "any")
bad.dst = "1000.0.0.1"
tests.unit.support.check_for_exception(self,
ufw.common.UFWError,
bad.normalize)
# Normalized rules
data = [
(False, '192.168.0.1', '192.168.0.1'),
(False, '192.168.0.1/31', '192.168.0.0/31'),
(False, '192.168.0.1/255.255.255.0', '192.168.0.0/24'),
(True, '::1', '::1'),
(True, 'ff80:123:4567:89ab:cdef:123:4567:89ab/112',
'ff80:123:4567:89ab:cdef:123:4567:89ab/112')
]
for (v6, addr, expected) in data:
rule = ufw.common.UFWRule("allow", "any", dst=addr)
rule.set_v6(v6)
rule.normalize()
self.assertEqual(expected, rule.dst,
"'%s' != '%s'" % (expected, rule.dst))
self.assertEqual(addr != expected, rule.updated,
"'%s' not updated" % addr)
rule = ufw.common.UFWRule("allow", "any", src=addr)
rule.set_v6(v6)
rule.normalize()
self.assertEqual(expected, rule.src,
"'%s' != '%s'" % (expected, rule.src))
self.assertEqual(addr != expected, rule.updated,
"'%s' not updated" % addr)
def test_match(self):
'''Test match()'''
x = self.rules["full-any"].dup_rule()
y = self.rules["full-any"].dup_rule()
self.assertEqual(ufw.common.UFWRule.match(x, y), 0)
for action in ['reject', 'deny', 'limit']:
y = self.rules["full-any"].dup_rule()
y.set_action(action)
self.assertEqual(ufw.common.UFWRule.match(x, y), -1)
for logtype in ['log', 'log-all']:
y = self.rules["full-any"].dup_rule()
y.set_logtype(logtype)
self.assertEqual(ufw.common.UFWRule.match(x, y), -1)
for comment in ['comment1', 'comment2']:
y = self.rules["full-any"].dup_rule()
y.set_comment(comment)
self.assertEqual(ufw.common.UFWRule.match(x, y), -2)
y = self.rules["full-any"].dup_rule()
y.set_port("456", loc="dst")
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
y = self.rules["full-any"].dup_rule()
y.set_port("456", loc="src")
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
y = self.rules["full-any"].dup_rule()
y.set_protocol("tcp")
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
y = self.rules["full-any"].dup_rule()
y.set_src("192.168.0.1")
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
y = self.rules["full-any"].dup_rule()
y.set_dst("192.168.0.1")
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
y = self.rules["full-any"].dup_rule()
y.set_dst("fe80::1")
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
y = ufw.common.UFWRule("allow", "tcp", dst="fe80::1")
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
y = self.rules["full-any"].dup_rule()
y.sapp = "OpenSSH"
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
y = self.rules["full-any"].dup_rule()
y.dapp = "OpenSSH"
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
y = self.rules["full-any"].dup_rule()
y.set_interface("in", "eth0")
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
x = ufw.common.UFWRule("allow", "tcp", direction="out")
y = x.dup_rule()
y.set_interface("out", "eth0")
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
x = self.rules["any"].dup_rule()
y = self.rules["any"].dup_rule()
y.v6 = True
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
x = self.rules["any"].dup_rule()
y = self.rules["any"].dup_rule()
y.forward = True
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
x = self.rules["full-any"].dup_rule()
y = self.rules["full-any"].dup_rule()
y.forward = True
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
x = self.rules["multi-both"].dup_rule()
y = self.rules["multi-both"].dup_rule()
y.forward = True
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
x = ufw.common.UFWRule("allow", "tcp", direction="out")
x.set_interface("out", "eth0")
y = x.dup_rule()
y.direction = "in"
self.assertEqual(ufw.common.UFWRule.match(x, y), 1)
tests.unit.support.check_for_exception(self, ValueError,
x.match,
None)
def test_fuzzy_dst_match(self):
'''Test fuzzy_dst_match()'''
x = self.rules["full-any"].dup_rule()
y = self.rules["full-any"].dup_rule()
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 0)
x.set_protocol("tcp")
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), -1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["multi-dport"].dup_rule()
y = self.rules["multi-dport"].dup_rule()
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 0)
y.set_protocol("any")
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), -1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["multi-dport"].dup_rule()
y = self.rules["multi-dport"].dup_rule()
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 0)
y.set_protocol("any")
y.set_port("%s,8181" % y.dport, "dst")
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["any"].dup_rule()
x.set_port("80")
x.set_protocol("tcp")
y = self.rules["multi-dport"].dup_rule()
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), -1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["any"].dup_rule()
x.set_port("8081")
x.set_protocol("tcp")
y = self.rules["multi-dport"].dup_rule()
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), -1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["any"].dup_rule()
x.set_port("8079")
x.set_protocol("tcp")
y = self.rules["multi-dport"].dup_rule()
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["full-any"].dup_rule()
y = self.rules["full-any"].dup_rule()
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 0)
y.set_direction("out")
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
x = self.rules["full-any"].dup_rule()
y = self.rules["full-any"].dup_rule()
y.set_dst("10.0.0.3")
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["full-any"].dup_rule()
y = self.rules["full-any"].dup_rule()
y.set_dst("11.0.0.0/8")
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["full-any"].dup_rule()
y = self.rules["full-any"].dup_rule()
y.set_interface("in", "eth0")
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), -1)
x = self.rules["full-any"].dup_rule()
x.set_interface("in", "eth0")
y = x.dup_rule()
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 0)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 0)
x = self.rules["full-any"].dup_rule()
x.set_interface("in", "eth0")
y = x.dup_rule()
y.set_interface("in", "eth1")
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["full-any"].dup_rule()
x.set_interface("in", "lo")
y = x.dup_rule()
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 0)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 0)
x = self.rules["full-any"].dup_rule()
x.set_interface("in", "lo")
y = x.dup_rule()
y.set_dst("11.0.0.0/8")
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["any"].dup_rule()
y = x.dup_rule()
y.set_v6(True)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
x = self.rules["any"].dup_rule()
y = x.dup_rule()
y.forward = True
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(x, y), 1)
self.assertEqual(ufw.common.UFWRule.fuzzy_dst_match(y, x), 1)
tests.unit.support.check_for_exception(self, ValueError,
x.fuzzy_dst_match,
None)
def test__is_anywhere(self):
'''Test _is_anywhere()'''
r = self.rules['any']
self.assertTrue(r._is_anywhere("::/0"))
self.assertTrue(r._is_anywhere("0.0.0.0/0"))
self.assertFalse(r._is_anywhere("::1"))
self.assertFalse(r._is_anywhere("fe80::1/16"))
self.assertFalse(r._is_anywhere("127.0.0.1"))
self.assertFalse(r._is_anywhere("127.0.0.1/32"))
def test_get_app_tuple(self):
'''Test get_app_tuple()'''
r = self.rules['dapp'].dup_rule()
t = r.get_app_tuple().split()
self.assertEqual(self.rules['dapp'].dapp, t[0])
self.assertEqual(self.rules['dapp'].dst, t[1])
self.assertEqual("any", t[2])
self.assertEqual("0.0.0.0/0", t[3])
r.set_direction("in")
t = r.get_app_tuple().split()
self.assertEqual(self.rules['dapp'].dapp, t[0])
self.assertEqual(self.rules['dapp'].dst, t[1])
self.assertEqual("any", t[2])
self.assertEqual("0.0.0.0/0", t[3])
self.assertEqual("in", t[4])
r.set_interface("in", "eth0")
t = r.get_app_tuple().split()
self.assertEqual(self.rules['dapp'].dapp, t[0])
self.assertEqual(self.rules['dapp'].dst, t[1])
self.assertEqual("any", t[2])
self.assertEqual("0.0.0.0/0", t[3])
self.assertEqual("in_eth0", t[4])
r = self.rules['sapp'].dup_rule()
t = r.get_app_tuple().split()
self.assertEqual("any", t[0])
self.assertEqual("0.0.0.0/0", t[1])
self.assertEqual(self.rules['sapp'].sapp, t[2])
self.assertEqual(self.rules['sapp'].src, t[3])
r.set_direction("out")
t = r.get_app_tuple().split()
self.assertEqual("any", t[0])
self.assertEqual("0.0.0.0/0", t[1])
self.assertEqual(self.rules['sapp'].sapp, t[2])
self.assertEqual(self.rules['sapp'].src, t[3])
self.assertEqual("out", t[4])
r.set_interface("out", "eth0")
t = r.get_app_tuple().split()
self.assertEqual("any", t[0])
self.assertEqual("0.0.0.0/0", t[1])
self.assertEqual(self.rules['sapp'].sapp, t[2])
self.assertEqual(self.rules['sapp'].src, t[3])
self.assertEqual("out_eth0", t[4])
# also test with '_' in the name (LP: #1098472)
r = self.rules['sapp'].dup_rule()
t = r.get_app_tuple().split()
self.assertEqual("any", t[0])
self.assertEqual("0.0.0.0/0", t[1])
self.assertEqual(self.rules['sapp'].sapp, t[2])
self.assertEqual(self.rules['sapp'].src, t[3])
r.set_interface("out", "br_lan")
t = r.get_app_tuple().split()
self.assertEqual("any", t[0])
self.assertEqual("0.0.0.0/0", t[1])
self.assertEqual(self.rules['sapp'].sapp, t[2])
self.assertEqual(self.rules['sapp'].src, t[3])
self.assertEqual("out_br_lan", t[4])
def test_main(): # used by runner.py
tests.unit.support.run_unittest(
CommonTestCase
)
if __name__ == "__main__": # used when standalone
unittest.main()
|