~ubuntu-branches/ubuntu/trusty/txtorcon/trusty-proposed

« back to all changes in this revision

Viewing changes to test/test_addrmap.py

  • Committer: Package Import Robot
  • Author(s): Jérémy Bobbio
  • Date: 2014-01-21 15:10:52 UTC
  • mfrom: (1.1.3)
  • Revision ID: package-import@ubuntu.com-20140121151052-r1dw06if9912ihbp
Tags: 0.9.1-1
* New upstream release.
* Update debian/watch and verify upstream signature
* Improve debian/README.source

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
import datetime
 
2
from twisted.trial import unittest
 
3
from twisted.internet import task
 
4
from twisted.internet.interfaces import IReactorTime
 
5
from zope.interface import implements
 
6
 
 
7
from txtorcon.addrmap import AddrMap
 
8
from txtorcon.interface import IAddrListener
 
9
 
 
10
 
 
11
class AddrMapTests(unittest.TestCase):
 
12
    implements(IAddrListener)
 
13
 
 
14
    fmt = '%Y-%m-%d %H:%M:%S'
 
15
 
 
16
    def test_parse(self):
 
17
        """
 
18
        Make sure it's parsing things properly.
 
19
        """
 
20
 
 
21
        now = datetime.datetime.now() + datetime.timedelta(seconds=10)
 
22
        nowutc = datetime.datetime.utcnow() + datetime.timedelta(seconds=10)
 
23
        ## we need to not-barf on extra args as per control-spec.txt
 
24
        line = 'www.example.com 72.30.2.43 "%s" EXPIRES="%s" FOO=bar BAR=baz' % (now.strftime(self.fmt), nowutc.strftime(self.fmt))
 
25
        am = AddrMap()
 
26
        am.update(line)
 
27
        addr = am.find('www.example.com')
 
28
 
 
29
        self.assertTrue(addr.ip == '72.30.2.43' or addr.ip.exploded == '72.30.2.43')
 
30
        ## maybe not the most robust, should convert to
 
31
        ## seconds-since-epoch instead? the net result of the parsing
 
32
        ## is we've rounded to seconds...
 
33
        self.assertEqual(addr.expires.ctime(), nowutc.ctime())
 
34
 
 
35
        ## this will have resulted in an expiry call, which we need to
 
36
        ## cancel to keep the reactor clean. for consistency, we use
 
37
        ## the IReactorTime interface from AddrMap
 
38
        am.scheduler.getDelayedCalls()[0].cancel()
 
39
 
 
40
    def test_expires(self):
 
41
        """
 
42
        Test simply expiry case
 
43
        """
 
44
 
 
45
        clock = task.Clock()
 
46
        am = AddrMap()
 
47
        am.scheduler = IReactorTime(clock)
 
48
 
 
49
        now = datetime.datetime.now() + datetime.timedelta(seconds=10)
 
50
        nowutc = datetime.datetime.utcnow() + datetime.timedelta(seconds=10)
 
51
        line = 'www.example.com 72.30.2.43 "%s" EXPIRES="%s"' % (now.strftime(self.fmt), nowutc.strftime(self.fmt))
 
52
 
 
53
        am.update(line)
 
54
 
 
55
        self.assertTrue('www.example.com' in am.addr)
 
56
        ## advance time past when the expiry should have occurred
 
57
        clock.advance(10)
 
58
        self.assertTrue('www.example.com' not in am.addr)
 
59
 
 
60
    def test_expires_never(self):
 
61
        """
 
62
        Test a NEVER expires line, as in what we'd get a startup for a
 
63
        configured address-mapping.
 
64
        """
 
65
 
 
66
        clock = task.Clock()
 
67
        am = AddrMap()
 
68
        am.scheduler = IReactorTime(clock)
 
69
 
 
70
        line = 'www.example.com 72.30.2.43 "NEVER"'
 
71
        am.update(line)
 
72
 
 
73
        self.assertTrue('www.example.com' in am.addr)
 
74
        self.assertEqual(len(clock.getDelayedCalls()), 0)
 
75
 
 
76
    def test_expires_old(self):
 
77
        """
 
78
        Test something that expires before "now"
 
79
        """
 
80
 
 
81
        clock = task.Clock()
 
82
        am = AddrMap()
 
83
        am.scheduler = IReactorTime(clock)
 
84
 
 
85
        now = datetime.datetime.now() + datetime.timedelta(seconds=-10)
 
86
        nowutc = datetime.datetime.utcnow() + datetime.timedelta(seconds=-10)
 
87
        line = 'www.example.com 72.30.2.43 "%s" EXPIRES="%s"' % (now.strftime(self.fmt), nowutc.strftime(self.fmt))
 
88
 
 
89
        am.update(line)
 
90
        self.assertTrue('www.example.com' in am.addr)
 
91
        ## arguably we shouldn't even have put this in the map maybe,
 
92
        ## but the reactor needs to iterate before our expiry callback
 
