1- import copy
2- import json
31import logging
42import os
53import sys
64import unittest
7- from typing import Dict , Any
85from datetime import datetime
96
107from tuf import exceptions
@@ -128,7 +125,7 @@ def test_update_with_invalid_json(self):
128125 root = Metadata .from_bytes (self .metadata ["root" ])
129126 root .signed .version += 1
130127 with self .assertRaises (exceptions .RepositoryError ):
131- TrustedMetadataSet (json . dumps ( root .to_dict ()). encode ())
128+ TrustedMetadataSet (root .to_bytes ())
132129
133130 # update_root called with the wrong metadata type
134131 with self .assertRaises (exceptions .RepositoryError ):
@@ -153,7 +150,7 @@ def test_update_with_invalid_json(self):
153150 # metadata is invalid
154151 md .signed .version += 1
155152 with self .assertRaises (exceptions .RepositoryError ):
156- update_func (json . dumps ( md .to_dict ()). encode ())
153+ update_func (md .to_bytes ())
157154
158155 # metadata is of wrong type
159156 with self .assertRaises (exceptions .RepositoryError ):
@@ -164,14 +161,11 @@ def test_update_with_invalid_json(self):
164161
165162 def test_update_root_new_root_cannot_be_verified_with_threshold (self ):
166163 # new_root data with threshold which cannot be verified.
167- modified_threshold_data = copy .deepcopy (
168- json .loads (self .metadata ["root" ])
169- )
170- # change something in root so signature doesn't match the content.
171- modified_threshold_data ["signed" ]["roles" ]["root" ]["version" ] = 2
172- modified_threshold_data = json .dumps (modified_threshold_data ).encode ()
164+ root = Metadata .from_bytes (self .metadata ["root" ])
165+ # remove root role keyids representing root signatures
166+ root .signed .roles ["root" ].keyids = []
173167 with self .assertRaises (exceptions .UnsignedMetadataError ):
174- self .trusted_set .update_root (modified_threshold_data )
168+ self .trusted_set .update_root (root . to_bytes () )
175169
176170 def test_update_root_new_root_ver_same_as_trusted_root_ver (self ):
177171 with self .assertRaises (exceptions .ReplayedMetadataError ):
@@ -182,8 +176,7 @@ def test_root_update_finished_expired(self):
182176 root = Metadata .from_bytes (self .metadata ["root" ])
183177 root .signed .expires = datetime (1970 , 1 , 1 )
184178 root .sign (self .keystore ["root" ])
185- modified_root_data = json .dumps (root .to_dict ()).encode ()
186- tmp_trusted_set = TrustedMetadataSet (modified_root_data )
179+ tmp_trusted_set = TrustedMetadataSet (root .to_bytes ())
187180 # call root_update_finished when trusted root has expired
188181 with self .assertRaises (exceptions .ExpiredMetadataError ):
189182 tmp_trusted_set .root_update_finished ()
@@ -209,9 +202,8 @@ def test_update_timestamp_expired(self):
209202 timestamp = Metadata .from_bytes (self .metadata ["timestamp" ])
210203 timestamp .signed .expires = datetime (1970 , 1 , 1 )
211204 timestamp .sign (self .keystore ["timestamp" ])
212- new_timestamp_byte_data = json .dumps (timestamp .to_dict ()).encode ()
213205 with self .assertRaises (exceptions .ExpiredMetadataError ):
214- self .trusted_set .update_timestamp (new_timestamp_byte_data )
206+ self .trusted_set .update_timestamp (timestamp . to_bytes () )
215207
216208
217209 def test_update_snapshot_cannot_verify_snapshot_with_threshold (self ):
@@ -236,9 +228,8 @@ def test_update_snapshot_after_successful_update_new_snapshot_no_meta(self):
236228 snapshot .sign (self .keystore ["snapshot" ])
237229 self .trusted_set .timestamp .signed .meta ["snapshot.json" ].hashes = None
238230 self .trusted_set .timestamp .signed .meta ["snapshot.json" ].length = None
239- modified_snapshot_data = json .dumps (snapshot .to_dict ()).encode ()
240231 with self .assertRaises (exceptions .RepositoryError ):
241- self .trusted_set .update_snapshot (modified_snapshot_data )
232+ self .trusted_set .update_snapshot (snapshot . to_bytes () )
242233
243234 def test_update_snapshot_after_succesfull_update_new_snapshot_meta_version_different (self ):
244235 self ._update_all_besides_targets ()
@@ -256,9 +247,8 @@ def test_update_snapshot_after_succesfull_expired_new_snapshot(self):
256247 snapshot .sign (self .keystore ["snapshot" ])
257248 self .trusted_set .timestamp .signed .meta ["snapshot.json" ].hashes = None
258249 self .trusted_set .timestamp .signed .meta ["snapshot.json" ].length = None
259- modified_snapshot_data = json .dumps (snapshot .to_dict ()).encode ()
260250 with self .assertRaises (exceptions .ExpiredMetadataError ):
261- self .trusted_set .update_snapshot (modified_snapshot_data )
251+ self .trusted_set .update_snapshot (snapshot . to_bytes () )
262252
263253
264254 def test_update_targets_no_meta_in_snapshot (self ):
@@ -290,9 +280,8 @@ def test_update_targets_expired_new_target(self):
290280 targets = Metadata .from_bytes (self .metadata ["targets" ])
291281 targets .signed .expires = datetime (1970 , 1 , 1 )
292282 targets .sign (self .keystore ["targets" ])
293- modified_targets_data = json .dumps (targets .to_dict ()).encode ()
294283 with self .assertRaises (exceptions .ExpiredMetadataError ):
295- self .trusted_set .update_targets (modified_targets_data )
284+ self .trusted_set .update_targets (targets . to_bytes () )
296285
297286 # TODO test updating over initial metadata (new keys, newer timestamp, etc)
298287
0 commit comments