55
66import builtins
77import datetime
8- import logging
98import os
109import sys
1110import tempfile
@@ -68,13 +67,13 @@ def setUp(self) -> None:
6867 def tearDown (self ) -> None :
6968 self .temp_dir .cleanup ()
7069
71- def _run_refresh (self , skip_bootstrap :bool = False ) -> Updater :
70+ def _run_refresh (self , skip_bootstrap : bool = False ) -> Updater :
7271 """Create a new Updater instance and refresh"""
7372 updater = self ._init_updater (skip_bootstrap )
7473 updater .refresh ()
7574 return updater
7675
77- def _init_updater (self , skip_bootstrap :bool = False ) -> Updater :
76+ def _init_updater (self , skip_bootstrap : bool = False ) -> Updater :
7877 """Create a new Updater instance"""
7978 if self .dump_dir is not None :
8079 self .sim .write ()
@@ -85,13 +84,15 @@ def _init_updater(self, skip_bootstrap:bool=False) -> Updater:
8584 self .targets_dir ,
8685 "https://example.com/targets/" ,
8786 self .sim ,
88- bootstrap = None if skip_bootstrap else self .sim .signed_roots [0 ]
87+ bootstrap = None if skip_bootstrap else self .sim .signed_roots [0 ],
8988 )
9089
9190 def _assert_files_exist (self , roles : Iterable [str ]) -> None :
9291 """Assert that local metadata files match 'roles'"""
9392 expected_files = [f"{ role } .json" for role in roles ]
94- found_files = [e .name for e in os .scandir (self .metadata_dir ) if e .is_file ()]
93+ found_files = [
94+ e .name for e in os .scandir (self .metadata_dir ) if e .is_file ()
95+ ]
9596
9697 self .assertListEqual (sorted (found_files ), sorted (expected_files ))
9798
@@ -700,9 +701,10 @@ def test_load_metadata_from_cache(self, wrapped_open: MagicMock) -> None:
700701 updater .get_targetinfo ("non_existent_target" )
701702
702703 # Test that metadata is loaded from cache and not downloaded
704+ root_dir = os .path .join (self .metadata_dir , "root_history" )
703705 wrapped_open .assert_has_calls (
704706 [
705- call (os .path .join (self . metadata_dir , "root_history/ 2.root.json" ), "rb" ),
707+ call (os .path .join (root_dir , "2.root.json" ), "rb" ),
706708 call (os .path .join (self .metadata_dir , "timestamp.json" ), "rb" ),
707709 call (os .path .join (self .metadata_dir , "snapshot.json" ), "rb" ),
708710 call (os .path .join (self .metadata_dir , "targets.json" ), "rb" ),
@@ -713,7 +715,6 @@ def test_load_metadata_from_cache(self, wrapped_open: MagicMock) -> None:
713715 expected_calls = [("root" , 2 ), ("timestamp" , None )]
714716 self .assertListEqual (self .sim .fetch_tracker .metadata , expected_calls )
715717
716-
717718 @patch .object (builtins , "open" , wraps = builtins .open )
718719 def test_intermediate_root_cache (self , wrapped_open : MagicMock ) -> None :
719720 """Test that refresh uses the intermediate roots from cache"""
@@ -727,17 +728,25 @@ def test_intermediate_root_cache(self, wrapped_open: MagicMock) -> None:
727728 self ._run_refresh ()
728729
729730 # assert that cache lookups happened but data was downloaded from remote
731+ root_dir = os .path .join (self .metadata_dir , "root_history" )
730732 wrapped_open .assert_has_calls (
731733 [
732- call (os .path .join (self . metadata_dir , "root_history/ 2.root.json" ), "rb" ),
733- call (os .path .join (self . metadata_dir , "root_history/ 3.root.json" ), "rb" ),
734- call (os .path .join (self . metadata_dir , "root_history/ 4.root.json" ), "rb" ),
734+ call (os .path .join (root_dir , "2.root.json" ), "rb" ),
735+ call (os .path .join (root_dir , "3.root.json" ), "rb" ),
736+ call (os .path .join (root_dir , "4.root.json" ), "rb" ),
735737 call (os .path .join (self .metadata_dir , "timestamp.json" ), "rb" ),
736738 call (os .path .join (self .metadata_dir , "snapshot.json" ), "rb" ),
737739 call (os .path .join (self .metadata_dir , "targets.json" ), "rb" ),
738740 ]
739741 )
740- expected_calls = [("root" , 2 ), ("root" , 3 ), ("root" , 4 ), ("timestamp" , None ), ("snapshot" , 1 ), ("targets" , 1 )]
742+ expected_calls = [
743+ ("root" , 2 ),
744+ ("root" , 3 ),
745+ ("root" , 4 ),
746+ ("timestamp" , None ),
747+ ("snapshot" , 1 ),
748+ ("targets" , 1 ),
749+ ]
741750 self .assertListEqual (self .sim .fetch_tracker .metadata , expected_calls )
742751
743752 # Clear statistics for open() calls and metadata requests
@@ -748,9 +757,9 @@ def test_intermediate_root_cache(self, wrapped_open: MagicMock) -> None:
748757 self ._run_refresh ()
749758 wrapped_open .assert_has_calls (
750759 [
751- call (os .path .join (self . metadata_dir , "root_history/ 2.root.json" ), "rb" ),
752- call (os .path .join (self . metadata_dir , "root_history/ 3.root.json" ), "rb" ),
753- call (os .path .join (self . metadata_dir , "root_history/ 4.root.json" ), "rb" ),
760+ call (os .path .join (root_dir , "2.root.json" ), "rb" ),
761+ call (os .path .join (root_dir , "3.root.json" ), "rb" ),
762+ call (os .path .join (root_dir , "4.root.json" ), "rb" ),
754763 call (os .path .join (self .metadata_dir , "timestamp.json" ), "rb" ),
755764 call (os .path .join (self .metadata_dir , "snapshot.json" ), "rb" ),
756765 call (os .path .join (self .metadata_dir , "targets.json" ), "rb" ),
@@ -771,7 +780,9 @@ def test_intermediate_root_cache_poisoning(self) -> None:
771780 self ._run_refresh ()
772781
773782 # Modify cached intermediate root v2 so that it's no longer signed correctly
774- root_path = os .path .join (self .metadata_dir , "root_history" , "2.root.json" )
783+ root_path = os .path .join (
784+ self .metadata_dir , "root_history" , "2.root.json"
785+ )
775786 md = Metadata .from_file (root_path )
776787 md .signatures .clear ()
777788 md .to_file (root_path )
0 commit comments