def test_main(verbose=False): if skip_expected: raise unittest.SkipTest("No SSL support") global CERTFILE, SVN_PYTHON_ORG_ROOT_CERT CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem") SVN_PYTHON_ORG_ROOT_CERT = os.path.join( os.path.dirname(__file__) or os.curdir, "https_svn_python_org_root.pem") if (not os.path.exists(CERTFILE) or not os.path.exists(SVN_PYTHON_ORG_ROOT_CERT)): raise support.TestFailed("Can't read certificate files!") tests = [BasicTests] if support.is_resource_enabled('network'): tests.append(NetworkedTests) if _have_threads: thread_info = support.threading_setup() if thread_info and support.is_resource_enabled('network'): tests.append(ThreadedTests) support.run_unittest(*tests) if _have_threads: support.threading_cleanup(*thread_info)
def test_random_files(self): # Test roundtrip on random python modules. # pass the '-ucpu' option to process the full directory. import glob, random fn = support.findfile("tokenize_tests.txt") tempdir = os.path.dirname(fn) or os.curdir testfiles = glob.glob(os.path.join(tempdir, "test*.py")) # Tokenize is broken on test_pep3131.py because regular expressions are # broken on the obscure unicode identifiers in it. *sigh* # With roundtrip extended to test the 5-tuple mode of untokenize, # 7 more testfiles fail. Remove them also until the failure is diagnosed. testfiles.remove(os.path.join(tempdir, "test_pep3131.py")) for f in ('buffer', 'builtin', 'fileio', 'inspect', 'os', 'platform', 'sys'): testfiles.remove(os.path.join(tempdir, "test_%s.py") % f) if not support.is_resource_enabled("cpu"): testfiles = random.sample(testfiles, 10) for testfile in testfiles: with open(testfile, 'rb') as f: with self.subTest(file=testfile): self.check_roundtrip(f)
def test_main(): tests = [ ChdirTestCase, ImportTestCase, ImportPackageTestCase, ZipimportTestCase, PyCompileTestCase, ExecfileTestCase, ExecfileTracebackTestCase, ListdirTestCase, DirsTestCase, FilesTestCase, SymlinkTestCase ] if WINDOWS: tests.append(WindowsChdirTestCase) tests.remove(SymlinkTestCase) # os.symlink ... Availability: Unix. if support.is_jython: tests.extend((ImportJavaClassTestCase, ImportJarTestCase)) if support.is_resource_enabled('subprocess'): tests.append(SubprocessTestCase) support.run_unittest(*tests)
def testPythonOrg(self): if not support.is_resource_enabled('network'): return parser = urllib.robotparser.RobotFileParser( "http://www.python.org/robots.txt") parser.read() self.assertTrue(parser.can_fetch("*", "http://www.python.org/robots.txt"))
def testPythonOrg(self): if not support.is_resource_enabled('network'): return parser = urllib.robotparser.RobotFileParser( "http://www.python.org/robots.txt") parser.read() self.assertTrue( parser.can_fetch("*", "http://www.python.org/robots.txt"))
def testPasswordProtectedSite(self): if not support.is_resource_enabled('network'): return # whole site is password-protected. url = 'http://mueblesmoraleda.com' parser = urllib.robotparser.RobotFileParser() parser.set_url(url) parser.read() self.assertEqual(parser.can_fetch("*", url + "/robots.txt"), False)
def testPasswordProtectedSite(self): if not support.is_resource_enabled('network'): return # whole site is password-protected. url = 'http://mueblesmoraleda.com' parser = urllib.robotparser.RobotFileParser() parser.set_url(url) parser.read() self.assertEqual(parser.can_fetch("*", url+"/robots.txt"), False)
def test_main(): global srv_ssl, cli_ssl tests = [UtilTests] if support.is_resource_enabled('network'): if ssl: cli_ssl = ssl_context() srv_ssl = ssl.SSLContext(ssl.PROTOCOL_TLSv1) srv_ssl.load_cert_chain(support.findfile('keycert.pem')) tests.extend((BasicTests, SSLTests)) debug_level(support.verbose - 1) support.run_unittest(*tests)
def load_tests(*args): tests = [TestImaplib] if support.is_resource_enabled('network'): if ssl: global CERTFILE CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem") if not os.path.exists(CERTFILE): raise support.TestFailed("Can't read certificate files!") tests.extend([ ThreadedNetworkedTests, ThreadedNetworkedTestsSSL, RemoteIMAPTest, RemoteIMAP_SSLTest, RemoteIMAP_STARTTLSTest, ]) return unittest.TestSuite([unittest.makeSuite(test) for test in tests])
def test_main(): tests = [TestImaplib] if support.is_resource_enabled('network'): if ssl: global CERTFILE CERTFILE = os.path.join(os.path.dirname(__file__) or os.curdir, "keycert.pem") if not os.path.exists(CERTFILE): raise support.TestFailed("Can't read certificate files!") tests.extend([ ThreadedNetworkedTests, ThreadedNetworkedTestsSSL, RemoteIMAPTest, RemoteIMAP_SSLTest, RemoteIMAP_STARTTLSTest, ]) support.run_unittest(*tests)
def test_main(): if not support.is_resource_enabled("xpickle"): print("test_xpickle -- skipping backwards compat tests.", file=sys.stderr) print("Use 'regrtest.py -u xpickle' to run them.", file=sys.stderr) sys.stderr.flush() support.run_unittest( DumpCPickle_LoadPickle, DumpPickle_LoadCPickle, CPicklePython24Compat, CPicklePython25Compat, CPicklePython26Compat, PicklePython24Compat, PicklePython25Compat, PicklePython26Compat, )
def test_no_leaking(self): # Make sure we leak no resources if (not hasattr(support, "is_resource_enabled") or support.is_resource_enabled("subprocess") and not mswindows): max_handles = 1026 # too much for most UNIX systems else: max_handles = 65 for i in range(max_handles): p = subprocess.Popen([sys.executable, "-c", "import sys;" "sys.stdout.write(sys.stdin.read())"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) data = p.communicate(b"lime")[0] self.assertEqual(data, b"lime")
def test_no_leaking(self): # Make sure we leak no resources if (not hasattr(support, "is_resource_enabled") or support.is_resource_enabled("subprocess") and not mswindows): max_handles = 1026 # too much for most UNIX systems else: max_handles = 65 for i in range(max_handles): p = subprocess.Popen([ sys.executable, "-c", "import sys;" "sys.stdout.write(sys.stdin.read())" ], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) data = p.communicate(b"lime")[0] self.assertEqual(data, b"lime")
def test_no_leaking(self): # Make sure we leak no resources if not hasattr(support, "is_resource_enabled") \ or support.is_resource_enabled("subprocess") and not mswindows \ and not jython: max_handles = 1026 # too much for most UNIX systems else: # Settle for 65 on jython: spawning jython processes takes a # long time max_handles = 65 for i in range(max_handles): p = subprocess.Popen([sys.executable, "-c", "import sys;sys.stdout.write(sys.stdin.read())"], stdin=subprocess.PIPE, stdout=subprocess.PIPE, stderr=subprocess.PIPE) data = p.communicate("lime")[0] self.assertEqual(data, "lime")
def test_large_file_ops(self): # On Windows and Mac OSX this test comsumes large resources; It takes # a long time to build the >2GB file and takes >2GB of disk space # therefore the resource must be enabled to run this test. if sys.platform[:3] == 'win' or sys.platform == 'darwin': if not support.is_resource_enabled("largefile"): print("\nTesting large file ops skipped on %s." % sys.platform, file=sys.stderr) print("It requires %d bytes and a long time." % self.LARGE, file=sys.stderr) print("Use 'regrtest.py -u largefile test_io' to run it.", file=sys.stderr) return f = io.open(support.TESTFN, "w+b", 0) self.large_file_ops(f) f.close() f = io.open(support.TESTFN, "w+b") self.large_file_ops(f) f.close()
class ChecksumBigBufferTestCase(unittest.TestCase): def setUp(self): with open(support.TESTFN, "wb+") as f: f.seek(_4G) f.write(b"asdf") f.flush() self.mapping = mmap.mmap(f.fileno(), 0, access=mmap.ACCESS_READ) def tearDown(self): self.mapping.close() support.unlink(support.TESTFN) @unittest.skipUnless(mmap, "mmap() is not available.") @unittest.skipUnless(sys.maxsize > _4G, "Can't run on a 32-bit system.") @unittest.skipUnless(support.is_resource_enabled("largefile"), "May use lots of disk space.") def test_big_buffer(self): self.assertEqual(zlib.crc32(self.mapping), 3058686908) self.assertEqual(zlib.adler32(self.mapping), 82837919)
def testSSLconnect(self): if not support.is_resource_enabled('network'): return s = ssl.wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_NONE) s.connect(("svn.python.org", 443)) c = s.getpeercert() if c: raise support.TestFailed("Peer cert %s shouldn't be here!") s.close() # this should fail because we have no verification certs s = ssl.wrap_socket(socket.socket(socket.AF_INET), cert_reqs=ssl.CERT_REQUIRED) try: s.connect(("svn.python.org", 443)) except ssl.SSLError: pass finally: s.close()
"""Unit tests for socket timeout feature.""" import functools import unittest from test import support # This requires the 'network' resource as given on the regrtest command line. skip_expected = not support.is_resource_enabled('network') import time import errno import socket @functools.lru_cache() def resolve_address(host, port): """Resolve an (host, port) to an address. We must perform name resolution before timeout tests, otherwise it will be performed by connect(). """ with support.transient_internet(host): return socket.getaddrinfo(host, port, socket.AF_INET, socket.SOCK_STREAM)[0][4] class CreationTestCase(unittest.TestCase): """Test case for socket.gettimeout() and socket.settimeout()""" def setUp(self): self.sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
with self.assertRaisesRegex( ssl.CertificateError, "hostname '127.0.0.1' doesn't match 'localhost'"): with self.reaped_server(SimpleIMAPHandler) as server: client = self.imap_class(*server.server_address, ssl_context=ssl_context) client.shutdown() with self.reaped_server(SimpleIMAPHandler) as server: client = self.imap_class("localhost", server.server_address[1], ssl_context=ssl_context) client.shutdown() @unittest.skipUnless( support.is_resource_enabled('network'), 'network resource disabled') class RemoteIMAPTest(unittest.TestCase): host = 'cyrus.andrew.cmu.edu' port = 143 username = '******' password = '******' imap_class = imaplib.IMAP4 def setUp(self): with transient_internet(self.host): self.server = self.imap_class(self.host, self.port) def tearDown(self): if self.server is not None: with transient_internet(self.host): self.server.logout()
ssl.CertificateError, "IP address mismatch, certificate is not valid for " "'127.0.0.1'"): with self.reaped_server(SimpleIMAPHandler) as server: client = self.imap_class(*server.server_address, ssl_context=ssl_context) client.shutdown() with self.reaped_server(SimpleIMAPHandler) as server: client = self.imap_class("localhost", server.server_address[1], ssl_context=ssl_context) client.shutdown() @unittest.skipUnless(support.is_resource_enabled('network'), 'network resource disabled') @unittest.skip('cyrus.andrew.cmu.edu blocks connections') class RemoteIMAPTest(unittest.TestCase): host = 'cyrus.andrew.cmu.edu' port = 143 username = '******' password = '******' imap_class = imaplib.IMAP4 def setUp(self): with transient_internet(self.host): self.server = self.imap_class(self.host, self.port) def tearDown(self): if self.server is not None:
def run_compat_test(python_name): return (support.is_resource_enabled("xpickle") and have_python_version(python_name))
ssl.CertificateError, "IP address mismatch, certificate is not valid for " "'127.0.0.1'"): with self.reaped_server(SimpleIMAPHandler) as server: client = self.imap_class(*server.server_address, ssl_context=ssl_context) client.shutdown() with self.reaped_server(SimpleIMAPHandler) as server: client = self.imap_class("localhost", server.server_address[1], ssl_context=ssl_context) client.shutdown() @unittest.skipUnless( support.is_resource_enabled('network'), 'network resource disabled') @unittest.skip('cyrus.andrew.cmu.edu blocks connections') class RemoteIMAPTest(unittest.TestCase): host = 'cyrus.andrew.cmu.edu' port = 143 username = '******' password = '******' imap_class = imaplib.IMAP4 def setUp(self): with socket_helper.transient_internet(self.host): self.server = self.imap_class(self.host, self.port) def tearDown(self): if self.server is not None: with socket_helper.transient_internet(self.host):