|
| 1 | +# @file |
| 2 | +# |
| 3 | +# Copyright (c) Microsoft Corporation. |
| 4 | +# SPDX-License-Identifier: BSD-2-Clause-Patent |
| 5 | +## |
| 6 | +"""Tests for the authenticode_transplant script.""" |
| 7 | + |
| 8 | +import os |
| 9 | +import subprocess |
| 10 | +import sys |
| 11 | +import tempfile |
| 12 | +import unittest |
| 13 | + |
| 14 | + |
| 15 | +class TestAuthenticodeTransplant(unittest.TestCase): |
| 16 | + """Test cases for authenticode transplant functionality.""" |
| 17 | + |
| 18 | + def setUp(self) -> None: |
| 19 | + """Set up test fixtures.""" |
| 20 | + self.test_dir = tempfile.mkdtemp() |
| 21 | + |
| 22 | + def tearDown(self) -> None: |
| 23 | + """Clean up test fixtures.""" |
| 24 | + # Clean up test files if they exist |
| 25 | + for file in os.listdir(self.test_dir): |
| 26 | + os.remove(os.path.join(self.test_dir, file)) |
| 27 | + os.rmdir(self.test_dir) |
| 28 | + |
| 29 | + def test_help_message(self) -> None: |
| 30 | + """Test that the help message can be generated without errors.""" |
| 31 | + try: |
| 32 | + result = subprocess.run( |
| 33 | + [sys.executable, "scripts/authenticode_transplant.py", "--help"], |
| 34 | + cwd="/home/runner/work/secureboot_objects/secureboot_objects", |
| 35 | + capture_output=True, |
| 36 | + text=True, |
| 37 | + timeout=10 |
| 38 | + ) |
| 39 | + self.assertEqual(result.returncode, 0) |
| 40 | + self.assertIn("Transplant Authenticode signature", result.stdout) |
| 41 | + self.assertIn("source_pe", result.stdout) |
| 42 | + self.assertIn("target_pe", result.stdout) |
| 43 | + self.assertIn("output_pe", result.stdout) |
| 44 | + self.assertIn("--force", result.stdout) |
| 45 | + self.assertIn("--debug", result.stdout) |
| 46 | + except subprocess.TimeoutExpired: |
| 47 | + self.fail("Help command timed out") |
| 48 | + |
| 49 | + def test_missing_arguments(self) -> None: |
| 50 | + """Test that missing arguments are handled properly.""" |
| 51 | + try: |
| 52 | + result = subprocess.run( |
| 53 | + [sys.executable, "scripts/authenticode_transplant.py"], |
| 54 | + cwd="/home/runner/work/secureboot_objects/secureboot_objects", |
| 55 | + capture_output=True, |
| 56 | + text=True, |
| 57 | + timeout=10 |
| 58 | + ) |
| 59 | + self.assertNotEqual(result.returncode, 0) # Should fail with missing args |
| 60 | + except subprocess.TimeoutExpired: |
| 61 | + self.fail("Command with no args timed out") |
| 62 | + |
| 63 | + def test_nonexistent_source_file(self) -> None: |
| 64 | + """Test handling of non-existent source file.""" |
| 65 | + target_file = os.path.join(self.test_dir, "target.exe") |
| 66 | + output_file = os.path.join(self.test_dir, "output.exe") |
| 67 | + |
| 68 | + # Create a dummy target file |
| 69 | + with open(target_file, 'wb') as f: |
| 70 | + f.write(b"dummy content") |
| 71 | + |
| 72 | + try: |
| 73 | + result = subprocess.run( |
| 74 | + [sys.executable, "scripts/authenticode_transplant.py", |
| 75 | + "/nonexistent/source.exe", target_file, output_file], |
| 76 | + cwd="/home/runner/work/secureboot_objects/secureboot_objects", |
| 77 | + capture_output=True, |
| 78 | + text=True, |
| 79 | + timeout=10 |
| 80 | + ) |
| 81 | + self.assertNotEqual(result.returncode, 0) |
| 82 | + self.assertIn("Source PE file not found", result.stderr) |
| 83 | + except subprocess.TimeoutExpired: |
| 84 | + self.fail("Command with nonexistent source timed out") |
| 85 | + |
| 86 | + def test_nonexistent_target_file(self) -> None: |
| 87 | + """Test handling of non-existent target file.""" |
| 88 | + source_file = os.path.join(self.test_dir, "source.exe") |
| 89 | + output_file = os.path.join(self.test_dir, "output.exe") |
| 90 | + |
| 91 | + # Create a dummy source file |
| 92 | + with open(source_file, 'wb') as f: |
| 93 | + f.write(b"dummy content") |
| 94 | + |
| 95 | + try: |
| 96 | + result = subprocess.run( |
| 97 | + [sys.executable, "scripts/authenticode_transplant.py", |
| 98 | + source_file, "/nonexistent/target.exe", output_file], |
| 99 | + cwd="/home/runner/work/secureboot_objects/secureboot_objects", |
| 100 | + capture_output=True, |
| 101 | + text=True, |
| 102 | + timeout=10 |
| 103 | + ) |
| 104 | + self.assertNotEqual(result.returncode, 0) |
| 105 | + self.assertIn("Target PE file not found", result.stderr) |
| 106 | + except subprocess.TimeoutExpired: |
| 107 | + self.fail("Command with nonexistent target timed out") |
| 108 | + |
| 109 | + def test_debug_flag(self) -> None: |
| 110 | + """Test that debug flag is accepted.""" |
| 111 | + source_file = os.path.join(self.test_dir, "source.exe") |
| 112 | + target_file = os.path.join(self.test_dir, "target.exe") |
| 113 | + output_file = os.path.join(self.test_dir, "output.exe") |
| 114 | + |
| 115 | + # Create dummy files |
| 116 | + with open(source_file, 'wb') as f: |
| 117 | + f.write(b"dummy content") |
| 118 | + with open(target_file, 'wb') as f: |
| 119 | + f.write(b"dummy content") |
| 120 | + |
| 121 | + try: |
| 122 | + result = subprocess.run( |
| 123 | + [sys.executable, "scripts/authenticode_transplant.py", |
| 124 | + "--debug", source_file, target_file, output_file], |
| 125 | + cwd="/home/runner/work/secureboot_objects/secureboot_objects", |
| 126 | + capture_output=True, |
| 127 | + text=True, |
| 128 | + timeout=10 |
| 129 | + ) |
| 130 | + # Should fail because these aren't real PE files, but debug flag should be accepted |
| 131 | + self.assertNotEqual(result.returncode, 0) |
| 132 | + except subprocess.TimeoutExpired: |
| 133 | + self.fail("Command with debug flag timed out") |
| 134 | + |
| 135 | + |
| 136 | +if __name__ == "__main__": |
| 137 | + unittest.main() |
0 commit comments