Skip to content

New implementation tests: apply black and isort automatic linting fixes #1658

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 19 additions & 13 deletions tests/repository_simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,47 +44,51 @@
updater.refresh()
"""

from collections import OrderedDict
from dataclasses import dataclass
from datetime import datetime, timedelta
import logging
import os
import tempfile
from collections import OrderedDict
from dataclasses import dataclass
from datetime import datetime, timedelta
from typing import Dict, Iterator, List, Optional, Tuple
from urllib import parse

import securesystemslib.hash as sslib_hash
from securesystemslib.keys import generate_ed25519_key
from securesystemslib.signer import SSlibSigner
from typing import Dict, Iterator, List, Optional, Tuple
from urllib import parse

from tuf.api.metadata import TOP_LEVEL_ROLE_NAMES
from tuf.api.serialization.json import JSONSerializer
from tuf.exceptions import FetcherHTTPError
from tuf.api.metadata import (
SPECIFICATION_VERSION,
TOP_LEVEL_ROLE_NAMES,
DelegatedRole,
Delegations,
Key,
Metadata,
MetaFile,
Role,
Root,
SPECIFICATION_VERSION,
Snapshot,
TargetFile,
Targets,
Timestamp,
)
from tuf.api.serialization.json import JSONSerializer
from tuf.exceptions import FetcherHTTPError, RepositoryError
from tuf.ngclient.fetcher import FetcherInterface

logger = logging.getLogger(__name__)

SPEC_VER = ".".join(SPECIFICATION_VERSION)


@dataclass
class RepositoryTarget:
"""Contains actual target data and the related target metadata"""

data: bytes
target_file: TargetFile


class RepositorySimulator(FetcherInterface):
def __init__(self):
self.md_root: Metadata[Root] = None
Expand Down Expand Up @@ -141,7 +145,7 @@ def create_key() -> Tuple[Key, SSlibSigner]:
sslib_key = generate_ed25519_key()
return Key.from_securesystemslib_key(sslib_key), SSlibSigner(sslib_key)

def add_signer(self, role:str, signer: SSlibSigner):
def add_signer(self, role: str, signer: SSlibSigner):
if role not in self.signers:
self.signers[role] = {}
self.signers[role][signer.key_dict["keyid"]] = signer
Expand Down Expand Up @@ -197,7 +201,7 @@ def fetch(self, url: str) -> Iterator[bytes]:
elif path.startswith("/targets/"):
# figure out target path and hash prefix
target_path = path[len("/targets/") :]
dir_parts, sep , prefixed_filename = target_path.rpartition("/")
dir_parts, sep, prefixed_filename = target_path.rpartition("/")
prefix, _, filename = prefixed_filename.partition(".")
target_path = f"{dir_parts}{sep}{filename}"

Expand All @@ -219,7 +223,9 @@ def _fetch_target(self, target_path: str, hash: Optional[str]) -> bytes:
logger.debug("fetched target %s", target_path)
return repo_target.data

def _fetch_metadata(self, role: str, version: Optional[int] = None) -> bytes:
def _fetch_metadata(
self, role: str, version: Optional[int] = None
) -> bytes:
"""Return signed metadata for 'role', using 'version' if it is given.

If version is None, non-versioned metadata is being requested
Expand Down Expand Up @@ -264,7 +270,7 @@ def _compute_hashes_and_length(
data = self._fetch_metadata(role)
digest_object = sslib_hash.digest(sslib_hash.DEFAULT_HASH_ALGORITHM)
digest_object.update(data)
hashes = {sslib_hash.DEFAULT_HASH_ALGORITHM: digest_object.hexdigest()}
hashes = {sslib_hash.DEFAULT_HASH_ALGORITHM: digest_object.hexdigest()}
return hashes, len(data)

def update_timestamp(self):
Expand Down
Loading