File: alltests.py

package info (click to toggle)
m2crypto 0.46.2-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 2,952 kB
  • sloc: python: 22,921; makefile: 213; ansic: 94; sh: 17
file content (121 lines) | stat: -rw-r--r-- 3,273 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
import platform
import struct
import sys


def suite():
    from M2Crypto import m2  # noqa
    import os
    import unittest

    def my_import(name):
        # See http://docs.python.org/lib/built-in-funcs.html#l2h-6
        components = name.split(".")
        try:
            # python setup.py test
            mod = __import__(name)
            for comp in components[1:]:
                mod = getattr(mod, comp)
        except ImportError:
            # python tests/alltests.py
            mod = __import__(components[1])
        return mod

    modules_to_test = [
        "tests.test_aes",
        "tests.test_asn1",
        "tests.test_bio",
        "tests.test_bio_membuf",
        "tests.test_bio_file",
        "tests.test_bio_iobuf",
        "tests.test_bio_ssl",
        "tests.test_bn",
        "tests.test_authcookie",
        "tests.test_dh",
        "tests.test_dsa",
        "tests.test_err",
        "tests.test_evp",
        "tests.test_init",
        "tests.test_obj",
        "tests.test_rand",
        "tests.test_rc4",
        "tests.test_rsa",
        "tests.test_smime",
        "tests.test_ssl_offline",
        "tests.test_threading",
        "tests.test_x509",
        "tests.test_util",
        "tests.test_timeout",
        "tests.test_xmlrpc",
    ]
    if os.name == "posix":
        modules_to_test.append("tests.test_ssl")
    elif os.name == "nt":
        modules_to_test.append("tests.test_ssl_win")
    if m2.OPENSSL_VERSION_NUMBER >= 0x90800F and m2.OPENSSL_NO_EC == 0:
        modules_to_test.append("tests.test_ecdh")
        modules_to_test.append("tests.test_ecdsa")
        modules_to_test.append("tests.test_ec_curves")
    alltests = unittest.TestSuite()
    for module in map(my_import, modules_to_test):
        alltests.addTest(module.suite())

    print(
        "Version of OpenSSL is {0:x} ({1:s})".format(
            m2.OPENSSL_VERSION_NUMBER, m2.OPENSSL_VERSION_TEXT
        )
    )
    print(
        '(struct.calcsize("P") * 8) == 32 : {}'.format((struct.calcsize("P") * 8) == 32)
    )
    print("not(sys.maxsize > 2**32) : {}".format(not (sys.maxsize > 2**32)))
    print("libc_ver = {}".format(platform.libc_ver()))

    return alltests


def dump_garbage():
    import gc

    print("\nGarbage:")
    gc.collect()
    if len(gc.garbage):

        print("\nLeaked objects:")
        for x in gc.garbage:
            s = str(x)
            if len(s) > 77:
                s = s[:73] + "..."
            print(type(x), "\n  ", s)

        print("There were %d leaks." % len(gc.garbage))
    else:
        print("Python garbage collector did not detect any leaks.")
        print("However, it is still possible there are leaks in the C code.")


def runall(report_leaks=0):
    report_leaks = report_leaks

    if report_leaks:
        import gc

        gc.enable()
        gc.set_debug(gc.DEBUG_LEAK & ~gc.DEBUG_SAVEALL)

    import os
    import unittest
    from M2Crypto import Rand

    try:
        Rand.load_file("tests/randpool.dat", -1)
        unittest.TextTestRunner(verbosity=2).run(suite())
        Rand.save_file("tests/randpool.dat")
    finally:
        if os.name == "posix":
            from .test_ssl import zap_servers

            zap_servers()

    if report_leaks:
        dump_garbage()