# SPDX-License-Identifier: MPL-2.0 import importlib.util from pathlib import Path import struct import sys import tempfile import unittest from unittest.mock import patch spec=importlib.util.spec_from_file_location('flash',Path(__file__).resolve().parents[1]/'tools/flash-standalone-sargo.py') flash=importlib.util.module_from_spec(spec);spec.loader.exec_module(flash) class FlashTests(unittest.TestCase): def plan(self): return {'userdataBytes':8*1024**3,'partitions':{name:{'bytes':4096} for name in flash.PARTITIONS}} def values(self,**changes): values={'product':'sargo','unlocked':'yes','current-slot':'b', **{'partition-size:'+name:hex(64*1024**3 if name=='userdata' else 64*1024**2) for name in flash.PARTITIONS}} values.update(changes);return values def test_both_slots_accepted_for_full_install(self): for slot in ('a','b'): values=self.values(**{'current-slot':slot}) with patch.object(flash,'getvar',side_effect=lambda _,__,name:values[name]): self.assertEqual(flash.preflight('fastboot','serial',self.plan())['current-slot'],slot) def test_wrong_model_locked_or_small_partition_rejected(self): for change in ({'product':'bonito'},{'unlocked':'no'},{'partition-size:userdata':'0x1000'}): values=self.values(**change) with patch.object(flash,'getvar',side_effect=lambda _,__,name:values[name]): with self.assertRaises(ValueError):flash.preflight('fastboot','serial',self.plan()) def test_review_never_contacts_device(self): with patch.object(sys,'argv',['flash']),patch.object(flash,'package',return_value=self.plan()),patch.object(flash.subprocess,'run') as run,patch.object(flash.subprocess,'check_output') as read: flash.main();run.assert_not_called();read.assert_not_called() def test_install_requires_explicit_userdata_replacement(self): with patch.object(sys,'argv',['flash','--install']),patch.object(flash,'package',return_value=self.plan()),patch.object(flash.subprocess,'run') as run: with self.assertRaises(SystemExit):flash.main() run.assert_not_called() def test_unqualified_package_requires_experimental_flag(self): with patch.object(sys,'argv',['flash','--install','--erase-userdata']),patch.object(flash,'package',return_value=self.plan()),patch.object(flash.subprocess,'run') as run: with self.assertRaises(SystemExit):flash.main() run.assert_not_called() def test_sparse_geometry(self): with tempfile.TemporaryDirectory() as directory: image=Path(directory)/'userdata.img' image.write_bytes(struct.pack('