diff --git a/tests/repository_data/generate_project_data.py b/tests/repository_data/generate_project_data.py new file mode 100755 index 00000000..19d6a8db --- /dev/null +++ b/tests/repository_data/generate_project_data.py @@ -0,0 +1,160 @@ +#!/usr/bin/env python + +""" + + generate_project_data.py + + + Santiago Torres + + + + See LICENSE for licensing information. + + + Generate a pre-fabricated set of metadata files to use for the unit testing. +""" + +import shutil +import datetime +import optparse +import os + +from tuf.developer_tool import * +import tuf.util + + +parser = optparse.OptionParser() + +parser.add_option("-d","--dry-run", action='store_true', dest="dry_run", + help="Do not write the files, just run", default=False) +(options, args) = parser.parse_args() + + +project_key_file = 'keystore/root_key' +targets_key_file = 'keystore/targets_key' +delegation_key_file = 'keystore/delegation_key' + +# the files we use for signing in the unit tests should exist, if they are not +# populated, run generate.py +assert os.path.exists(project_key_file) +assert os.path.exists(targets_key_file) +assert os.path.exists(delegation_key_file) + +# Import the public keys. These keys are needed so that metadata roles are +# assigned verification keys, which clients use to verify the signatures created +# by the corresponding private keys. +project_public = import_rsa_publickey_from_file(project_key_file+'.pub') +targets_public = import_rsa_publickey_from_file(targets_key_file+'.pub') +delegation_public = import_rsa_publickey_from_file(delegation_key_file+'.pub') + +# Import the private keys. These private keys are needed to generate the +# signatures included in metadata. +project_private = import_rsa_privatekey_from_file(project_key_file, 'password') +targets_private = import_rsa_privatekey_from_file(targets_key_file, 'password') +delegation_private = import_rsa_privatekey_from_file(delegation_key_file, 'password') + +os.mkdir("project") +os.mkdir("project/targets") + +# Create the target files (downloaded by clients) whose file size and digest +# are specified in the 'targets.json' file. +target1_filepath = 'project/targets/file1.txt' +tuf.util.ensure_parent_dir(target1_filepath) +target2_filepath = 'project/targets/file2.txt' +tuf.util.ensure_parent_dir(target2_filepath) +target3_filepath = 'project/targets/file3.txt' +tuf.util.ensure_parent_dir(target2_filepath) + +if not options.dry_run: + with open(target1_filepath, 'wt') as file_object: + file_object.write('This is an example target file.') + + with open(target2_filepath, 'wt') as file_object: + file_object.write('This is an another example target file.') + + with open(target3_filepath, 'wt') as file_object: + file_object.write('This is role1\'s target file.') + + +project = create_new_project("test-flat", 'project/test-flat', 'prefix', + 'project/targets') + +# Add target files to the top-level 'targets.json' role. These target files +# should already exist. +project.add_target(target1_filepath) +project.add_target(target2_filepath) + +# add keys to the project +project.add_verification_key(project_public) +project.load_signing_key(project_private) + +project.delegate('role1', [delegation_public], [target3_filepath]) +project('role1').load_signing_key(delegation_private) + +# Set the top-level expiration times far into the future so that +# they do not expire anytime soon, or else the tests fail. Unit tests may +# modify the expiration datetimes (of the copied files), if they wish. +project.expiration = datetime.datetime(2030, 1, 1, 0, 0) +project('role1').expiration = datetime.datetime(2030, 1, 1, 0, 0) + +# Compress the 'targets.json' role so that the unit tests have a pre-generated +# example of compressed metadata. +project.compressions = ['gz'] + +# Create the actual metadata files, which are saved to 'metadata.staged'. +if not options.dry_run: + project.write() + +tuf.roledb.clear_roledb() +tuf.keydb.clear_keydb() + +project = create_new_project("test-repo-like", "project/test-repo", 'prefix') + +# Create the target files (downloaded by clients) whose file size and digest +# are specified in the 'targets.json' file. +target1_filepath = 'project/test-repo/targets/file1.txt' +tuf.util.ensure_parent_dir(target1_filepath) +target2_filepath = 'project/test-repo/targets/file2.txt' +tuf.util.ensure_parent_dir(target2_filepath) +target3_filepath = 'project/test-repo/targets/file3.txt' +tuf.util.ensure_parent_dir(target2_filepath) + +if not options.dry_run: + with open(target1_filepath, 'wt') as file_object: + file_object.write('This is an example target file.') + + with open(target2_filepath, 'wt') as file_object: + file_object.write('This is an another example target file.') + + with open(target3_filepath, 'wt') as file_object: + file_object.write('This is role1\'s target file.') + + +# Add target files to the top-level 'targets.json' role. These target files +# should already exist. +project.add_target(target1_filepath) +project.add_target(target2_filepath) + +# add keys to the project +project.add_verification_key(project_public) +project.load_signing_key(project_private) + +project.delegate('role1', [delegation_public], [target3_filepath]) +project('role1').load_signing_key(delegation_private) + +# Set the top-level expiration times far into the future so that +# they do not expire anytime soon, or else the tests fail. Unit tests may +# modify the expiration datetimes (of the copied files), if they wish. +project.expiration = datetime.datetime(2030, 1, 1, 0, 0) +project('role1').expiration = datetime.datetime(2030, 1, 1, 0, 0) + +# Compress the 'targets.json' role so that the unit tests have a pre-generated +# example of compressed metadata. +project.compressions = ['gz'] + +# Create the actual metadata files, which are saved to 'metadata.staged'. +if not options.dry_run: + project.write() + + diff --git a/tests/repository_data/project/targets/file1.txt b/tests/repository_data/project/targets/file1.txt new file mode 100644 index 00000000..7bf3499f --- /dev/null +++ b/tests/repository_data/project/targets/file1.txt @@ -0,0 +1 @@ +This is an example target file. \ No newline at end of file diff --git a/tests/repository_data/project/targets/file2.txt b/tests/repository_data/project/targets/file2.txt new file mode 100644 index 00000000..606f18ef --- /dev/null +++ b/tests/repository_data/project/targets/file2.txt @@ -0,0 +1 @@ +This is an another example target file. \ No newline at end of file diff --git a/tests/repository_data/project/targets/file3.txt b/tests/repository_data/project/targets/file3.txt new file mode 100644 index 00000000..60464604 --- /dev/null +++ b/tests/repository_data/project/targets/file3.txt @@ -0,0 +1 @@ +This is role1's target file. \ No newline at end of file diff --git a/tests/repository_data/project/test-flat/project.cfg b/tests/repository_data/project/test-flat/project.cfg new file mode 100644 index 00000000..61b14806 --- /dev/null +++ b/tests/repository_data/project/test-flat/project.cfg @@ -0,0 +1 @@ +{"project_name": "test-flat", "targets_location": "/home/santiago/Documents/v2014/TUF/tuf/tests/repository_data/project/targets", "prefix": "prefix", "metadata_location": "/home/santiago/Documents/v2014/TUF/tuf/tests/repository_data/project/test-flat", "threshold": 1, "public_keys": {"6986b667c736a3b37471e030cf4ce7aa6c7e0d530325e64c2660276b77be3754": {"keytype": "rsa", "keyval": {"public": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7J15ZaeDQPrhQsRj29wB\nPhibH+Do59xsT2396L+uCg793gZlar5wZN2eHSh725cNQWyTAa9LwG+lXaKMukQ+\n8176CKR2J5sv3DezrGVu3x8V1qhyJyy79FlNZRVYTVqNaYzvJzxsVnFPpg7f8B7C\nffiqWJr9XkpqwRlCpxooXm4hplZ7uek5Ku21CzQ4OWg7hbuc+ZjCGzpXfm8NuosU\n7TipnKGpEt0Agiph5g6TB2/scoeFar1CKMONIl80maxzAQk+xkWgiJ00+Z2qFCsx\nESfis/YkILS6RMFyZz7oa1WwMtUjYmrsRuz+jlFcbNuxZpIkaISiG9a2YdGcJ1Aj\n3QIDAQAB\n-----END PUBLIC KEY-----"}}}, "layout_type": "flat"} \ No newline at end of file diff --git a/tests/repository_data/project/test-flat/test-flat.json b/tests/repository_data/project/test-flat/test-flat.json new file mode 100644 index 00000000..14741724 Binary files /dev/null and b/tests/repository_data/project/test-flat/test-flat.json differ diff --git a/tests/repository_data/project/test-flat/test-flat/role1.json b/tests/repository_data/project/test-flat/test-flat/role1.json new file mode 100644 index 00000000..4e38e30f Binary files /dev/null and b/tests/repository_data/project/test-flat/test-flat/role1.json differ diff --git a/tests/repository_data/project/test-repo/metadata/test-repo-like.json b/tests/repository_data/project/test-repo/metadata/test-repo-like.json new file mode 100644 index 00000000..cad9c4c1 Binary files /dev/null and b/tests/repository_data/project/test-repo/metadata/test-repo-like.json differ diff --git a/tests/repository_data/project/test-repo/metadata/test-repo-like/role1.json b/tests/repository_data/project/test-repo/metadata/test-repo-like/role1.json new file mode 100644 index 00000000..caac5a0d Binary files /dev/null and b/tests/repository_data/project/test-repo/metadata/test-repo-like/role1.json differ diff --git a/tests/repository_data/project/test-repo/project.cfg b/tests/repository_data/project/test-repo/project.cfg new file mode 100644 index 00000000..e3a93c8d --- /dev/null +++ b/tests/repository_data/project/test-repo/project.cfg @@ -0,0 +1 @@ +{"project_name": "test-repo-like", "targets_location": "/home/santiago/Documents/v2014/TUF/tuf/tests/repository_data/project/test-repo/targets", "prefix": "prefix", "metadata_location": "/home/santiago/Documents/v2014/TUF/tuf/tests/repository_data/project/test-repo/metadata", "threshold": 1, "public_keys": {"6986b667c736a3b37471e030cf4ce7aa6c7e0d530325e64c2660276b77be3754": {"keytype": "rsa", "keyval": {"public": "-----BEGIN PUBLIC KEY-----\nMIIBIjANBgkqhkiG9w0BAQEFAAOCAQ8AMIIBCgKCAQEA7J15ZaeDQPrhQsRj29wB\nPhibH+Do59xsT2396L+uCg793gZlar5wZN2eHSh725cNQWyTAa9LwG+lXaKMukQ+\n8176CKR2J5sv3DezrGVu3x8V1qhyJyy79FlNZRVYTVqNaYzvJzxsVnFPpg7f8B7C\nffiqWJr9XkpqwRlCpxooXm4hplZ7uek5Ku21CzQ4OWg7hbuc+ZjCGzpXfm8NuosU\n7TipnKGpEt0Agiph5g6TB2/scoeFar1CKMONIl80maxzAQk+xkWgiJ00+Z2qFCsx\nESfis/YkILS6RMFyZz7oa1WwMtUjYmrsRuz+jlFcbNuxZpIkaISiG9a2YdGcJ1Aj\n3QIDAQAB\n-----END PUBLIC KEY-----"}}}, "layout_type": "repo-like"} \ No newline at end of file diff --git a/tests/repository_data/project/test-repo/targets/file1.txt b/tests/repository_data/project/test-repo/targets/file1.txt new file mode 100644 index 00000000..7bf3499f --- /dev/null +++ b/tests/repository_data/project/test-repo/targets/file1.txt @@ -0,0 +1 @@ +This is an example target file. \ No newline at end of file diff --git a/tests/repository_data/project/test-repo/targets/file2.txt b/tests/repository_data/project/test-repo/targets/file2.txt new file mode 100644 index 00000000..606f18ef --- /dev/null +++ b/tests/repository_data/project/test-repo/targets/file2.txt @@ -0,0 +1 @@ +This is an another example target file. \ No newline at end of file diff --git a/tests/repository_data/project/test-repo/targets/file3.txt b/tests/repository_data/project/test-repo/targets/file3.txt new file mode 100644 index 00000000..60464604 --- /dev/null +++ b/tests/repository_data/project/test-repo/targets/file3.txt @@ -0,0 +1 @@ +This is role1's target file. \ No newline at end of file