generate_test_cert_macros: embed input args
- Embed input arguments inside the script so as to simplify the
calls in Makefiles/CMakeLists.
- add a new "--list-dependencies" command line option to print
out the list of dependencies.
- Modify tests/Makefile accordinlgy.
Signed-off-by: Valerio Setti <valerio.setti@nordicsemi.no>
diff --git a/tests/Makefile b/tests/Makefile
index ad7affb..7fb4f35 100644
--- a/tests/Makefile
+++ b/tests/Makefile
@@ -112,68 +112,9 @@
mbedtls_test: $(MBEDTLS_TEST_OBJS)
-TEST_CERTS_H_INPUT_FILES=\
- data_files/test-ca2.crt \
- data_files/test-ca2.crt.der \
- data_files/test-ca2.key.enc \
- data_files/test-ca2.key.der \
- data_files/test-ca-sha256.crt \
- data_files/test-ca-sha256.crt.der \
- data_files/test-ca-sha1.crt \
- data_files/test-ca-sha1.crt.der \
- data_files/test-ca.key \
- data_files/test-ca.key.der \
- data_files/server5.crt \
- data_files/server5.crt.der \
- data_files/server5.key \
- data_files/server5.key.der \
- data_files/server2-sha256.crt \
- data_files/server2-sha256.crt.der \
- data_files/server2.crt \
- data_files/server2.crt.der \
- data_files/server2.key \
- data_files/server2.key.der \
- data_files/cli2.crt \
- data_files/cli2.crt.der \
- data_files/cli2.key \
- data_files/cli2.key.der \
- data_files/cli-rsa-sha256.crt \
- data_files/cli-rsa-sha256.crt.der \
- data_files/cli-rsa.key \
- data_files/cli-rsa.key.der
src/test_certs.h: scripts/generate_test_cert_macros.py \
- $(TEST_CERTS_H_INPUT_FILES)
- $(PYTHON) scripts/generate_test_cert_macros.py --output $@ \
- --string TEST_CA_CRT_EC_PEM=data_files/test-ca2.crt \
- --binary TEST_CA_CRT_EC_DER=data_files/test-ca2.crt.der \
- --string TEST_CA_KEY_EC_PEM=data_files/test-ca2.key.enc \
- --password TEST_CA_PWD_EC_PEM=PolarSSLTest \
- --binary TEST_CA_KEY_EC_DER=data_files/test-ca2.key.der \
- --string TEST_CA_CRT_RSA_SHA256_PEM=data_files/test-ca-sha256.crt \
- --binary TEST_CA_CRT_RSA_SHA256_DER=data_files/test-ca-sha256.crt.der \
- --string TEST_CA_CRT_RSA_SHA1_PEM=data_files/test-ca-sha1.crt \
- --binary TEST_CA_CRT_RSA_SHA1_DER=data_files/test-ca-sha1.crt.der \
- --string TEST_CA_KEY_RSA_PEM=data_files/test-ca.key \
- --password TEST_CA_PWD_RSA_PEM=PolarSSLTest \
- --binary TEST_CA_KEY_RSA_DER=data_files/test-ca.key.der \
- --string TEST_SRV_CRT_EC_PEM=data_files/server5.crt \
- --binary TEST_SRV_CRT_EC_DER=data_files/server5.crt.der \
- --string TEST_SRV_KEY_EC_PEM=data_files/server5.key \
- --binary TEST_SRV_KEY_EC_DER=data_files/server5.key.der \
- --string TEST_SRV_CRT_RSA_SHA256_PEM=data_files/server2-sha256.crt \
- --binary TEST_SRV_CRT_RSA_SHA256_DER=data_files/server2-sha256.crt.der \
- --string TEST_SRV_CRT_RSA_SHA1_PEM=data_files/server2.crt \
- --binary TEST_SRV_CRT_RSA_SHA1_DER=data_files/server2.crt.der \
- --string TEST_SRV_KEY_RSA_PEM=data_files/server2.key \
- --binary TEST_SRV_KEY_RSA_DER=data_files/server2.key.der \
- --string TEST_CLI_CRT_EC_PEM=data_files/cli2.crt \
- --binary TEST_CLI_CRT_EC_DER=data_files/cli2.crt.der \
- --string TEST_CLI_KEY_EC_PEM=data_files/cli2.key \
- --binary TEST_CLI_KEY_EC_DER=data_files/cli2.key.der \
- --string TEST_CLI_CRT_RSA_PEM=data_files/cli-rsa-sha256.crt \
- --binary TEST_CLI_CRT_RSA_DER=data_files/cli-rsa-sha256.crt.der \
- --string TEST_CLI_KEY_RSA_PEM=data_files/cli-rsa.key \
- --binary TEST_CLI_KEY_RSA_DER=data_files/cli-rsa.key.der
+ $($(PYTHON) scripts/generate_test_cert_macros.py --list-dependencies)
+ $(PYTHON) scripts/generate_test_cert_macros.py --output $@
src/test_keys.h: scripts/generate_test_keys.py
$(PYTHON) scripts/generate_test_keys.py --output $@
diff --git a/tests/scripts/generate_test_cert_macros.py b/tests/scripts/generate_test_cert_macros.py
index a3bca7e..e612f62 100755
--- a/tests/scripts/generate_test_cert_macros.py
+++ b/tests/scripts/generate_test_cert_macros.py
@@ -14,51 +14,61 @@
import argparse
import jinja2
-class MacroDefineAction(argparse.Action):
- #pylint: disable=signature-differs, too-few-public-methods
- def __call__(self, parser, namespace, values, option_string):
- if not hasattr(namespace, 'values'):
- setattr(namespace, 'values', [])
- macro_name, filename = values
- if self.dest in ('string', 'binary') and not os.path.exists(filename):
- raise argparse.ArgumentError(
- None, '`{}`: Input file does not exist.'.format(filename))
- namespace.values.append((self.dest, macro_name, filename))
+this_dir = os.path.dirname(os.path.abspath(__file__))
+data_files_path = os.path.join(this_dir, '..', 'data_files')
-
-def macro_define_type(value):
- ret = value.split('=', 1)
- if len(ret) != 2:
- raise argparse.ArgumentTypeError(
- '`{}` is not MACRO=value format'.format(value))
- return ret
-
-
-def build_argparser(parser):
- parser.description = __doc__
- parser.add_argument('--string', type=macro_define_type, action=MacroDefineAction,
- metavar='MACRO_NAME=path/to/file', help='PEM to C string. ')
- parser.add_argument('--binary', type=macro_define_type, action=MacroDefineAction,
- metavar='MACRO_NAME=path/to/file',
- help='DER to C arrary.')
- parser.add_argument('--password', type=macro_define_type, action=MacroDefineAction,
- metavar='MACRO_NAME=password', help='Password to C string.')
- parser.add_argument('--output', type=str, required=True)
-
+INPUT_ARGS = [
+ ("string", "TEST_CA_CRT_EC_PEM", data_files_path + "/test-ca2.crt"),
+ ("binary", "TEST_CA_CRT_EC_DER", data_files_path + "/test-ca2.crt.der"),
+ ("string", "TEST_CA_KEY_EC_PEM", data_files_path + "/test-ca2.key.enc"),
+ ("password", "TEST_CA_PWD_EC_PEM", "PolarSSLTest"),
+ ("binary", "TEST_CA_KEY_EC_DER", data_files_path + "/test-ca2.key.der"),
+ ("string", "TEST_CA_CRT_RSA_SHA256_PEM", data_files_path + "/test-ca-sha256.crt"),
+ ("binary", "TEST_CA_CRT_RSA_SHA256_DER", data_files_path + "/test-ca-sha256.crt.der"),
+ ("string", "TEST_CA_CRT_RSA_SHA1_PEM", data_files_path + "/test-ca-sha1.crt"),
+ ("binary", "TEST_CA_CRT_RSA_SHA1_DER", data_files_path + "/test-ca-sha1.crt.der"),
+ ("string", "TEST_CA_KEY_RSA_PEM", data_files_path + "/test-ca.key"),
+ ("password", "TEST_CA_PWD_RSA_PEM", "PolarSSLTest"),
+ ("binary", "TEST_CA_KEY_RSA_DER", data_files_path + "/test-ca.key.der"),
+ ("string", "TEST_SRV_CRT_EC_PEM", data_files_path + "/server5.crt"),
+ ("binary", "TEST_SRV_CRT_EC_DER", data_files_path + "/server5.crt.der"),
+ ("string", "TEST_SRV_KEY_EC_PEM", data_files_path + "/server5.key"),
+ ("binary", "TEST_SRV_KEY_EC_DER", data_files_path + "/server5.key.der"),
+ ("string", "TEST_SRV_CRT_RSA_SHA256_PEM", data_files_path + "/server2-sha256.crt"),
+ ("binary", "TEST_SRV_CRT_RSA_SHA256_DER", data_files_path + "/server2-sha256.crt.der"),
+ ("string", "TEST_SRV_CRT_RSA_SHA1_PEM", data_files_path + "/server2.crt"),
+ ("binary", "TEST_SRV_CRT_RSA_SHA1_DER", data_files_path + "/server2.crt.der"),
+ ("string", "TEST_SRV_KEY_RSA_PEM", data_files_path + "/server2.key"),
+ ("binary", "TEST_SRV_KEY_RSA_DER", data_files_path + "/server2.key.der"),
+ ("string", "TEST_CLI_CRT_EC_PEM", data_files_path + "/cli2.crt"),
+ ("binary", "TEST_CLI_CRT_EC_DER", data_files_path + "/cli2.crt.der"),
+ ("string", "TEST_CLI_KEY_EC_PEM", data_files_path + "/cli2.key"),
+ ("binary", "TEST_CLI_KEY_EC_DER", data_files_path + "/cli2.key.der"),
+ ("string", "TEST_CLI_CRT_RSA_PEM", data_files_path + "/cli-rsa-sha256.crt"),
+ ("binary", "TEST_CLI_CRT_RSA_DER", data_files_path + "/cli-rsa-sha256.crt.der"),
+ ("string", "TEST_CLI_KEY_RSA_PEM", data_files_path + "/cli-rsa.key"),
+ ("binary", "TEST_CLI_KEY_RSA_DER", data_files_path + "/cli-rsa.key.der"),
+]
def main():
parser = argparse.ArgumentParser()
- build_argparser(parser)
+ default_output_path = os.path.join(this_dir, '..', 'test_certs.h')
+ parser.add_argument('--output', type=str, default=default_output_path)
+ parser.add_argument('--list-dependencies', action='store_true')
args = parser.parse_args()
- return generate(**vars(args))
+
+ if (args.list_dependencies is True):
+ files_list = [arg[2] for arg in INPUT_ARGS]
+ print(" ".join(files_list))
+ return
+
+ return generate(INPUT_ARGS, output=args.output)
#pylint: disable=dangerous-default-value, unused-argument
-def generate(values=[], output=None, **kwargs):
+def generate(values=[], output=None):
"""Generate C header file.
"""
- this_dir = os.path.dirname(os.path.abspath(__file__))
- template_loader = jinja2.FileSystemLoader(
- searchpath=os.path.join(this_dir, '..', 'data_files'))
+ template_loader = jinja2.FileSystemLoader(data_files_path)
template_env = jinja2.Environment(
loader=template_loader, lstrip_blocks=True, trim_blocks=True)