|
| 1 | +import json |
| 2 | +import logging |
| 3 | +import os |
| 4 | +import shutil |
| 5 | +import sys |
| 6 | +import tempfile |
| 7 | +import unittest |
| 8 | + |
| 9 | +from tuf import exceptions |
| 10 | +from tuf.api.metadata import Metadata |
| 11 | +from tuf.client_rework.metadata_bundle import MetadataBundle |
| 12 | + |
| 13 | +from tests import utils |
| 14 | + |
| 15 | +logger = logging.getLogger(__name__) |
| 16 | + |
| 17 | +class TestMetadataBundle(unittest.TestCase): |
| 18 | + |
| 19 | + def test_update(self): |
| 20 | + repo_dir = os.path.join(os.getcwd(), 'repository_data', 'repository', 'metadata') |
| 21 | + |
| 22 | + with open(os.path.join(repo_dir, "root.json"), "rb") as f: |
| 23 | + bundle = MetadataBundle(f.read()) |
| 24 | + bundle.root_update_finished() |
| 25 | + |
| 26 | + with open(os.path.join(repo_dir, "timestamp.json"), "rb") as f: |
| 27 | + bundle.update_timestamp(f.read()) |
| 28 | + with open(os.path.join(repo_dir, "snapshot.json"), "rb") as f: |
| 29 | + bundle.update_snapshot(f.read()) |
| 30 | + with open(os.path.join(repo_dir, "targets.json"), "rb") as f: |
| 31 | + bundle.update_targets(f.read()) |
| 32 | + with open(os.path.join(repo_dir, "role1.json"), "rb") as f: |
| 33 | + bundle.update_delegated_targets(f.read(), "role1", "targets") |
| 34 | + with open(os.path.join(repo_dir, "role2.json"), "rb") as f: |
| 35 | + bundle.update_delegated_targets(f.read(), "role2", "role1") |
| 36 | + |
| 37 | + def test_out_of_order_ops(self): |
| 38 | + repo_dir = os.path.join(os.getcwd(), 'repository_data', 'repository', 'metadata') |
| 39 | + data={} |
| 40 | + for md in ["root", "timestamp", "snapshot", "targets", "role1"]: |
| 41 | + with open(os.path.join(repo_dir, f"{md}.json"), "rb") as f: |
| 42 | + data[md] = f.read() |
| 43 | + |
| 44 | + bundle = MetadataBundle(data["root"]) |
| 45 | + |
| 46 | + # Update timestamp before root is finished |
| 47 | + with self.assertRaises(RuntimeError): |
| 48 | + bundle.update_timestamp(data["timestamp"]) |
| 49 | + |
| 50 | + bundle.root_update_finished() |
| 51 | + with self.assertRaises(RuntimeError): |
| 52 | + bundle.root_update_finished() |
| 53 | + |
| 54 | + # Update snapshot before timestamp |
| 55 | + with self.assertRaises(RuntimeError): |
| 56 | + bundle.update_snapshot(data["snapshot"]) |
| 57 | + |
| 58 | + bundle.update_timestamp(data["timestamp"]) |
| 59 | + |
| 60 | + # Update targets before snapshot |
| 61 | + with self.assertRaises(RuntimeError): |
| 62 | + bundle.update_targets(data["targets"]) |
| 63 | + |
| 64 | + bundle.update_snapshot(data["snapshot"]) |
| 65 | + |
| 66 | + #update timestamp after snapshot |
| 67 | + with self.assertRaises(RuntimeError): |
| 68 | + bundle.update_timestamp(data["timestamp"]) |
| 69 | + |
| 70 | + # Update delegated targets before targets |
| 71 | + with self.assertRaises(RuntimeError): |
| 72 | + bundle.update_delegated_targets(data["role1"], "role1", "targets") |
| 73 | + |
| 74 | + bundle.update_targets(data["targets"]) |
| 75 | + bundle.update_delegated_targets(data["role1"], "role1", "targets") |
| 76 | + |
| 77 | + def test_update_with_invalid_json(self): |
| 78 | + repo_dir = os.path.join(os.getcwd(), 'repository_data', 'repository', 'metadata') |
| 79 | + data={} |
| 80 | + for md in ["root", "timestamp", "snapshot", "targets", "role1"]: |
| 81 | + with open(os.path.join(repo_dir, f"{md}.json"), "rb") as f: |
| 82 | + data[md] = f.read() |
| 83 | + |
| 84 | + # root.json not a json file at all |
| 85 | + with self.assertRaises(exceptions.RepositoryError): |
| 86 | + MetadataBundle(b"") |
| 87 | + # root.json is invalid |
| 88 | + root = Metadata.from_bytes(data["root"]) |
| 89 | + root.signed.version += 1 |
| 90 | + with self.assertRaises(exceptions.RepositoryError): |
| 91 | + MetadataBundle(json.dumps(root.to_dict()).encode()) |
| 92 | + |
| 93 | + bundle = MetadataBundle(data["root"]) |
| 94 | + bundle.root_update_finished() |
| 95 | + |
| 96 | + top_level_md = [ |
| 97 | + (data["timestamp"], bundle.update_timestamp), |
| 98 | + (data["snapshot"], bundle.update_snapshot), |
| 99 | + (data["targets"], bundle.update_targets), |
| 100 | + ] |
| 101 | + for metadata, update_func in top_level_md: |
| 102 | + # metadata is not json |
| 103 | + with self.assertRaises(exceptions.RepositoryError): |
| 104 | + update_func(b"") |
| 105 | + # metadata is invalid |
| 106 | + md = Metadata.from_bytes(metadata) |
| 107 | + md.signed.version += 1 |
| 108 | + with self.assertRaises(exceptions.RepositoryError): |
| 109 | + update_func(json.dumps(md.to_dict()).encode()) |
| 110 | + |
| 111 | + # metadata is of wrong type |
| 112 | + with self.assertRaises(exceptions.RepositoryError): |
| 113 | + update_func(data["root"]) |
| 114 | + |
| 115 | + update_func(metadata) |
| 116 | + |
| 117 | + |
| 118 | + # TODO test updating over initial metadata (new keys, newer timestamp, etc) |
| 119 | + # TODO test the actual specification checks |
| 120 | + |
| 121 | + |
| 122 | +if __name__ == '__main__': |
| 123 | + utils.configure_test_logging(sys.argv) |
| 124 | + unittest.main() |
0 commit comments