-
Notifications
You must be signed in to change notification settings - Fork 40
Expand file tree
/
Copy path__init__.py
More file actions
158 lines (130 loc) · 3.86 KB
/
__init__.py
File metadata and controls
158 lines (130 loc) · 3.86 KB
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
import logging
import os
import socket
import subprocess
import sys
import tempfile
from unittest import TestCase
import importlib.resources
import six
from pyff import __version__ as pyffversion
# range of ports where available ports can be found
PORT_RANGE = [33000, 60000]
class ExitException(Exception):
def __init__(self, code):
self.code = code
def __str__(self):
return "would have exited with %d" % self.code
def find_unbound_port(i=0):
"""
Returns an unbound port number on 127.0.0.1 in a high port range
"""
for port in range(*PORT_RANGE):
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
try:
sock.bind(("127.0.0.1", port))
except socket.error:
pass
return port
def run_pyff(*args):
return run_cmdline('pyff', args)
def run_pyffd(*args):
return run_cmdline('pyffd', args)
def run_cmdline(script, *args):
argv = list(*args)
starter = tempfile.NamedTemporaryFile('w').name
print("starting %s using %s" % (script, starter))
with open(starter, 'w') as fd:
fd.write(
"""#!%s
import sys
import coverage
import os
from pkg_resources import load_entry_point
if __name__ == '__main__':
cov = coverage.coverage(cover_pylib=False, source=['pyff'], omit=['test'])
cov.start()
rv = 0
try:
rv = load_entry_point('pyFF==%s', 'console_scripts', '%s')()
except Exception as ex:
raise ex
finally:
cov.stop()
cov.save()
os.rename('.coverage','.coverage.%%d' %% os.getpid())
sys.exit(rv)
"""
% (sys.executable, pyffversion, script)
)
os.chmod(starter, 0o700)
argv.insert(0, starter)
proc = _pstart(argv)
out, err = proc.communicate()
rv = proc.wait()
os.unlink(starter)
if isinstance(out, six.binary_type):
out = out.decode('utf-8')
if isinstance(err, six.binary_type):
err = err.decode('utf-8')
print(">> STDOUT ---")
print(out)
print(">> STDERR ---")
print(err)
print("rv=%d" % rv)
print("<< EOF")
return out, err, rv
def _pstart(args, outf=None, ignore_exit=False):
env = {}
logging.debug(" ".join(args))
proc = subprocess.Popen(args, stdout=subprocess.PIPE, stderr=subprocess.PIPE, env=env)
return proc
def _p(args, outf=None, ignore_exit=False):
proc = _pstart(args)
out, err = proc.communicate()
if err is not None and len(err) > 0:
logging.error(err)
if outf is not None:
with open(outf, "w") as fd:
fd.write(out.decode('UTF-8'))
else:
if out is not None and len(out) > 0:
logging.debug(out.decode('UTF-8'))
rv = proc.wait()
if rv and not ignore_exit:
raise RuntimeError("command exited with code != 0: %d" % rv)
class SignerTestCase(TestCase):
datadir = None
private_keyspec = None
public_keyspec = None
def sys_exit(self, code):
raise ExitException(code)
@classmethod
def setUpClass(cls):
cls.datadir = importlib.resources.files(
__name__,
).joinpath('data')
cls.private_keyspec = tempfile.NamedTemporaryFile('w').name
cls.public_keyspec = tempfile.NamedTemporaryFile('w').name
_p(['openssl', 'genrsa', '2048'], outf=cls.private_keyspec, ignore_exit=True)
_p(
[
'openssl',
'req',
'-x509',
'-sha1',
'-new',
'-subj',
'/CN=Signer',
'-key',
cls.private_keyspec,
'-out',
cls.public_keyspec,
]
)
@classmethod
def tearDownClass(cls):
if os.path.exists(cls.private_keyspec):
os.unlink(cls.private_keyspec)
if os.path.exists(cls.public_keyspec):
os.unlink(cls.public_keyspec)