def setUp(self): self.foo = AddressWizard() self.foo.lookup_fn = self.lookup_fn
class AddressWizardTestCase(TankTestCase): results = { "127.0.0.1": ("127.0.0.1", socket.AF_INET), "ipv4host": ("192.168.0.1", socket.AF_INET), "::": ("::", socket.AF_INET6), "2001:db8::1": ("2001:db8::1", socket.AF_INET6), "ipv6host": ("2001:db8::1", socket.AF_INET6), "exc1": socket.gaierror("Simulated error") } def lookup_fn(self, host, port, family=None, socktype=None, proto=None, flags=None): """ Mocking real resolver for unit testing purpose """ if host not in self.results: raise socket.gaierror("Host not found: %s" % host) logging.debug("Mocking resolve %s=>%s", host, self.results[host]) if isinstance(self.results[host], IOError): raise self.results[host] return [(self.results[host][1], None, None, None, (self.results[host][0], port))] def setUp(self): self.foo = AddressWizard() self.foo.lookup_fn = self.lookup_fn def test_v4_noport_resolve(self): res = self.foo.resolve("ipv4host") self.assertEquals((False, "192.168.0.1", 80, "ipv4host"), res) def test_v6_noport_resolve(self): res = self.foo.resolve("ipv6host") self.assertEquals((True, "2001:db8::1", 80, "ipv6host"), res) def test_v4_port_resolve(self): res = self.foo.resolve("ipv4host:443") self.assertEquals((False, "192.168.0.1", 443, "ipv4host"), res) def test_v6_port_resolve(self): res = self.foo.resolve("ipv6host:443") self.assertEquals((True, "2001:db8::1", 443, "ipv6host"), res) def test_v4_noport_noresolve(self): res = self.foo.resolve("127.0.0.1") self.assertEquals((False, "127.0.0.1", 80, "127.0.0.1"), res) def test_v6_noport_noresolve(self): res = self.foo.resolve("2001:db8::1") self.assertEquals((True, "2001:db8::1", 80, "2001:db8::1"), res) def test_v4_port_noresolve(self): res = self.foo.resolve("127.0.0.1:443") self.assertEquals((False, "127.0.0.1", 443, "127.0.0.1"), res) def test_v6_port_noresolve_braces(self): res = self.foo.resolve("[2001:db8::1]:443") self.assertEquals((True, "2001:db8::1", 443, "2001:db8::1"), res) def test_v4_port_noresolve_braces(self): res = self.foo.resolve("[127.0.0.1]:443") self.assertEquals((False, "127.0.0.1", 443, "127.0.0.1"), res) def test_v6_port_resolve_braces(self): res = self.foo.resolve("[ipv6host]:443") self.assertEquals((True, "2001:db8::1", 443, "ipv6host"), res) def test_v4_port_resolve_braces(self): res = self.foo.resolve("[ipv4host]:443") self.assertEquals((False, "192.168.0.1", 443, "ipv4host"), res) def test_v4_noport_resolve_braces(self): res = self.foo.resolve("[ipv4host]") self.assertEquals((False, "192.168.0.1", 80, "ipv4host"), res) def test_v4_noport_noresolve_braces(self): res = self.foo.resolve("[127.0.0.1]") self.assertEquals((False, "127.0.0.1", 80, "127.0.0.1"), res) def test_v6_noport_resolve_braces(self): res = self.foo.resolve("[ipv6host]") self.assertEquals((True, "2001:db8::1", 80, "ipv6host"), res) def test_v6_noport_noresolve_braces(self): res = self.foo.resolve("[2001:db8::1]") self.assertEquals((True, "2001:db8::1", 80, "2001:db8::1"), res) def test_error1(self): try: res = self.foo.resolve("ipv4host:20:30") self.fail() except: pass def test_error2(self): try: res = self.foo.resolve("exc1:30") self.fail() except: pass