def test_connect_using_sslcontext_verified(self):
     support.get_attribute(smtplib, 'SMTP_SSL')
     context = ssl.create_default_context()
     with support.transient_internet(self.testServer):
         server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context)
         server.ehlo()
         server.quit()
Exemple #2
0
 def test_connect_using_sslcontext(self):
     context = ssl.SSLContext(ssl.PROTOCOL_SSLv23)
     support.get_attribute(smtplib, 'SMTP_SSL')
     with support.transient_internet(self.testServer):
         server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context)
         server.ehlo()
         server.quit()
Exemple #3
0
 def test_connect_using_sslcontext(self):
     context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
     context.check_hostname = False
     context.verify_mode = ssl.CERT_NONE
     support.get_attribute(smtplib, 'SMTP_SSL')
     with support.transient_internet(self.testServer):
         server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context)
         server.ehlo()
         server.quit()
Exemple #4
0
    def test_connect_using_sslcontext_verified(self):
        with support.transient_internet(self.testServer):
            can_verify = check_ssl_verifiy(self.testServer, self.remotePort)
            if not can_verify:
                self.skipTest("SSL certificate can't be verified")

        support.get_attribute(smtplib, 'SMTP_SSL')
        context = ssl.create_default_context()
        with support.transient_internet(self.testServer):
            server = smtplib.SMTP_SSL(self.testServer, self.remotePort, context=context)
            server.ehlo()
            server.quit()
 def test_connect_starttls(self):
     support.get_attribute(smtplib, "SMTP_SSL")
     with support.transient_internet(self.testServer):
         server = smtplib.SMTP(self.testServer, self.remotePort)
         try:
             server.starttls(context=self.context)
         except smtplib.SMTPException as e:
             if e.args[0] == "STARTTLS extension not supported by server.":
                 unittest.skip(e.args[0])
             else:
                 raise
         server.ehlo()
         server.quit()
Exemple #6
0
 def test_refleaks_in_hash___init__(self):
     gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
     sha1_hash = c_hashlib.new('sha1')
     refs_before = gettotalrefcount()
     for i in range(100):
         sha1_hash.__init__('sha1')
     self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
Exemple #7
0
 def test_refleaks_in___init__(self):
     gettotalrefcount = support.get_attribute(sys, 'gettotalrefcount')
     bzd = BZ2Decompressor()
     refs_before = gettotalrefcount()
     for i in range(100):
         bzd.__init__()
     self.assertAlmostEqual(gettotalrefcount() - refs_before, 0, delta=10)
Exemple #8
0
 def test_connect_starttls(self):
     support.get_attribute(smtplib, 'SMTP_SSL')
     context = ssl.SSLContext(ssl.PROTOCOL_TLS_CLIENT)
     context.check_hostname = False
     context.verify_mode = ssl.CERT_NONE
     with support.transient_internet(self.testServer):
         server = smtplib.SMTP(self.testServer, self.remotePort)
         try:
             server.starttls(context=context)
         except smtplib.SMTPException as e:
             if e.args[0] == 'STARTTLS extension not supported by server.':
                 unittest.skip(e.args[0])
             else:
                 raise
         server.ehlo()
         server.quit()
Exemple #9
0
 def test_issue31315(self):
     # There shouldn't be an assertion failure in imp.create_dynamic(),
     # when spec.name is not a string.
     create_dynamic = support.get_attribute(imp, 'create_dynamic')
     class BadSpec:
         name = None
         origin = 'foo'
     with self.assertRaises(TypeError):
         create_dynamic(BadSpec())
Exemple #10
0
    def test_interrupted_write(self):
        # BaseHandler._write() and _flush() have to write all data, even if
        # it takes multiple send() calls.  Test this by interrupting a send()
        # call with a Unix signal.
        threading = support.import_module("threading")
        pthread_kill = support.get_attribute(signal, "pthread_kill")

        def app(environ, start_response):
            start_response("200 OK", [])
            return [b'\0' * support.SOCK_MAX_SIZE]

        class WsgiHandler(NoLogRequestHandler, WSGIRequestHandler):
            pass

        server = make_server(support.HOST, 0, app, handler_class=WsgiHandler)
        self.addCleanup(server.server_close)
        interrupted = threading.Event()

        def signal_handler(signum, frame):
            interrupted.set()

        original = signal.signal(signal.SIGUSR1, signal_handler)
        self.addCleanup(signal.signal, signal.SIGUSR1, original)
        received = None
        main_thread = threading.get_ident()

        def run_client():
            http = HTTPConnection(*server.server_address)
            http.request("GET", "/")
            with http.getresponse() as response:
                response.read(100)
                # The main thread should now be blocking in a send() system
                # call.  But in theory, it could get interrupted by other
                # signals, and then retried.  So keep sending the signal in a
                # loop, in case an earlier signal happens to be delivered at
                # an inconvenient moment.
                while True:
                    pthread_kill(main_thread, signal.SIGUSR1)
                    if interrupted.wait(timeout=float(1)):
                        break
                nonlocal received
                received = len(response.read())
            http.close()

        background = threading.Thread(target=run_client)
        background.start()
        server.handle_request()
        background.join()
        self.assertEqual(received, support.SOCK_MAX_SIZE - 100)
