summaryrefslogtreecommitdiffstats
path: root/MdePkg/Test/UnitTest/Library/DevicePathLib/TestDevicePathLib.c
blob: 687497278c95251cd67652bc72bdab0e7e316aa2 (plain)
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
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
/** @file
  UEFI OS based application for unit testing the DevicePathLib.

  Copyright (c) 2023, Intel Corporation. All rights reserved.<BR>
  SPDX-License-Identifier: BSD-2-Clause-Patent
**/

#include "TestDevicePathLib.h"

#define UNIT_TEST_NAME     "DevicePathLib Unit Test Application"
#define UNIT_TEST_VERSION  "0.1"

typedef struct {
  ACPI_HID_DEVICE_PATH        AcpiPath;
  PCI_DEVICE_PATH             PciPathRootPort;
  PCI_DEVICE_PATH             PciPathEndPoint;
  USB_DEVICE_PATH             UsbPath;
  EFI_DEVICE_PATH_PROTOCOL    End;
} TEST_COMPLEX_DEVICE_PATH;

GLOBAL_REMOVE_IF_UNREFERENCED TEST_COMPLEX_DEVICE_PATH  mComplexDevicePath = {
  { // ACPI device path with root bridge EISA_PNP_ID
    {
      ACPI_DEVICE_PATH,
      ACPI_DP,
      {
        (UINT8)(sizeof (ACPI_HID_DEVICE_PATH)),
        (UINT8)((sizeof (ACPI_HID_DEVICE_PATH)) >> 8)
      }
    },
    EISA_PNP_ID (0x0A03),
    0
  },
  { // PCI device path - root port (0x2:0x0)
    {
      HARDWARE_DEVICE_PATH,
      HW_PCI_DP,
      {
        (UINT8)(sizeof (PCI_DEVICE_PATH)),
        (UINT8)((sizeof (PCI_DEVICE_PATH)) >> 8)
      }
    },
    0x2,
    0x0
  },
  { // PCI device path - endpoint (0x0:0x0)
    {
      HARDWARE_DEVICE_PATH,
      HW_PCI_DP,
      {
        (UINT8)(sizeof (PCI_DEVICE_PATH)),
        (UINT8)((sizeof (PCI_DEVICE_PATH)) >> 8)
      }
    },
    0x0,
    0x0
  },
  { // USB interface
    {
      MESSAGING_DEVICE_PATH,
      MSG_USB_DP,
      {
        (UINT8)(sizeof (USB_DEVICE_PATH)),
        (UINT8)((sizeof (USB_CLASS_DEVICE_PATH)) >> 8)
      }
    },
    0,
    2
  },
  {
    END_DEVICE_PATH_TYPE,
    END_ENTIRE_DEVICE_PATH_SUBTYPE,
    {
      (UINT8)(sizeof (EFI_DEVICE_PATH_PROTOCOL)),
      (UINT8)((sizeof (EFI_DEVICE_PATH_PROTOCOL)) >> 8)
    }
  }
};

CONST GLOBAL_REMOVE_IF_UNREFERENCED CHAR16  *mComplexDevicePathString = L"PciRoot(0x0)/Pci(0x0,0x2)/Pci(0x0,0x0)/USB(0x0,0x2)";

CONST GLOBAL_REMOVE_IF_UNREFERENCED CHAR16  *mPciEndPointPathString = L"Pci(0x0, 0x0)";

typedef struct {
  ACPI_HID_DEVICE_PATH        AcpiPath;
  EFI_DEVICE_PATH_PROTOCOL    End;
} TEST_SIMPLE_DEVICE_PATH;

GLOBAL_REMOVE_IF_UNREFERENCED TEST_SIMPLE_DEVICE_PATH  mSimpleDevicePath = {
  { // ACPI device path with root bridge EISA_PNP_ID
    {
      ACPI_DEVICE_PATH,
      ACPI_DP,
      {
        (UINT8)(sizeof (ACPI_HID_DEVICE_PATH)),
        (UINT8)((sizeof (ACPI_HID_DEVICE_PATH)) >> 8)
      }
    },
    EISA_PNP_ID (0x0A03),
    0
  },
  {
    END_DEVICE_PATH_TYPE,
    END_ENTIRE_DEVICE_PATH_SUBTYPE,
    {
      (UINT8)(sizeof (EFI_DEVICE_PATH_PROTOCOL)),
      (UINT8)((sizeof (EFI_DEVICE_PATH_PROTOCOL)) >> 8)
    }
  }
};

