]>
Commit | Line | Data |
---|---|---|
184ee9b1 MK |
1 | /** @file\r |
2 | Unit tests of the DxeResetSystemLib instance of the ResetSystemLib class\r | |
3 | \r | |
4 | Copyright (C) Microsoft Corporation.\r | |
5 | SPDX-License-Identifier: BSD-2-Clause-Patent\r | |
6 | \r | |
7 | **/\r | |
8 | \r | |
9 | #include <stdio.h>\r | |
10 | #include <string.h>\r | |
11 | #include <stdarg.h>\r | |
12 | #include <stddef.h>\r | |
13 | #include <setjmp.h>\r | |
14 | #include <cmocka.h>\r | |
15 | \r | |
16 | #include <Uefi.h>\r | |
17 | #include <Library/BaseLib.h>\r | |
18 | #include <Library/BaseMemoryLib.h>\r | |
19 | #include <Library/DebugLib.h>\r | |
20 | #include <Library/MemoryAllocationLib.h>\r | |
21 | \r | |
22 | #include <Library/UnitTestLib.h>\r | |
23 | #include <Library/ResetSystemLib.h>\r | |
24 | \r | |
1436aea4 MK |
25 | #define UNIT_TEST_APP_NAME "DxeResetSystemLib Unit Tests"\r |
26 | #define UNIT_TEST_APP_VERSION "1.0"\r | |
184ee9b1 MK |
27 | \r |
28 | /**\r | |
29 | Resets the entire platform.\r | |
30 | \r | |
31 | @param[in] ResetType The type of reset to perform.\r | |
32 | @param[in] ResetStatus The status code for the reset.\r | |
33 | @param[in] DataSize The size, in bytes, of ResetData.\r | |
34 | @param[in] ResetData For a ResetType of EfiResetCold, EfiResetWarm, or\r | |
35 | EfiResetShutdown the data buffer starts with a Null-terminated\r | |
36 | string, optionally followed by additional binary data.\r | |
37 | The string is a description that the caller may use to further\r | |
38 | indicate the reason for the system reset.\r | |
39 | For a ResetType of EfiResetPlatformSpecific the data buffer\r | |
40 | also starts with a Null-terminated string that is followed\r | |
41 | by an EFI_GUID that describes the specific type of reset to perform.\r | |
42 | **/\r | |
43 | STATIC\r | |
44 | VOID\r | |
45 | EFIAPI\r | |
46 | MockResetSystem (\r | |
1436aea4 MK |
47 | IN EFI_RESET_TYPE ResetType,\r |
48 | IN EFI_STATUS ResetStatus,\r | |
49 | IN UINTN DataSize,\r | |
50 | IN VOID *ResetData OPTIONAL\r | |
184ee9b1 MK |
51 | )\r |
52 | {\r | |
53 | check_expected_ptr (ResetType);\r | |
54 | check_expected_ptr (ResetStatus);\r | |
55 | \r | |
56 | //\r | |
57 | // NOTE: Mocked functions can also return values, but that\r | |
58 | // is for another demo.\r | |
59 | }\r | |
60 | \r | |
61 | ///\r | |
62 | /// Mock version of the UEFI Runtime Services Table\r | |
63 | ///\r | |
64 | EFI_RUNTIME_SERVICES MockRuntime = {\r | |
65 | {\r | |
66 | EFI_RUNTIME_SERVICES_SIGNATURE, // Signature\r | |
67 | EFI_RUNTIME_SERVICES_REVISION, // Revision\r | |
68 | sizeof (EFI_RUNTIME_SERVICES), // HeaderSize\r | |
69 | 0, // CRC32\r | |
70 | 0 // Reserved\r | |
71 | },\r | |
72 | NULL, // GetTime\r | |
73 | NULL, // SetTime\r | |
74 | NULL, // GetWakeupTime\r | |
75 | NULL, // SetWakeupTime\r | |
76 | NULL, // SetVirtualAddressMap\r | |
77 | NULL, // ConvertPointer\r | |
78 | NULL, // GetVariable\r | |
79 | NULL, // GetNextVariableName\r | |
80 | NULL, // SetVariable\r | |
81 | NULL, // GetNextHighMonotonicCount\r | |
82 | MockResetSystem, // ResetSystem\r | |
83 | NULL, // UpdateCapsule\r | |
84 | NULL, // QueryCapsuleCapabilities\r | |
85 | NULL // QueryVariableInfo\r | |
86 | };\r | |
87 | \r | |
88 | /**\r | |
89 | Unit test for ColdReset () API of the ResetSystemLib.\r | |
90 | \r | |
91 | @param[in] Context [Optional] An optional parameter that enables:\r | |
92 | 1) test-case reuse with varied parameters and\r | |
93 | 2) test-case re-entry for Target tests that need a\r | |
94 | reboot. This parameter is a VOID* and it is the\r | |
95 | responsibility of the test author to ensure that the\r | |
96 | contents are well understood by all test cases that may\r | |
97 | consume it.\r | |
98 | \r | |
99 | @retval UNIT_TEST_PASSED The Unit test has completed and the test\r | |
100 | case was successful.\r | |
101 | @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r | |
102 | **/\r | |
103 | UNIT_TEST_STATUS\r | |
104 | EFIAPI\r | |
105 | ResetColdShouldIssueAColdReset (\r | |
106 | IN UNIT_TEST_CONTEXT Context\r | |
107 | )\r | |
108 | {\r | |
109 | expect_value (MockResetSystem, ResetType, EfiResetCold);\r | |
110 | expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r | |
111 | \r | |
112 | ResetCold ();\r | |
113 | \r | |
114 | return UNIT_TEST_PASSED;\r | |
115 | }\r | |
116 | \r | |
117 | /**\r | |
118 | Unit test for WarmReset () API of the ResetSystemLib.\r | |
119 | \r | |
120 | @param[in] Context [Optional] An optional parameter that enables:\r | |
121 | 1) test-case reuse with varied parameters and\r | |
122 | 2) test-case re-entry for Target tests that need a\r | |
123 | reboot. This parameter is a VOID* and it is the\r | |
124 | responsibility of the test author to ensure that the\r | |
125 | contents are well understood by all test cases that may\r | |
126 | consume it.\r | |
127 | \r | |
128 | @retval UNIT_TEST_PASSED The Unit test has completed and the test\r | |
129 | case was successful.\r | |
130 | @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r | |
131 | **/\r | |
132 | UNIT_TEST_STATUS\r | |
133 | EFIAPI\r | |
134 | ResetWarmShouldIssueAWarmReset (\r | |
135 | IN UNIT_TEST_CONTEXT Context\r | |
136 | )\r | |
137 | {\r | |
138 | expect_value (MockResetSystem, ResetType, EfiResetWarm);\r | |
139 | expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r | |
140 | \r | |
141 | ResetWarm ();\r | |
142 | \r | |
143 | return UNIT_TEST_PASSED;\r | |
144 | }\r | |
145 | \r | |
146 | /**\r | |
147 | Unit test for ResetShutdown () API of the ResetSystemLib.\r | |
148 | \r | |
149 | @param[in] Context [Optional] An optional parameter that enables:\r | |
150 | 1) test-case reuse with varied parameters and\r | |
151 | 2) test-case re-entry for Target tests that need a\r | |
152 | reboot. This parameter is a VOID* and it is the\r | |
153 | responsibility of the test author to ensure that the\r | |
154 | contents are well understood by all test cases that may\r | |
155 | consume it.\r | |
156 | \r | |
157 | @retval UNIT_TEST_PASSED The Unit test has completed and the test\r | |
158 | case was successful.\r | |
159 | @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r | |
160 | **/\r | |
161 | UNIT_TEST_STATUS\r | |
162 | EFIAPI\r | |
163 | ResetShutdownShouldIssueAShutdown (\r | |
164 | IN UNIT_TEST_CONTEXT Context\r | |
165 | )\r | |
166 | {\r | |
167 | expect_value (MockResetSystem, ResetType, EfiResetShutdown);\r | |
168 | expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r | |
169 | \r | |
170 | ResetShutdown ();\r | |
171 | \r | |
172 | return UNIT_TEST_PASSED;\r | |
173 | }\r | |
174 | \r | |
175 | /**\r | |
176 | Unit test for ResetPlatformSpecific () API of the ResetSystemLib.\r | |
177 | \r | |
178 | @param[in] Context [Optional] An optional parameter that enables:\r | |
179 | 1) test-case reuse with varied parameters and\r | |
180 | 2) test-case re-entry for Target tests that need a\r | |
181 | reboot. This parameter is a VOID* and it is the\r | |
182 | responsibility of the test author to ensure that the\r | |
183 | contents are well understood by all test cases that may\r | |
184 | consume it.\r | |
185 | \r | |
186 | @retval UNIT_TEST_PASSED The Unit test has completed and the test\r | |
187 | case was successful.\r | |
188 | @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r | |
189 | **/\r | |
190 | UNIT_TEST_STATUS\r | |
191 | EFIAPI\r | |
192 | ResetPlatformSpecificShouldIssueAPlatformSpecificReset (\r | |
193 | IN UNIT_TEST_CONTEXT Context\r | |
194 | )\r | |
195 | {\r | |
196 | expect_value (MockResetSystem, ResetType, EfiResetPlatformSpecific);\r | |
197 | expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r | |
198 | \r | |
199 | ResetPlatformSpecific (0, NULL);\r | |
200 | \r | |
201 | return UNIT_TEST_PASSED;\r | |
202 | }\r | |
203 | \r | |
204 | /**\r | |
205 | Unit test for ResetSystem () API of the ResetSystemLib.\r | |
206 | \r | |
207 | @param[in] Context [Optional] An optional parameter that enables:\r | |
208 | 1) test-case reuse with varied parameters and\r | |
209 | 2) test-case re-entry for Target tests that need a\r | |
210 | reboot. This parameter is a VOID* and it is the\r | |
211 | responsibility of the test author to ensure that the\r | |
212 | contents are well understood by all test cases that may\r | |
213 | consume it.\r | |
214 | \r | |
215 | @retval UNIT_TEST_PASSED The Unit test has completed and the test\r | |
216 | case was successful.\r | |
217 | @retval UNIT_TEST_ERROR_TEST_FAILED A test case assertion has failed.\r | |
218 | **/\r | |
219 | UNIT_TEST_STATUS\r | |
220 | EFIAPI\r | |
221 | ResetSystemShouldPassTheParametersThrough (\r | |
222 | IN UNIT_TEST_CONTEXT Context\r | |
223 | )\r | |
224 | {\r | |
225 | expect_value (MockResetSystem, ResetType, EfiResetCold);\r | |
226 | expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r | |
227 | \r | |
228 | ResetSystem (EfiResetCold, EFI_SUCCESS, 0, NULL);\r | |
229 | \r | |
230 | expect_value (MockResetSystem, ResetType, EfiResetShutdown);\r | |
231 | expect_value (MockResetSystem, ResetStatus, EFI_SUCCESS);\r | |
232 | \r | |
233 | ResetSystem (EfiResetShutdown, EFI_SUCCESS, 0, NULL);\r | |
234 | \r | |
235 | return UNIT_TEST_PASSED;\r | |
236 | }\r | |
237 | \r | |
238 | /**\r | |
239 | Initialze the unit test framework, suite, and unit tests for the\r | |
240 | ResetSystemLib and run the ResetSystemLib unit test.\r | |
241 | \r | |
242 | @retval EFI_SUCCESS All test cases were dispatched.\r | |
243 | @retval EFI_OUT_OF_RESOURCES There are not enough resources available to\r | |
244 | initialize the unit tests.\r | |
245 | **/\r | |
246 | STATIC\r | |
247 | EFI_STATUS\r | |
248 | EFIAPI\r | |
249 | UnitTestingEntry (\r | |
250 | VOID\r | |
251 | )\r | |
252 | {\r | |
253 | EFI_STATUS Status;\r | |
254 | UNIT_TEST_FRAMEWORK_HANDLE Framework;\r | |
255 | UNIT_TEST_SUITE_HANDLE ResetTests;\r | |
256 | \r | |
257 | Framework = NULL;\r | |
258 | \r | |
1436aea4 | 259 | DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_APP_NAME, UNIT_TEST_APP_VERSION));\r |
184ee9b1 MK |
260 | \r |
261 | //\r | |
262 | // Start setting up the test framework for running the tests.\r | |
263 | //\r | |
264 | Status = InitUnitTestFramework (&Framework, UNIT_TEST_APP_NAME, gEfiCallerBaseName, UNIT_TEST_APP_VERSION);\r | |
265 | if (EFI_ERROR (Status)) {\r | |
1436aea4 MK |
266 | DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));\r |
267 | goto EXIT;\r | |
184ee9b1 MK |
268 | }\r |
269 | \r | |
270 | //\r | |
271 | // Populate the ResetSytemLib Unit Test Suite.\r | |
272 | //\r | |
273 | Status = CreateUnitTestSuite (&ResetTests, Framework, "DxeResetSystemLib Reset Tests", "ResetSystemLib.Reset", NULL, NULL);\r | |
274 | if (EFI_ERROR (Status)) {\r | |
275 | DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for ResetTests\n"));\r | |
276 | Status = EFI_OUT_OF_RESOURCES;\r | |
277 | goto EXIT;\r | |
278 | }\r | |
279 | \r | |
280 | //\r | |
281 | // --------------Suite-----------Description--------------Name----------Function--------Pre---Post-------------------Context-----------\r | |
282 | //\r | |
283 | AddTestCase (ResetTests, "ResetCold should issue a cold reset", "Cold", ResetColdShouldIssueAColdReset, NULL, NULL, NULL);\r | |
284 | AddTestCase (ResetTests, "ResetWarm should issue a warm reset", "Warm", ResetWarmShouldIssueAWarmReset, NULL, NULL, NULL);\r | |
285 | AddTestCase (ResetTests, "ResetShutdown should issue a shutdown", "Shutdown", ResetShutdownShouldIssueAShutdown, NULL, NULL, NULL);\r | |
286 | AddTestCase (ResetTests, "ResetPlatformSpecific should issue a platform-specific reset", "Platform", ResetPlatformSpecificShouldIssueAPlatformSpecificReset, NULL, NULL, NULL);\r | |
287 | AddTestCase (ResetTests, "ResetSystem should pass all parameters through", "Parameters", ResetSystemShouldPassTheParametersThrough, NULL, NULL, NULL);\r | |
288 | \r | |
289 | //\r | |
290 | // Execute the tests.\r | |
291 | //\r | |
292 | Status = RunAllTestSuites (Framework);\r | |
293 | \r | |
294 | EXIT:\r | |
295 | if (Framework) {\r | |
296 | FreeUnitTestFramework (Framework);\r | |
297 | }\r | |
298 | \r | |
299 | return Status;\r | |
300 | }\r | |
301 | \r | |
302 | /**\r | |
303 | Standard POSIX C entry point for host based unit test execution.\r | |
304 | **/\r | |
305 | int\r | |
306 | main (\r | |
1436aea4 MK |
307 | int argc,\r |
308 | char *argv[]\r | |
184ee9b1 MK |
309 | )\r |
310 | {\r | |
311 | return UnitTestingEntry ();\r | |
312 | }\r |