Exemple #11
0
 def test_symlink(self):
     # Issue 7880
     symlink = get_attribute(os, "symlink")
     def get(python):
         cmd = [python, '-c',
                'import sysconfig; print sysconfig.get_platform()']
         p = subprocess.Popen(cmd, stdout=subprocess.PIPE)
         return p.communicate()
     real = os.path.realpath(sys.executable)
     link = os.path.abspath(TESTFN)
     symlink(real, link)
     try:
         self.assertEqual(get(real), get(link))
     finally:
         unlink(link)
"""This test checks for correct fork() behavior.
"""

import imp
import os
import signal
import sys
import time

from test.fork_wait import ForkWait
from test.support import run_unittest, reap_children, get_attribute, import_module
threading = import_module('threading')

# Skip test if fork does not exist.
get_attribute(os, 'fork')


class ForkTest(ForkWait):
    def wait_impl(self, cpid):
        for i in range(10):
            # waitpid() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status = os.waitpid(cpid, os.WNOHANG)
            if spid == cpid:
                break
            time.sleep(1.0)

        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))

    def test_import_lock_fork(self):
Exemple #13
0
"""This test checks for correct wait4() behavior.
"""

import os
import time
import sys
from test.fork_wait import ForkWait
from test.support import run_unittest, reap_children, get_attribute

# If either of these do not exist, skip this test.
get_attribute(os, 'fork')
get_attribute(os, 'wait4')


