Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 1 | # Copyright 2018 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | """Unittests for the security_test_image script.""" |
| 6 | |
Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 7 | import os |
| 8 | |
| 9 | from chromite.lib import cros_build_lib |
| 10 | from chromite.lib import cros_test_lib |
| 11 | from chromite.lib import image_lib |
| 12 | from chromite.scripts import security_test_image |
| 13 | |
| 14 | |
Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 15 | class SecurityTestImageTest(cros_test_lib.MockTempDirTestCase): |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 16 | """Security test image script tests.""" |
Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 17 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 18 | def setUp(self): |
| 19 | D = cros_test_lib.Directory |
| 20 | filesystem = ( |
| 21 | D("board", ("recovery_image.bin",)), |
| 22 | "other_image.bin", |
| 23 | ) |
| 24 | cros_test_lib.CreateOnDiskHierarchy(self.tempdir, filesystem) |
Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 25 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 26 | def testParseArgs(self): |
| 27 | """Argument parsing tests.""" |
| 28 | # pylint: disable=protected-access |
| 29 | # Test no arguments. |
| 30 | with self.assertRaises(SystemExit): |
| 31 | security_test_image._ParseArgs([]) |
Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 32 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 33 | # Test board is set but not used when we have the full image path. |
| 34 | self.PatchObject( |
| 35 | cros_build_lib, "GetDefaultBoard", return_value="board" |
| 36 | ) |
| 37 | opts = security_test_image._ParseArgs( |
| 38 | ["--image", os.path.join(self.tempdir, "other_image.bin")] |
| 39 | ) |
| 40 | self.assertEqual("board", opts.board) |
| 41 | self.assertEqual( |
| 42 | opts.image, os.path.join(self.tempdir, "other_image.bin") |
| 43 | ) |
Alex Klein | fa719c9 | 2018-10-15 14:04:22 -0600 | [diff] [blame] | 44 | |
Alex Klein | 1699fab | 2022-09-08 08:46:06 -0600 | [diff] [blame^] | 45 | # Test the board is fetched and used when using the default image basename. |
| 46 | self.PatchObject( |
| 47 | image_lib, |
| 48 | "GetLatestImageLink", |
| 49 | return_value=os.path.join(self.tempdir, "board"), |
| 50 | ) |
| 51 | opts = security_test_image._ParseArgs([]) |
| 52 | self.assertEqual("board", opts.board) |
| 53 | self.assertEqual( |
| 54 | opts.image, os.path.join(self.tempdir, "board/recovery_image.bin") |
| 55 | ) |