Diff of /jython/Lib/test/test_socket.py [cd240a] .. [c54550]  Maximize  Restore

Switch to side-by-side view

--- a/jython/Lib/test/test_socket.py
+++ b/jython/Lib/test/test_socket.py
@@ -1501,6 +1501,73 @@
         doAddressTest(socket.getaddrinfo("localhost", 0, socket.AF_INET6, socket.SOCK_STREAM, 0, 0))
         doAddressTest(socket.getaddrinfo("localhost", 0, socket.AF_UNSPEC, socket.SOCK_STREAM, 0, 0))
         socket._use_ipv4_addresses_only(False)
+
+    def testAddrTupleTypes(self):
+        ipv4_address_tuple = socket.getaddrinfo("localhost", 80, socket.AF_INET, socket.SOCK_STREAM, 0, 0)[0][4]
+        self.failUnlessEqual(ipv4_address_tuple[0], "127.0.0.1")
+        self.failUnlessEqual(ipv4_address_tuple[1], 80)
+        self.failUnlessRaises(IndexError, lambda: ipv4_address_tuple[2])
+        self.failUnlessEqual(str(ipv4_address_tuple), "('127.0.0.1', 80)")
+
+        ipv6_address_tuple = socket.getaddrinfo("localhost", 80, socket.AF_INET6, socket.SOCK_STREAM, 0, 0)[0][4]
+        self.failUnless     (ipv6_address_tuple[0] in ["::1", "0:0:0:0:0:0:0:1"])
+        self.failUnlessEqual(ipv6_address_tuple[1], 80)
+        self.failUnlessEqual(ipv6_address_tuple[2], 0)
+        # Can't have an expectation for scope
+        try:
+            ipv6_address_tuple[3]
+        except IndexError:
+            self.fail("Failed to retrieve third element of ipv6 4-tuple")
+        self.failUnlessRaises(IndexError, lambda: ipv6_address_tuple[4])
+        self.failUnless(str(ipv6_address_tuple) in ["('::1', 80)", "('0:0:0:0:0:0:0:1', 80)"])
+
+class TestJython_get_jsockaddr(unittest.TestCase):
+    "These tests are specific to jython: they test a key internal routine"
+
+    def testIPV4AddressesFromGetAddrInfo(self):
+        local_addr = socket.getaddrinfo("localhost", 80, socket.AF_INET, socket.SOCK_STREAM, 0, 0)[0][4]
+        sockaddr = socket._get_jsockaddr(local_addr)
+        self.failUnless(isinstance(sockaddr, java.net.InetSocketAddress), "_get_jsockaddr returned wrong type: '%s'" % str(type(sockaddr)))
+        self.failUnlessEqual(sockaddr.address.hostAddress, "127.0.0.1")
+        self.failUnlessEqual(sockaddr.port, 80)
+
+    def testIPV6AddressesFromGetAddrInfo(self):
+        local_addr = socket.getaddrinfo("localhost", 80, socket.AF_INET6, socket.SOCK_STREAM, 0, 0)[0][4]
+        sockaddr = socket._get_jsockaddr(local_addr)
+        self.failUnless(isinstance(sockaddr, java.net.InetSocketAddress), "_get_jsockaddr returned wrong type: '%s'" % str(type(sockaddr)))
+        self.failUnless(sockaddr.address.hostAddress in ["::1", "0:0:0:0:0:0:0:1"])
+        self.failUnlessEqual(sockaddr.port, 80)
+
+    def testAddressesFrom2Tuple(self):
+        for addr_tuple in [
+            ("localhost", 80),
+            ]:
+            sockaddr = socket._get_jsockaddr(addr_tuple)
+            self.failUnless(isinstance(sockaddr, java.net.InetSocketAddress), "_get_jsockaddr returned wrong type: '%s'" % str(type(sockaddr)))
+            self.failUnless(sockaddr.address.hostAddress in ["127.0.0.1", "::1", "0:0:0:0:0:0:0:1"])
+            self.failUnlessEqual(sockaddr.port, 80)
+
+    def testAddressesFrom4Tuple(self):
+        # This test disabled: cannot construct IPV6 addresses from 4-tuple
+        return
+        for addr_tuple in [
+            ("localhost", 80, 0, 0),
+            ]:
+            sockaddr = socket._get_jsockaddr(addr_tuple)
+            self.failUnless(isinstance(sockaddr, java.net.InetSocketAddress), "_get_jsockaddr returned wrong type: '%s'" % str(type(sockaddr)))
+            self.failUnless(isinstance(sockaddr.address, java.net.Inet6Address), "_get_jsockaddr returned wrong address type: '%s'" % str(type(sockaddr.address)))
+            self.failUnless(sockaddr.address.hostAddress in ["::1", "0:0:0:0:0:0:0:1"])
+            self.failUnlessEqual(sockaddr.address.scopeId, 0)
+            self.failUnlessEqual(sockaddr.port, 80)
+
+    def testSpecialHostnames(self):
+        for addr_tuple, for_udp, expected_hostname in [
+            ( ("", 80),            False, socket.INADDR_ANY),
+            ( ("", 80),            True,  socket.INADDR_ANY),
+            ( ("<broadcast>", 80), True,  socket.INADDR_BROADCAST),
+            ]:
+            sockaddr = socket._get_jsockaddr(addr_tuple, for_udp)
+            self.failUnlessEqual(sockaddr.hostName, expected_hostname, "_get_jsockaddr returned wrong hostname '%s' for special hostname '%s'" % (sockaddr.hostName, expected_hostname))
 
 class TestExceptions(unittest.TestCase):
 
@@ -1728,10 +1795,12 @@
     if sys.platform[:4] == 'java':
         tests.append(TestJythonTCPExceptions)
         tests.append(TestJythonUDPExceptions)
+        tests.append(TestJython_get_jsockaddr)
     # TODO: Broadcast requires permission, and is blocked by some firewalls
     # Need some way to discover the network setup on the test machine
     if False:
         tests.append(UDPBroadcastTest)
+#    tests = [TestJython_get_jsockaddr]
     suites = [unittest.makeSuite(klass, 'test') for klass in tests]
     test_support.run_suite(unittest.TestSuite(suites))
 

Get latest updates about Open Source Projects, Conferences and News.

Sign up for the SourceForge newsletter:





No, thanks