@@ -765,6 +765,198 @@ def test_check_fit_image_signature_error(self, mock_run):
765
765
self .assertFalse (is_signed )
766
766
self .assertIn ("dumpimage failed" , error_msg )
767
767
768
+ @patch (
769
+ "check_secure_boot_state.FITImageSecureBootChecker"
770
+ "._get_boot_kernel_path"
771
+ )
772
+ def test_find_fit_images_with_boot_kernel (self , mock_get_boot_kernel ):
773
+ """Test _find_fit_images when boot kernel is found."""
774
+ mock_get_boot_kernel .return_value = "/boot/vmlinuz-5.4.0"
775
+ images = self .checker ._find_fit_images ()
776
+ self .assertEqual (images , ["/boot/vmlinuz-5.4.0" ])
777
+
778
+ @patch (
779
+ "check_secure_boot_state.FITImageSecureBootChecker"
780
+ "._get_boot_kernel_path"
781
+ )
782
+ @patch (
783
+ "check_secure_boot_state.FITImageSecureBootChecker"
784
+ "._get_snap_kernel_patterns"
785
+ )
786
+ @patch (
787
+ "check_secure_boot_state.FITImageSecureBootChecker"
788
+ "._build_search_patterns"
789
+ )
790
+ @patch (
791
+ "check_secure_boot_state.FITImageSecureBootChecker"
792
+ "._find_files_by_patterns"
793
+ )
794
+ def test_find_fit_images_core_variant (
795
+ self ,
796
+ mock_find_files ,
797
+ mock_build_patterns ,
798
+ mock_get_patterns ,
799
+ mock_get_boot_kernel ,
800
+ ):
801
+ """Test _find_fit_images for Ubuntu Core variant."""
802
+ # Setup mocks
803
+ mock_get_boot_kernel .return_value = None
804
+ self .checker .ubuntu_variant = UbuntuVariant .CORE
805
+ mock_get_patterns .return_value = ["/snap/*/current/kernel.img" ]
806
+ mock_build_patterns .return_value = ["/snap/*/current/kernel.img" ]
807
+ mock_find_files .return_value = ["/snap/test/current/kernel.img" ]
808
+
809
+ images = self .checker ._find_fit_images ()
810
+
811
+ self .assertEqual (images , ["/snap/test/current/kernel.img" ])
812
+ mock_get_patterns .assert_called_once ()
813
+ mock_build_patterns .assert_called_once_with (
814
+ ["/snap/*/current/kernel.img" ]
815
+ )
816
+
817
+ @patch (
818
+ "check_secure_boot_state.FITImageSecureBootChecker"
819
+ "._get_boot_kernel_path"
820
+ )
821
+ @patch (
822
+ "check_secure_boot_state.FITImageSecureBootChecker"
823
+ "._get_classic_fit_patterns"
824
+ )
825
+ @patch (
826
+ "check_secure_boot_state.FITImageSecureBootChecker"
827
+ "._build_search_patterns"
828
+ )
829
+ @patch (
830
+ "check_secure_boot_state.FITImageSecureBootChecker"
831
+ "._find_files_by_patterns"
832
+ )
833
+ def test_find_fit_images_classic_variant (
834
+ self ,
835
+ mock_find_files ,
836
+ mock_build_patterns ,
837
+ mock_get_patterns ,
838
+ mock_get_boot_kernel ,
839
+ ):
840
+ """Test _find_fit_images for Ubuntu Classic variant."""
841
+ # Setup mocks
842
+ mock_get_boot_kernel .return_value = None
843
+ self .checker .ubuntu_variant = UbuntuVariant .CLASSIC
844
+ mock_get_patterns .return_value = ["/boot/*.img" ]
845
+ mock_build_patterns .return_value = ["/boot/*.img" ]
846
+ mock_find_files .return_value = ["/boot/vmlinuz-5.4.0" ]
847
+
848
+ images = self .checker ._find_fit_images ()
849
+
850
+ self .assertEqual (images , ["/boot/vmlinuz-5.4.0" ])
851
+ mock_get_patterns .assert_called_once ()
852
+ mock_build_patterns .assert_called_once_with (["/boot/*.img" ])
853
+
854
+ @patch (
855
+ "check_secure_boot_state.FITImageSecureBootChecker._get_boot_kernel_path"
856
+ )
857
+ @patch (
858
+ "check_secure_boot_state.FITImageSecureBootChecker._get_snap_kernel_patterns"
859
+ )
860
+ @patch (
861
+ "check_secure_boot_state.FITImageSecureBootChecker._build_search_patterns"
862
+ )
863
+ @patch (
864
+ "check_secure_boot_state.FITImageSecureBootChecker._find_files_by_patterns"
865
+ )
866
+ def test_find_fit_images_no_images_found (
867
+ self ,
868
+ mock_find_files ,
869
+ mock_build_patterns ,
870
+ mock_get_patterns ,
871
+ mock_get_boot_kernel ,
872
+ ):
873
+ """Test _find_fit_images when no images are found."""
874
+ # Setup mocks
875
+ mock_get_boot_kernel .return_value = None
876
+ self .checker .ubuntu_variant = UbuntuVariant .CORE
877
+ mock_get_patterns .return_value = ["/snap/*/current/kernel.img" ]
878
+ mock_build_patterns .return_value = ["/snap/*/current/kernel.img" ]
879
+ mock_find_files .return_value = []
880
+
881
+ images = self .checker ._find_fit_images ()
882
+
883
+ self .assertEqual (images , [])
884
+
885
+ @patch (
886
+ "check_secure_boot_state.FITImageSecureBootChecker._get_boot_kernel_path"
887
+ )
888
+ @patch (
889
+ "check_secure_boot_state.FITImageSecureBootChecker._get_snap_kernel_patterns"
890
+ )
891
+ @patch (
892
+ "check_secure_boot_state.FITImageSecureBootChecker._build_search_patterns"
893
+ )
894
+ @patch (
895
+ "check_secure_boot_state.FITImageSecureBootChecker._find_files_by_patterns"
896
+ )
897
+ def test_find_fit_images_with_hostfs_prefix (
898
+ self ,
899
+ mock_find_files ,
900
+ mock_build_patterns ,
901
+ mock_get_patterns ,
902
+ mock_get_boot_kernel ,
903
+ ):
904
+ """Test _find_fit_images with hostfs prefix."""
905
+ # Setup mocks
906
+ mock_get_boot_kernel .return_value = None
907
+ self .checker .ubuntu_variant = UbuntuVariant .CORE
908
+ self .checker .hostfs_prefix = "/var/lib/snapd/hostfs"
909
+ mock_get_patterns .return_value = ["/snap/*/current/kernel.img" ]
910
+ mock_build_patterns .return_value = [
911
+ "/var/lib/snapd/hostfs/snap/*/current/kernel.img" ,
912
+ "/snap/*/current/kernel.img" ,
913
+ ]
914
+ mock_find_files .return_value = [
915
+ "/var/lib/snapd/hostfs/snap/test/current/kernel.img"
916
+ ]
917
+
918
+ images = self .checker ._find_fit_images ()
919
+
920
+ self .assertEqual (
921
+ images , ["/var/lib/snapd/hostfs/snap/test/current/kernel.img" ]
922
+ )
923
+ mock_build_patterns .assert_called_once_with (
924
+ ["/snap/*/current/kernel.img" ]
925
+ )
926
+
927
+ @patch (
928
+ "check_secure_boot_state.FITImageSecureBootChecker._get_boot_kernel_path"
929
+ )
930
+ @patch (
931
+ "check_secure_boot_state.FITImageSecureBootChecker._get_classic_fit_patterns"
932
+ )
933
+ @patch (
934
+ "check_secure_boot_state.FITImageSecureBootChecker._build_search_patterns"
935
+ )
936
+ @patch (
937
+ "check_secure_boot_state.FITImageSecureBootChecker._find_files_by_patterns"
938
+ )
939
+ def test_find_fit_images_unknown_variant (
940
+ self ,
941
+ mock_find_files ,
942
+ mock_build_patterns ,
943
+ mock_get_patterns ,
944
+ mock_get_boot_kernel ,
945
+ ):
946
+ """Test _find_fit_images for unknown Ubuntu variant."""
947
+ # Setup mocks
948
+ mock_get_boot_kernel .return_value = None
949
+ self .checker .ubuntu_variant = UbuntuVariant .UNKNOWN
950
+ mock_get_patterns .return_value = ["/boot/*.img" ]
951
+ mock_build_patterns .return_value = ["/boot/*.img" ]
952
+ mock_find_files .return_value = ["/boot/vmlinuz-5.4.0" ]
953
+
954
+ images = self .checker ._find_fit_images ()
955
+
956
+ self .assertEqual (images , ["/boot/vmlinuz-5.4.0" ])
957
+ # Should default to classic patterns for unknown variant
958
+ mock_get_patterns .assert_called_once ()
959
+
768
960
769
961
class TestUtilityFunctions (unittest .TestCase ):
770
962
"""Test cases for utility functions."""
@@ -970,6 +1162,45 @@ def test_main_success(
970
1162
971
1163
mock_exit .assert_called_with (0 )
972
1164
1165
+ @patch ("sys.exit" )
1166
+ @patch ("check_secure_boot_state.create_checker" )
1167
+ @patch ("check_secure_boot_state.create_parser" )
1168
+ @patch ("check_secure_boot_state.SecureBootLogger" )
1169
+ def test_main_success_disabled (
1170
+ self ,
1171
+ mock_logger_class ,
1172
+ mock_create_parser ,
1173
+ mock_create_checker ,
1174
+ mock_exit ,
1175
+ ):
1176
+ """Test main function - success case with disabled check."""
1177
+ # Mock parser
1178
+ mock_parser = Mock ()
1179
+ mock_args = Mock ()
1180
+ mock_args .enable = False
1181
+ mock_args .disabled = True
1182
+ mock_args .verbose = False
1183
+ mock_args .method = "auto"
1184
+ mock_parser .parse_args .return_value = mock_args
1185
+ mock_create_parser .return_value = mock_parser
1186
+
1187
+ # Mock checker
1188
+ mock_checker = Mock ()
1189
+ mock_checker .get_secure_boot_state .return_value = (
1190
+ SecureBootState .DISABLED ,
1191
+ None ,
1192
+ )
1193
+ mock_create_checker .return_value = (mock_checker , "Test Checker" )
1194
+
1195
+ # Mock logger
1196
+ mock_logger = Mock ()
1197
+ mock_logger_class .return_value = mock_logger
1198
+
1199
+ with patch ("check_secure_boot_state.logger" , mock_logger ):
1200
+ main ()
1201
+
1202
+ mock_exit .assert_called_with (0 )
1203
+
973
1204
@patch ("sys.exit" )
974
1205
@patch ("check_secure_boot_state.create_checker" )
975
1206
@patch ("check_secure_boot_state.create_parser" )
0 commit comments