class Wait4Test(ForkWait):
    def wait_impl(self, cpid):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        deadline = time.monotonic() + 10.0
        while time.monotonic() <= deadline:
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(0.1)
        self.assertEqual(spid, cpid)
        self.assertEqual(
 def test_connect(self):
     support.get_attribute(smtplib, 'SMTP_SSL')
     server = smtplib.SMTP_SSL(self.testServer, self.remotePort)
     server.ehlo()
     server.quit()
Exemple #15
0
#
# empty.vbs is an empty file (except for a comment), which does
# nothing when run with cscript or wscript.
#
# A possible improvement would be to have empty.vbs do something that
# we can detect here, to make sure that not only the os.startfile()
# call succeeded, but also the script actually has run.

import unittest
from test import support
import os
import platform
import sys
from os import path

startfile = support.get_attribute(os, 'startfile')


class TestCase(unittest.TestCase):
    def test_nonexisting(self):
        self.assertRaises(OSError, startfile, "nonexisting.vbs")

    @unittest.skipIf(
        platform.win32_is_iot(),
        "starting files is not supported on Windows IoT Core or nanoserver")
    def test_empty(self):
        # We need to make sure the child process starts in a directory
        # we're not about to delete. If we're running under -j, that
        # means the test harness provided directory isn't a safe option.
        # See http://bugs.python.org/issue15526 for more details
        with support.change_cwd(path.dirname(sys.executable)):
Exemple #16
0
 def test_connect_default_port(self):
     support.get_attribute(smtplib, 'SMTP_SSL')
     with support.transient_internet(self.testServer):
         server = smtplib.SMTP_SSL(self.testServer)
         server.ehlo()
         server.quit()
Exemple #17
0
 def test_connect(self):
     support.get_attribute(smtplib, 'SMTP_SSL')
     with support.transient_internet(self.testServer):
         server = smtplib.SMTP_SSL(self.testServer, self.remotePort)
         server.ehlo()
         server.quit()
Exemple #18
0
 def test_get_attribute(self):
     self.assertEqual(support.get_attribute(self, "test_get_attribute"),
                     self.test_get_attribute)
     self.assertRaises(unittest.SkipTest, support.get_attribute, self, "foo")
     with self.assertRaisesRegexp(unittest.SkipTest, 'unittest'):
         support.get_attribute(unittest, 'foo')
     with self.assertRaisesRegexp(unittest.SkipTest, 'ClassicClass'):
         support.get_attribute(ClassicClass, 'foo')
     with self.assertRaisesRegexp(unittest.SkipTest, 'ClassicClass'):
         support.get_attribute(ClassicClass(), 'foo')
     with self.assertRaisesRegexp(unittest.SkipTest, 'NewStyleClass'):
         support.get_attribute(NewStyleClass, 'foo')
     with self.assertRaisesRegexp(unittest.SkipTest, 'NewStyleClass'):
         support.get_attribute(NewStyleClass(), 'foo')
Exemple #19
0
"""This test checks for correct wait4() behavior.
"""

import os
import time
import sys
import unittest
from test.fork_wait import ForkWait
from test import support

# If either of these do not exist, skip this test.
support.get_attribute(os, 'fork')
support.get_attribute(os, 'wait4')


class Wait4Test(ForkWait):
    def wait_impl(self, cpid, *, exitcode):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        deadline = time.monotonic() + support.SHORT_TIMEOUT
        while time.monotonic() <= deadline:
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(0.1)
        self.assertEqual(spid, cpid)
import array
import unittest
from test.support import run_unittest, import_module, get_attribute
import os, struct
fcntl = import_module('fcntl')
termios = import_module('termios')
get_attribute(termios, 'TIOCGPGRP') #Can't run tests without this feature

try:
    tty = open("/dev/tty", "rb")
except OSError:
    raise unittest.SkipTest("Unable to open /dev/tty")
else:
    # Skip if another process is in foreground
    r = fcntl.ioctl(tty, termios.TIOCGPGRP, "    ")
    tty.close()
    rpgrp = struct.unpack("i", r)[0]
    if rpgrp not in (os.getpgrp(), os.getsid(0)):
        raise unittest.SkipTest("Neither the process group nor the session "
                                "are attached to /dev/tty")
    del tty, r, rpgrp

try:
    import pty
except ImportError:
    pty = None

class IoctlTests(unittest.TestCase):
    def test_ioctl(self):
        # If this process has been put into the background, TIOCGPGRP returns
        # the session ID instead of the process group id.
Exemple #21
0
import array
import unittest
from test.support import import_module, get_attribute
import os, struct

fcntl = import_module('fcntl')
termios = import_module('termios')
get_attribute(termios, 'TIOCGPGRP')
try:
    tty = open('/dev/tty', 'rb')
except OSError:
    raise unittest.SkipTest('Unable to open /dev/tty')
else:
    r = fcntl.ioctl(tty, termios.TIOCGPGRP, '    ')
    tty.close()
    rpgrp = struct.unpack('i', r)[0]
    if rpgrp not in (os.getpgrp(), os.getsid(0)):
        raise unittest.SkipTest(
            'Neither the process group nor the session are attached to /dev/tty'
        )
    del tty, r, rpgrp
try:
    import pty
except ImportError:
    pty = None


class IoctlTests(unittest.TestCase):
    def test_ioctl(self):
        ids = os.getpgrp(), os.getsid(0)
        with open('/dev/tty', 'rb') as tty:
Exemple #22
0
 def test_get_attribute(self):
     self.assertEqual(support.get_attribute(self, "test_get_attribute"),
                     self.test_get_attribute)
     self.assertRaises(unittest.SkipTest, support.get_attribute, self, "foo")
Exemple #23
0
 def test_connect_default_port(self):
     support.get_attribute(smtplib, 'SMTP_SSL')
     with socket_helper.transient_internet(self.testServer):
         server = smtplib.SMTP_SSL(self.testServer)
         server.ehlo()
         server.quit()
Exemple #24
0
import array
import unittest
from test.support import get_attribute
from test.support.import_helper import import_module
import os, struct
fcntl = import_module('fcntl')
termios = import_module('termios')
get_attribute(termios, 'TIOCGPGRP')  #Can't run tests without this feature

try:
    tty = open("/dev/tty", "rb")
except OSError:
    raise unittest.SkipTest("Unable to open /dev/tty")
else:
    with tty:
        # Skip if another process is in foreground
        r = fcntl.ioctl(tty, termios.TIOCGPGRP, "    ")
    rpgrp = struct.unpack("i", r)[0]
    if rpgrp not in (os.getpgrp(), os.getsid(0)):
        raise unittest.SkipTest("Neither the process group nor the session "
                                "are attached to /dev/tty")
    del tty, r, rpgrp

try:
    import pty
except ImportError:
    pty = None


class IoctlTests(unittest.TestCase):
    def test_ioctl(self):
Exemple #25
0
"""This test checks for correct fork() behavior.
"""

import _imp as imp
import os
import signal
import sys
import time

from test.fork_wait import ForkWait
from test.support import reap_children, get_attribute, import_module, verbose

threading = import_module("threading")

# Skip test if fork does not exist.
get_attribute(os, "fork")


class ForkTest(ForkWait):
    def wait_impl(self, cpid):
        deadline = time.monotonic() + 10.0
        while time.monotonic() <= deadline:
            # waitpid() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status = os.waitpid(cpid, os.WNOHANG)
            if spid == cpid:
                break
            time.sleep(0.1)

        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status & 0xFF, status >> 8))
