pqc/test/test_symbol_namespace.py

61 рядки
1.7 KiB
Python

"""
2019-02-18 12:04:59 +00:00
Checks that the all exported symbols are properly namespaced, i.e., all
start with "PQCLEAN_SCHEMENAME_".
"""
import pqclean
import helpers
import sys
import unittest
2019-02-18 12:04:59 +00:00
def test_symbol_namespace():
2019-02-18 12:04:59 +00:00
if sys.platform not in ['linux', 'darwin']:
raise unittest.SkipTest()
for scheme in pqclean.Scheme.all_schemes():
for implementation in scheme.implementations:
yield check_symbol_namespace, scheme.name, implementation.name
2019-02-18 12:04:59 +00:00
def check_symbol_namespace(scheme_name, implementation_name):
2019-02-18 12:04:59 +00:00
implementation = pqclean.Implementation.by_name(
scheme_name, implementation_name)
helpers.run_subprocess(
['make'],
implementation.path()
)
out = helpers.run_subprocess(
['nm', '-g', 'lib{}_{}.a'.format(scheme_name, implementation_name)],
2019-02-18 12:04:59 +00:00
implementation.path()
)
lines = out.strip().split("\n")
symbols = []
for line in lines:
if ' T ' in line or ' D ' in line or ' S ' in line:
symbols.append(line)
namespace = implementation.namespace_prefix()
non_namespaced = []
for symbolstr in symbols:
*_, symtype, symbol = symbolstr.split()
if symtype in 'TR':
2019-02-18 12:04:59 +00:00
if (not symbol.startswith(namespace) and
not symbol.startswith('_' + namespace)):
non_namespaced.append(symbol)
if non_namespaced:
print("Missing namespace literal {}".format(namespace))
for symbol in non_namespaced:
print("\ttype: {}, symbol: {}".format(symtype, symbol))
assert(False)
if __name__ == '__main__':
2019-02-18 12:51:01 +00:00
try:
import nose2
nose2.main()
except ImportError:
import nose
nose.runmodule()