def test_import_rsa_publickey_from_file(self):
        # Test normal case.
        temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory)

        # Load one of the pre-generated key files from 'tuf/tests/repository_data'.
        key_filepath = os.path.join('repository_data', 'keystore',
                                    'root_key.pub')
        self.assertTrue(os.path.exists(key_filepath))

        imported_rsa_key = repo_lib.import_rsa_publickey_from_file(
            key_filepath)
        self.assertTrue(tuf.formats.RSAKEY_SCHEMA.matches(imported_rsa_key))

        # Test improperly formatted argument.
        self.assertRaises(tuf.FormatError,
                          repo_lib.import_rsa_privatekey_from_file, 3)

        # Test invalid argument.
        # Non-existent key file.
        nonexistent_keypath = os.path.join(temporary_directory,
                                           'nonexistent_keypath')
        self.assertRaises(IOError, repo_lib.import_rsa_publickey_from_file,
                          nonexistent_keypath)

        # Invalid key file argument.
        invalid_keyfile = os.path.join(temporary_directory, 'invalid_keyfile')
        with open(invalid_keyfile, 'wb') as file_object:
            file_object.write(b'bad keyfile')
        self.assertRaises(tuf.Error, repo_lib.import_rsa_publickey_from_file,
                          invalid_keyfile)
    def test_generate_and_write_rsa_keypair(self):

        # Test normal case.
        temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory)
        test_keypath = os.path.join(temporary_directory, "rsa_key")

        repo_lib.generate_and_write_rsa_keypair(test_keypath, password="******")
        self.assertTrue(os.path.exists(test_keypath))
        self.assertTrue(os.path.exists(test_keypath + ".pub"))

        # Ensure the generated key files are importable.
        imported_pubkey = repo_lib.import_rsa_publickey_from_file(test_keypath + ".pub")
        self.assertTrue(tuf.formats.RSAKEY_SCHEMA.matches(imported_pubkey))

        imported_privkey = repo_lib.import_rsa_privatekey_from_file(test_keypath, "pw")
        self.assertTrue(tuf.formats.RSAKEY_SCHEMA.matches(imported_privkey))

        # Custom 'bits' argument.
        os.remove(test_keypath)
        os.remove(test_keypath + ".pub")
        repo_lib.generate_and_write_rsa_keypair(test_keypath, bits=2048, password="******")
        self.assertTrue(os.path.exists(test_keypath))
        self.assertTrue(os.path.exists(test_keypath + ".pub"))

        # Test improperly formatted arguments.
        self.assertRaises(tuf.FormatError, repo_lib.generate_and_write_rsa_keypair, 3, bits=2048, password="******")
        self.assertRaises(
            tuf.FormatError, repo_lib.generate_and_write_rsa_keypair, test_keypath, bits="bad", password="******"
        )
        self.assertRaises(tuf.FormatError, repo_lib.generate_and_write_rsa_keypair, test_keypath, bits=2048, password=3)

        # Test invalid 'bits' argument.
        self.assertRaises(
            tuf.FormatError, repo_lib.generate_and_write_rsa_keypair, test_keypath, bits=1024, password="******"
        )
    def test_generate_and_write_rsa_keypair(self):

        # Test normal case.
        temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory)
        test_keypath = os.path.join(temporary_directory, 'rsa_key')

        repo_lib.generate_and_write_rsa_keypair(test_keypath, password='******')
        self.assertTrue(os.path.exists(test_keypath))
        self.assertTrue(os.path.exists(test_keypath + '.pub'))

        # Ensure the generated key files are importable.
        imported_pubkey = \
          repo_lib.import_rsa_publickey_from_file(test_keypath + '.pub')
        self.assertTrue(tuf.formats.RSAKEY_SCHEMA.matches(imported_pubkey))

        imported_privkey = \
          repo_lib.import_rsa_privatekey_from_file(test_keypath, 'pw')
        self.assertTrue(tuf.formats.RSAKEY_SCHEMA.matches(imported_privkey))

        # Custom 'bits' argument.
        os.remove(test_keypath)
        os.remove(test_keypath + '.pub')
        repo_lib.generate_and_write_rsa_keypair(test_keypath,
                                                bits=2048,
                                                password='******')
        self.assertTrue(os.path.exists(test_keypath))
        self.assertTrue(os.path.exists(test_keypath + '.pub'))

        # Test improperly formatted arguments.
        self.assertRaises(tuf.FormatError,
                          repo_lib.generate_and_write_rsa_keypair,
                          3,
                          bits=2048,
                          password='******')
        self.assertRaises(tuf.FormatError,
                          repo_lib.generate_and_write_rsa_keypair,
                          test_keypath,
                          bits='bad',
                          password='******')
        self.assertRaises(tuf.FormatError,
                          repo_lib.generate_and_write_rsa_keypair,
                          test_keypath,
                          bits=2048,
                          password=3)

        # Test invalid 'bits' argument.
        self.assertRaises(tuf.FormatError,
                          repo_lib.generate_and_write_rsa_keypair,
                          test_keypath,
                          bits=1024,
                          password='******')
    def test_import_rsa_publickey_from_file(self):
        # Test normal case.
        temporary_directory = tempfile.mkdtemp(dir=self.temporary_directory)

        # Load one of the pre-generated key files from 'tuf/tests/repository_data'.
        key_filepath = os.path.join("repository_data", "keystore", "root_key.pub")
        self.assertTrue(os.path.exists(key_filepath))

        imported_rsa_key = repo_lib.import_rsa_publickey_from_file(key_filepath)
        self.assertTrue(tuf.formats.RSAKEY_SCHEMA.matches(imported_rsa_key))

        # Test improperly formatted argument.
        self.assertRaises(tuf.FormatError, repo_lib.import_rsa_privatekey_from_file, 3)

        # Test invalid argument.
        # Non-existent key file.
        nonexistent_keypath = os.path.join(temporary_directory, "nonexistent_keypath")
        self.assertRaises(IOError, repo_lib.import_rsa_publickey_from_file, nonexistent_keypath)

        # Invalid key file argument.
        invalid_keyfile = os.path.join(temporary_directory, "invalid_keyfile")
        with open(invalid_keyfile, "wb") as file_object:
            file_object.write(b"bad keyfile")
        self.assertRaises(tuf.Error, repo_lib.import_rsa_publickey_from_file, invalid_keyfile)
示例#5
0
def import_rsa_publickey_from_file(filepath):
  return repo_lib.import_rsa_publickey_from_file(filepath)