[ip6] new methods in Header and add unit test (#7561)

This commit adds new methods in `Ip6::Header` to get and set Traffic
Class, Flow fields. It also updates and renames some of the existing
methods. This commit also adds a unit test `test_ip6_header` to
validate the IPv6 header parsing and preparation.
This commit is contained in:
Abtin Keshavarzian
2022-04-06 13:47:48 -07:00
committed by GitHub
parent 47fd27be3e
commit fdf3c3034f
12 changed files with 303 additions and 132 deletions
-60
View File
@@ -28,15 +28,11 @@
#include <limits.h>
#include "common/encoding.hpp"
#include "net/ip4_address.hpp"
#include "net/ip6_address.hpp"
#include "net/ip6_headers.hpp"
#include "test_util.h"
using ot::Encoding::BigEndian::ReadUint16;
template <typename AddressType> struct TestVector
{
const char * mString;
@@ -407,61 +403,6 @@ void TestIp4Ip6Translation(void)
}
}
void TestIp6Header(void)
{
ot::Ip6::Header header;
ot::Ip6::Address source;
ot::Ip6::Address destination;
const uint8_t * headerBytes = reinterpret_cast<const uint8_t *>(&header);
enum : uint16_t
{
kPayloadLength = 650,
};
enum : uint8_t
{
kHopLimit = 0xd1,
};
memset(&header, 0, sizeof(header));
SuccessOrQuit(source.FromString("0102:0304:0506:0708:090a:0b0c:0d0e:0f12"), "Address::FromString() failed");
SuccessOrQuit(destination.FromString("1122:3344:5566::7788:99aa:bbcc:ddee:ff23"), "Address::FromString() failed");
header.Init();
VerifyOrQuit(header.IsVersion6(), "Header::Init() failed");
header.SetDscp(ot::Ip6::kDscpCs7);
header.SetPayloadLength(kPayloadLength);
header.SetNextHeader(ot::Ip6::kProtoUdp);
header.SetHopLimit(kHopLimit);
header.SetSource(source);
header.SetDestination(destination);
VerifyOrQuit(header.IsValid());
VerifyOrQuit(header.IsVersion6());
VerifyOrQuit(header.GetDscp() == ot::Ip6::kDscpCs7);
VerifyOrQuit(header.GetPayloadLength() == kPayloadLength);
VerifyOrQuit(header.GetNextHeader() == ot::Ip6::kProtoUdp);
VerifyOrQuit(header.GetHopLimit() == kHopLimit);
VerifyOrQuit(header.GetSource() == source);
VerifyOrQuit(header.GetDestination() == destination);
// Verify the offsets to different fields.
VerifyOrQuit(ReadUint16(headerBytes + ot::Ip6::Header::kPayloadLengthFieldOffset) == kPayloadLength,
"kPayloadLengthFieldOffset is incorrect");
VerifyOrQuit(headerBytes[ot::Ip6::Header::kNextHeaderFieldOffset] == ot::Ip6::kProtoUdp,
"kNextHeaderFieldOffset is incorrect");
VerifyOrQuit(headerBytes[ot::Ip6::Header::kHopLimitFieldOffset] == kHopLimit, "kHopLimitFieldOffset is incorrect");
VerifyOrQuit(memcmp(&headerBytes[ot::Ip6::Header::kSourceFieldOffset], &source, sizeof(source)) == 0,
"kSourceFieldOffset is incorrect");
VerifyOrQuit(memcmp(&headerBytes[ot::Ip6::Header::kDestinationFieldOffset], &destination, sizeof(destination)) == 0,
"kSourceFieldOffset is incorrect");
}
int main(void)
{
TestIp6AddressSetPrefix();
@@ -469,7 +410,6 @@ int main(void)
TestIp6AddressFromString();
TestIp6Prefix();
TestIp4Ip6Translation();
TestIp6Header();
printf("All tests passed\n");
return 0;
}