|
| 1 | +#!/usr/bin/python |
| 2 | +# Copyright (c) 2013 Robert Mibus & Internode |
| 3 | +# |
| 4 | +# Permission is hereby granted, free of charge, to any person |
| 5 | +# obtaining a copy of this software and associated documentation |
| 6 | +# files (the "Software"), to deal in the Software without |
| 7 | +# restriction, including without limitation the rights to use, |
| 8 | +# copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 9 | +# copies of the Software, and to permit persons to whom the |
| 10 | +# Software is furnished to do so, subject to the following |
| 11 | +# conditions: |
| 12 | +# |
| 13 | +# The above copyright notice and this permission notice shall be |
| 14 | +# included in all copies or substantial portions of the Software. |
| 15 | +# |
| 16 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, |
| 17 | +# EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES |
| 18 | +# OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
| 19 | +# NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT |
| 20 | + |
| 21 | +import unittest |
| 22 | + |
| 23 | +import struct |
| 24 | +import ipaddr |
| 25 | +import string |
| 26 | + |
| 27 | +import pymdsautogen6fwd |
| 28 | +import pymdsautogen6rev |
| 29 | + |
| 30 | +testdomain = 'v6.example.net' |
| 31 | +testdomain_packed = string.join([struct.pack('!B',len(x)) + x for x in testdomain.split('.')],'') + "\0" |
| 32 | +fwd = pymdsautogen6fwd.Source(testdomain, '20010db8') |
| 33 | +rev = pymdsautogen6rev.Source(testdomain, '20010db8') |
| 34 | + |
| 35 | +class ForwardsTests(unittest.TestCase): |
| 36 | + def test_working_aaaa(self): |
| 37 | + ret, resp = fwd.get_response('2001-db8--',testdomain,28,0,0) |
| 38 | + addr_resp = ipaddr.IPv6Address(resp[0]['rdata']) |
| 39 | + self.assertEquals(addr_resp.compressed, '2001:db8::') |
| 40 | + def test_working_not_aaaa(self): |
| 41 | + ret, resp = fwd.get_response('2001-db8--',testdomain,1,0,0) |
| 42 | + self.assertEquals(ret, 0) |
| 43 | + self.assertEquals(resp, []) |
| 44 | + def test_outside_aaaa(self): |
| 45 | + ret, resp = fwd.get_response('2001-44b8--',testdomain,1,0,0) |
| 46 | + self.assertEquals(ret, 3) |
| 47 | + self.assertEquals(resp, []) |
| 48 | + def test_outside_not_aaaa(self): |
| 49 | + ret, resp = fwd.get_response('2001-44b8--',testdomain,1,0,0) |
| 50 | + self.assertEquals(ret, 3) |
| 51 | + self.assertEquals(resp, []) |
| 52 | + def test_broken_aaaa(self): |
| 53 | + ret, resp = fwd.get_response('2001-zzz--',testdomain,28,0,0) |
| 54 | + self.assertEquals(ret, 3) |
| 55 | + self.assertEquals(resp, []) |
| 56 | + def test_broken_not_aaaa(self): |
| 57 | + ret, resp = fwd.get_response('2001-zzz--',testdomain,1,0,0) |
| 58 | + self.assertEquals(ret, 3) |
| 59 | + self.assertEquals(resp, []) |
| 60 | + |
| 61 | +class ReverseTests(unittest.TestCase): |
| 62 | + def addr_to_list(self, addr): |
| 63 | + # This drops the shared prefix, drops the '-'s, and reverses the string into a list |
| 64 | + return [x for x in addr[:9:-1] if x != '-'] |
| 65 | + def test_working_ptr(self): |
| 66 | + addr = '2001-0db8-4321-4321-4321-4321-4321-4321' |
| 67 | + ret, resp = rev.get_response(self.addr_to_list(addr),'',12,0,0) |
| 68 | + self.assertEquals(ret, 0) |
| 69 | + data = struct.pack('!B', len(addr)) + addr + testdomain_packed |
| 70 | + self.assertEquals(resp[0]['rdata'], data) |
| 71 | + def test_broken_ptr(self): |
| 72 | + addr = '2001-0db8-4321-4321-4321-4321-4321-432z' |
| 73 | + ret, resp = rev.get_response(self.addr_to_list(addr),'',12,0,0) |
| 74 | + data = struct.pack('!B', len(addr)) + addr + testdomain_packed |
| 75 | + self.assertEquals(ret, 3) |
| 76 | + |
| 77 | +if __name__ == "__main__": |
| 78 | + unittest.main() |
0 commit comments