1
1
import unittest
2
- import os
3
- import sys
4
2
from unittest .mock import patch , MagicMock
5
3
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
+ )
8
8
9
9
class 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
+
21
23
@patch ('builtins.print' )
22
24
@patch ('subprocess.getoutput' )
23
25
@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 ):
25
27
mock_getoutput .return_value = '/dev/block/boot'
26
28
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
+
33
32
@patch ('builtins.print' )
34
33
@patch ('subprocess.getoutput' )
35
34
@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' ])
46
71
47
72
if __name__ == '__main__' :
48
73
unittest .main ()
74
+
75
+
0 commit comments