1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607
|
/** @file
This is a host-based unit test for the VariableLockRequestToLock shim.
Copyright (c) Microsoft Corporation.
SPDX-License-Identifier: BSD-2-Clause-Patent
**/
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stddef.h>
#include <setjmp.h>
#include <cmocka.h>
#include <Uefi.h>
#include <Library/DebugLib.h>
#include <Library/BaseMemoryLib.h>
#include <Library/MemoryAllocationLib.h>
#include <Library/UnitTestLib.h>
#include <Library/VariablePolicyLib.h>
#include <Library/VariablePolicyHelperLib.h>
#include <Protocol/VariableLock.h>
#define UNIT_TEST_NAME "VarPol/VarLock Shim Unit Test"
#define UNIT_TEST_VERSION "1.0"
/// === CODE UNDER TEST ===========================================================================
EFI_STATUS
EFIAPI
VariableLockRequestToLock (
IN CONST EDKII_VARIABLE_LOCK_PROTOCOL *This,
IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid
);
/// === TEST DATA ==================================================================================
//
// Test GUID 1 {F955BA2D-4A2C-480C-BFD1-3CC522610592}
//
EFI_GUID mTestGuid1 = {
0xf955ba2d, 0x4a2c, 0x480c, { 0xbf, 0xd1, 0x3c, 0xc5, 0x22, 0x61, 0x5, 0x92 }
};
//
// Test GUID 2 {2DEA799E-5E73-43B9-870E-C945CE82AF3A}
//
EFI_GUID mTestGuid2 = {
0x2dea799e, 0x5e73, 0x43b9, { 0x87, 0xe, 0xc9, 0x45, 0xce, 0x82, 0xaf, 0x3a }
};
//
// Test GUID 3 {698A2BFD-A616-482D-B88C-7100BD6682A9}
//
EFI_GUID mTestGuid3 = {
0x698a2bfd, 0xa616, 0x482d, { 0xb8, 0x8c, 0x71, 0x0, 0xbd, 0x66, 0x82, 0xa9 }
};
#define TEST_VAR_1_NAME L"TestVar1"
#define TEST_VAR_2_NAME L"TestVar2"
#define TEST_VAR_3_NAME L"TestVar3"
#define TEST_POLICY_ATTRIBUTES_NULL 0
#define TEST_POLICY_MIN_SIZE_NULL 0
#define TEST_POLICY_MAX_SIZE_NULL MAX_UINT32
#define TEST_POLICY_MIN_SIZE_10 10
#define TEST_POLICY_MAX_SIZE_200 200
/// === HELPER FUNCTIONS ===========================================================================
/**
Mocked version of GetVariable, for testing.
@param VariableName
@param VendorGuid
@param Attributes
@param DataSize
@param Data
**/
EFI_STATUS
EFIAPI
StubGetVariableNull (
IN CHAR16 *VariableName,
IN EFI_GUID *VendorGuid,
OUT UINT32 *Attributes OPTIONAL,
IN OUT UINTN *DataSize,
OUT VOID *Data OPTIONAL
)
{
UINT32 MockedAttr;
UINTN MockedDataSize;
VOID *MockedData;
EFI_STATUS MockedReturn;
check_expected_ptr (VariableName);
check_expected_ptr (VendorGuid);
check_expected_ptr (DataSize);
MockedAttr = (UINT32)mock ();
MockedDataSize = (UINTN)mock ();
MockedData = (VOID *)(UINTN)mock ();
MockedReturn = (EFI_STATUS)mock ();
if (Attributes != NULL) {
*Attributes = MockedAttr;
}
if ((Data != NULL) && !EFI_ERROR (MockedReturn)) {
CopyMem (Data, MockedData, MockedDataSize);
}
*DataSize = MockedDataSize;
return MockedReturn;
}
//
// Anything you think might be helpful that isn't a test itself.
//
/**
This is a common setup function that will ensure the library is always
initialized with the stubbed GetVariable.
Not used by all test cases, but by most.
@param[in] Context Unit test case context
**/
STATIC
UNIT_TEST_STATUS
EFIAPI
LibInitMocked (
IN UNIT_TEST_CONTEXT Context
)
{
return EFI_ERROR (InitVariablePolicyLib (StubGetVariableNull)) ? UNIT_TEST_ERROR_PREREQUISITE_NOT_MET : UNIT_TEST_PASSED;
}
/**
Common cleanup function to make sure that the library is always de-initialized
prior to the next test case.
@param[in] Context Unit test case context
**/
STATIC
VOID
EFIAPI
LibCleanup (
IN UNIT_TEST_CONTEXT Context
)
{
DeinitVariablePolicyLib ();
}
/// === TEST CASES =================================================================================
/// ===== SHIM SUITE ===========================================================
/**
Test Case that locks a single variable using the Variable Lock Protocol.
The call is expected to succeed.
@param[in] Context Unit test case context
**/
UNIT_TEST_STATUS
EFIAPI
LockingWithoutAnyPoliciesShouldSucceed (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
Status = VariableLockRequestToLock (NULL, TEST_VAR_1_NAME, &mTestGuid1);
UT_ASSERT_NOT_EFI_ERROR (Status);
return UNIT_TEST_PASSED;
}
/**
Test Case that locks the same variable twice using the Variable Lock Protocol.
Both calls are expected to succeed.
@param[in] Context Unit test case context
**/
UNIT_TEST_STATUS
EFIAPI
LockingTwiceShouldSucceed (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
Status = VariableLockRequestToLock (NULL, TEST_VAR_1_NAME, &mTestGuid1);
UT_ASSERT_NOT_EFI_ERROR (Status);
Status = VariableLockRequestToLock (NULL, TEST_VAR_1_NAME, &mTestGuid1);
UT_ASSERT_NOT_EFI_ERROR (Status);
return UNIT_TEST_PASSED;
}
/**
Test Case that locks a variable using the Variable Policy Protocol then locks
the same variable using the Variable Lock Protocol.
Both calls are expected to succeed.
@param[in] Context Unit test case context
**/
UNIT_TEST_STATUS
EFIAPI
LockingALockedVariableShouldSucceed (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
VARIABLE_POLICY_ENTRY *NewEntry;
//
// Create a variable policy that locks the variable.
//
Status = CreateBasicVariablePolicy (
&mTestGuid1,
TEST_VAR_1_NAME,
TEST_POLICY_MIN_SIZE_NULL,
TEST_POLICY_MAX_SIZE_200,
TEST_POLICY_ATTRIBUTES_NULL,
TEST_POLICY_ATTRIBUTES_NULL,
VARIABLE_POLICY_TYPE_LOCK_NOW,
&NewEntry
);
UT_ASSERT_NOT_EFI_ERROR (Status);
//
// Register the new policy.
//
Status = RegisterVariablePolicy (NewEntry);
Status = VariableLockRequestToLock (NULL, TEST_VAR_1_NAME, &mTestGuid1);
UT_ASSERT_NOT_EFI_ERROR (Status);
FreePool (NewEntry);
return UNIT_TEST_PASSED;
}
/**
Test Case that locks a variable using the Variable Policy Protocol with a
policy other than LOCK_NOW then attempts to lock the same variable using the
Variable Lock Protocol. The call to Variable Policy is expected to succeed
and the call to Variable Lock is expected to fail.
@param[in] Context Unit test case context
**/
UNIT_TEST_STATUS
EFIAPI
LockingAnUnlockedVariableShouldFail (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
VARIABLE_POLICY_ENTRY *NewEntry;
// Create a variable policy that locks the variable.
Status = CreateVarStateVariablePolicy (
&mTestGuid1,
TEST_VAR_1_NAME,
TEST_POLICY_MIN_SIZE_NULL,
TEST_POLICY_MAX_SIZE_200,
TEST_POLICY_ATTRIBUTES_NULL,
TEST_POLICY_ATTRIBUTES_NULL,
&mTestGuid2,
1,
TEST_VAR_2_NAME,
&NewEntry
);
UT_ASSERT_NOT_EFI_ERROR (Status);
// Register the new policy.
Status = RegisterVariablePolicy (NewEntry);
// Configure the stub to not care about parameters. We're testing errors.
expect_any_always (StubGetVariableNull, VariableName);
expect_any_always (StubGetVariableNull, VendorGuid);
expect_any_always (StubGetVariableNull, DataSize);
// With a policy, make sure that writes still work, since the variable doesn't exist.
will_return (StubGetVariableNull, TEST_POLICY_ATTRIBUTES_NULL); // Attributes
will_return (StubGetVariableNull, 0); // Size
will_return (StubGetVariableNull, (UINTN)NULL); // DataPtr
will_return (StubGetVariableNull, EFI_NOT_FOUND); // Status
Status = VariableLockRequestToLock (NULL, TEST_VAR_1_NAME, &mTestGuid1);
UT_ASSERT_TRUE (EFI_ERROR (Status));
FreePool (NewEntry);
return UNIT_TEST_PASSED;
}
/**
Test Case that locks a variable using the Variable Policy Protocol with a
policy other than LOCK_NOW, but is currently locked. Then attempts to lock
the same variable using the Variable Lock Protocol. The call to Variable
Policy is expected to succeed and the call to Variable Lock also expected to
succeed.
@param[in] Context Unit test case context
**/
UNIT_TEST_STATUS
EFIAPI
LockingALockedVariableWithMatchingDataShouldSucceed (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
VARIABLE_POLICY_ENTRY *NewEntry;
UINT8 Data;
// Create a variable policy that locks the variable.
Status = CreateVarStateVariablePolicy (
&mTestGuid1,
TEST_VAR_1_NAME,
TEST_POLICY_MIN_SIZE_NULL,
TEST_POLICY_MAX_SIZE_200,
TEST_POLICY_ATTRIBUTES_NULL,
TEST_POLICY_ATTRIBUTES_NULL,
&mTestGuid2,
1,
TEST_VAR_2_NAME,
&NewEntry
);
UT_ASSERT_NOT_EFI_ERROR (Status);
// Register the new policy.
Status = RegisterVariablePolicy (NewEntry);
// Configure the stub to not care about parameters. We're testing errors.
expect_any_always (StubGetVariableNull, VariableName);
expect_any_always (StubGetVariableNull, VendorGuid);
expect_any_always (StubGetVariableNull, DataSize);
// With a policy, make sure that writes still work, since the variable doesn't exist.
Data = 1;
will_return (StubGetVariableNull, TEST_POLICY_ATTRIBUTES_NULL); // Attributes
will_return (StubGetVariableNull, sizeof (Data)); // Size
will_return (StubGetVariableNull, (UINTN)&Data); // DataPtr
will_return (StubGetVariableNull, EFI_SUCCESS); // Status
Status = VariableLockRequestToLock (NULL, TEST_VAR_1_NAME, &mTestGuid1);
UT_ASSERT_TRUE (!EFI_ERROR (Status));
FreePool (NewEntry);
return UNIT_TEST_PASSED;
}
/**
Test Case that locks a variable using the Variable Policy Protocol with a
policy other than LOCK_NOW, but variable data does not match. Then attempts
to lock the same variable using the Variable Lock Protocol. The call to
Variable Policy is expected to succeed and the call to Variable Lock is
expected to fail.
@param[in] Context Unit test case context
**/
UNIT_TEST_STATUS
EFIAPI
LockingALockedVariableWithNonMatchingDataShouldFail (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
VARIABLE_POLICY_ENTRY *NewEntry;
UINT8 Data;
// Create a variable policy that locks the variable.
Status = CreateVarStateVariablePolicy (
&mTestGuid1,
TEST_VAR_1_NAME,
TEST_POLICY_MIN_SIZE_NULL,
TEST_POLICY_MAX_SIZE_200,
TEST_POLICY_ATTRIBUTES_NULL,
TEST_POLICY_ATTRIBUTES_NULL,
&mTestGuid2,
1,
TEST_VAR_2_NAME,
&NewEntry
);
UT_ASSERT_NOT_EFI_ERROR (Status);
// Register the new policy.
Status = RegisterVariablePolicy (NewEntry);
// Configure the stub to not care about parameters. We're testing errors.
expect_any_always (StubGetVariableNull, VariableName);
expect_any_always (StubGetVariableNull, VendorGuid);
expect_any_always (StubGetVariableNull, DataSize);
// With a policy, make sure that writes still work, since the variable doesn't exist.
Data = 2;
will_return (StubGetVariableNull, TEST_POLICY_ATTRIBUTES_NULL); // Attributes
will_return (StubGetVariableNull, sizeof (Data)); // Size
will_return (StubGetVariableNull, (UINTN)&Data); // DataPtr
will_return (StubGetVariableNull, EFI_SUCCESS); // Status
Status = VariableLockRequestToLock (NULL, TEST_VAR_1_NAME, &mTestGuid1);
UT_ASSERT_TRUE (EFI_ERROR (Status));
FreePool (NewEntry);
return UNIT_TEST_PASSED;
}
/**
Test Case that locks a variable using Variable Lock Protocol Policy Protocol
then and then attempts to lock the same variable using the Variable Policy
Protocol. The call to Variable Lock is expected to succeed and the call to
Variable Policy is expected to fail.
@param[in] Context Unit test case context
**/
UNIT_TEST_STATUS
EFIAPI
SettingPolicyForALockedVariableShouldFail (
IN UNIT_TEST_CONTEXT Context
)
{
EFI_STATUS Status;
VARIABLE_POLICY_ENTRY *NewEntry;
// Lock the variable.
Status = VariableLockRequestToLock (NULL, TEST_VAR_1_NAME, &mTestGuid1);
UT_ASSERT_NOT_EFI_ERROR (Status);
// Create a variable policy that locks the variable.
Status = CreateVarStateVariablePolicy (
&mTestGuid1,
TEST_VAR_1_NAME,
TEST_POLICY_MIN_SIZE_NULL,
TEST_POLICY_MAX_SIZE_200,
TEST_POLICY_ATTRIBUTES_NULL,
TEST_POLICY_ATTRIBUTES_NULL,
&mTestGuid2,
1,
TEST_VAR_2_NAME,
&NewEntry
);
UT_ASSERT_NOT_EFI_ERROR (Status);
// Register the new policy.
Status = RegisterVariablePolicy (NewEntry);
UT_ASSERT_TRUE (EFI_ERROR (Status));
FreePool (NewEntry);
return UNIT_TEST_PASSED;
}
/**
Main entry point to this unit test application.
Sets up and runs the test suites.
**/
VOID
EFIAPI
UnitTestMain (
VOID
)
{
EFI_STATUS Status;
UNIT_TEST_FRAMEWORK_HANDLE Framework;
UNIT_TEST_SUITE_HANDLE ShimTests;
Framework = NULL;
DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));
//
// Start setting up the test framework for running the tests.
//
Status = InitUnitTestFramework (&Framework, UNIT_TEST_NAME, gEfiCallerBaseName, UNIT_TEST_VERSION);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed in InitUnitTestFramework. Status = %r\n", Status));
goto EXIT;
}
//
// Add all test suites and tests.
//
Status = CreateUnitTestSuite (
&ShimTests,
Framework,
"Variable Lock Shim Tests",
"VarPolicy.VarLockShim",
NULL,
NULL
);
if (EFI_ERROR (Status)) {
DEBUG ((DEBUG_ERROR, "Failed in CreateUnitTestSuite for ShimTests\n"));
Status = EFI_OUT_OF_RESOURCES;
goto EXIT;
}
AddTestCase (
ShimTests,
"Locking a variable with no matching policies should always work",
"EmptyPolicies",
LockingWithoutAnyPoliciesShouldSucceed,
LibInitMocked,
LibCleanup,
NULL
);
AddTestCase (
ShimTests,
"Locking a variable twice should always work",
"DoubleLock",
LockingTwiceShouldSucceed,
LibInitMocked,
LibCleanup,
NULL
);
AddTestCase (
ShimTests,
"Locking a variable that's already locked by another policy should work",
"LockAfterPolicy",
LockingALockedVariableShouldSucceed,
LibInitMocked,
LibCleanup,
NULL
);
AddTestCase (
ShimTests,
"Locking a variable that already has an unlocked policy should fail",
"LockAfterUnlockedPolicy",
LockingAnUnlockedVariableShouldFail,
LibInitMocked,
LibCleanup,
NULL
);
AddTestCase (
ShimTests,
"Locking a variable that already has an locked policy should succeed",
"LockAfterLockedPolicyMatchingData",
LockingALockedVariableWithMatchingDataShouldSucceed,
LibInitMocked,
LibCleanup,
NULL
);
AddTestCase (
ShimTests,
"Locking a variable that already has an locked policy with matching data should succeed",
"LockAfterLockedPolicyNonMatchingData",
LockingALockedVariableWithNonMatchingDataShouldFail,
LibInitMocked,
LibCleanup,
NULL
);
AddTestCase (
ShimTests,
"Adding a policy for a variable that has previously been locked should always fail",
"SetPolicyAfterLock",
SettingPolicyForALockedVariableShouldFail,
LibInitMocked,
LibCleanup,
NULL
);
//
// Execute the tests.
//
Status = RunAllTestSuites (Framework);
EXIT:
if (Framework != NULL) {
FreeUnitTestFramework (Framework);
}
return;
}
///
/// Avoid ECC error for function name that starts with lower case letter
///
#define Main main
/**
Standard POSIX C entry point for host based unit test execution.
@param[in] Argc Number of arguments
@param[in] Argv Array of pointers to arguments
@retval 0 Success
@retval other Error
**/
INT32
Main (
IN INT32 Argc,
IN CHAR8 *Argv[]
)
{
UnitTestMain ();
return 0;
}
|