#!/usr/bin/env python # The software in this package is distributed under the GNU General # Public License version 2 (with a special exception described below). # # A copy of GNU General Public License (GPL) is included in this distribution, # in the file COPYING.GPL. # # As a special exception, if other files instantiate templates or use macros # or inline functions from this file, or you compile this file and link it # with other works to produce a work based on this file, this file # does not by itself cause the resulting work to be covered # by the GNU General Public License. # # However the source code for this file must still be made available # in accordance with section (3) of the GNU General Public License. # # This exception does not invalidate any other reasons why a work based # on this file might be covered by the GNU General Public License. # # Copyright (c) 2016-2018 Intra2net AG import unittest from src import v4_addr_range specific_range_lo = 13 specific_range_large = 37 specific_range_short = 2 specific_range_extreme = 0xffffffff specific_range_invalid = -1 class V4_addr_range_test(unittest.TestCase): def test_create_default_range(self): r = v4_addr_range.V4_addr_range() self.assertIsNotNone(r) def test_create_specific_range(self): r = v4_addr_range.V4_addr_range( specific_range_lo, specific_range_large) self.assertIsNotNone(r) def test_get_from_range(self): r = v4_addr_range.V4_addr_range(specific_range_lo) a = r.get() self.assertEqual(a, specific_range_lo) b = r.get() self.assertEqual(b, specific_range_lo + 1) def test_exhaust_range(self): r = v4_addr_range.V4_addr_range( specific_range_lo, specific_range_short) self.assertIsNotNone(r.get()) self.assertIsNotNone(r.get()) # I have absolutely no idea why the following should work. The # ``.assertRaises`` method should test exceptions for equality, yet # ``error.TestError != Exception``. Whatever. self.assertRaises(Exception, r.get) def test_overflow(self): self.assertRaises(Exception, v4_addr_range.V4_addr_range, specific_range_lo, specific_range_extreme) def test_nonnatural(self): self.assertRaises(Exception, v4_addr_range.V4_addr_range, addr_range=specific_range_invalid) self.assertRaises(Exception, v4_addr_range.V4_addr_range, addr_range=0) def test_type_inconsistency(self): self.assertRaises(TypeError, v4_addr_range.V4_addr_range, lo="") self.assertRaises(TypeError, v4_addr_range.V4_addr_range, addr_range="") def test_bound_methods(self): r = v4_addr_range.V4_addr_range( specific_range_lo, specific_range_large) self.assertEqual(r.get(), specific_range_lo) self.assertEqual(r.lo(), specific_range_lo) self.assertEqual(r.range(), specific_range_large) self.assertEqual(r.hi(), specific_range_lo + specific_range_large) def test_access(self): r = v4_addr_range.V4_addr_range( specific_range_lo, specific_range_short) self.assertEqual(r.get(), specific_range_lo) self.assertTrue(r[specific_range_lo]) self.assertFalse(r[specific_range_lo + 42]) def test_len(self): r = v4_addr_range.V4_addr_range( specific_range_lo, specific_range_short) _ = r.get() self.assertEqual(len(r), 1) _ = r.get() self.assertEqual(len(r), 2) if __name__ == '__main__': unittest.main()