From 71f665e3037085e6f9ee8ff8c8116aaa5fe2c3b2 Mon Sep 17 00:00:00 2001 From: David Blacka Date: Tue, 1 Jul 2008 11:23:41 -0400 Subject: [PATCH] v6addr tests moved --- rwhoisd/v6addr.py | 41 ----------------------------- test/TestV6addr.py | 65 ++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 65 insertions(+), 41 deletions(-) create mode 100644 test/TestV6addr.py diff --git a/rwhoisd/v6addr.py b/rwhoisd/v6addr.py index d42d63b..2a33dc0 100644 --- a/rwhoisd/v6addr.py +++ b/rwhoisd/v6addr.py @@ -145,44 +145,3 @@ except (AttributeError, NameError, socket.error): socket.inet_pton = inet_pton socket.inet_ntop = inet_ntop socket.AF_INET6 = 'AF_INET6' - - -# test driver -if __name__ == "__main__": - - def try_good_addr(addr): - try: - a = v6str_to_addr(addr) - b = v6addr_to_str(a) - except socket.error, e: - print "addr was invalid!:", e - else: - print "%s => %s" % (addr, b) - - try_good_addr("::"); - try_good_addr("::7"); - try_good_addr("f::"); - try_good_addr("ab:0:0:c:0:0:0:d") - try_good_addr("ab:0:0:0:c:0:0:d") - try_good_addr("1:2:3:4:5:6:7:8") - try_good_addr("1:2:3::7:8") - try_good_addr("2001:3c09:102::23:af") - try_good_addr("::ffff:1.2.3.4") - try_good_addr("1:2:3:4:5:6:4.3.2.1") - - def try_bad_addr(addr): - try: - a = v6str_to_addr(addr) - except socket.error, e: - print e - else: - print "addr was valid! %s => %s" % (addr, v6addr_to_str(a)) - - # things that shouldn't parse - try_bad_addr(":") - try_bad_addr(":::") - try_bad_addr("1::2::3") - try_bad_addr("::3::") - try_bad_addr("::1.2.3") - try_bad_addr("12345::1") - try_bad_addr("1:2:3:4:5:6:7:4.3.2.1") diff --git a/test/TestV6addr.py b/test/TestV6addr.py new file mode 100644 index 0000000..adc103f --- /dev/null +++ b/test/TestV6addr.py @@ -0,0 +1,65 @@ +# This file is part of python-rwhoisd +# +# Copyright (C) 2008 David E. Blacka +# +# This program is free software; you can redistribute it and/or modify +# it under the terms of the GNU General Public License as published by +# the Free Software Foundation; either version 2 of the License, or +# (at your option) any later version. +# +# 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, write to the Free Software +# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 +# USA + + +import path +import v6addr, socket +import unittest, types + + +class TestGood(unittest.TestCase): + + data = [ "::", "::7", "f::", "ab:0:0:c:0:0:0:d", "ab:0:0:0:c:0:0:d", + "1:2:3:4:5:6:7:8", "1:2:3::7:8", "2001:3c09:102::23:af", + "::ffff:1.2.3.4", "1:2:3:4:5:6:4.3.2.1" ] + + def testGood(self): + for addr in self.data: + a = v6addr.v6str_to_addr(addr) + b = v6addr.v6addr_to_str(a) + c = v6addr.v6str_to_addr(b) + self.assertEquals(a, c) + + +class TestBad(unittest.TestCase): + + data = [ ":", ":::", "1::2::3", "::3::", "::1.2.3", "12345::1", + "1:2:3:4:5:6:7:4.3.2.1" ] + + def testBad(self): + for addr in self.data: + self.assertRaises(socket.error, v6addr.v6str_to_addr, addr) + + +class TestNormalize(unittest.TestCase): + + data = [ ("0:0:0:0:0:0:0:0", "::"), ("0::0:0:7", "::7"), + ("00ab:00:00:c:00:00:00:d", "ab:0:0:c::d"), + ("00ab:0000:0000:0000:000c:0000:0000:000d", "ab::c:0:0:d"), + ("1:02:3:04:5:006:7:0008", "1:2:3:4:5:6:7:8"), + ("::FFFF:1.2.3.4", "::ffff:102:304") ] + + def testNormalize(self): + for addr, b in self.data: + numaddr = v6addr.v6str_to_addr(addr) + a = v6addr.v6addr_to_str(numaddr) + self.assertEquals(a, b) + +if __name__ == "__main__": + unittest.main() -- 2.36.6