Skip to content
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

New metadata API: add MetadataInfo and TargetFile classes #1223

Closed
44 changes: 27 additions & 17 deletions tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,12 @@ def setUpModule():
import tuf.exceptions
from tuf.api.metadata import (
Metadata,
MetadataInfo,
Root,
Snapshot,
Timestamp,
Targets
Targets,
TargetInfo
)

from securesystemslib.interface import (
Expand Down Expand Up @@ -92,6 +95,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 @@ -137,7 +141,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_json_file(path)

Expand Down Expand Up @@ -222,15 +226,20 @@ 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 All @@ -257,14 +266,18 @@ def test_metadata_timestamp(self):
self.assertEqual(timestamp.signed.expires, datetime(2036, 1, 3, 0, 0))

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

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

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


def test_metadata_root(self):
Expand Down Expand Up @@ -312,15 +325,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