2019-03-04 14:12:38 +00:00
|
|
|
import functools
|
2019-02-27 11:58:00 +00:00
|
|
|
import os
|
2019-02-14 03:25:34 +00:00
|
|
|
import subprocess
|
2019-03-04 14:12:38 +00:00
|
|
|
import unittest
|
2019-03-06 16:24:02 +00:00
|
|
|
import shutil
|
|
|
|
import sys
|
2019-02-14 03:25:34 +00:00
|
|
|
|
2019-04-10 22:03:02 +01:00
|
|
|
import pqclean
|
|
|
|
|
2019-04-18 09:00:08 +01:00
|
|
|
import logging
|
|
|
|
|
2019-02-18 12:04:59 +00:00
|
|
|
|
2019-02-27 11:58:00 +00:00
|
|
|
def run_subprocess(command, working_dir='.', env=None, expected_returncode=0):
|
2019-02-18 12:04:59 +00:00
|
|
|
"""
|
|
|
|
Helper function to run a shell command and report success/failure
|
|
|
|
depending on the exit status of the shell command.
|
|
|
|
"""
|
2019-02-27 11:58:00 +00:00
|
|
|
if env is not None:
|
|
|
|
env_ = os.environ.copy()
|
|
|
|
env_.update(env)
|
|
|
|
env = env_
|
|
|
|
|
2019-02-18 12:04:59 +00:00
|
|
|
# Note we need to capture stdout/stderr from the subprocess,
|
|
|
|
# then print it, which nose/unittest will then capture and
|
|
|
|
# buffer appropriately
|
2019-03-04 14:12:38 +00:00
|
|
|
print(working_dir + " > " + " ".join(command))
|
2019-02-14 03:25:34 +00:00
|
|
|
result = subprocess.run(
|
|
|
|
command,
|
2019-02-18 12:04:59 +00:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT,
|
2019-02-27 11:58:00 +00:00
|
|
|
cwd=working_dir,
|
|
|
|
env=env,
|
2019-02-14 03:25:34 +00:00
|
|
|
)
|
|
|
|
print(result.stdout.decode('utf-8'))
|
2019-04-11 18:22:50 +01:00
|
|
|
if expected_returncode is not None:
|
|
|
|
assert result.returncode == expected_returncode, \
|
|
|
|
"Got unexpected return code {}".format(result.returncode)
|
|
|
|
else:
|
|
|
|
return (result.returncode, result.stdout.decode('utf-8'))
|
2019-02-14 16:29:49 +00:00
|
|
|
return result.stdout.decode('utf-8')
|
2019-02-27 11:44:21 +00:00
|
|
|
|
|
|
|
|
2019-03-04 15:54:37 +00:00
|
|
|
def make(*args, working_dir='.', env=None, expected_returncode=0, **kwargs):
|
2019-02-27 11:44:21 +00:00
|
|
|
"""
|
|
|
|
Runs a make target in the specified working directory
|
|
|
|
|
|
|
|
Usage:
|
|
|
|
make('clean', 'targetb', SCHEME='bla')
|
|
|
|
"""
|
2019-03-04 14:12:38 +00:00
|
|
|
if os.name == 'nt':
|
2019-03-05 10:45:00 +00:00
|
|
|
make_command = ['nmake', '/f', 'Makefile.Microsoft_nmake',
|
|
|
|
'/NOLOGO', '/E']
|
2019-03-04 14:12:38 +00:00
|
|
|
# we need SCHEME_UPPERCASE and IMPLEMENTATION_UPPERCASE with nmake
|
|
|
|
for envvar in ['IMPLEMENTATION', 'SCHEME']:
|
|
|
|
if envvar in kwargs:
|
2019-03-06 16:24:02 +00:00
|
|
|
kwargs['{}_UPPERCASE'.format(envvar)] = (
|
|
|
|
kwargs[envvar].upper().replace('-', ''))
|
2019-03-04 14:12:38 +00:00
|
|
|
else:
|
|
|
|
make_command = ['make']
|
|
|
|
|
2019-02-27 11:44:21 +00:00
|
|
|
return run_subprocess(
|
|
|
|
[
|
2019-03-04 14:12:38 +00:00
|
|
|
*make_command,
|
2019-02-27 11:44:21 +00:00
|
|
|
*['{}={}'.format(k, v) for k, v in kwargs.items()],
|
2019-03-04 14:12:38 +00:00
|
|
|
*args,
|
2019-02-27 11:44:21 +00:00
|
|
|
],
|
|
|
|
working_dir=working_dir,
|
2019-02-27 11:58:00 +00:00
|
|
|
env=env,
|
2019-03-04 15:54:37 +00:00
|
|
|
expected_returncode=expected_returncode,
|
2019-02-27 11:44:21 +00:00
|
|
|
)
|
2019-03-04 14:12:38 +00:00
|
|
|
|
|
|
|
|
|
|
|
def skip_windows(message="This test is not supported on Windows"):
|
|
|
|
def wrapper(f):
|
|
|
|
@functools.wraps(f)
|
|
|
|
def skip_windows(*args, **kwargs):
|
|
|
|
raise unittest.SkipTest(message)
|
|
|
|
if os.name == 'nt':
|
|
|
|
return skip_windows
|
|
|
|
else:
|
|
|
|
return f
|
2019-03-06 16:24:02 +00:00
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2019-04-17 10:17:46 +01:00
|
|
|
def slow_test(f):
|
|
|
|
@functools.wraps(f)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if 'CI' in os.environ and 'RUN_SLOW' not in os.environ:
|
|
|
|
raise unittest.SkipTest("Slow test skipped on CI run")
|
|
|
|
return f(*args, **kwargs)
|
|
|
|
return wrapper
|
|
|
|
|
|
|
|
|
2019-03-06 16:24:02 +00:00
|
|
|
def ensure_available(executable):
|
|
|
|
"""
|
|
|
|
Checks if a command is available.
|
|
|
|
|
|
|
|
If a command MUST be available, because we are in a CI environment,
|
|
|
|
raises an AssertionError.
|
|
|
|
|
|
|
|
In the docker containers, on Travis and on Windows, CI=true is set.
|
|
|
|
"""
|
|
|
|
path = shutil.which(executable)
|
|
|
|
if path:
|
|
|
|
return path
|
|
|
|
|
|
|
|
# Installing clang-tidy on LLVM will be too much of a mess.
|
|
|
|
if ((executable == 'clang-tidy' and sys.platform == 'darwin')
|
|
|
|
or 'CI' not in os.environ):
|
|
|
|
raise unittest.SkipTest(
|
|
|
|
"{} is not available on PATH. Install it to run this test.{}"
|
|
|
|
.format(executable, "" if not os.name == 'nt'
|
|
|
|
else "On Windows, make sure to add it to PATH")
|
|
|
|
)
|
|
|
|
raise AssertionError("{} not available on CI".format(executable))
|
2019-04-10 22:03:02 +01:00
|
|
|
|
|
|
|
|
2019-04-18 09:00:08 +01:00
|
|
|
def permit_test(testname, thing, *args, **kwargs):
|
2019-04-10 22:03:02 +01:00
|
|
|
if 'PQCLEAN_ONLY_TESTS' in os.environ:
|
|
|
|
if not(testname.lower() in os.environ['PQCLEAN_ONLY_TESTS'].lower().split(',')):
|
|
|
|
return False
|
|
|
|
if 'PQCLEAN_SKIP_TESTS' in os.environ:
|
|
|
|
if testname.lower() in os.environ['PQCLEAN_SKIP_TESTS'].lower().split(','):
|
|
|
|
return False
|
|
|
|
|
|
|
|
if isinstance(thing, pqclean.Implementation):
|
|
|
|
scheme = thing.scheme
|
2019-04-11 18:47:53 +01:00
|
|
|
elif isinstance(thing, pqclean.Scheme):
|
2019-04-10 22:03:02 +01:00
|
|
|
scheme = thing
|
2019-04-11 18:47:53 +01:00
|
|
|
else:
|
|
|
|
return True
|
2019-04-10 22:03:02 +01:00
|
|
|
|
|
|
|
if 'PQCLEAN_ONLY_TYPES' in os.environ:
|
|
|
|
if not(scheme.type.lower() in os.environ['PQCLEAN_ONLY_TYPES'].lower().split(',')):
|
|
|
|
return False
|
|
|
|
if 'PQCLEAN_SKIP_TYPES' in os.environ:
|
|
|
|
if scheme.type.lower() in os.environ['PQCLEAN_SKIP_TYPES'].lower().split(','):
|
|
|
|
return False
|
|
|
|
if 'PQCLEAN_ONLY_SCHEMES' in os.environ:
|
|
|
|
if not(scheme.name.lower() in os.environ['PQCLEAN_ONLY_SCHEMES'].lower().split(',')):
|
|
|
|
return False
|
|
|
|
if 'PQCLEAN_SKIP_SCHEMES' in os.environ:
|
|
|
|
if scheme.name.lower() in os.environ['PQCLEAN_SKIP_SCHEMES'].lower().split(','):
|
|
|
|
return False
|
|
|
|
|
2019-04-11 20:23:39 +01:00
|
|
|
if 'PQCLEAN_ONLY_DIFF' in os.environ:
|
2019-04-16 13:17:18 +01:00
|
|
|
if shutil.which('git') is not None:
|
2019-04-11 20:23:39 +01:00
|
|
|
# if we're on a non-master branch, and the only changes are in schemes,
|
|
|
|
# only run tests on those schemes
|
|
|
|
branch_result = subprocess.run(
|
|
|
|
['git', 'status', '--porcelain=2', '--branch'],
|
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT
|
|
|
|
)
|
|
|
|
# ensure we're in a working directory
|
|
|
|
if branch_result.returncode != 0:
|
|
|
|
return True
|
|
|
|
# ensure we're not on master branch
|
|
|
|
for branch_line in branch_result.stdout.decode('utf-8').splitlines():
|
|
|
|
tokens = branch_line.split(' ')
|
|
|
|
if tokens[0] == '#' and tokens[1] == 'branch.head':
|
|
|
|
if tokens[2] == 'master':
|
|
|
|
return True
|
|
|
|
# where are there changes?
|
|
|
|
diff_result = subprocess.run(
|
2019-04-16 14:01:31 +01:00
|
|
|
['git', 'diff', '--name-only', 'origin/master'],
|
2019-04-11 20:23:39 +01:00
|
|
|
stdout=subprocess.PIPE,
|
|
|
|
stderr=subprocess.STDOUT
|
|
|
|
)
|
|
|
|
assert(diff_result.returncode == 0), "Got unexpected return code {}".format(diff_result.returncode)
|
|
|
|
for diff_line in diff_result.stdout.decode('utf-8').splitlines():
|
|
|
|
# don't skip test if there are any changes outside schemes
|
|
|
|
if not(diff_line.startswith('crypto_kem')) and not (diff_line.startswith('crypto_sign')):
|
2019-04-18 09:00:08 +01:00
|
|
|
logging.info("Running all tests as there are changes outside of schemes")
|
2019-04-11 20:23:39 +01:00
|
|
|
return True
|
|
|
|
# do test if the scheme in question has been changed
|
|
|
|
if diff_line.startswith(thing.path(base='')):
|
|
|
|
return True
|
|
|
|
# there were no changes outside schemes, and the scheme in question had no diffs
|
|
|
|
return False
|
|
|
|
|
2019-04-10 22:03:02 +01:00
|
|
|
return True
|
2019-04-18 09:00:08 +01:00
|
|
|
|
|
|
|
|
|
|
|
def filtered_test(func):
|
|
|
|
funcname = func.__name__[len("check_"):]
|
|
|
|
|
|
|
|
@functools.wraps(func)
|
|
|
|
def wrapper(*args, **kwargs):
|
|
|
|
if permit_test(funcname, *args, **kwargs):
|
|
|
|
return func(*args, **kwargs)
|
|
|
|
else:
|
|
|
|
raise unittest.SkipTest("Test disabled by filter")
|
|
|
|
return wrapper
|