blob: 7674d5ce5b5e57a0ebb4993d19fc729bd83fd42f [file] [log] [blame]
Mike Frysingere58c0e22017-10-04 15:43:30 -04001# -*- coding: utf-8 -*-
David Pursellf1d16a62015-03-25 13:31:04 -07002# Copyright 2015 The Chromium OS Authors. All rights reserved.
3# Use of this source code is governed by a BSD-style license that can be
4# found in the LICENSE file.
5
6"""Unit tests for the flash module."""
7
8from __future__ import print_function
9
David Pursellf1d16a62015-03-25 13:31:04 -070010import os
Mike Frysinger3f087aa2020-03-20 06:03:16 -040011import sys
David Pursellf1d16a62015-03-25 13:31:04 -070012
Mike Frysinger6db648e2018-07-24 19:57:58 -040013import mock
14
David Pursellf1d16a62015-03-25 13:31:04 -070015from chromite.cli import flash
Sanika Kulkarnia8c4e3a2019-09-20 16:47:25 -070016from chromite.lib import auto_updater_transfer
David Pursellf1d16a62015-03-25 13:31:04 -070017from chromite.lib import commandline
18from chromite.lib import cros_build_lib
Ralph Nathan9b997232015-05-15 13:13:12 -070019from chromite.lib import cros_logging as logging
David Pursellf1d16a62015-03-25 13:31:04 -070020from chromite.lib import cros_test_lib
21from chromite.lib import dev_server_wrapper
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -070022from chromite.lib import osutils
David Pursellf1d16a62015-03-25 13:31:04 -070023from chromite.lib import partial_mock
24from chromite.lib import remote_access
Mike Frysingerdda695b2019-11-23 20:58:59 -050025from chromite.lib import remote_access_unittest
David Pursellf1d16a62015-03-25 13:31:04 -070026
Amin Hassanic0f06fa2019-01-28 15:24:47 -080027from chromite.lib.paygen import paygen_payload_lib
28from chromite.lib.paygen import paygen_stateful_payload_lib
29
David Pursellf1d16a62015-03-25 13:31:04 -070030
Mike Frysinger3f087aa2020-03-20 06:03:16 -040031assert sys.version_info >= (3, 6), 'This module requires Python 3.6+'
32
33
David Pursellf1d16a62015-03-25 13:31:04 -070034class RemoteDeviceUpdaterMock(partial_mock.PartialCmdMock):
35 """Mock out RemoteDeviceUpdater."""
Amin Hassani9800d432019-07-24 14:23:39 -070036 TARGET = 'chromite.lib.auto_updater.ChromiumOSUpdater'
xixuane851dfb2016-05-02 18:02:37 -070037 ATTRS = ('UpdateStateful', 'UpdateRootfs', 'SetupRootfsUpdate',
Sanika Kulkarnie3b177b2019-11-26 14:42:48 -080038 'RebootAndVerify', 'PreparePayloadPropsFile',
39 '_FixPayloadPropertiesFile')
David Pursellf1d16a62015-03-25 13:31:04 -070040
41 def __init__(self):
42 partial_mock.PartialCmdMock.__init__(self)
43
44 def UpdateStateful(self, _inst, *_args, **_kwargs):
45 """Mock out UpdateStateful."""
46
47 def UpdateRootfs(self, _inst, *_args, **_kwargs):
48 """Mock out UpdateRootfs."""
49
50 def SetupRootfsUpdate(self, _inst, *_args, **_kwargs):
51 """Mock out SetupRootfsUpdate."""
52
xixuane851dfb2016-05-02 18:02:37 -070053 def RebootAndVerify(self, _inst, *_args, **_kwargs):
54 """Mock out RebootAndVerify."""
David Pursellf1d16a62015-03-25 13:31:04 -070055
Sanika Kulkarni00b9d682019-11-26 09:43:20 -080056 def PreparePayloadPropsFile(self, _inst, *_args, **_kwargs):
57 """Mock out PreparePayloadPropsFile."""
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +020058
Sanika Kulkarnie3b177b2019-11-26 14:42:48 -080059 def _FixPayloadPropertiesFile(self, _inst, *_args, **_kwargs):
60 """Mock out _FixPayloadPropertiesFile."""
61
62
Mike Frysingerdda695b2019-11-23 20:58:59 -050063class RemoteAccessMock(remote_access_unittest.RemoteShMock):
64 """Mock out RemoteAccess."""
65
66 ATTRS = ('RemoteSh', 'Rsync', 'Scp')
67
68 def Rsync(self, *_args, **_kwargs):
69 return cros_build_lib.CommandResult(returncode=0)
70
71 def Scp(self, *_args, **_kwargs):
72 return cros_build_lib.CommandResult(returncode=0)
73
74
David Pursellf1d16a62015-03-25 13:31:04 -070075class RemoteDeviceUpdaterTest(cros_test_lib.MockTempDirTestCase):
76 """Test the flow of flash.Flash() with RemoteDeviceUpdater."""
77
78 IMAGE = '/path/to/image'
79 DEVICE = commandline.Device(scheme=commandline.DEVICE_SCHEME_SSH,
Mike Frysingerb5a297f2019-11-23 21:17:41 -050080 hostname=remote_access.TEST_IP)
David Pursellf1d16a62015-03-25 13:31:04 -070081
82 def setUp(self):
83 """Patches objects."""
84 self.updater_mock = self.StartPatcher(RemoteDeviceUpdaterMock())
85 self.PatchObject(dev_server_wrapper, 'GenerateXbuddyRequest',
86 return_value='xbuddy/local/latest')
David Pursellf1d16a62015-03-25 13:31:04 -070087 self.PatchObject(dev_server_wrapper, 'GetImagePathWithXbuddy',
Gilad Arnolde62ec902015-04-24 14:41:02 -070088 return_value=('taco-paladin/R36/chromiumos_test_image.bin',
89 'remote/taco-paladin/R36/test'))
Amin Hassanic0f06fa2019-01-28 15:24:47 -080090 self.PatchObject(paygen_payload_lib, 'GenerateUpdatePayload')
91 self.PatchObject(paygen_stateful_payload_lib, 'GenerateStatefulPayload')
David Pursellf1d16a62015-03-25 13:31:04 -070092 self.PatchObject(remote_access, 'CHECK_INTERVAL', new=0)
Mike Frysingerdda695b2019-11-23 20:58:59 -050093 self.PatchObject(remote_access.ChromiumOSDevice, 'Pingable',
94 return_value=True)
95 m = self.StartPatcher(RemoteAccessMock())
96 m.AddCmdResult(['cat', '/etc/lsb-release'],
97 stdout='CHROMEOS_RELEASE_BOARD=board')
98 m.SetDefaultCmdResult()
David Pursellf1d16a62015-03-25 13:31:04 -070099
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800100 def _ExistsMock(self, path, ret=True):
101 """Mock function for os.path.exists.
102
103 os.path.exists is used a lot; we only want to mock it for devserver/static,
104 and actually check if the file exists in all other cases (using os.access).
105
106 Args:
107 path: path to check.
108 ret: return value of mock.
109
110 Returns:
111 ret for paths under devserver/static, and the expected value of
112 os.path.exists otherwise.
113 """
114 if path.startswith(flash.DEVSERVER_STATIC_DIR):
115 return ret
116 return os.access(path, os.F_OK)
117
David Pursellf1d16a62015-03-25 13:31:04 -0700118 def testUpdateAll(self):
119 """Tests that update methods are called correctly."""
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800120 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Bertrand SIMONNETb34a98b2015-04-22 14:30:04 -0700121 flash.Flash(self.DEVICE, self.IMAGE)
122 self.assertTrue(self.updater_mock.patched['UpdateStateful'].called)
123 self.assertTrue(self.updater_mock.patched['UpdateRootfs'].called)
David Pursellf1d16a62015-03-25 13:31:04 -0700124
125 def testUpdateStateful(self):
126 """Tests that update methods are called correctly."""
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800127 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Bertrand SIMONNETb34a98b2015-04-22 14:30:04 -0700128 flash.Flash(self.DEVICE, self.IMAGE, rootfs_update=False)
129 self.assertTrue(self.updater_mock.patched['UpdateStateful'].called)
130 self.assertFalse(self.updater_mock.patched['UpdateRootfs'].called)
David Pursellf1d16a62015-03-25 13:31:04 -0700131
132 def testUpdateRootfs(self):
133 """Tests that update methods are called correctly."""
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800134 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Bertrand SIMONNETb34a98b2015-04-22 14:30:04 -0700135 flash.Flash(self.DEVICE, self.IMAGE, stateful_update=False)
136 self.assertFalse(self.updater_mock.patched['UpdateStateful'].called)
137 self.assertTrue(self.updater_mock.patched['UpdateRootfs'].called)
David Pursellf1d16a62015-03-25 13:31:04 -0700138
139 def testMissingPayloads(self):
140 """Tests we raise FlashError when payloads are missing."""
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800141 with mock.patch('os.path.exists',
142 side_effect=lambda p: self._ExistsMock(p, ret=False)):
Sanika Kulkarnia8c4e3a2019-09-20 16:47:25 -0700143 self.assertRaises(auto_updater_transfer.ChromiumOSTransferError,
144 flash.Flash, self.DEVICE, self.IMAGE)
David Pursellf1d16a62015-03-25 13:31:04 -0700145
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200146 def testFullPayload(self):
147 """Tests that we download full_payload and stateful using xBuddy."""
148 with mock.patch.object(
149 dev_server_wrapper,
150 'GetImagePathWithXbuddy',
151 return_value=('translated/xbuddy/path',
152 'resolved/xbuddy/path')) as mock_xbuddy:
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800153 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200154 flash.Flash(self.DEVICE, self.IMAGE)
155
156 # Call to download full_payload and stateful. No other calls.
157 mock_xbuddy.assert_has_calls(
Achuith Bhandarkaree1336f2020-04-18 11:44:09 +0000158 [mock.call('/path/to/image/full_payload', mock.ANY, mock.ANY,
Achuith Bhandarkar12f43c72019-11-21 16:44:24 -0800159 static_dir=flash.DEVSERVER_STATIC_DIR, silent=True),
Achuith Bhandarkaree1336f2020-04-18 11:44:09 +0000160 mock.call('/path/to/image/stateful', mock.ANY, mock.ANY,
Achuith Bhandarkar12f43c72019-11-21 16:44:24 -0800161 static_dir=flash.DEVSERVER_STATIC_DIR, silent=True)])
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200162 self.assertEqual(mock_xbuddy.call_count, 2)
163
164 def testTestImage(self):
165 """Tests that we download the test image when the full payload fails."""
166 with mock.patch.object(
167 dev_server_wrapper,
168 'GetImagePathWithXbuddy',
169 side_effect=(dev_server_wrapper.ImagePathError,
170 ('translated/xbuddy/path',
171 'resolved/xbuddy/path'))) as mock_xbuddy:
Achuith Bhandarkar1f54a212019-12-09 12:08:35 -0800172 with mock.patch('os.path.exists', side_effect=self._ExistsMock):
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200173 flash.Flash(self.DEVICE, self.IMAGE)
174
175 # Call to download full_payload and image. No other calls.
176 mock_xbuddy.assert_has_calls(
Achuith Bhandarkaree1336f2020-04-18 11:44:09 +0000177 [mock.call('/path/to/image/full_payload', mock.ANY, mock.ANY,
Achuith Bhandarkar12f43c72019-11-21 16:44:24 -0800178 static_dir=flash.DEVSERVER_STATIC_DIR, silent=True),
Achuith Bhandarkaree1336f2020-04-18 11:44:09 +0000179 mock.call('/path/to/image', mock.ANY, mock.ANY,
Achuith Bhandarkar4d4275f2019-10-01 17:07:23 +0200180 static_dir=flash.DEVSERVER_STATIC_DIR)])
181 self.assertEqual(mock_xbuddy.call_count, 2)
182
David Pursellf1d16a62015-03-25 13:31:04 -0700183
184class USBImagerMock(partial_mock.PartialCmdMock):
185 """Mock out USBImager."""
186 TARGET = 'chromite.cli.flash.USBImager'
187 ATTRS = ('CopyImageToDevice', 'InstallImageToDevice',
188 'ChooseRemovableDevice', 'ListAllRemovableDevices',
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -0700189 'GetRemovableDeviceDescription')
David Pursellf1d16a62015-03-25 13:31:04 -0700190 VALID_IMAGE = True
191
192 def __init__(self):
193 partial_mock.PartialCmdMock.__init__(self)
194
195 def CopyImageToDevice(self, _inst, *_args, **_kwargs):
196 """Mock out CopyImageToDevice."""
197
198 def InstallImageToDevice(self, _inst, *_args, **_kwargs):
199 """Mock out InstallImageToDevice."""
200
201 def ChooseRemovableDevice(self, _inst, *_args, **_kwargs):
202 """Mock out ChooseRemovableDevice."""
203
204 def ListAllRemovableDevices(self, _inst, *_args, **_kwargs):
205 """Mock out ListAllRemovableDevices."""
206 return ['foo', 'taco', 'milk']
207
208 def GetRemovableDeviceDescription(self, _inst, *_args, **_kwargs):
209 """Mock out GetRemovableDeviceDescription."""
210
David Pursellf1d16a62015-03-25 13:31:04 -0700211
212class USBImagerTest(cros_test_lib.MockTempDirTestCase):
213 """Test the flow of flash.Flash() with USBImager."""
214 IMAGE = '/path/to/image'
215
216 def Device(self, path):
217 """Create a USB device for passing to flash.Flash()."""
218 return commandline.Device(scheme=commandline.DEVICE_SCHEME_USB,
219 path=path)
220
221 def setUp(self):
222 """Patches objects."""
223 self.usb_mock = USBImagerMock()
224 self.imager_mock = self.StartPatcher(self.usb_mock)
225 self.PatchObject(dev_server_wrapper, 'GenerateXbuddyRequest',
226 return_value='xbuddy/local/latest')
David Pursellf1d16a62015-03-25 13:31:04 -0700227 self.PatchObject(dev_server_wrapper, 'GetImagePathWithXbuddy',
Gilad Arnolde62ec902015-04-24 14:41:02 -0700228 return_value=('taco-paladin/R36/chromiumos_test_image.bin',
229 'remote/taco-paladin/R36/test'))
David Pursellf1d16a62015-03-25 13:31:04 -0700230 self.PatchObject(os.path, 'exists', return_value=True)
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -0700231 self.isgpt_mock = self.PatchObject(flash, '_IsFilePathGPTDiskImage',
232 return_value=True)
David Pursellf1d16a62015-03-25 13:31:04 -0700233
234 def testLocalImagePathCopy(self):
235 """Tests that imaging methods are called correctly."""
236 with mock.patch('os.path.isfile', return_value=True):
237 flash.Flash(self.Device('/dev/foo'), self.IMAGE)
238 self.assertTrue(self.imager_mock.patched['CopyImageToDevice'].called)
239
240 def testLocalImagePathInstall(self):
241 """Tests that imaging methods are called correctly."""
242 with mock.patch('os.path.isfile', return_value=True):
243 flash.Flash(self.Device('/dev/foo'), self.IMAGE, board='taco',
244 install=True)
245 self.assertTrue(self.imager_mock.patched['InstallImageToDevice'].called)
246
247 def testLocalBadImagePath(self):
248 """Tests that using an image not having the magic bytes has prompt."""
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -0700249 self.isgpt_mock.return_value = False
David Pursellf1d16a62015-03-25 13:31:04 -0700250 with mock.patch('os.path.isfile', return_value=True):
251 with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt:
252 mock_prompt.return_value = False
253 flash.Flash(self.Device('/dev/foo'), self.IMAGE)
254 self.assertTrue(mock_prompt.called)
255
256 def testNonLocalImagePath(self):
257 """Tests that we try to get the image path using xbuddy."""
258 with mock.patch.object(
259 dev_server_wrapper,
260 'GetImagePathWithXbuddy',
Gilad Arnolde62ec902015-04-24 14:41:02 -0700261 return_value=('translated/xbuddy/path',
262 'resolved/xbuddy/path')) as mock_xbuddy:
David Pursellf1d16a62015-03-25 13:31:04 -0700263 with mock.patch('os.path.isfile', return_value=False):
264 with mock.patch('os.path.isdir', return_value=False):
265 flash.Flash(self.Device('/dev/foo'), self.IMAGE)
266 self.assertTrue(mock_xbuddy.called)
267
268 def testConfirmNonRemovableDevice(self):
269 """Tests that we ask user to confirm if the device is not removable."""
270 with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt:
271 flash.Flash(self.Device('/dev/dummy'), self.IMAGE)
272 self.assertTrue(mock_prompt.called)
273
274 def testSkipPromptNonRemovableDevice(self):
275 """Tests that we skip the prompt for non-removable with --yes."""
276 with mock.patch.object(cros_build_lib, 'BooleanPrompt') as mock_prompt:
277 flash.Flash(self.Device('/dev/dummy'), self.IMAGE, yes=True)
278 self.assertFalse(mock_prompt.called)
279
280 def testChooseRemovableDevice(self):
281 """Tests that we ask user to choose a device if none is given."""
282 flash.Flash(self.Device(''), self.IMAGE)
283 self.assertTrue(self.imager_mock.patched['ChooseRemovableDevice'].called)
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -0700284
285
Benjamin Gordon121a2aa2018-05-04 16:24:45 -0600286class UsbImagerOperationTest(cros_test_lib.RunCommandTestCase):
Ralph Nathan9b997232015-05-15 13:13:12 -0700287 """Tests for flash.UsbImagerOperation."""
288 # pylint: disable=protected-access
289
290 def setUp(self):
291 self.PatchObject(flash.UsbImagerOperation, '__init__', return_value=None)
292
293 def testUsbImagerOperationCalled(self):
294 """Test that flash.UsbImagerOperation is called when log level <= NOTICE."""
295 expected_cmd = ['dd', 'if=foo', 'of=bar', 'bs=4M', 'iflag=fullblock',
Frank Huang8e626432019-06-24 19:51:08 +0800296 'oflag=direct', 'conv=fdatasync']
Achuith Bhandarkaree1336f2020-04-18 11:44:09 +0000297 usb_imager = flash.USBImager('dummy_device', 'board', 'foo', 'latest')
Ralph Nathan9b997232015-05-15 13:13:12 -0700298 run_mock = self.PatchObject(flash.UsbImagerOperation, 'Run')
299 self.PatchObject(logging.Logger, 'getEffectiveLevel',
300 return_value=logging.NOTICE)
301 usb_imager.CopyImageToDevice('foo', 'bar')
302
303 # Check that flash.UsbImagerOperation.Run() is called correctly.
Mike Frysinger45602c72019-09-22 02:15:11 -0400304 run_mock.assert_called_with(cros_build_lib.sudo_run, expected_cmd,
Mike Frysinger3d5de8f2019-10-23 00:48:39 -0400305 debug_level=logging.NOTICE, encoding='utf-8',
306 update_period=0.5)
Ralph Nathan9b997232015-05-15 13:13:12 -0700307
308 def testSudoRunCommandCalled(self):
Mike Frysinger45602c72019-09-22 02:15:11 -0400309 """Test that sudo_run is called when log level > NOTICE."""
Ralph Nathan9b997232015-05-15 13:13:12 -0700310 expected_cmd = ['dd', 'if=foo', 'of=bar', 'bs=4M', 'iflag=fullblock',
Frank Huang8e626432019-06-24 19:51:08 +0800311 'oflag=direct', 'conv=fdatasync']
Achuith Bhandarkaree1336f2020-04-18 11:44:09 +0000312 usb_imager = flash.USBImager('dummy_device', 'board', 'foo', 'latest')
Mike Frysinger45602c72019-09-22 02:15:11 -0400313 run_mock = self.PatchObject(cros_build_lib, 'sudo_run')
Ralph Nathan9b997232015-05-15 13:13:12 -0700314 self.PatchObject(logging.Logger, 'getEffectiveLevel',
315 return_value=logging.WARNING)
316 usb_imager.CopyImageToDevice('foo', 'bar')
317
Mike Frysinger45602c72019-09-22 02:15:11 -0400318 # Check that sudo_run() is called correctly.
Ralph Nathan9b997232015-05-15 13:13:12 -0700319 run_mock.assert_any_call(expected_cmd, debug_level=logging.NOTICE,
320 print_cmd=False)
321
322 def testPingDD(self):
323 """Test that UsbImagerOperation._PingDD() sends the correct signal."""
324 expected_cmd = ['kill', '-USR1', '5']
Mike Frysinger45602c72019-09-22 02:15:11 -0400325 run_mock = self.PatchObject(cros_build_lib, 'sudo_run')
Ralph Nathan9b997232015-05-15 13:13:12 -0700326 op = flash.UsbImagerOperation('foo')
327 op._PingDD(5)
328
Mike Frysinger45602c72019-09-22 02:15:11 -0400329 # Check that sudo_run was called correctly.
Ralph Nathan9b997232015-05-15 13:13:12 -0700330 run_mock.assert_called_with(expected_cmd, print_cmd=False)
331
332 def testGetDDPidFound(self):
333 """Check that the expected pid is returned for _GetDDPid()."""
334 expected_pid = 5
335 op = flash.UsbImagerOperation('foo')
336 self.PatchObject(osutils, 'IsChildProcess', return_value=True)
337 self.rc.AddCmdResult(partial_mock.Ignore(),
338 output='%d\n10\n' % expected_pid)
339
340 pid = op._GetDDPid()
341
342 # Check that the correct pid was returned.
343 self.assertEqual(pid, expected_pid)
344
345 def testGetDDPidNotFound(self):
346 """Check that -1 is returned for _GetDDPid() if the pids aren't valid."""
347 expected_pid = -1
348 op = flash.UsbImagerOperation('foo')
349 self.PatchObject(osutils, 'IsChildProcess', return_value=False)
350 self.rc.AddCmdResult(partial_mock.Ignore(), output='5\n10\n')
351
352 pid = op._GetDDPid()
353
354 # Check that the correct pid was returned.
355 self.assertEqual(pid, expected_pid)
356
357
Bertrand SIMONNET56f773d2015-05-04 14:02:39 -0700358class FlashUtilTest(cros_test_lib.MockTempDirTestCase):
359 """Tests the helpers from cli.flash."""
360
361 def testChooseImage(self):
362 """Tests that we can detect a GPT image."""
363 # pylint: disable=protected-access
364
365 with self.PatchObject(flash, '_IsFilePathGPTDiskImage', return_value=True):
366 # No images defined. Choosing the image should raise an error.
367 with self.assertRaises(ValueError):
368 flash._ChooseImageFromDirectory(self.tempdir)
369
370 file_a = os.path.join(self.tempdir, 'a')
371 osutils.Touch(file_a)
372 # Only one image available, it should be selected automatically.
373 self.assertEqual(file_a, flash._ChooseImageFromDirectory(self.tempdir))
374
375 osutils.Touch(os.path.join(self.tempdir, 'b'))
376 file_c = os.path.join(self.tempdir, 'c')
377 osutils.Touch(file_c)
378 osutils.Touch(os.path.join(self.tempdir, 'd'))
379
380 # Multiple images available, we should ask the user to select the right
381 # image.
382 with self.PatchObject(cros_build_lib, 'GetChoice', return_value=2):
383 self.assertEqual(file_c, flash._ChooseImageFromDirectory(self.tempdir))
Mike Frysinger32759e42016-12-21 18:40:16 -0500384
385 def testIsFilePathGPTDiskImage(self):
386 """Tests the GPT image probing."""
387 # pylint: disable=protected-access
388
Mike Frysinger3d5de8f2019-10-23 00:48:39 -0400389 INVALID_PMBR = b' ' * 0x200
390 INVALID_GPT = b' ' * 0x200
391 VALID_PMBR = (b' ' * 0x1fe) + b'\x55\xaa'
392 VALID_GPT = b'EFI PART' + (b' ' * 0x1f8)
Mike Frysinger32759e42016-12-21 18:40:16 -0500393 TESTCASES = (
394 (False, False, INVALID_PMBR + INVALID_GPT),
395 (False, False, VALID_PMBR + INVALID_GPT),
396 (False, True, INVALID_PMBR + VALID_GPT),
397 (True, True, VALID_PMBR + VALID_GPT),
398 )
399
400 img = os.path.join(self.tempdir, 'img.bin')
401 for exp_pmbr_t, exp_pmbr_f, data in TESTCASES:
Mike Frysinger3d5de8f2019-10-23 00:48:39 -0400402 osutils.WriteFile(img, data, mode='wb')
Mike Frysinger32759e42016-12-21 18:40:16 -0500403 self.assertEqual(
404 flash._IsFilePathGPTDiskImage(img, require_pmbr=True), exp_pmbr_t)
405 self.assertEqual(
406 flash._IsFilePathGPTDiskImage(img, require_pmbr=False), exp_pmbr_f)