1
- import copy
2
- import json
3
1
import logging
4
2
import os
5
3
import sys
6
4
import unittest
7
- from typing import Dict , Any
8
5
from datetime import datetime
9
6
10
7
from tuf import exceptions
@@ -128,7 +125,7 @@ def test_update_with_invalid_json(self):
128
125
root = Metadata .from_bytes (self .metadata ["root" ])
129
126
root .signed .version += 1
130
127
with self .assertRaises (exceptions .RepositoryError ):
131
- TrustedMetadataSet (json . dumps ( root .to_dict ()). encode ())
128
+ TrustedMetadataSet (root .to_bytes ())
132
129
133
130
# update_root called with the wrong metadata type
134
131
with self .assertRaises (exceptions .RepositoryError ):
@@ -153,7 +150,7 @@ def test_update_with_invalid_json(self):
153
150
# metadata is invalid
154
151
md .signed .version += 1
155
152
with self .assertRaises (exceptions .RepositoryError ):
156
- update_func (json . dumps ( md .to_dict ()). encode ())
153
+ update_func (md .to_bytes ())
157
154
158
155
# metadata is of wrong type
159
156
with self .assertRaises (exceptions .RepositoryError ):
@@ -164,14 +161,11 @@ def test_update_with_invalid_json(self):
164
161
165
162
def test_update_root_new_root_cannot_be_verified_with_threshold (self ):
166
163
# 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 = []
173
167
with self .assertRaises (exceptions .UnsignedMetadataError ):
174
- self .trusted_set .update_root (modified_threshold_data )
168
+ self .trusted_set .update_root (root . to_bytes () )
175
169
176
170
def test_update_root_new_root_ver_same_as_trusted_root_ver (self ):
177
171
with self .assertRaises (exceptions .ReplayedMetadataError ):
@@ -182,8 +176,7 @@ def test_root_update_finished_expired(self):
182
176
root = Metadata .from_bytes (self .metadata ["root" ])
183
177
root .signed .expires = datetime (1970 , 1 , 1 )
184
178
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 ())
187
180
# call root_update_finished when trusted root has expired
188
181
with self .assertRaises (exceptions .ExpiredMetadataError ):
189
182
tmp_trusted_set .root_update_finished ()
@@ -209,9 +202,8 @@ def test_update_timestamp_expired(self):
209
202
timestamp = Metadata .from_bytes (self .metadata ["timestamp" ])
210
203
timestamp .signed .expires = datetime (1970 , 1 , 1 )
211
204
timestamp .sign (self .keystore ["timestamp" ])
212
- new_timestamp_byte_data = json .dumps (timestamp .to_dict ()).encode ()
213
205
with self .assertRaises (exceptions .ExpiredMetadataError ):
214
- self .trusted_set .update_timestamp (new_timestamp_byte_data )
206
+ self .trusted_set .update_timestamp (timestamp . to_bytes () )
215
207
216
208
217
209
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):
236
228
snapshot .sign (self .keystore ["snapshot" ])
237
229
self .trusted_set .timestamp .signed .meta ["snapshot.json" ].hashes = None
238
230
self .trusted_set .timestamp .signed .meta ["snapshot.json" ].length = None
239
- modified_snapshot_data = json .dumps (snapshot .to_dict ()).encode ()
240
231
with self .assertRaises (exceptions .RepositoryError ):
241
- self .trusted_set .update_snapshot (modified_snapshot_data )
232
+ self .trusted_set .update_snapshot (snapshot . to_bytes () )
242
233
243
234
def test_update_snapshot_after_succesfull_update_new_snapshot_meta_version_different (self ):
244
235
self ._update_all_besides_targets ()
@@ -256,9 +247,8 @@ def test_update_snapshot_after_succesfull_expired_new_snapshot(self):
256
247
snapshot .sign (self .keystore ["snapshot" ])
257
248
self .trusted_set .timestamp .signed .meta ["snapshot.json" ].hashes = None
258
249
self .trusted_set .timestamp .signed .meta ["snapshot.json" ].length = None
259
- modified_snapshot_data = json .dumps (snapshot .to_dict ()).encode ()
260
250
with self .assertRaises (exceptions .ExpiredMetadataError ):
261
- self .trusted_set .update_snapshot (modified_snapshot_data )
251
+ self .trusted_set .update_snapshot (snapshot . to_bytes () )
262
252
263
253
264
254
def test_update_targets_no_meta_in_snapshot (self ):
@@ -290,9 +280,8 @@ def test_update_targets_expired_new_target(self):
290
280
targets = Metadata .from_bytes (self .metadata ["targets" ])
291
281
targets .signed .expires = datetime (1970 , 1 , 1 )
292
282
targets .sign (self .keystore ["targets" ])
293
- modified_targets_data = json .dumps (targets .to_dict ()).encode ()
294
283
with self .assertRaises (exceptions .ExpiredMetadataError ):
295
- self .trusted_set .update_targets (modified_targets_data )
284
+ self .trusted_set .update_targets (targets . to_bytes () )
296
285
297
286
# TODO test updating over initial metadata (new keys, newer timestamp, etc)
298
287
0 commit comments