Git Repository Public Repository

nip.io

URLs

Copy to Clipboard
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
# Copyright 2019 Exentrique Solutions Ltd
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

import collections
import os
import sys
import unittest

from assertpy import assert_that
from mock.mock import patch, call

from nipio.backend import DynamicBackend


class DynamicBackendTest(unittest.TestCase):
    def setUp(self):
        self.mock_sys_patcher = patch("nipio.backend.sys")
        self.mock_sys = self.mock_sys_patcher.start()

        self.mock_sys.stderr.write = sys.stderr.write

        import nipio
        nipio.backend._is_debug = lambda: True

    def tearDown(self):
        sys.stderr.flush()

        self.mock_sys_patcher.stop()

    def test_backend_ends_response_to_ANY_request_if_ip_is_blacklisted(self):
        self._send_commands(["Q", "subdomain.127.0.0.2.lcl.io", "IN", "ANY", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(["LOG", "Blacklisted: 127.0.0.2"])

    def test_backend_ends_response_to_A_request_if_ip_is_blacklisted(self):
        self._send_commands(["Q", "subdomain.127.0.0.2.lcl.io", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["LOG", "Blacklisted: 127.0.0.2"]
        )

    def test_backend_responds_to_ANY_request_with_valid_ip(self):
        self._send_commands(["Q", "subdomain.127.0.0.1.lcl.io", "IN", "ANY", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.0.1.lcl.io", "IN", "A", "200", "22", "127.0.0.1"],
            ["DATA", "subdomain.127.0.0.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.0.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_A_request_with_valid_ip(self):
        self._send_commands(["Q", "subdomain.127.0.0.1.lcl.io", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.0.1.lcl.io", "IN", "A", "200", "22", "127.0.0.1"],
            ["DATA", "subdomain.127.0.0.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.0.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_invalid_ip_in_ANY_request_with_self_ip(self):
        self._send_commands(["Q", "subdomain.127.0.1.lcl.io", "IN", "ANY", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_invalid_ip_in_A_request_with_self(self):
        self._send_commands(["Q", "subdomain.127.0.1.lcl.io", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_short_ip_in_ANY_request_with_self_ip(self):
        self._send_commands(["Q", "127.0.1.lcl.io", "IN", "ANY", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "127.0.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "127.0.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "127.0.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_short_ip_in_A_request_with_self(self):
        self._send_commands(["Q", "127.0.1.lcl.io", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "127.0.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "127.0.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "127.0.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_large_ip_in_ANY_request_with_self(self):
        self._send_commands(["Q", "subdomain.127.0.300.1.lcl.io", "IN", "ANY", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.300.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "subdomain.127.0.300.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.300.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_large_ip_in_A_request_with_self(self):
        self._send_commands(["Q", "subdomain.127.0.300.1.lcl.io", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.300.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "subdomain.127.0.300.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.300.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_string_in_ip_in_ANY_request_with_self(self):
        self._send_commands(["Q", "subdomain.127.0.STRING.1.lcl.io", "IN", "ANY", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.string.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "subdomain.127.0.string.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.string.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_string_in_ip_in_A_request_with_self(self):
        self._send_commands(["Q", "subdomain.127.0.STRING.1.lcl.io", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.string.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "subdomain.127.0.string.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.string.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_no_ip_in_ANY_request_with_self(self):
        self._send_commands(["Q", "subdomain.127.0.1.lcl.io", "IN", "ANY", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_no_ip_in_A_request_with_self(self):
        self._send_commands(["Q", "subdomain.127.0.1.lcl.io", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_self_domain_to_A_request(self):
        self._send_commands(["Q", "lcl.io", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_self_domain_to_ANY_request(self):
        self._send_commands(["Q", "lcl.io", "IN", "ANY", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "lcl.io", "IN", "A", "200", "22", "127.0.0.33"],
            ["DATA", "lcl.io", "IN", "NS", "200", "22", "ns1.lcl.io"],
            ["DATA", "lcl.io", "IN", "NS", "200", "22", "ns2.lcl.io"],
        )

    def test_backend_responds_to_name_servers_A_request_with_valid_ip(self):
        self._send_commands(["Q", "ns1.lcl.io", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "ns1.lcl.io", "IN", "A", "200", "22", "127.0.0.34"],
        )

    def test_backend_responds_to_name_servers_ANY_request_with_valid_ip(self):
        self._send_commands(["Q", "ns2.lcl.io", "IN", "ANY", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "ns2.lcl.io", "IN", "A", "200", "22", "127.0.0.35"],
        )

    def test_backend_responds_to_SOA_request_for_self(self):
        self._send_commands(["Q", "lcl.io", "IN", "SOA", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "lcl.io", "IN", "SOA", "200", "22", "MY_SOA"]
        )

    def test_backend_responds_to_SOA_request_for_valid_ip(self):
        self._send_commands(["Q", "subdomain.127.0.0.1.lcl.io", "IN", "SOA", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.0.1.lcl.io", "IN", "SOA", "200", "22", "MY_SOA"]
        )

    def test_backend_responds_to_SOA_request_for_invalid_ip(self):
        self._send_commands(["Q", "subdomain.127.0.1.lcl.io", "IN", "SOA", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.127.0.1.lcl.io", "IN", "SOA", "200", "22", "MY_SOA"]
        )

    def test_backend_responds_to_SOA_request_for_no_ip(self):
        self._send_commands(["Q", "subdomain.lcl.io", "IN", "SOA", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "subdomain.lcl.io", "IN", "SOA", "200", "22", "MY_SOA"]
        )

    def test_backend_responds_to_SOA_request_for_nameserver(self):
        self._send_commands(["Q", "ns1.lcl.io", "IN", "SOA", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["DATA", "ns1.lcl.io", "IN", "SOA", "200", "22", "MY_SOA"]
        )

    def test_backend_responds_to_A_request_for_unknown_domain_with_invalid_response(self):
        self._send_commands(["Q", "unknown.domain", "IN", "A", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["LOG", "Unknown type: A, domain: unknown.domain"]
        )

    def test_backend_responds_to_invalid_request_with_invalid_response(self):
        self._send_commands(["Q", "lcl.io", "IN", "INVALID", "1", "127.0.0.1"])

        self._run_backend()

        self._assert_expected_responses(
            ["LOG", "Unknown type: INVALID, domain: lcl.io"]
        )

    def test_backend_responds_to_invalid_command_with_fail(self):
        self._send_commands(["INVALID", "COMMAND"])

        self._run_backend()

        calls = [
            call("OK"),
            call("\t"),
            call("We are good"),
            call("\n"),

            call("FAIL"),
            call("\n"),
        ]

        self.mock_sys.stdout.write.assert_has_calls(calls)
        assert_that(self.mock_sys.stdout.write.call_count).is_equal_to(len(calls))

        assert_that(self.mock_sys.stdout.flush.call_count).is_equal_to(2)

    def test_configure_with_full_config(self):
        backend = self._configure_backend()

        assert_that(backend.id).is_equal_to("55")
        assert_that(backend.ip_address).is_equal_to("127.0.0.40")
        assert_that(backend.domain).is_equal_to("lcl.io")
        assert_that(backend.ttl).is_equal_to("1000")
        assert_that(backend.name_servers).is_equal_to({"ns1.lcl.io": "127.0.0.41", "ns2.lcl.io" : "127.0.0.42"})
        assert_that(backend.blacklisted_ips).is_equal_to(["10.0.0.100"])
        assert_that(backend.soa).is_equal_to("ns1.lcl.io emailaddress@lcl.io 55")

    def test_configure_with_config_missing_blacklists(self):
        backend = self._configure_backend(filename="backend_test_no_blacklist.conf")

        assert_that(backend.blacklisted_ips).is_empty()

    def _run_backend(self):
        backend = self._create_backend()
        backend.run()

    def _send_commands(self, *commands):
        commands_to_send = ["HELO\t1\n"]

        for command in commands:
            commands_to_send.append("\t".join(command) + "\n")

        commands_to_send.append("END\n")

        self.mock_sys.stdin.readline.side_effect = commands_to_send

    def _assert_expected_responses(self, *responses):
        calls = [
            call("OK"),
            call("\t"),
            call("We are good"),
            call("\n"),
        ]

        for response in responses:
            tab_separated = ["\t"] * (len(response) * 2 - 1)
            tab_separated[0::2] = response
            tab_separated.append("\n")

            calls.extend([call(response_item) for response_item in tab_separated])

        calls.extend([
            call("END"),
            call("\n"),
        ])

        self.mock_sys.stdout.write.assert_has_calls(calls)
        assert_that(self.mock_sys.stdout.write.call_count).is_equal_to(len(calls))

        assert_that(self.mock_sys.stdout.flush.call_count).is_equal_to(len(responses) + 2)

    @staticmethod
    def _create_backend():
        backend = DynamicBackend()
        backend.id = "22"
        backend.soa = "MY_SOA"
        backend.ip_address = "127.0.0.33"
        backend.ttl = "200"
        backend.name_servers = collections.OrderedDict([
            ("ns1.lcl.io", "127.0.0.34"),
            ("ns2.lcl.io", "127.0.0.35"),
        ])
        backend.domain = "lcl.io"
        backend.blacklisted_ips = ["127.0.0.2"]
        return backend

    def _configure_backend(self, filename="backend_test.conf"):
        backend = DynamicBackend()
        backend._get_config_filename = lambda: self._get_test_config_filename(filename)
        backend.configure()
        return backend

    def _get_test_config_filename(self, filename):
        return os.path.join(os.path.dirname(os.path.realpath(__file__)), filename)

Commits for nip.io/nipio_tests/backend_test.py

Diff revisions: vs.
Revision Author Commited Message
6af3f8 ... Diff Diff rs picture rs Fri 08 Feb, 2019 07:47:11 +0000

Add apache2 license notices

ff3961 ... rs picture rs Fri 08 Feb, 2019 07:09:51 +0000

Add tests and support for blacklisting IPs