|
| 1 | +/** @file |
| 2 | + Tests for Ip6Option.c. |
| 3 | +
|
| 4 | + Copyright (c) Microsoft Corporation |
| 5 | + SPDX-License-Identifier: BSD-2-Clause-Patent |
| 6 | +**/ |
| 7 | +#include <gtest/gtest.h> |
| 8 | + |
| 9 | +extern "C" { |
| 10 | + #include <Uefi.h> |
| 11 | + #include <Library/BaseLib.h> |
| 12 | + #include <Library/DebugLib.h> |
| 13 | + #include "../Ip6Impl.h" |
| 14 | + #include "../Ip6Option.h" |
| 15 | +} |
| 16 | + |
| 17 | +///////////////////////////////////////////////////////////////////////// |
| 18 | +// Defines |
| 19 | +/////////////////////////////////////////////////////////////////////// |
| 20 | + |
| 21 | +#define IP6_PREFIX_INFO_OPTION_DATA_LEN 32 |
| 22 | +#define OPTION_HEADER_IP6_PREFIX_DATA_LEN (sizeof (IP6_OPTION_HEADER) + IP6_PREFIX_INFO_OPTION_DATA_LEN) |
| 23 | + |
| 24 | +//////////////////////////////////////////////////////////////////////// |
| 25 | +// Symbol Definitions |
| 26 | +// These functions are not directly under test - but required to compile |
| 27 | +//////////////////////////////////////////////////////////////////////// |
| 28 | +UINT32 mIp6Id; |
| 29 | + |
| 30 | +EFI_STATUS |
| 31 | +Ip6SendIcmpError ( |
| 32 | + IN IP6_SERVICE *IpSb, |
| 33 | + IN NET_BUF *Packet, |
| 34 | + IN EFI_IPv6_ADDRESS *SourceAddress OPTIONAL, |
| 35 | + IN EFI_IPv6_ADDRESS *DestinationAddress, |
| 36 | + IN UINT8 Type, |
| 37 | + IN UINT8 Code, |
| 38 | + IN UINT32 *Pointer OPTIONAL |
| 39 | + ) |
| 40 | +{ |
| 41 | + // .. |
| 42 | + return EFI_SUCCESS; |
| 43 | +} |
| 44 | + |
| 45 | +//////////////////////////////////////////////////////////////////////// |
| 46 | +// Ip6OptionValidation Tests |
| 47 | +//////////////////////////////////////////////////////////////////////// |
| 48 | + |
| 49 | +// Define a fixture for your tests if needed |
| 50 | +class Ip6OptionValidationTest : public ::testing::Test { |
| 51 | +protected: |
| 52 | + // Add any setup code if needed |
| 53 | + virtual void |
| 54 | + SetUp ( |
| 55 | + ) |
| 56 | + { |
| 57 | + // Initialize any resources or variables |
| 58 | + } |
| 59 | + |
| 60 | + // Add any cleanup code if needed |
| 61 | + virtual void |
| 62 | + TearDown ( |
| 63 | + ) |
| 64 | + { |
| 65 | + // Clean up any resources or variables |
| 66 | + } |
| 67 | +}; |
| 68 | + |
| 69 | +// Test Description: |
| 70 | +// Null option should return false |
| 71 | +TEST_F (Ip6OptionValidationTest, NullOptionShouldReturnFalse) { |
| 72 | + UINT8 *option = nullptr; |
| 73 | + UINT16 optionLen = 10; // Provide a suitable length |
| 74 | + |
| 75 | + EXPECT_FALSE (Ip6IsNDOptionValid (option, optionLen)); |
| 76 | +} |
| 77 | + |
| 78 | +// Test Description: |
| 79 | +// Truncated option should return false |
| 80 | +TEST_F (Ip6OptionValidationTest, TruncatedOptionShouldReturnFalse) { |
| 81 | + UINT8 option[] = { 0x01 }; // Provide a truncated option |
| 82 | + UINT16 optionLen = 1; |
| 83 | + |
| 84 | + EXPECT_FALSE (Ip6IsNDOptionValid (option, optionLen)); |
| 85 | +} |
| 86 | + |
| 87 | +// Test Description: |
| 88 | +// Ip6OptionPrefixInfo Option with zero length should return false |
| 89 | +TEST_F (Ip6OptionValidationTest, OptionWithZeroLengthShouldReturnFalse) { |
| 90 | + IP6_OPTION_HEADER optionHeader; |
| 91 | + |
| 92 | + optionHeader.Type = Ip6OptionPrefixInfo; |
| 93 | + optionHeader.Length = 0; |
| 94 | + UINT8 option[sizeof (IP6_OPTION_HEADER)]; |
| 95 | + |
| 96 | + CopyMem (option, &optionHeader, sizeof (IP6_OPTION_HEADER)); |
| 97 | + UINT16 optionLen = sizeof (IP6_OPTION_HEADER); |
| 98 | + |
| 99 | + EXPECT_FALSE (Ip6IsNDOptionValid (option, optionLen)); |
| 100 | +} |
| 101 | + |
| 102 | +// Test Description: |
| 103 | +// Ip6OptionPrefixInfo Option with valid length should return true |
| 104 | +TEST_F (Ip6OptionValidationTest, ValidPrefixInfoOptionShouldReturnTrue) { |
| 105 | + IP6_OPTION_HEADER optionHeader; |
| 106 | + |
| 107 | + optionHeader.Type = Ip6OptionPrefixInfo; |
| 108 | + optionHeader.Length = 4; // Length 4 * 8 = 32 |
| 109 | + UINT8 option[OPTION_HEADER_IP6_PREFIX_DATA_LEN]; |
| 110 | + |
| 111 | + CopyMem (option, &optionHeader, sizeof (IP6_OPTION_HEADER)); |
| 112 | + |
| 113 | + EXPECT_TRUE (Ip6IsNDOptionValid (option, IP6_PREFIX_INFO_OPTION_DATA_LEN)); |
| 114 | +} |
| 115 | + |
| 116 | +// Test Description: |
| 117 | +// Ip6OptionPrefixInfo Option with invalid length should return false |
| 118 | +TEST_F (Ip6OptionValidationTest, InvalidPrefixInfoOptionLengthShouldReturnFalse) { |
| 119 | + IP6_OPTION_HEADER optionHeader; |
| 120 | + |
| 121 | + optionHeader.Type = Ip6OptionPrefixInfo; |
| 122 | + optionHeader.Length = 3; // Length 3 * 8 = 24 (Invalid) |
| 123 | + UINT8 option[sizeof (IP6_OPTION_HEADER)]; |
| 124 | + |
| 125 | + CopyMem (option, &optionHeader, sizeof (IP6_OPTION_HEADER)); |
| 126 | + UINT16 optionLen = sizeof (IP6_OPTION_HEADER); |
| 127 | + |
| 128 | + EXPECT_FALSE (Ip6IsNDOptionValid (option, optionLen)); |
| 129 | +} |
0 commit comments