11import  unittest 
2- import  os 
3- import  sys 
42from  unittest .mock  import  patch , MagicMock 
53
6- sys .path .append (os .path .abspath (os .path .join (os .path .dirname (__file__ ), '..' )))
7- 
4+ from  scripts .boot_image_extractor  import  (
5+  print_banner , exit_with_error , extract_boot_image_for_legacy_device ,
6+  extract_boot_image_for_ab_device , main 
7+ )
88
99class  TestBootImageExtractor (unittest .TestCase ):
10-  """ 
11-  Provides unit tests for the Boot Image Extractor script to ensure its proper functionality 
12-  in various scenarios. Additional test cases can be added to improve test coverage. 
13-  """ 
14-  
15-  def  test_print_banner (self ):
16-  from  scripts .boot_image_extractor  import  print_banner 
17-  with  patch ('builtins.print' ) as  mock_print :
18-  print_banner ("Test Banner" )
19-  mock_print .assert_called_once ()
20-  
10+  """Provides unit tests for the Boot Image Extractor script to ensure its proper functionality 
11+  in various scenarios. Additional test cases can be added to improve test coverage.""" 
12+ 
13+  def  infinite_side_effect (self , * values ):
14+  while  True :
15+  for  value  in  values :
16+  yield  value 
17+ 
18+  @patch ('builtins.print' ) 
19+  def  test_print_banner (self , mock_print ):
20+  print_banner ("Test Banner" )
21+  mock_print .assert_called ()
22+ 
2123 @patch ('builtins.print' ) 
2224 @patch ('subprocess.getoutput' ) 
2325 @patch ('os.geteuid' , MagicMock (return_value = 0 )) 
24-  def  test_extract_boot_image_single_slot (self , mock_getoutput , mock_print ):
26+  def  test_extract_boot_image_for_legacy_device (self , mock_getoutput , mock_print ):
2527 mock_getoutput .return_value  =  '/dev/block/boot' 
2628 with  patch ('subprocess.check_call' , MagicMock ()) as  mock_check_call :
27-  with  patch ('os.path.basename' , MagicMock (return_value = 'test' )):
28-  with  patch ('subprocess.getoutput' , MagicMock (return_value = 'a' )):
29-  from  scripts .boot_image_extractor  import  extract_boot_image_single_slot 
30-  extract_boot_image_single_slot ('boot_path' )
31-  mock_check_call .assert_called_with (['dd' , 'if=boot_path' , 'of=./boot.img' ])
32-  
29+  extract_boot_image_for_legacy_device ('/dev/block/boot' )
30+  mock_check_call .assert_called_with (['dd' , 'if=/dev/block/boot' , 'of=./boot.img' ])
31+ 
3332 @patch ('builtins.print' ) 
3433 @patch ('subprocess.getoutput' ) 
3534 @patch ('os.geteuid' , MagicMock (return_value = 0 )) 
36-  def  test_extract_boot_image_dual_slot (self , mock_getoutput , mock_print ):
37-  mock_getoutput .side_effect  =  ['/dev/block/boot_a' , '/dev/block/boot_b' , 'a' ]
38-  with  patch ('builtins.input' , return_value = 'a' ):
39-  with  patch ('subprocess.check_call' , MagicMock ()) as  mock_check_call :
40-  with  patch ('os.path.basename' , MagicMock (return_value = 'test' )):
41-  with  patch ('subprocess.getoutput' , MagicMock (return_value = 'a' )):
42-  from  scripts .boot_image_extractor  import  extract_boot_image_dual_slot 
43-  extract_boot_image_dual_slot ('boot_a_path' , 'boot_b_path' )
44-  mock_check_call .assert_called_with (['dd' , 'if=boot_a_path' , 'of=./boota.img' ])
45-  
35+  @patch ('builtins.input' , return_value = 'a' ) 
36+  def  test_extract_boot_image_for_ab_device (self , mock_input , mock_getoutput , mock_print ):
37+  mock_getoutput .side_effect  =  ['_a' , 'a' ]
38+  with  patch ('subprocess.check_call' , MagicMock ()) as  mock_check_call :
39+  extract_boot_image_for_ab_device ('/dev/block/boot_a' , '/dev/block/boot_b' )
40+  mock_check_call .assert_called_with (['dd' , 'if=/dev/block/boot_a' , 'of=./boot_a.img' ])
41+ 
42+  @patch ('os.geteuid' , MagicMock (return_value = 0 )) 
43+  @patch ('subprocess.getoutput' , return_value = '' ) 
44+  @patch ('scripts.boot_image_extractor.print_banner' , MagicMock ()) 
45+  @patch ('scripts.boot_image_extractor.exit_with_error' ) 
46+  def  test_main_no_boot_partition_found (self , mock_exit_with_error , mock_getoutput ):
47+  main ()
48+  mock_exit_with_error .assert_called_with ("No boot partition found" , "Unable to locate block device files." )
49+  
50+  @patch ('os.geteuid' , MagicMock (return_value = 0 )) 
51+  @patch ('subprocess.getoutput' ) 
52+  @patch ('builtins.print' ) 
53+  def  test_main_legacy_partition_style (self , mock_print , mock_getoutput ):
54+  mock_getoutput .side_effect  =  ['/dev/block/boot' , '' , '' ]
55+  with  patch ('subprocess.check_call' , MagicMock ()) as  mock_check_call :
56+  main ()
57+  mock_print .assert_any_call ("\n - Legacy(non-A/B) partition style detected!." )
58+  mock_check_call .assert_called_with (['dd' , 'if=/dev/block/boot' , 'of=./boot.img' ])
59+  
60+  @patch ('os.geteuid' , MagicMock (return_value = 0 )) 
61+  @patch ('subprocess.getoutput' ) 
62+  @patch ('builtins.print' ) 
63+  @patch ('builtins.input' , return_value = 'a' ) 
64+  def  test_main_ab_partition_style (self , mock_input , mock_print , mock_getoutput ):
65+  # Set the side_effect to our infinite_side_effect function 
66+  mock_getoutput .side_effect  =  self .infinite_side_effect ('_a' , '/dev/block/boot_a' , '/dev/block/boot_b' )
67+  with  patch ('subprocess.check_call' , MagicMock ()) as  mock_check_call :
68+  main ()
69+  mock_print .assert_any_call ("\n - A/B partition style detected!." )
70+  mock_check_call .assert_called_with (['dd' , 'if=/dev/block/boot_a' , 'of=./boot_a.img' ]) 
4671
4772if  __name__  ==  '__main__' :
4873 unittest .main ()
74+ 
75+  
0 commit comments