GLOBAL_REMOVE_IF_UNREFERENCED TEST_SIMPLE_DEVICE_PATH  mInvalidSimpleDevicePath = {
  { // ACPI device path with root bridge EISA_PNP_ID
    {
      ACPI_DEVICE_PATH,
      ACPI_DP,
      {
        0,
        0
      }
    },
    EISA_PNP_ID (0x0A03),
    0
  },
  {
    END_DEVICE_PATH_TYPE,
    END_ENTIRE_DEVICE_PATH_SUBTYPE,
    {
      (UINT8)(sizeof (EFI_DEVICE_PATH_PROTOCOL)),
      (UINT8)((sizeof (EFI_DEVICE_PATH_PROTOCOL)) >> 8)
    }
  }
};

typedef struct {
  TEST_SIMPLE_DEVICE_PATH     *SimpleDevicePath;
  TEST_SIMPLE_DEVICE_PATH     *InvalidDevicePath;
  TEST_COMPLEX_DEVICE_PATH    *ComplexDevicePath;
} SIMPLE_TEST_SUITE_CONTEXT;

UNIT_TEST_STATUS
EFIAPI
TestIsDevicePathValid (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  BOOLEAN                    IsValid;
  SIMPLE_TEST_SUITE_CONTEXT  *TestContext;

  TestContext = (SIMPLE_TEST_SUITE_CONTEXT *)Context;

  IsValid = IsDevicePathValid ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->SimpleDevicePath, sizeof (TEST_SIMPLE_DEVICE_PATH));
  UT_ASSERT_TRUE (IsValid);

  IsValid = IsDevicePathValid ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->ComplexDevicePath, sizeof (TEST_COMPLEX_DEVICE_PATH));
  UT_ASSERT_TRUE (IsValid);

  IsValid = IsDevicePathValid ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->ComplexDevicePath, 0);
  UT_ASSERT_TRUE (IsValid);

  // Device path can't be NULL
  IsValid = IsDevicePathValid (NULL, 0);
  UT_ASSERT_FALSE (IsValid);

  // MaxSize can't be less then the size of the device path
  IsValid = IsDevicePathValid ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->SimpleDevicePath, sizeof (TEST_SIMPLE_DEVICE_PATH) - 1);
  UT_ASSERT_FALSE (IsValid);

  // If MaxSize != 0 it must be bigger then EFI_DEVICE_PATH_PROTOCOL
  IsValid = IsDevicePathValid ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->SimpleDevicePath, sizeof (EFI_DEVICE_PATH_PROTOCOL) - 1);
  UT_ASSERT_FALSE (IsValid);

  IsValid = IsDevicePathValid ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->InvalidDevicePath, 0);
  UT_ASSERT_FALSE (IsValid);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestDevicePathType (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  UINT8                      Type;
  SIMPLE_TEST_SUITE_CONTEXT  *TestContext;

  TestContext = (SIMPLE_TEST_SUITE_CONTEXT *)Context;

  // Test 2 types just in case the implementation is returning constant value
  // NOTE: passing NULL to this function causes NULL pointer dereference.
  Type = DevicePathType (&TestContext->ComplexDevicePath->AcpiPath);
  UT_ASSERT_EQUAL (Type, ACPI_DEVICE_PATH);

  Type = DevicePathType (&TestContext->ComplexDevicePath->PciPathRootPort);
  UT_ASSERT_EQUAL (Type, HARDWARE_DEVICE_PATH);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestDevicePathSubType (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  UINT8                      SubType;
  SIMPLE_TEST_SUITE_CONTEXT  *TestContext;

  TestContext = (SIMPLE_TEST_SUITE_CONTEXT *)Context;

  // Test 2 sub types just in case the implementation is returning constant value
  // NOTE: passing NULL to this function causes NULL pointer dereference.
  SubType = DevicePathSubType (&TestContext->ComplexDevicePath->AcpiPath);
  UT_ASSERT_EQUAL (SubType, ACPI_DP);

  SubType = DevicePathSubType (&TestContext->ComplexDevicePath->PciPathRootPort);
  UT_ASSERT_EQUAL (SubType, HW_PCI_DP);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestDevicePathNodeLength (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  UINTN                      Length;
  SIMPLE_TEST_SUITE_CONTEXT  *TestContext;

  TestContext = (SIMPLE_TEST_SUITE_CONTEXT *)Context;

  // Test 2 nodes just in case the implementation is returning constant value
  // NOTE: passing NULL to this function causes NULL pointer dereference.
  Length = DevicePathNodeLength (&TestContext->ComplexDevicePath->AcpiPath);
  UT_ASSERT_EQUAL (Length, sizeof (ACPI_HID_DEVICE_PATH));

  Length = DevicePathNodeLength (&TestContext->ComplexDevicePath->PciPathRootPort);
  UT_ASSERT_EQUAL (Length, sizeof (PCI_DEVICE_PATH));

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestNextDevicePathNode (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  VOID                       *Node;
  SIMPLE_TEST_SUITE_CONTEXT  *TestContext;

  TestContext = (SIMPLE_TEST_SUITE_CONTEXT *)Context;

  Node = &mComplexDevicePath;
  Node = NextDevicePathNode (Node);
  UT_ASSERT_MEM_EQUAL (Node, &TestContext->ComplexDevicePath->PciPathRootPort, DevicePathNodeLength (Node));

  Node = NextDevicePathNode (Node);
  UT_ASSERT_MEM_EQUAL (Node, &TestContext->ComplexDevicePath->PciPathEndPoint, DevicePathNodeLength (Node));

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestIsDevicePathEndType (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  BOOLEAN                    IsEndType;
  SIMPLE_TEST_SUITE_CONTEXT  *TestContext;

  TestContext = (SIMPLE_TEST_SUITE_CONTEXT *)Context;

  IsEndType = IsDevicePathEndType (&TestContext->ComplexDevicePath->PciPathRootPort);
  UT_ASSERT_FALSE (IsEndType);

  IsEndType = IsDevicePathEndType (&TestContext->ComplexDevicePath->End);
  UT_ASSERT_TRUE (IsEndType);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestIsDevicePathEnd (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  BOOLEAN                    IsEnd;
  SIMPLE_TEST_SUITE_CONTEXT  *TestContext;

  TestContext = (SIMPLE_TEST_SUITE_CONTEXT *)Context;

  IsEnd = IsDevicePathEnd (&TestContext->ComplexDevicePath->PciPathRootPort);
  UT_ASSERT_FALSE (IsEnd);

  IsEnd = IsDevicePathEnd (&TestContext->ComplexDevicePath->End);
  UT_ASSERT_TRUE (IsEnd);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestSetDevicePathNodeLength (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  EFI_DEVICE_PATH_PROTOCOL  DevPath;

  // NOTE: Node == NULL or NodeLength >= 0x10000 NodeLength < sizeof (EFI_DEVICE_PATH_PROTOCOL)
  // are all invalid parameters. However there are only ASSERTS added to catch them so there is no
  // way to test it.
  SetDevicePathNodeLength (&DevPath, sizeof (EFI_DEVICE_PATH_PROTOCOL));
  UT_ASSERT_EQUAL (DevicePathNodeLength (&DevPath), sizeof (EFI_DEVICE_PATH_PROTOCOL));

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestSetDevicePathEndNode (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  EFI_DEVICE_PATH_PROTOCOL  EndNode;

  SetDevicePathEndNode (&EndNode);
  UT_ASSERT_EQUAL (EndNode.Type, END_DEVICE_PATH_TYPE);
  UT_ASSERT_EQUAL (EndNode.SubType, END_ENTIRE_DEVICE_PATH_SUBTYPE);
  UT_ASSERT_EQUAL (DevicePathNodeLength (&EndNode), END_DEVICE_PATH_LENGTH);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestGetDevicePathSize (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  UINTN                      Size;
  SIMPLE_TEST_SUITE_CONTEXT  *TestContext;

  TestContext = (SIMPLE_TEST_SUITE_CONTEXT *)Context;

  Size = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->SimpleDevicePath);
  UT_ASSERT_EQUAL (Size, sizeof (TEST_SIMPLE_DEVICE_PATH));

  Size = GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->ComplexDevicePath);
  UT_ASSERT_EQUAL (Size, sizeof (TEST_COMPLEX_DEVICE_PATH));

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestDuplicateDevicePath (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  EFI_DEVICE_PATH_PROTOCOL   *Duplicate;
  SIMPLE_TEST_SUITE_CONTEXT  *TestContext;

  TestContext = (SIMPLE_TEST_SUITE_CONTEXT *)Context;

  Duplicate = DuplicateDevicePath ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->ComplexDevicePath);
  UT_ASSERT_EQUAL (GetDevicePathSize (Duplicate), GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->ComplexDevicePath));
  UT_ASSERT_MEM_EQUAL (Duplicate, TestContext->ComplexDevicePath, GetDevicePathSize ((EFI_DEVICE_PATH_PROTOCOL *)TestContext->ComplexDevicePath));
  FreePool (Duplicate);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestAppendDevicePath (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  EFI_DEVICE_PATH_PROTOCOL  *Appended;
  EFI_DEVICE_PATH_PROTOCOL  *NextNode;

  Appended = AppendDevicePath ((EFI_DEVICE_PATH_PROTOCOL *)&mSimpleDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&mComplexDevicePath);
  NextNode = NextDevicePathNode (Appended);
  UT_ASSERT_MEM_EQUAL (NextNode, &mSimpleDevicePath.AcpiPath, sizeof (ACPI_HID_DEVICE_PATH));
  FreePool (Appended);

  // If one of the paths is invalid result device path should be NULL
  Appended = AppendDevicePath ((EFI_DEVICE_PATH_PROTOCOL *)&mSimpleDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&mInvalidSimpleDevicePath);
  UT_ASSERT_EQUAL ((uintptr_t)Appended, (uintptr_t)NULL);

  Appended = AppendDevicePath (NULL, NULL);
  UT_ASSERT_EQUAL (Appended->Type, END_DEVICE_PATH_TYPE);
  UT_ASSERT_EQUAL (Appended->SubType, END_ENTIRE_DEVICE_PATH_SUBTYPE);
  UT_ASSERT_EQUAL (DevicePathNodeLength (Appended), END_DEVICE_PATH_LENGTH);
  FreePool (Appended);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestAppendDevicePathNode (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  EFI_DEVICE_PATH_PROTOCOL  *Appended;
  EFI_DEVICE_PATH_PROTOCOL  *NextNode;
  BOOLEAN                   IsValid;
  ACPI_HID_DEVICE_PATH      AcpiPath =
  {
    {
      ACPI_DEVICE_PATH,
      ACPI_DP,
      {
        (UINT8)(sizeof (ACPI_HID_DEVICE_PATH)),
        (UINT8)((sizeof (ACPI_HID_DEVICE_PATH)) >> 8)
      }
    },
    EISA_PNP_ID (0x0AAB),
    0
  };

  Appended = AppendDevicePathNode ((EFI_DEVICE_PATH_PROTOCOL *)&mSimpleDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&AcpiPath);
  NextNode = NextDevicePathNode (Appended);
  UT_ASSERT_MEM_EQUAL (NextNode, &AcpiPath, sizeof (ACPI_HID_DEVICE_PATH));
  FreePool (Appended);

  Appended = AppendDevicePathNode (NULL, (EFI_DEVICE_PATH_PROTOCOL *)&AcpiPath);
  UT_ASSERT_MEM_EQUAL (Appended, &AcpiPath, sizeof (ACPI_HID_DEVICE_PATH));
  IsValid = IsDevicePathValid (Appended, 0);
  UT_ASSERT_TRUE (IsValid);
  FreePool (Appended);

  Appended = AppendDevicePathNode (NULL, NULL);
  UT_ASSERT_EQUAL (Appended->Type, END_DEVICE_PATH_TYPE);
  UT_ASSERT_EQUAL (Appended->SubType, END_ENTIRE_DEVICE_PATH_SUBTYPE);
  UT_ASSERT_EQUAL (DevicePathNodeLength (Appended), END_DEVICE_PATH_LENGTH);
  FreePool (Appended);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestAppendDevicePathInstance (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  EFI_DEVICE_PATH_PROTOCOL  *Appended;
  EFI_DEVICE_PATH_PROTOCOL  *NextInstance;
  EFI_DEVICE_PATH_PROTOCOL  *NextInstanceRet;
  BOOLEAN                   IsMultiInstance;
  UINTN                     Size;

  Appended        = AppendDevicePathInstance ((EFI_DEVICE_PATH_PROTOCOL *)&mSimpleDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&mComplexDevicePath);
  IsMultiInstance = IsDevicePathMultiInstance (Appended);
  UT_ASSERT_TRUE (IsMultiInstance);
  UT_ASSERT_MEM_EQUAL (Appended, &mSimpleDevicePath, sizeof (ACPI_DEVICE_PATH));
  NextInstance    = Appended;
  NextInstanceRet = GetNextDevicePathInstance (&NextInstance, &Size);
  UT_ASSERT_MEM_EQUAL (NextInstance, &mComplexDevicePath, Size);
  FreePool (Appended);
  FreePool (NextInstanceRet);

  Appended = AppendDevicePathInstance (NULL, (EFI_DEVICE_PATH_PROTOCOL *)&mSimpleDevicePath);
  UT_ASSERT_MEM_EQUAL (Appended, &mSimpleDevicePath, sizeof (TEST_SIMPLE_DEVICE_PATH));
  FreePool (Appended);

  Appended = AppendDevicePathInstance (NULL, NULL);
  UT_ASSERT_EQUAL ((uintptr_t)Appended, (uintptr_t)NULL);
  FreePool (Appended);

  Appended = AppendDevicePathInstance ((EFI_DEVICE_PATH_PROTOCOL *)&mSimpleDevicePath, (EFI_DEVICE_PATH_PROTOCOL *)&mInvalidSimpleDevicePath);
  UT_ASSERT_EQUAL ((uintptr_t)Appended, (uintptr_t)NULL);
  FreePool (Appended);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestDevicePathFromHandle (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  EFI_HANDLE                Handle;
  EFI_DEVICE_PATH_PROTOCOL  *DevicePath;
  UINTN                     FakeHandle;

  Handle     = NULL;
  DevicePath = DevicePathFromHandle (Handle);
  UT_ASSERT_EQUAL ((uintptr_t)DevicePath, (uintptr_t)NULL);

  Handle     = (EFI_HANDLE)&FakeHandle;
  DevicePath = DevicePathFromHandle (Handle);
  UT_ASSERT_EQUAL ((uintptr_t)DevicePath, (uintptr_t)NULL);

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestCreateDeviceNode (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  EFI_DEVICE_PATH_PROTOCOL  *DevNode;

  DevNode = CreateDeviceNode (HARDWARE_DEVICE_PATH, HW_PCI_DP, sizeof (PCI_DEVICE_PATH));
  UT_ASSERT_EQUAL (DevNode->Type, HARDWARE_DEVICE_PATH);
  UT_ASSERT_EQUAL (DevNode->SubType, HW_PCI_DP);
  UT_ASSERT_EQUAL (DevicePathNodeLength (DevNode), sizeof (PCI_DEVICE_PATH));

  return UNIT_TEST_PASSED;
}

UNIT_TEST_STATUS
EFIAPI
TestFileDevicePath (
  IN UNIT_TEST_CONTEXT  Context
  )
{
  EFI_HANDLE            Handle;
  FILEPATH_DEVICE_PATH  *DevicePath;
  CONST CHAR16          *TestFilePath = L"FS0:/Boot/EFI/BootMgr.efi";

  Handle     = NULL;
  DevicePath = (FILEPATH_DEVICE_PATH *)FileDevicePath (Handle, TestFilePath);
  UT_ASSERT_NOT_NULL (DevicePath);
  UT_ASSERT_EQUAL (DevicePath->Header.Type, MEDIA_DEVICE_PATH);
  UT_ASSERT_EQUAL (DevicePath->Header.Type, MEDIA_FILEPATH_DP);
  UT_ASSERT_MEM_EQUAL (DevicePath->PathName, TestFilePath, StrSize (TestFilePath));

  return UNIT_TEST_PASSED;
}

/**

  Main fuction sets up the unit test environment

**/
EFI_STATUS
EFIAPI
UefiTestMain (
  VOID
  )
{
  EFI_STATUS                  Status;
  UNIT_TEST_FRAMEWORK_HANDLE  Framework;
  UNIT_TEST_SUITE_HANDLE      DevicePathSimpleTestSuite;
  UNIT_TEST_SUITE_HANDLE      DevicePathAppendTestSuite;
  UNIT_TEST_SUITE_HANDLE      DevicePathFileTestSuite;
  SIMPLE_TEST_SUITE_CONTEXT   SimpleTestContext;

  DEBUG ((DEBUG_INFO, "%a v%a\n", UNIT_TEST_NAME, UNIT_TEST_VERSION));

  Framework                 = NULL;
  DevicePathSimpleTestSuite = NULL;
  DevicePathAppendTestSuite = NULL;
  DevicePathFileTestSuite   = NULL;

  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;
  }

  Status = CreateUnitTestSuite (&DevicePathSimpleTestSuite, Framework, "Simple device path operations test suite", "Common.DevicePath.SimpleOps", NULL, NULL);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Failed to create simple device path test suite\n"));
    goto EXIT;
  }

  SimpleTestContext.SimpleDevicePath  = &mSimpleDevicePath;
  SimpleTestContext.InvalidDevicePath = &mInvalidSimpleDevicePath;
  SimpleTestContext.ComplexDevicePath = &mComplexDevicePath;

  AddTestCase (DevicePathSimpleTestSuite, "Test IsDevicePathValid", "TestIsDevicePathValid", TestIsDevicePathValid, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test DevicePathType", "TestDevicePathType", TestDevicePathType, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test DevicePathSubType", "TestDevicePathSubType", TestDevicePathSubType, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test DevicePathNodeLength", "TestDevicePathNodeLength", TestDevicePathNodeLength, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test NextDevicePathNode", "TestNextDevicePathNode", TestNextDevicePathNode, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test IsDevicePathEndType", "TestIsDevicePathEndType", TestIsDevicePathEndType, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test IsDevicePathEnd", "TestIsDevicePathEnd", TestIsDevicePathEnd, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test SetDevicePathNodeLength", "TestSetDevicePathNodeLength", TestSetDevicePathNodeLength, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test GetDevicePathSize", "TestGetDevicePathSize", TestGetDevicePathSize, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test CreateDeviceNode", "TestCreateDeviceNode", TestCreateDeviceNode, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathSimpleTestSuite, "Test SetDevicePathEndNode", "TestSetDevicePathEndNode", TestSetDevicePathEndNode, NULL, NULL, &SimpleTestContext);
  AddTestCase (DevicePathAppendTestSuite, "Test DuplicateDevicePath", "TestDuplicateDevicePath", TestDuplicateDevicePath, NULL, NULL, &SimpleTestContext);

  Status = CreateUnitTestSuite (&DevicePathAppendTestSuite, Framework, "Device path append operations test suite", "Common.DevicePath.Append", NULL, NULL);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Failed to create append device path test suite\n"));
    goto EXIT;
  }

  AddTestCase (DevicePathAppendTestSuite, "Test AppendDevicePath", "TestAppendDevicePath", TestAppendDevicePath, NULL, NULL, NULL);
  AddTestCase (DevicePathAppendTestSuite, "Test AppendDevicePathNode", "TestAppendDevicePathNode", TestAppendDevicePathNode, NULL, NULL, NULL);
  AddTestCase (DevicePathAppendTestSuite, "Test AppendDevicePathInstance", "TestAppendDevicePathInstance", TestAppendDevicePathInstance, NULL, NULL, NULL);

  Status = CreateDevicePathStringConversionsTestSuite (Framework);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Failed to create conversions test suite\n"));
    goto EXIT;
  }

  Status = CreateUnitTestSuite (&DevicePathFileTestSuite, Framework, "Device path file operations test suite", "Common.DevicePath.FileDevPath", NULL, NULL);
  if (EFI_ERROR (Status)) {
    DEBUG ((DEBUG_ERROR, "Failed to create device path file test suite\n"));
    goto EXIT;
  }

  AddTestCase (DevicePathFileTestSuite, "Test DevicePathFromHandle", "TestDevicePathFromHandle", TestDevicePathFromHandle, NULL, NULL, NULL);
  AddTestCase (DevicePathFileTestSuite, "Test FileDevicePath", "TestFileDevicePath", TestFileDevicePath, NULL, NULL, NULL);

  Status = RunAllTestSuites (Framework);

EXIT:
  if (Framework != NULL) {
    FreeUnitTestFramework (Framework);
  }

  return Status;
}

int
main (
  int   argc,
  char  *argv[]
  )
{
  return UefiTestMain ();
}