Livt packages

Livt.Net

Version 1.4.1

Ethernet, IPv4, TCP, UDP, ARP, TFTP, and Telnet networking components for Livt hardware.

Published files (163)

tests/Ipv4HeaderBuilderTest.lvt

namespace Livt.Net.Tests

using Livt.Net

/**
 * Checks the fixed IPv4 layout and caller-supplied addresses and checksum.
 */
@Test
component Ipv4HeaderBuilderTest
{
	/**
	 * Compares emitted header fields with explicit expected octets; no timing property is asserted.
	 */
	@Test
	fn BuildsFixedIpv4TcpResponseHeader()
	{
		var request: byte[128]
		var clientIp: byte[4]
		clientIp[0] = 0x0A
		clientIp[1] = 0x00
		clientIp[2] = 0x00
		clientIp[3] = 0x02
		var localIp: byte[4]
		localIp[0] = 0x0A
		localIp[1] = 0x00
		localIp[2] = 0x00
		localIp[3] = 0x01
		var totalLength: int = 40
		var identification: byte[2]
		identification[0] = 0x12
		identification[1] = 0x34

		request[26] = clientIp[0]
		request[27] = clientIp[1]
		request[28] = clientIp[2]
		request[29] = clientIp[3]

		var destinationIp: byte[4] = [request[26], request[27], request[28], request[29]]
		assert Ipv4HeaderBuilder.GetByte(0, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x45
		assert Ipv4HeaderBuilder.GetByte(1, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x00
		assert Ipv4HeaderBuilder.GetByte(2, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x00
		assert Ipv4HeaderBuilder.GetByte(3, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x28
		assert Ipv4HeaderBuilder.GetByte(4, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x12
		assert Ipv4HeaderBuilder.GetByte(5, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x34
		assert Ipv4HeaderBuilder.GetByte(6, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x40
		assert Ipv4HeaderBuilder.GetByte(7, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x00
		assert Ipv4HeaderBuilder.GetByte(8, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x40
		assert Ipv4HeaderBuilder.GetByte(9, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x06
	}

	/**
	 * Compares emitted header fields with explicit expected octets; no timing property is asserted.
	 */
	@Test
	fn EmitsSuppliedChecksum()
	{
		var request: byte[128]
		var localIp: byte[4]
		localIp[0] = 0x0A
		localIp[1] = 0x00
		localIp[2] = 0x00
		localIp[3] = 0x01
		var totalLength: int = 40
		var identification: byte[2]
		identification[0] = 0x12
		identification[1] = 0x34

		var destinationIp: byte[4] = [request[26], request[27], request[28], request[29]]
		assert Ipv4HeaderBuilder.GetByte(10, totalLength, identification, localIp, destinationIp, 0x06, 0xABCD) == 0xAB
		assert Ipv4HeaderBuilder.GetByte(11, totalLength, identification, localIp, destinationIp, 0x06, 0xABCD) == 0xCD
	}

	/**
	 * Compares emitted header fields with explicit expected octets; no timing property is asserted.
	 */
	@Test
	fn WritesConfiguredIpv4Addresses()
	{
		var request: byte[128]
		var clientIp: byte[4]
		clientIp[0] = 0x0A
		clientIp[1] = 0x00
		clientIp[2] = 0x00
		clientIp[3] = 0x02
		var localIp: byte[4]
		localIp[0] = 0x0A
		localIp[1] = 0x00
		localIp[2] = 0x00
		localIp[3] = 0x01
		var totalLength: int = 40
		var identification: byte[2]
		identification[0] = 0x12
		identification[1] = 0x34

		request[26] = clientIp[0]
		request[27] = clientIp[1]
		request[28] = clientIp[2]
		request[29] = clientIp[3]

		var destinationIp: byte[4] = [request[26], request[27], request[28], request[29]]
		assert Ipv4HeaderBuilder.GetByte(12, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x0A
		assert Ipv4HeaderBuilder.GetByte(15, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x01
		assert Ipv4HeaderBuilder.GetByte(16, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x0A
		assert Ipv4HeaderBuilder.GetByte(19, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x02
		assert Ipv4HeaderBuilder.GetByte(20, totalLength, identification, localIp, destinationIp, 0x06, 0) == 0x00
	}
}