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