]> granicus.if.org Git - python/commitdiff
Forward port new test for SSLSocket.connect_ex()
authorAntoine Pitrou <solipsis@pitrou.net>
Fri, 28 Dec 2012 18:07:43 +0000 (19:07 +0100)
committerAntoine Pitrou <solipsis@pitrou.net>
Fri, 28 Dec 2012 18:07:43 +0000 (19:07 +0100)
1  2 
Lib/test/test_ssl.py

index 2f0b3e648c4e032b4978526f19cb4c2f6a9fa41a,9f5138719e89fc87dad390173453eeed673e97e4..4f254a98f325966c22f7dcc8c7b7b40c597b7d5f
@@@ -558,69 -297,17 +558,80 @@@ class NetworkedTests(unittest.TestCase)
              finally:
                  s.close()
  
 -        with test_support.transient_internet("svn.python.org"):
+     def test_connect_ex_error(self):
++        with support.transient_internet("svn.python.org"):
+             s = ssl.wrap_socket(socket.socket(socket.AF_INET),
+                                 cert_reqs=ssl.CERT_REQUIRED,
+                                 ca_certs=SVN_PYTHON_ORG_ROOT_CERT)
+             try:
+                 self.assertEqual(errno.ECONNREFUSED,
+                                  s.connect_ex(("svn.python.org", 444)))
+             finally:
+                 s.close()
 +    def test_connect_with_context(self):
 +        with support.transient_internet("svn.python.org"):
 +            # Same as test_connect, but with a separately created context
 +            ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
 +            s = ctx.wrap_socket(socket.socket(socket.AF_INET))
 +            s.connect(("svn.python.org", 443))
 +            try:
 +                self.assertEqual({}, s.getpeercert())
 +            finally:
 +                s.close()
 +            # Same with a server hostname
 +            s = ctx.wrap_socket(socket.socket(socket.AF_INET),
 +                                server_hostname="svn.python.org")
 +            if ssl.HAS_SNI:
 +                s.connect(("svn.python.org", 443))
 +                s.close()
 +            else:
 +                self.assertRaises(ValueError, s.connect, ("svn.python.org", 443))
 +            # This should fail because we have no verification certs
 +            ctx.verify_mode = ssl.CERT_REQUIRED
 +            s = ctx.wrap_socket(socket.socket(socket.AF_INET))
 +            self.assertRaisesRegex(ssl.SSLError, "certificate verify failed",
 +                                    s.connect, ("svn.python.org", 443))
 +            s.close()
 +            # This should succeed because we specify the root cert
 +            ctx.load_verify_locations(SVN_PYTHON_ORG_ROOT_CERT)
 +            s = ctx.wrap_socket(socket.socket(socket.AF_INET))
 +            s.connect(("svn.python.org", 443))
 +            try:
 +                cert = s.getpeercert()
 +                self.assertTrue(cert)
 +            finally:
 +                s.close()
 +
 +    def test_connect_capath(self):
 +        # Verify server certificates using the `capath` argument
 +        # NOTE: the subject hashing algorithm has been changed between
 +        # OpenSSL 0.9.8n and 1.0.0, as a result the capath directory must
 +        # contain both versions of each certificate (same content, different
 +        # filename) for this test to be portable across OpenSSL releases.
 +        with support.transient_internet("svn.python.org"):
 +            ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
 +            ctx.verify_mode = ssl.CERT_REQUIRED
 +            ctx.load_verify_locations(capath=CAPATH)
 +            s = ctx.wrap_socket(socket.socket(socket.AF_INET))
 +            s.connect(("svn.python.org", 443))
 +            try:
 +                cert = s.getpeercert()
 +                self.assertTrue(cert)
 +            finally:
 +                s.close()
 +            # Same with a bytes `capath` argument
 +            ctx = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
 +            ctx.verify_mode = ssl.CERT_REQUIRED
 +            ctx.load_verify_locations(capath=BYTES_CAPATH)
 +            s = ctx.wrap_socket(socket.socket(socket.AF_INET))
 +            s.connect(("svn.python.org", 443))
 +            try:
 +                cert = s.getpeercert()
 +                self.assertTrue(cert)
 +            finally:
 +                s.close()
 +
      @unittest.skipIf(os.name == "nt", "Can't use a socket as a file under Windows")
      def test_makefile_close(self):
          # Issue #5238: creating a file-like object with makefile() shouldn't