44import sys
55import ext4
66
7+ from io import BufferedReader
78from typing import cast
89from typing import Callable
910from typing import Any
@@ -41,6 +42,38 @@ def _assert(source: str, debug: Callable[[], Any] | None = None): # pyright: ig
4142 print (f" { debug ()} " )
4243
4344
45+ def test_magic_error (f : BufferedReader ):
46+ global FAILED
47+ try :
48+ print ("check MagicError: " , end = "" )
49+ _ = ext4 .Volume (f , offset = 0 )
50+ FAILED = True # pyright: ignore[reportConstantRedefinition]
51+ print ("fail" )
52+ print (" MagicError not raised" )
53+ except ext4 .struct .MagicError :
54+ print ("pass" )
55+
56+ except Exception as e :
57+ FAILED = True # pyright: ignore[reportConstantRedefinition]
58+ print ("fail" )
59+ print (" " , end = "" )
60+ print (e )
61+
62+
63+ def test_root_inode (volume : ext4 .Volume ):
64+ global FAILED
65+ try :
66+ print ("Validate root inode: " , end = "" )
67+ volume .root .validate ()
68+ print ("pass" )
69+
70+ except ext4 .struct .ChecksumError as e :
71+ FAILED = True # pyright: ignore[reportConstantRedefinition]
72+ print ("fail" )
73+ print (" " , end = "" )
74+ print (e )
75+
76+
4477print ("check ext4.Volume stream validation: " , end = "" )
4578try :
4679 _ = ext4 .Volume (1 ) # pyright: ignore[reportArgumentType]
@@ -64,37 +97,17 @@ def _assert(source: str, debug: Callable[[], Any] | None = None): # pyright: ig
6497test_path_tuple (b"/test/test" , (b"test" , b"test" ))
6598
6699for img_file in ("test32.ext4" , "test64.ext4" ):
100+ print (f"Testing image: { img_file } " )
67101 offset = os .path .getsize (img_file ) - os .path .getsize (f"{ img_file } .tmp" )
68- _assert ("offset > 0" )
69- with open (img_file , "rb" ) as f :
70- try :
71- print ("check MagicError: " , end = "" )
72- _ = ext4 .Volume (f , offset = 0 )
73- FAILED = True # pyright: ignore[reportConstantRedefinition]
74- print ("fail" )
75- print (" MagicError not raised" )
76- except ext4 .struct .MagicError :
77- print ("pass" )
78-
79- except Exception as e :
80- FAILED = True # pyright: ignore[reportConstantRedefinition]
81- print ("fail" )
82- print (" " , end = "" )
83- print (e )
84-
85- # Extract specific file
86- volume = ext4 .Volume (f , offset = offset )
102+ _assert ("offset > 0" , lambda : offset )
103+ if offset < 0 :
104+ continue
87105
88- try :
89- print ("Validate root inode: " , end = "" )
90- volume .root .validate ()
91- print ("pass" )
106+ with open (img_file , "rb" ) as f :
107+ test_magic_error (f )
92108
93- except ext4 .struct .ChecksumError as e :
94- FAILED = True # pyright: ignore[reportConstantRedefinition]
95- print ("fail" )
96- print (" " , end = "" )
97- print (e )
109+ volume = ext4 .Volume (f , offset = offset )
110+ test_root_inode (volume )
98111
99112 inode = cast (ext4 .File , volume .inode_at ("/test.txt" ))
100113 _assert ("isinstance(inode, ext4.File)" )
@@ -124,5 +137,12 @@ def _assert(source: str, debug: Callable[[], Any] | None = None): # pyright: ig
124137 _ = b .seek (0 )
125138 _assert (f"b.read({ x } ) == { data [:x ]} " , lambda : b .seek (0 ) == 0 and b .read (x ))
126139
140+ img_file = "test_htree.ext4"
141+ print (f"Testing image: { img_file } " )
142+ with open (img_file , "rb" ) as f :
143+ volume = ext4 .Volume (f )
144+ _assert ("volume.root.is_htree" )
145+ test_root_inode (volume )
146+
127147if FAILED :
128148 sys .exit (1 )
0 commit comments