1"""Tests for certbot_apache._internal.obj."""
2import unittest
3
4
5class VirtualHostTest(unittest.TestCase):
6    """Test the VirtualHost class."""
7
8    def setUp(self):
9        from certbot_apache._internal.obj import Addr
10        from certbot_apache._internal.obj import VirtualHost
11
12        self.addr1 = Addr.fromstring("127.0.0.1")
13        self.addr2 = Addr.fromstring("127.0.0.1:443")
14        self.addr_default = Addr.fromstring("_default_:443")
15
16        self.vhost1 = VirtualHost(
17            "filep", "vh_path", {self.addr1}, False, False, "localhost")
18
19        self.vhost1b = VirtualHost(
20            "filep", "vh_path", {self.addr1}, False, False, "localhost")
21
22        self.vhost2 = VirtualHost(
23            "fp", "vhp", {self.addr2}, False, False, "localhost")
24
25    def test_repr(self):
26        self.assertEqual(repr(self.addr2),
27            "certbot_apache._internal.obj.Addr(('127.0.0.1', '443'))")
28
29    def test_eq(self):
30        self.assertEqual(self.vhost1b, self.vhost1)
31        self.assertNotEqual(self.vhost1, self.vhost2)
32        self.assertEqual(str(self.vhost1b), str(self.vhost1))
33        self.assertNotEqual(self.vhost1b, 1234)
34
35    def test_ne(self):
36        self.assertNotEqual(self.vhost1, self.vhost2)
37        self.assertEqual(self.vhost1, self.vhost1b)
38
39    def test_conflicts(self):
40        from certbot_apache._internal.obj import Addr
41        from certbot_apache._internal.obj import VirtualHost
42
43        complex_vh = VirtualHost(
44            "fp", "vhp",
45            {Addr.fromstring("*:443"), Addr.fromstring("1.2.3.4:443")},
46            False, False)
47        self.assertTrue(complex_vh.conflicts([self.addr1]))
48        self.assertTrue(complex_vh.conflicts([self.addr2]))
49        self.assertFalse(complex_vh.conflicts([self.addr_default]))
50
51        self.assertTrue(self.vhost1.conflicts([self.addr2]))
52        self.assertFalse(self.vhost1.conflicts([self.addr_default]))
53
54        self.assertFalse(self.vhost2.conflicts([self.addr1,
55                                                self.addr_default]))
56
57    def test_same_server(self):
58        from certbot_apache._internal.obj import VirtualHost
59        no_name1 = VirtualHost(
60            "fp", "vhp", {self.addr1}, False, False, None)
61        no_name2 = VirtualHost(
62            "fp", "vhp", {self.addr2}, False, False, None)
63        no_name3 = VirtualHost(
64            "fp", "vhp", {self.addr_default},
65            False, False, None)
66        no_name4 = VirtualHost(
67            "fp", "vhp", {self.addr2, self.addr_default},
68            False, False, None)
69
70        self.assertTrue(self.vhost1.same_server(self.vhost2))
71        self.assertTrue(no_name1.same_server(no_name2))
72
73        self.assertFalse(self.vhost1.same_server(no_name1))
74        self.assertFalse(no_name1.same_server(no_name3))
75        self.assertFalse(no_name1.same_server(no_name4))
76
77
78class AddrTest(unittest.TestCase):
79    """Test obj.Addr."""
80    def setUp(self):
81        from certbot_apache._internal.obj import Addr
82        self.addr = Addr.fromstring("*:443")
83
84        self.addr1 = Addr.fromstring("127.0.0.1")
85        self.addr2 = Addr.fromstring("127.0.0.1:*")
86
87        self.addr_defined = Addr.fromstring("127.0.0.1:443")
88        self.addr_default = Addr.fromstring("_default_:443")
89
90    def test_wildcard(self):
91        self.assertFalse(self.addr.is_wildcard())
92        self.assertTrue(self.addr1.is_wildcard())
93        self.assertTrue(self.addr2.is_wildcard())
94
95    def test_get_sni_addr(self):
96        from certbot_apache._internal.obj import Addr
97        self.assertEqual(
98            self.addr.get_sni_addr("443"), Addr.fromstring("*:443"))
99        self.assertEqual(
100            self.addr.get_sni_addr("225"), Addr.fromstring("*:225"))
101        self.assertEqual(
102            self.addr1.get_sni_addr("443"), Addr.fromstring("127.0.0.1"))
103
104    def test_conflicts(self):
105        # Note: Defined IP is more important than defined port in match
106        self.assertTrue(self.addr.conflicts(self.addr1))
107        self.assertTrue(self.addr.conflicts(self.addr2))
108        self.assertTrue(self.addr.conflicts(self.addr_defined))
109        self.assertFalse(self.addr.conflicts(self.addr_default))
110
111        self.assertFalse(self.addr1.conflicts(self.addr))
112        self.assertTrue(self.addr1.conflicts(self.addr_defined))
113        self.assertFalse(self.addr1.conflicts(self.addr_default))
114
115        self.assertFalse(self.addr_defined.conflicts(self.addr1))
116        self.assertFalse(self.addr_defined.conflicts(self.addr2))
117        self.assertFalse(self.addr_defined.conflicts(self.addr))
118        self.assertFalse(self.addr_defined.conflicts(self.addr_default))
119
120        self.assertTrue(self.addr_default.conflicts(self.addr))
121        self.assertTrue(self.addr_default.conflicts(self.addr1))
122        self.assertTrue(self.addr_default.conflicts(self.addr_defined))
123
124        # Self test
125        self.assertTrue(self.addr.conflicts(self.addr))
126        self.assertTrue(self.addr1.conflicts(self.addr1))
127        # This is a tricky one...
128        self.assertTrue(self.addr1.conflicts(self.addr2))
129
130    def test_equal(self):
131        self.assertEqual(self.addr1, self.addr2)
132        self.assertNotEqual(self.addr, self.addr1)
133        self.assertNotEqual(self.addr, 123)
134
135    def test_not_equal(self):
136        self.assertEqual(self.addr1, self.addr2)
137        self.assertNotEqual(self.addr, self.addr1)
138
139
140if __name__ == "__main__":
141    unittest.main()  # pragma: no cover
142