Reference implementations of PQC
您最多选择25个主题 主题必须以字母或数字开头,可以包含连字符 (-),并且长度不得超过35个字符
 
 
 
 

79 行
2.2 KiB

  1. """
  2. Checks that the implementation does not make use of the `char` type.
  3. This is ambiguous; compilers can freely choose `signed` or `unsigned` char.
  4. """
  5. import os
  6. import pytest
  7. import helpers
  8. import pqclean
  9. import pycparser
  10. def setup_module():
  11. if not(os.path.exists(os.path.join('pycparser', '.git'))):
  12. print("Please run `git submodule update --init`")
  13. def walk_tree(ast, parent=[]):
  14. if type(ast) is pycparser.c_ast.IdentifierType:
  15. if ast.names == ['char']:
  16. # allow casts in certain function calls
  17. try:
  18. if parent[-5].name.name in ['_mm256_set1_epi8']:
  19. return
  20. except:
  21. pass
  22. yield ast
  23. for (_, child) in ast.children():
  24. # recursively yield prohibited nodes
  25. yield from walk_tree(child, parent=parent + [ast])
  26. @pytest.mark.parametrize(
  27. 'implementation',
  28. pqclean.Scheme.all_implementations(),
  29. ids=str,
  30. )
  31. @helpers.skip_windows()
  32. @helpers.filtered_test
  33. def test_char(implementation):
  34. errors = []
  35. for fname in os.listdir(implementation.path()):
  36. if not fname.endswith(".c"):
  37. continue
  38. tdir, _ = os.path.split(os.path.realpath(__file__))
  39. ast = pycparser.parse_file(
  40. os.path.join(implementation.path(), fname),
  41. use_cpp=True,
  42. cpp_path='cc', # not all platforms link cpp correctly; cc -E works
  43. cpp_args=[
  44. '-E',
  45. '-std=c99',
  46. '-nostdinc', # pycparser cannot deal with e.g. __attribute__
  47. '-I{}'.format(os.path.join(tdir, "../common")),
  48. # necessary to mock e.g. <stdint.h>
  49. '-I{}'.format(
  50. os.path.join(tdir, 'pycparser/utils/fake_libc_include')),
  51. ]
  52. )
  53. for node in walk_tree(ast):
  54. # flatten nodes to a string to easily enforce uniqueness
  55. err = "\n at {c.file}:{c.line}:{c.column}".format(c=node.coord)
  56. if err not in errors:
  57. errors.append(err)
  58. if errors:
  59. raise AssertionError(
  60. "Prohibited use of char without explicit signed/unsigned" +
  61. "".join(errors)
  62. )
  63. if __name__ == "__main__":
  64. import sys
  65. pytest.main(sys.argv)