Skip to content

Commit

Permalink
Merge 0f582dc into 5220786
Browse files Browse the repository at this point in the history
  • Loading branch information
MVrachev committed Apr 7, 2021
2 parents 5220786 + 0f582dc commit 2f8ff60
Show file tree
Hide file tree
Showing 2 changed files with 256 additions and 131 deletions.
35 changes: 22 additions & 13 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,12 @@
import tuf.exceptions
from tuf.api.metadata import (
Metadata,
MetadataInfo,
Root,
Snapshot,
Timestamp,
Targets
Targets,
TargetInfo
)

from tuf.api.serialization import (
Expand Down Expand Up @@ -96,6 +99,7 @@ def tearDownClass(cls):

def test_generic_read(self):
for metadata, inner_metadata_cls in [
('root', Root),
('snapshot', Snapshot),
('timestamp', Timestamp),
('targets', Targets)]:
Expand Down Expand Up @@ -141,7 +145,7 @@ def test_compact_json(self):


def test_read_write_read_compare(self):
for metadata in ['snapshot', 'timestamp', 'targets']:
for metadata in ['root', 'snapshot', 'timestamp', 'targets']:
path = os.path.join(self.repo_dir, 'metadata', metadata + '.json')
metadata_obj = Metadata.from_file(path)

Expand Down Expand Up @@ -228,15 +232,19 @@ def test_metadata_snapshot(self):
# Create a dict representing what we expect the updated data to be
fileinfo = copy.deepcopy(snapshot.signed.meta)
hashes = {'sha256': 'c2986576f5fdfd43944e2b19e775453b96748ec4fe2638a6d2f32f1310967095'}
fileinfo['role1.json']['version'] = 2
fileinfo['role1.json']['hashes'] = hashes
fileinfo['role1.json']['length'] = 123
fileinfo['role1.json'].version = 2
fileinfo['role1.json'].hashes = hashes
fileinfo['role1.json'].length = 123


self.assertNotEqual(snapshot.signed.meta, fileinfo)
snapshot.signed.update('role1', 2, 123, hashes)
self.assertEqual(snapshot.signed.meta, fileinfo)

# Update only version. Length and hashes are optional.
snapshot.signed.update('role1', 3)
fileinfo['role1.json'] = MetadataInfo(3)
self.assertEqual(snapshot.signed.meta, fileinfo)

def test_metadata_timestamp(self):
timestamp_path = os.path.join(
Expand Down Expand Up @@ -264,14 +272,18 @@ def test_metadata_timestamp(self):

hashes = {'sha256': '0ae9664468150a9aa1e7f11feecb32341658eb84292851367fea2da88e8a58dc'}
fileinfo = copy.deepcopy(timestamp.signed.meta['snapshot.json'])
fileinfo['hashes'] = hashes
fileinfo['version'] = 2
fileinfo['length'] = 520
fileinfo.hashes = hashes
fileinfo.version = 2
fileinfo.length = 520

self.assertNotEqual(timestamp.signed.meta['snapshot.json'], fileinfo)
timestamp.signed.update(2, 520, hashes)
self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)

# Update only version. Length and hashes are optional.
timestamp.signed.update(3)
fileinfo = MetadataInfo(version=3)
self.assertEqual(timestamp.signed.meta['snapshot.json'], fileinfo)

def test_metadata_root(self):
root_path = os.path.join(
Expand Down Expand Up @@ -318,15 +330,12 @@ def test_metadata_targets(self):
"sha512": "ef5beafa16041bcdd2937140afebd485296cd54f7348ecd5a4d035c09759608de467a7ac0eb58753d0242df873c305e8bffad2454aa48f44480f15efae1cacd0"
},

fileinfo = {
'hashes': hashes,
'length': 28
}
fileinfo = TargetInfo(length=28, hashes=hashes)

# Assert that data is not aleady equal
self.assertNotEqual(targets.signed.targets[filename], fileinfo)
# Update an already existing fileinfo
targets.signed.update(filename, fileinfo)
targets.signed.update(filename, fileinfo.to_dict())
# Verify that data is updated
self.assertEqual(targets.signed.targets[filename], fileinfo)

Expand Down
Loading

0 comments on commit 2f8ff60

Please sign in to comment.