1 | /** @file
|
---|
2 | Host based unit test for PxeBcDhcp6.c.
|
---|
3 |
|
---|
4 | Copyright (c) Microsoft Corporation
|
---|
5 | SPDX-License-Identifier: BSD-2-Clause-Patent
|
---|
6 | **/
|
---|
7 | #include <Library/GoogleTestLib.h>
|
---|
8 | #include <GoogleTest/Library/MockUefiLib.h>
|
---|
9 | #include <GoogleTest/Library/MockUefiRuntimeServicesTableLib.h>
|
---|
10 | #include <GoogleTest/Library/MockUefiBootServicesTableLib.h>
|
---|
11 | #include <GoogleTest/Protocol/MockRng.h>
|
---|
12 |
|
---|
13 | extern "C" {
|
---|
14 | #include <Uefi.h>
|
---|
15 | #include <Library/BaseLib.h>
|
---|
16 | #include <Library/DebugLib.h>
|
---|
17 | #include "../PxeBcImpl.h"
|
---|
18 | #include "../PxeBcDhcp6.h"
|
---|
19 | #include "PxeBcDhcp6GoogleTest.h"
|
---|
20 | }
|
---|
21 |
|
---|
22 | ///////////////////////////////////////////////////////////////////////////////
|
---|
23 | // Definitions
|
---|
24 | ///////////////////////////////////////////////////////////////////////////////
|
---|
25 |
|
---|
26 | #define PACKET_SIZE (1500)
|
---|
27 | #define REQUEST_OPTION_LENGTH (120)
|
---|
28 |
|
---|
29 | typedef struct {
|
---|
30 | UINT16 OptionCode; // The option code for DHCP6_OPT_SERVER_ID (e.g., 0x03)
|
---|
31 | UINT16 OptionLen; // The length of the option (e.g., 16 bytes)
|
---|
32 | UINT8 ServerId[16]; // The 16-byte DHCPv6 Server Identifier
|
---|
33 | } DHCP6_OPTION_SERVER_ID;
|
---|
34 |
|
---|
35 | ///////////////////////////////////////////////////////////////////////////////
|
---|
36 | /// Symbol Definitions
|
---|
37 | ///////////////////////////////////////////////////////////////////////////////
|
---|
38 |
|
---|
39 | EFI_STATUS
|
---|
40 | MockUdpWrite (
|
---|
41 | IN EFI_PXE_BASE_CODE_PROTOCOL *This,
|
---|
42 | IN UINT16 OpFlags,
|
---|
43 | IN EFI_IP_ADDRESS *DestIp,
|
---|
44 | IN EFI_PXE_BASE_CODE_UDP_PORT *DestPort,
|
---|
45 | IN EFI_IP_ADDRESS *GatewayIp OPTIONAL,
|
---|
46 | IN EFI_IP_ADDRESS *SrcIp OPTIONAL,
|
---|
47 | IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
|
---|
48 | IN UINTN *HeaderSize OPTIONAL,
|
---|
49 | IN VOID *HeaderPtr OPTIONAL,
|
---|
50 | IN UINTN *BufferSize,
|
---|
51 | IN VOID *BufferPtr
|
---|
52 | )
|
---|
53 | {
|
---|
54 | return EFI_SUCCESS;
|
---|
55 | }
|
---|
56 |
|
---|
57 | EFI_STATUS
|
---|
58 | MockUdpRead (
|
---|
59 | IN EFI_PXE_BASE_CODE_PROTOCOL *This,
|
---|
60 | IN UINT16 OpFlags,
|
---|
61 | IN OUT EFI_IP_ADDRESS *DestIp OPTIONAL,
|
---|
62 | IN OUT EFI_PXE_BASE_CODE_UDP_PORT *DestPort OPTIONAL,
|
---|
63 | IN OUT EFI_IP_ADDRESS *SrcIp OPTIONAL,
|
---|
64 | IN OUT EFI_PXE_BASE_CODE_UDP_PORT *SrcPort OPTIONAL,
|
---|
65 | IN UINTN *HeaderSize OPTIONAL,
|
---|
66 | IN VOID *HeaderPtr OPTIONAL,
|
---|
67 | IN OUT UINTN *BufferSize,
|
---|
68 | IN VOID *BufferPtr
|
---|
69 | )
|
---|
70 | {
|
---|
71 | return EFI_SUCCESS;
|
---|
72 | }
|
---|
73 |
|
---|
74 | EFI_STATUS
|
---|
75 | MockConfigure (
|
---|
76 | IN EFI_UDP6_PROTOCOL *This,
|
---|
77 | IN EFI_UDP6_CONFIG_DATA *UdpConfigData OPTIONAL
|
---|
78 | )
|
---|
79 | {
|
---|
80 | return EFI_SUCCESS;
|
---|
81 | }
|
---|
82 |
|
---|
83 | // Needed by PxeBcSupport
|
---|
84 | EFI_STATUS
|
---|
85 | PxeBcDns6 (
|
---|
86 | IN PXEBC_PRIVATE_DATA *Private,
|
---|
87 | IN CHAR16 *HostName,
|
---|
88 | OUT EFI_IPv6_ADDRESS *IpAddress
|
---|
89 | )
|
---|
90 | {
|
---|
91 | return EFI_SUCCESS;
|
---|
92 | }
|
---|
93 |
|
---|
94 | UINT32
|
---|
95 | PxeBcBuildDhcp6Options (
|
---|
96 | IN PXEBC_PRIVATE_DATA *Private,
|
---|
97 | OUT EFI_DHCP6_PACKET_OPTION **OptList,
|
---|
98 | IN UINT8 *Buffer
|
---|
99 | )
|
---|
100 | {
|
---|
101 | return EFI_SUCCESS;
|
---|
102 | }
|
---|
103 |
|
---|
104 | EFI_STATUS
|
---|
105 | EFIAPI
|
---|
106 | QueueDpc (
|
---|
107 | IN EFI_TPL DpcTpl,
|
---|
108 | IN EFI_DPC_PROCEDURE DpcProcedure,
|
---|
109 | IN VOID *DpcContext OPTIONAL
|
---|
110 | )
|
---|
111 | {
|
---|
112 | return EFI_SUCCESS;
|
---|
113 | }
|
---|
114 |
|
---|
115 | ///////////////////////////////////////////////////////////////////////////////
|
---|
116 | // PxeBcHandleDhcp6OfferTest Tests
|
---|
117 | ///////////////////////////////////////////////////////////////////////////////
|
---|
118 |
|
---|
119 | class PxeBcHandleDhcp6OfferTest : public ::testing::Test {
|
---|
120 | public:
|
---|
121 | PXEBC_PRIVATE_DATA Private = { 0 };
|
---|
122 | EFI_UDP6_PROTOCOL Udp6Read;
|
---|
123 | EFI_PXE_BASE_CODE_MODE Mode = { 0 };
|
---|
124 |
|
---|
125 | protected:
|
---|
126 | // Add any setup code if needed
|
---|
127 | virtual void
|
---|
128 | SetUp (
|
---|
129 | )
|
---|
130 | {
|
---|
131 | Private.Dhcp6Request = (EFI_DHCP6_PACKET *)AllocateZeroPool (PACKET_SIZE);
|
---|
132 |
|
---|
133 | // Need to setup the EFI_PXE_BASE_CODE_PROTOCOL
|
---|
134 | // The function under test really only needs the following:
|
---|
135 | // UdpWrite
|
---|
136 | // UdpRead
|
---|
137 |
|
---|
138 | Private.PxeBc.UdpWrite = (EFI_PXE_BASE_CODE_UDP_WRITE)MockUdpWrite;
|
---|
139 | Private.PxeBc.UdpRead = (EFI_PXE_BASE_CODE_UDP_READ)MockUdpRead;
|
---|
140 |
|
---|
141 | // Need to setup EFI_UDP6_PROTOCOL
|
---|
142 | // The function under test really only needs the following:
|
---|
143 | // Configure
|
---|
144 |
|
---|
145 | Udp6Read.Configure = (EFI_UDP6_CONFIGURE)MockConfigure;
|
---|
146 | Private.Udp6Read = &Udp6Read;
|
---|
147 |
|
---|
148 | // Need to setup the EFI_PXE_BASE_CODE_MODE
|
---|
149 | Private.PxeBc.Mode = &Mode;
|
---|
150 |
|
---|
151 | // for this test it doesn't really matter what the Dhcpv6 ack is set to
|
---|
152 | }
|
---|
153 |
|
---|
154 | // Add any cleanup code if needed
|
---|
155 | virtual void
|
---|
156 | TearDown (
|
---|
157 | )
|
---|
158 | {
|
---|
159 | if (Private.Dhcp6Request != NULL) {
|
---|
160 | FreePool (Private.Dhcp6Request);
|
---|
161 | }
|
---|
162 |
|
---|
163 | // Clean up any resources or variables
|
---|
164 | }
|
---|
165 | };
|
---|
166 |
|
---|
167 | // Note:
|
---|
168 | // Testing PxeBcHandleDhcp6Offer() is difficult because it depends on a
|
---|
169 | // properly setup Private structure. Attempting to properly test this function
|
---|
170 | // without a significant refactor is a fools errand. Instead, we will test
|
---|
171 | // that we can prevent an overflow in the function.
|
---|
172 | TEST_F (PxeBcHandleDhcp6OfferTest, BasicUsageTest) {
|
---|
173 | PXEBC_DHCP6_PACKET_CACHE *Cache6 = NULL;
|
---|
174 | EFI_DHCP6_PACKET_OPTION Option = { 0 };
|
---|
175 |
|
---|
176 | Private.SelectIndex = 1; // SelectIndex is 1-based
|
---|
177 | Cache6 = &Private.OfferBuffer[Private.SelectIndex - 1].Dhcp6;
|
---|
178 |
|
---|
179 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER] = &Option;
|
---|
180 | // Setup the DHCPv6 offer packet
|
---|
181 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpCode = DHCP6_OPT_SERVER_ID;
|
---|
182 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpLen = NTOHS (1337);
|
---|
183 |
|
---|
184 | ASSERT_EQ (PxeBcHandleDhcp6Offer (&(PxeBcHandleDhcp6OfferTest::Private)), EFI_DEVICE_ERROR);
|
---|
185 | }
|
---|
186 |
|
---|
187 | ///////////////////////////////////////////////////////////////////////////////
|
---|
188 | // PxeBcCacheDnsServerAddresses Tests
|
---|
189 | ///////////////////////////////////////////////////////////////////////////////
|
---|
190 |
|
---|
191 | class PxeBcCacheDnsServerAddressesTest : public ::testing::Test {
|
---|
192 | public:
|
---|
193 | PXEBC_PRIVATE_DATA Private = { 0 };
|
---|
194 |
|
---|
195 | protected:
|
---|
196 | // Add any setup code if needed
|
---|
197 | virtual void
|
---|
198 | SetUp (
|
---|
199 | )
|
---|
200 | {
|
---|
201 | }
|
---|
202 |
|
---|
203 | // Add any cleanup code if needed
|
---|
204 | virtual void
|
---|
205 | TearDown (
|
---|
206 | )
|
---|
207 | {
|
---|
208 | }
|
---|
209 | };
|
---|
210 |
|
---|
211 | // Test Description
|
---|
212 | // Test that we cache the DNS server address from the DHCPv6 offer packet
|
---|
213 | TEST_F (PxeBcCacheDnsServerAddressesTest, BasicUsageTest) {
|
---|
214 | UINT8 SearchPattern[16] = { 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF, 0xDE, 0xAD, 0xBE, 0xEF };
|
---|
215 | EFI_DHCP6_PACKET_OPTION *Option;
|
---|
216 | PXEBC_DHCP6_PACKET_CACHE *Cache6 = NULL;
|
---|
217 |
|
---|
218 | Option = (EFI_DHCP6_PACKET_OPTION *)AllocateZeroPool (sizeof (EFI_DHCP6_PACKET_OPTION) + sizeof (SearchPattern));
|
---|
219 | ASSERT_NE (Option, nullptr);
|
---|
220 |
|
---|
221 | Option->OpCode = DHCP6_OPT_SERVER_ID;
|
---|
222 | Option->OpLen = NTOHS (sizeof (SearchPattern));
|
---|
223 | CopyMem (Option->Data, SearchPattern, sizeof (SearchPattern));
|
---|
224 |
|
---|
225 | Private.SelectIndex = 1; // SelectIndex is 1-based
|
---|
226 | Cache6 = &Private.OfferBuffer[Private.SelectIndex - 1].Dhcp6;
|
---|
227 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER] = Option;
|
---|
228 |
|
---|
229 | Private.DnsServer = nullptr;
|
---|
230 |
|
---|
231 | ASSERT_EQ (PxeBcCacheDnsServerAddresses (&(PxeBcCacheDnsServerAddressesTest::Private), Cache6), EFI_SUCCESS);
|
---|
232 | ASSERT_NE (Private.DnsServer, nullptr);
|
---|
233 | ASSERT_EQ (CompareMem (Private.DnsServer, SearchPattern, sizeof (SearchPattern)), 0);
|
---|
234 |
|
---|
235 | if (Private.DnsServer) {
|
---|
236 | FreePool (Private.DnsServer);
|
---|
237 | }
|
---|
238 |
|
---|
239 | if (Option) {
|
---|
240 | FreePool (Option);
|
---|
241 | }
|
---|
242 | }
|
---|
243 |
|
---|
244 | // Test Description
|
---|
245 | // Test that we can prevent an overflow in the function
|
---|
246 | TEST_F (PxeBcCacheDnsServerAddressesTest, AttemptOverflowTest) {
|
---|
247 | EFI_DHCP6_PACKET_OPTION Option = { 0 };
|
---|
248 | PXEBC_DHCP6_PACKET_CACHE *Cache6 = NULL;
|
---|
249 |
|
---|
250 | Private.SelectIndex = 1; // SelectIndex is 1-based
|
---|
251 | Cache6 = &Private.OfferBuffer[Private.SelectIndex - 1].Dhcp6;
|
---|
252 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER] = &Option;
|
---|
253 | // Setup the DHCPv6 offer packet
|
---|
254 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpCode = DHCP6_OPT_SERVER_ID;
|
---|
255 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpLen = NTOHS (1337);
|
---|
256 |
|
---|
257 | Private.DnsServer = NULL;
|
---|
258 |
|
---|
259 | ASSERT_EQ (PxeBcCacheDnsServerAddresses (&(PxeBcCacheDnsServerAddressesTest::Private), Cache6), EFI_DEVICE_ERROR);
|
---|
260 | ASSERT_EQ (Private.DnsServer, nullptr);
|
---|
261 |
|
---|
262 | if (Private.DnsServer) {
|
---|
263 | FreePool (Private.DnsServer);
|
---|
264 | }
|
---|
265 | }
|
---|
266 |
|
---|
267 | // Test Description
|
---|
268 | // Test that we can prevent an underflow in the function
|
---|
269 | TEST_F (PxeBcCacheDnsServerAddressesTest, AttemptUnderflowTest) {
|
---|
270 | EFI_DHCP6_PACKET_OPTION Option = { 0 };
|
---|
271 | PXEBC_DHCP6_PACKET_CACHE *Cache6 = NULL;
|
---|
272 |
|
---|
273 | Private.SelectIndex = 1; // SelectIndex is 1-based
|
---|
274 | Cache6 = &Private.OfferBuffer[Private.SelectIndex - 1].Dhcp6;
|
---|
275 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER] = &Option;
|
---|
276 | // Setup the DHCPv6 offer packet
|
---|
277 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpCode = DHCP6_OPT_SERVER_ID;
|
---|
278 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpLen = NTOHS (2);
|
---|
279 |
|
---|
280 | Private.DnsServer = NULL;
|
---|
281 |
|
---|
282 | ASSERT_EQ (PxeBcCacheDnsServerAddresses (&(PxeBcCacheDnsServerAddressesTest::Private), Cache6), EFI_DEVICE_ERROR);
|
---|
283 | ASSERT_EQ (Private.DnsServer, nullptr);
|
---|
284 |
|
---|
285 | if (Private.DnsServer) {
|
---|
286 | FreePool (Private.DnsServer);
|
---|
287 | }
|
---|
288 | }
|
---|
289 |
|
---|
290 | // Test Description
|
---|
291 | // Test that we can handle recursive dns (multiple dns entries)
|
---|
292 | TEST_F (PxeBcCacheDnsServerAddressesTest, MultipleDnsEntries) {
|
---|
293 | EFI_DHCP6_PACKET_OPTION Option = { 0 };
|
---|
294 | PXEBC_DHCP6_PACKET_CACHE *Cache6 = NULL;
|
---|
295 |
|
---|
296 | Private.SelectIndex = 1; // SelectIndex is 1-based
|
---|
297 | Cache6 = &Private.OfferBuffer[Private.SelectIndex - 1].Dhcp6;
|
---|
298 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER] = &Option;
|
---|
299 | // Setup the DHCPv6 offer packet
|
---|
300 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpCode = DHCP6_OPT_SERVER_ID;
|
---|
301 |
|
---|
302 | EFI_IPv6_ADDRESS addresses[2] = {
|
---|
303 | // 2001:db8:85a3::8a2e:370:7334
|
---|
304 | { 0x20, 0x01, 0x0d, 0xb8, 0x85, 0xa3, 0x00, 0x00, 0x00, 0x00, 0x8a, 0x2e, 0x03, 0x70, 0x73, 0x34 },
|
---|
305 | // fe80::d478:91c3:ecd7:4ff9
|
---|
306 | { 0xfe, 0x80, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xd4, 0x78, 0x91, 0xc3, 0xec, 0xd7, 0x4f, 0xf9 }
|
---|
307 | };
|
---|
308 |
|
---|
309 | CopyMem (Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->Data, &addresses, sizeof (addresses));
|
---|
310 |
|
---|
311 | Cache6->OptList[PXEBC_DHCP6_IDX_DNS_SERVER]->OpLen = NTOHS (sizeof (addresses));
|
---|
312 |
|
---|
313 | Private.DnsServer = NULL;
|
---|
314 |
|
---|
315 | ASSERT_EQ (PxeBcCacheDnsServerAddresses (&(PxeBcCacheDnsServerAddressesTest::Private), Cache6), EFI_SUCCESS);
|
---|
316 |
|
---|
317 | ASSERT_NE (Private.DnsServer, nullptr);
|
---|
318 |
|
---|
319 | //
|
---|
320 | // This is expected to fail until DnsServer supports multiple DNS servers
|
---|
321 | //
|
---|
322 | // This is tracked in https://bugzilla.tianocore.org/show_bug.cgi?id=1886
|
---|
323 | //
|
---|
324 | // Disabling:
|
---|
325 | // ASSERT_EQ (CompareMem(Private.DnsServer, &addresses, sizeof(addresses)), 0);
|
---|
326 |
|
---|
327 | if (Private.DnsServer) {
|
---|
328 | FreePool (Private.DnsServer);
|
---|
329 | }
|
---|
330 | }
|
---|
331 |
|
---|
332 | ///////////////////////////////////////////////////////////////////////////////
|
---|
333 | // PxeBcRequestBootServiceTest Test Cases
|
---|
334 | ///////////////////////////////////////////////////////////////////////////////
|
---|
335 |
|
---|
336 | class PxeBcRequestBootServiceTest : public ::testing::Test {
|
---|
337 | public:
|
---|
338 | PXEBC_PRIVATE_DATA Private = { 0 };
|
---|
339 | EFI_UDP6_PROTOCOL Udp6Read;
|
---|
340 |
|
---|
341 | protected:
|
---|
342 | // Add any setup code if needed
|
---|
343 | virtual void
|
---|
344 | SetUp (
|
---|
345 | )
|
---|
346 | {
|
---|
347 | Private.Dhcp6Request = (EFI_DHCP6_PACKET *)AllocateZeroPool (PACKET_SIZE);
|
---|
348 |
|
---|
349 | // Need to setup the EFI_PXE_BASE_CODE_PROTOCOL
|
---|
350 | // The function under test really only needs the following:
|
---|
351 | // UdpWrite
|
---|
352 | // UdpRead
|
---|
353 |
|
---|
354 | Private.PxeBc.UdpWrite = (EFI_PXE_BASE_CODE_UDP_WRITE)MockUdpWrite;
|
---|
355 | Private.PxeBc.UdpRead = (EFI_PXE_BASE_CODE_UDP_READ)MockUdpRead;
|
---|
356 |
|
---|
357 | // Need to setup EFI_UDP6_PROTOCOL
|
---|
358 | // The function under test really only needs the following:
|
---|
359 | // Configure
|
---|
360 |
|
---|
361 | Udp6Read.Configure = (EFI_UDP6_CONFIGURE)MockConfigure;
|
---|
362 | Private.Udp6Read = &Udp6Read;
|
---|
363 | }
|
---|
364 |
|
---|
365 | // Add any cleanup code if needed
|
---|
366 | virtual void
|
---|
367 | TearDown (
|
---|
368 | )
|
---|
369 | {
|
---|
370 | if (Private.Dhcp6Request != NULL) {
|
---|
371 | FreePool (Private.Dhcp6Request);
|
---|
372 | }
|
---|
373 |
|
---|
374 | // Clean up any resources or variables
|
---|
375 | }
|
---|
376 | };
|
---|
377 |
|
---|
378 | TEST_F (PxeBcRequestBootServiceTest, ServerDiscoverBasicUsageTest) {
|
---|
379 | PxeBcRequestBootServiceTest::Private.OfferBuffer[0].Dhcp6.OfferType = PxeOfferTypeProxyBinl;
|
---|
380 |
|
---|
381 | DHCP6_OPTION_SERVER_ID Server = { 0 };
|
---|
382 |
|
---|
383 | Server.OptionCode = HTONS (DHCP6_OPT_SERVER_ID);
|
---|
384 | Server.OptionLen = HTONS (16); // valid length
|
---|
385 | UINT8 Index = 0;
|
---|
386 |
|
---|
387 | EFI_DHCP6_PACKET *Packet = (EFI_DHCP6_PACKET *)&Private.OfferBuffer[Index].Dhcp6.Packet.Offer;
|
---|
388 |
|
---|
389 | UINT8 *Cursor = (UINT8 *)(Packet->Dhcp6.Option);
|
---|
390 |
|
---|
391 | CopyMem (Cursor, &Server, sizeof (Server));
|
---|
392 | Cursor += sizeof (Server);
|
---|
393 |
|
---|
394 | // Update the packet length
|
---|
395 | Packet->Length = (UINT16)(Cursor - (UINT8 *)Packet);
|
---|
396 | Packet->Size = PACKET_SIZE;
|
---|
397 |
|
---|
398 | ASSERT_EQ (PxeBcRequestBootService (&(PxeBcRequestBootServiceTest::Private), Index), EFI_SUCCESS);
|
---|
399 | }
|
---|
400 |
|
---|
401 | TEST_F (PxeBcRequestBootServiceTest, AttemptDiscoverOverFlowExpectFailure) {
|
---|
402 | PxeBcRequestBootServiceTest::Private.OfferBuffer[0].Dhcp6.OfferType = PxeOfferTypeProxyBinl;
|
---|
403 |
|
---|
404 | DHCP6_OPTION_SERVER_ID Server = { 0 };
|
---|
405 |
|
---|
406 | Server.OptionCode = HTONS (DHCP6_OPT_SERVER_ID);
|
---|
407 | Server.OptionLen = HTONS (1500); // This length would overflow without a check
|
---|
408 | UINT8 Index = 0;
|
---|
409 |
|
---|
410 | EFI_DHCP6_PACKET *Packet = (EFI_DHCP6_PACKET *)&Private.OfferBuffer[Index].Dhcp6.Packet.Offer;
|
---|
411 |
|
---|
412 | UINT8 *Cursor = (UINT8 *)(Packet->Dhcp6.Option);
|
---|
413 |
|
---|
414 | CopyMem (Cursor, &Server, sizeof (Server));
|
---|
415 | Cursor += sizeof (Server);
|
---|
416 |
|
---|
417 | // Update the packet length
|
---|
418 | Packet->Length = (UINT16)(Cursor - (UINT8 *)Packet);
|
---|
419 | Packet->Size = PACKET_SIZE;
|
---|
420 |
|
---|
421 | // This is going to be stopped by the duid overflow check
|
---|
422 | ASSERT_EQ (PxeBcRequestBootService (&(PxeBcRequestBootServiceTest::Private), Index), EFI_INVALID_PARAMETER);
|
---|
423 | }
|
---|
424 |
|
---|
425 | TEST_F (PxeBcRequestBootServiceTest, RequestBasicUsageTest) {
|
---|
426 | EFI_DHCP6_PACKET_OPTION RequestOpt = { 0 }; // the data section doesn't really matter
|
---|
427 |
|
---|
428 | RequestOpt.OpCode = HTONS (0x1337);
|
---|
429 | RequestOpt.OpLen = 0; // valid length
|
---|
430 |
|
---|
431 | UINT8 Index = 0;
|
---|
432 |
|
---|
433 | EFI_DHCP6_PACKET *Packet = (EFI_DHCP6_PACKET *)&Private.Dhcp6Request[Index];
|
---|
434 |
|
---|
435 | UINT8 *Cursor = (UINT8 *)(Packet->Dhcp6.Option);
|
---|
436 |
|
---|
437 | CopyMem (Cursor, &RequestOpt, sizeof (RequestOpt));
|
---|
438 | Cursor += sizeof (RequestOpt);
|
---|
439 |
|
---|
440 | // Update the packet length
|
---|
441 | Packet->Length = (UINT16)(Cursor - (UINT8 *)Packet);
|
---|
442 | Packet->Size = PACKET_SIZE;
|
---|
443 |
|
---|
444 | ASSERT_EQ (PxeBcRequestBootService (&(PxeBcRequestBootServiceTest::Private), Index), EFI_SUCCESS);
|
---|
445 | }
|
---|
446 |
|
---|
447 | TEST_F (PxeBcRequestBootServiceTest, AttemptRequestOverFlowExpectFailure) {
|
---|
448 | EFI_DHCP6_PACKET_OPTION RequestOpt = { 0 }; // the data section doesn't really matter
|
---|
449 |
|
---|
450 | RequestOpt.OpCode = HTONS (0x1337);
|
---|
451 | RequestOpt.OpLen = 1500; // this length would overflow without a check
|
---|
452 |
|
---|
453 | UINT8 Index = 0;
|
---|
454 |
|
---|
455 | EFI_DHCP6_PACKET *Packet = (EFI_DHCP6_PACKET *)&Private.Dhcp6Request[Index];
|
---|
456 |
|
---|
457 | UINT8 *Cursor = (UINT8 *)(Packet->Dhcp6.Option);
|
---|
458 |
|
---|
459 | CopyMem (Cursor, &RequestOpt, sizeof (RequestOpt));
|
---|
460 | Cursor += sizeof (RequestOpt);
|
---|
461 |
|
---|
462 | // Update the packet length
|
---|
463 | Packet->Length = (UINT16)(Cursor - (UINT8 *)Packet);
|
---|
464 | Packet->Size = PACKET_SIZE;
|
---|
465 |
|
---|
466 | ASSERT_EQ (PxeBcRequestBootService (&(PxeBcRequestBootServiceTest::Private), Index), EFI_OUT_OF_RESOURCES);
|
---|
467 | }
|
---|
468 |
|
---|
469 | ///////////////////////////////////////////////////////////////////////////////
|
---|
470 | // PxeBcDhcp6Discover Test
|
---|
471 | ///////////////////////////////////////////////////////////////////////////////
|
---|
472 |
|
---|
473 | class PxeBcDhcp6DiscoverTest : public ::testing::Test {
|
---|
474 | public:
|
---|
475 | PXEBC_PRIVATE_DATA Private = { 0 };
|
---|
476 | // create a mock md5 hash
|
---|
477 | UINT8 Md5Hash[16] = { 0 };
|
---|
478 |
|
---|
479 | EFI_UDP6_PROTOCOL Udp6Read;
|
---|
480 |
|
---|
481 | protected:
|
---|
482 | MockUefiRuntimeServicesTableLib RtServicesMock;
|
---|
483 | MockUefiBootServicesTableLib BsMock;
|
---|
484 | MockRng RngMock;
|
---|
485 |
|
---|
486 | // Add any setup code if needed
|
---|
487 | virtual void
|
---|
488 | SetUp (
|
---|
489 | )
|
---|
490 | {
|
---|
491 | Private.Dhcp6Request = (EFI_DHCP6_PACKET *)AllocateZeroPool (PACKET_SIZE);
|
---|
492 |
|
---|
493 | // Need to setup the EFI_PXE_BASE_CODE_PROTOCOL
|
---|
494 | // The function under test really only needs the following:
|
---|
495 | // UdpWrite
|
---|
496 | // UdpRead
|
---|
497 |
|
---|
498 | Private.PxeBc.UdpWrite = (EFI_PXE_BASE_CODE_UDP_WRITE)MockUdpWrite;
|
---|
499 | Private.PxeBc.UdpRead = (EFI_PXE_BASE_CODE_UDP_READ)MockUdpRead;
|
---|
500 |
|
---|
501 | // Need to setup EFI_UDP6_PROTOCOL
|
---|
502 | // The function under test really only needs the following:
|
---|
503 | // Configure
|
---|
504 |
|
---|
505 | Udp6Read.Configure = (EFI_UDP6_CONFIGURE)MockConfigure;
|
---|
506 | Private.Udp6Read = &Udp6Read;
|
---|
507 | }
|
---|
508 |
|
---|
509 | // Add any cleanup code if needed
|
---|
510 | virtual void
|
---|
511 | TearDown (
|
---|
512 | )
|
---|
513 | {
|
---|
514 | if (Private.Dhcp6Request != NULL) {
|
---|
515 | FreePool (Private.Dhcp6Request);
|
---|
516 | }
|
---|
517 |
|
---|
518 | // Clean up any resources or variables
|
---|
519 | }
|
---|
520 | };
|
---|
521 |
|
---|
522 | // Test Description
|
---|
523 | // This will cause an overflow by an untrusted packet during the option parsing
|
---|
524 | TEST_F (PxeBcDhcp6DiscoverTest, BasicOverflowTest) {
|
---|
525 | EFI_IPv6_ADDRESS DestIp = { 0 };
|
---|
526 | EFI_DHCP6_PACKET_OPTION RequestOpt = { 0 }; // the data section doesn't really matter
|
---|
527 |
|
---|
528 | RequestOpt.OpCode = HTONS (0x1337);
|
---|
529 | RequestOpt.OpLen = HTONS (0xFFFF); // overflow
|
---|
530 |
|
---|
531 | UINT8 *Cursor = (UINT8 *)(Private.Dhcp6Request->Dhcp6.Option);
|
---|
532 |
|
---|
533 | CopyMem (Cursor, &RequestOpt, sizeof (RequestOpt));
|
---|
534 | Cursor += sizeof (RequestOpt);
|
---|
535 |
|
---|
536 | Private.Dhcp6Request->Length = (UINT16)(Cursor - (UINT8 *)Private.Dhcp6Request);
|
---|
537 |
|
---|
538 | EXPECT_CALL (BsMock, gBS_LocateProtocol)
|
---|
539 | .WillOnce (
|
---|
540 | ::testing::DoAll (
|
---|
541 | ::testing::SetArgPointee<2> (::testing::ByRef (gRngProtocol)),
|
---|
542 | ::testing::Return (EFI_SUCCESS)
|
---|
543 | )
|
---|
544 | );
|
---|
545 |
|
---|
546 | EXPECT_CALL (RngMock, GetRng)
|
---|
547 | .WillOnce (
|
---|
548 | ::testing::DoAll (
|
---|
549 | ::testing::SetArgPointee<3> (::testing::ByRef (Md5Hash[0])),
|
---|
550 | ::testing::Return (EFI_SUCCESS)
|
---|
551 | )
|
---|
552 | );
|
---|
553 |
|
---|
554 | ASSERT_EQ (
|
---|
555 | PxeBcDhcp6Discover (
|
---|
556 | &(PxeBcDhcp6DiscoverTest::Private),
|
---|
557 | 0,
|
---|
558 | NULL,
|
---|
559 | FALSE,
|
---|
560 | (EFI_IP_ADDRESS *)&DestIp
|
---|
561 | ),
|
---|
562 | EFI_OUT_OF_RESOURCES
|
---|
563 | );
|
---|
564 | }
|
---|
565 |
|
---|
566 | // Test Description
|
---|
567 | // This will test that we can handle a packet with a valid option length
|
---|
568 | TEST_F (PxeBcDhcp6DiscoverTest, BasicUsageTest) {
|
---|
569 | EFI_IPv6_ADDRESS DestIp = { 0 };
|
---|
570 | EFI_DHCP6_PACKET_OPTION RequestOpt = { 0 }; // the data section doesn't really matter
|
---|
571 |
|
---|
572 | RequestOpt.OpCode = HTONS (0x1337);
|
---|
573 | RequestOpt.OpLen = HTONS (0x30);
|
---|
574 |
|
---|
575 | UINT8 *Cursor = (UINT8 *)(Private.Dhcp6Request->Dhcp6.Option);
|
---|
576 |
|
---|
577 | CopyMem (Cursor, &RequestOpt, sizeof (RequestOpt));
|
---|
578 | Cursor += sizeof (RequestOpt);
|
---|
579 |
|
---|
580 | Private.Dhcp6Request->Length = (UINT16)(Cursor - (UINT8 *)Private.Dhcp6Request);
|
---|
581 |
|
---|
582 | EXPECT_CALL (BsMock, gBS_LocateProtocol)
|
---|
583 | .WillOnce (
|
---|
584 | ::testing::DoAll (
|
---|
585 | ::testing::SetArgPointee<2> (::testing::ByRef (gRngProtocol)),
|
---|
586 | ::testing::Return (EFI_SUCCESS)
|
---|
587 | )
|
---|
588 | );
|
---|
589 |
|
---|
590 | EXPECT_CALL (RngMock, GetRng)
|
---|
591 | .WillOnce (
|
---|
592 | ::testing::DoAll (
|
---|
593 | ::testing::SetArgPointee<3> (::testing::ByRef (Md5Hash[0])),
|
---|
594 | ::testing::Return (EFI_SUCCESS)
|
---|
595 | )
|
---|
596 | );
|
---|
597 |
|
---|
598 | ASSERT_EQ (
|
---|
599 | PxeBcDhcp6Discover (
|
---|
600 | &(PxeBcDhcp6DiscoverTest::Private),
|
---|
601 | 0,
|
---|
602 | NULL,
|
---|
603 | FALSE,
|
---|
604 | (EFI_IP_ADDRESS *)&DestIp
|
---|
605 | ),
|
---|
606 | EFI_SUCCESS
|
---|
607 | );
|
---|
608 | }
|
---|
609 |
|
---|
610 | TEST_F (PxeBcDhcp6DiscoverTest, MultipleRequestsAttemptOverflow) {
|
---|
611 | EFI_IPv6_ADDRESS DestIp = { 0 };
|
---|
612 | EFI_DHCP6_PACKET_OPTION RequestOpt = { 0 }; // the data section doesn't really matter
|
---|
613 |
|
---|
614 | RequestOpt.OpCode = HTONS (0x1337);
|
---|
615 | RequestOpt.OpLen = HTONS (REQUEST_OPTION_LENGTH); // this length would overflow without a check
|
---|
616 | UINT8 RequestOptBuffer[REQUEST_OPTION_LENGTH] = { 0 };
|
---|
617 |
|
---|
618 | // make sure we have enough space for 10 of these options
|
---|
619 | ASSERT_TRUE (REQUEST_OPTION_LENGTH * 10 <= PACKET_SIZE);
|
---|
620 |
|
---|
621 | UINT8 Index = 0;
|
---|
622 | EFI_DHCP6_PACKET *Packet = (EFI_DHCP6_PACKET *)&Private.Dhcp6Request[Index];
|
---|
623 | UINT8 *Cursor = (UINT8 *)(Packet->Dhcp6.Option);
|
---|
624 |
|
---|
625 | // let's add 10 of these options - this should overflow
|
---|
626 | for (UINT8 i = 0; i < 10; i++) {
|
---|
627 | CopyMem (Cursor, &RequestOpt, sizeof (RequestOpt));
|
---|
628 | Cursor += sizeof (RequestOpt) - 1;
|
---|
629 | CopyMem (Cursor, RequestOptBuffer, REQUEST_OPTION_LENGTH);
|
---|
630 | Cursor += REQUEST_OPTION_LENGTH;
|
---|
631 | }
|
---|
632 |
|
---|
633 | // Update the packet length
|
---|
634 | Packet->Length = (UINT16)(Cursor - (UINT8 *)Packet);
|
---|
635 | Packet->Size = PACKET_SIZE;
|
---|
636 |
|
---|
637 | // Make sure we're larger than the buffer we're trying to write into
|
---|
638 | ASSERT_TRUE (Packet->Length > sizeof (EFI_PXE_BASE_CODE_DHCPV6_PACKET));
|
---|
639 |
|
---|
640 | EXPECT_CALL (BsMock, gBS_LocateProtocol)
|
---|
641 | .WillOnce (
|
---|
642 | ::testing::DoAll (
|
---|
643 | ::testing::SetArgPointee<2> (::testing::ByRef (gRngProtocol)),
|
---|
644 | ::testing::Return (EFI_SUCCESS)
|
---|
645 | )
|
---|
646 | );
|
---|
647 |
|
---|
648 | EXPECT_CALL (RngMock, GetRng)
|
---|
649 | .WillOnce (
|
---|
650 | ::testing::DoAll (
|
---|
651 | ::testing::SetArgPointee<3> (::testing::ByRef (Md5Hash[0])),
|
---|
652 | ::testing::Return (EFI_SUCCESS)
|
---|
653 | )
|
---|
654 | );
|
---|
655 |
|
---|
656 | ASSERT_EQ (
|
---|
657 | PxeBcDhcp6Discover (
|
---|
658 | &(PxeBcDhcp6DiscoverTest::Private),
|
---|
659 | 0,
|
---|
660 | NULL,
|
---|
661 | FALSE,
|
---|
662 | (EFI_IP_ADDRESS *)&DestIp
|
---|
663 | ),
|
---|
664 | EFI_OUT_OF_RESOURCES
|
---|
665 | );
|
---|
666 | }
|
---|