"""

import imp
import os
import signal
import sys
import time

from test.fork_wait import ForkWait
from test.support import (run_unittest, reap_children, get_attribute,
                          import_module, verbose)

threading = import_module('threading')

# Skip test if fork does not exist.
get_attribute(os, 'fork')


class ForkTest(ForkWait):
    def wait_impl(self, cpid):
        for i in range(10):
            # waitpid() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status = os.waitpid(cpid, os.WNOHANG)
            if spid == cpid:
                break
            time.sleep(1.0)

        self.assertEqual(spid, cpid)
        self.assertEqual(
            status, 0, "cause = %d, exit = %d" % (status & 0xff, status >> 8))
# Ridiculously simple test of the os.startfile function for Windows.
#
# empty.vbs is an empty file (except for a comment), which does
# nothing when run with cscript or wscript.
#
# A possible improvement would be to have empty.vbs do something that
# we can detect here, to make sure that not only the os.startfile()
# call succeeded, but also the script actually has run.

import unittest
from test import support
import os
import sys
from os import path

startfile = support.get_attribute(os, 'startfile')


class TestCase(unittest.TestCase):
    def test_nonexisting(self):
        self.assertRaises(OSError, startfile, "nonexisting.vbs")

    def test_empty(self):
        # We need to make sure the child process starts in a directory
        # we're not about to delete. If we're running under -j, that
        # means the test harness provided directory isn't a safe option.
        # See http://bugs.python.org/issue15526 for more details
        with support.change_cwd(path.dirname(sys.executable)):
            empty = path.join(path.dirname(__file__), "empty.vbs")
            startfile(empty)
            startfile(empty, "open")
 def test_connect(self):
     support.get_attribute(smtplib, "SMTP_SSL")
     with support.transient_internet(self.testServer):
         server = smtplib.SMTP_SSL(self.testServer, self.remotePort)
     server.ehlo()
     server.quit()
Exemple #29
0
 def test_get_attribute(self):
     self.assertEqual(support.get_attribute(self, "test_get_attribute"),
                     self.test_get_attribute)
     self.assertRaises(unittest.SkipTest, support.get_attribute, self, "foo")
Exemple #30
0
 def test_get_attribute(self):
     self.assertEqual(support.get_attribute(self, "test_get_attribute"),
                     self.test_get_attribute)
     self.assertRaises(unittest.SkipTest, support.get_attribute, self, "foo")
     with self.assertRaisesRegexp(unittest.SkipTest, 'unittest'):
         support.get_attribute(unittest, 'foo')
     with self.assertRaisesRegexp(unittest.SkipTest, 'ClassicClass'):
         support.get_attribute(ClassicClass, 'foo')
     with self.assertRaisesRegexp(unittest.SkipTest, 'ClassicClass'):
         support.get_attribute(ClassicClass(), 'foo')
     with self.assertRaisesRegexp(unittest.SkipTest, 'NewStyleClass'):
         support.get_attribute(NewStyleClass, 'foo')
     with self.assertRaisesRegexp(unittest.SkipTest, 'NewStyleClass'):
         support.get_attribute(NewStyleClass(), 'foo')
Exemple #31
0
"""This test checks for correct wait4() behavior.
"""

import os
import time
import sys
from test.fork_wait import ForkWait
from test.support import run_unittest, reap_children, get_attribute

# If either of these do not exist, skip this test.
get_attribute(os, 'fork')
get_attribute(os, 'wait4')


class Wait4Test(ForkWait):
    def wait_impl(self, cpid):
        option = os.WNOHANG
        if sys.platform.startswith('aix'):
            # Issue #11185: wait4 is broken on AIX and will always return 0
            # with WNOHANG.
            option = 0
        deadline = time.monotonic() + 10.0
        while time.monotonic() <= deadline:
            # wait4() shouldn't hang, but some of the buildbots seem to hang
            # in the forking tests.  This is an attempt to fix the problem.
            spid, status, rusage = os.wait4(cpid, option)
            if spid == cpid:
                break
            time.sleep(0.1)
        self.assertEqual(spid, cpid)
        self.assertEqual(status, 0, "cause = %d, exit = %d" % (status&0xff, status>>8))
Exemple #32
0
 def test_connect(self):
     support.get_attribute(smtplib, 'SMTP_SSL')
     server = smtplib.SMTP_SSL(self.testServer, self.remotePort)
     server.ehlo()
     server.quit()