93
        ## gets called (right away) which is simulated by the
 
94
        ## clock.advance call
 
95
        clock.advance(0)
 
96
        self.assertTrue('www.example.com' not in am.addr)
 
97
 
 
98
    def test_expires_with_update(self):
 
99
        """
 
100
        This test updates the expiry time and checks that we properly
 
101
        delay our expiry callback.
 
102
        """
 
103
        clock = task.Clock()
 
104
        am = AddrMap()
 
105
        am.scheduler = IReactorTime(clock)
 
106
 
 
107
        ## now do an actual update to an existing Addr entry.
 
108
        now = datetime.datetime.now() + datetime.timedelta(seconds=10)
 
109
        nowutc = datetime.datetime.utcnow() + datetime.timedelta(seconds=10)
 
110
        line = 'www.example.com 72.30.2.43 "%s" EXPIRES="%s"' % (now.strftime(self.fmt), nowutc.strftime(self.fmt))
 
111
        am.update(line)
 
112
        self.assertTrue(am.find('www.example.com'))
 
113
 
 
114
        ## the update
 
115
        now = datetime.datetime.now() + datetime.timedelta(seconds=20)
 
116
        nowutc = datetime.datetime.utcnow() + datetime.timedelta(seconds=20)
 
117
        line = 'www.example.com 72.30.2.43 "%s" EXPIRES="%s"' % (now.strftime(self.fmt), nowutc.strftime(self.fmt))
 
118
        am.update(line)
 
119
        self.assertTrue('www.example.com' in am.addr)
 
120
 
 
121
        ## advance time by the old expiry value and we should still
 
122
        ## find the entry
 
123
        clock.advance(10)
 
124
        self.assertTrue('www.example.com' in am.addr)
 
125
 
 
126
        ## ...but advance past the new expiry (another 10 seconds) and
 
127
        ## it should vanish
 
128
        clock.advance(10)
 
129
        self.assertTrue('www.example.com' not in am.addr)
 
130
 
 
131
    def test_8596_cached_1(self):
 
132
        clock = task.Clock()
 
133
        am = AddrMap()
 
134
        am.scheduler = IReactorTime(clock)
 
135
 
 
136
        line = 'example.com 192.0.2.1 NEVER CACHED="YES"'
 
137
        am.update(line)
 
138
 
 
139
        self.assertTrue('example.com' in am.addr)
 
140
        self.assertEqual(len(clock.getDelayedCalls()), 0)
 
141
 
 
142
    def test_8596_cached_2(self):
 
143
        clock = task.Clock()
 
144
        am = AddrMap()
 
145
        am.scheduler = IReactorTime(clock)
 
146
 
 
147
        line = 'example.com 192.0.43.10 "2013-04-03 22:29:11" EXPIRES="2013-04-03 20:29:11" CACHED="NO"'
 
148
        am.update(line)
 
149
 
 
150
        self.assertTrue('example.com' in am.addr)
 
151
        self.assertEqual(len(clock.getDelayedCalls()), 1)
 
152
 
 
153
    def test_8596_cached_3(self):
 
154
        clock = task.Clock()
 
155
        am = AddrMap()
 
156
        am.scheduler = IReactorTime(clock)
 
157
 
 
158
        line = 'example.invalid <error> "2013-04-03 08:28:52" error=yes EXPIRES="2013-04-03 06:28:52" CACHE="NO"'
 
159
        am.update(line)
 
160
 
 
161
        self.assertTrue('example.invalid' not in am.addr)
 
162
        self.assertEqual(len(clock.getDelayedCalls()), 0)
 
163
 
 
164
    def addrmap_expired(self, name):
 
165
        self.expires.append(name)
 
166
 
 
167
    def addrmap_added(self, addr):
 
168
        self.addrmap.append(addr)
 
169
 
 
170
    def test_listeners(self):
 
171
        self.expires = []
 
172
        self.addrmap = []
 
173
 
 
174
        clock = task.Clock()
 
175
        am = AddrMap()
 
176
        am.scheduler = IReactorTime(clock)
 
177
        am.add_listener(self)
 
178
 
 
179
        now = datetime.datetime.now() + datetime.timedelta(seconds=10)
 
180
        nowutc = datetime.datetime.utcnow() + datetime.timedelta(seconds=10)
 
181
        line = 'www.example.com 72.30.2.43 "%s" EXPIRES="%s"' % (now.strftime(self.fmt), nowutc.strftime(self.fmt))
 
182
 
 
183
        am.update(line)
 
184
 
 
185
        ## see if our listener got an update
 
186
        a = am.find('www.example.com')
 
187
        self.assertEqual(self.addrmap, [a])
 
188
 
 
189
        ## advance time past when the expiry should have occurred
 
190
        clock.advance(10)
 
191
 
 
192
        ## check that our listener got an expires event
 
193
        self.assertEqual(self.expires, ['www.example.com'])