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/InternetChecksumTest.lvt

namespace Livt.Net.Tests

using Livt.Net

/**
 * Resets the owned accumulator for each independent stream scenario.
 */
@Test
component InternetChecksumTest
{
	cs: InternetChecksum

	/**
	 * Owns one accumulator; each scenario resets it before adding bytes.
	 */
	new()
	{
		this.cs = new InternetChecksum()
	}

	/**
	 * Checks that an empty accumulator exposes a zero unfolded sum.
	 */
	@Test
	fn ReturnsZeroAfterReset()
	{
		this.cs.Reset()

		assert this.cs.GetWordSum() == 0
	}

	/**
	 * Accumulates a word, resets, and verifies that the earlier contribution is discarded.
	 */
	@Test
	fn ResetClearsAccumulatedSum()
	{
		this.cs.Reset()
		this.cs.AddByte(0x3C)
		this.cs.AddByte(0x21)
		this.cs.Reset()

		assert this.cs.GetWordSum() == 0
	}

	/**
	 * Checks zero-padding of the low octet when the stream ends after one byte.
	 */
	@Test
	fn SingleByteIsTreatedAsHighByteOfWord()
	{
		// 0x3C alone -> word 0x3C00 = 15360
		this.cs.Reset()
		this.cs.AddByte(0x3C)

		assert this.cs.GetWordSum() == 15360
	}

	/**
	 * Checks network-order pairing of two successive octets.
	 */
	@Test
	fn TwoBytesFormOneWord()
	{
		// 0x3C, 0x21 -> word 0x3C21 = 60 * 256 + 33 = 15393
		this.cs.Reset()
		this.cs.AddByte(0x3C)
		this.cs.AddByte(0x21)

		assert this.cs.GetWordSum() == 15393
	}

	/**
	 * Checks that consecutive words contribute independently to the unfolded sum.
	 */
	@Test
	fn FourBytesFormTwoWords()
	{
		// 0x3C21 = 15393, 0x646F = 100 * 256 + 111 = 25711 -> sum = 41104
		this.cs.Reset()
		this.cs.AddByte(0x3C)
		this.cs.AddByte(0x21)
		this.cs.AddByte(0x64)
		this.cs.AddByte(0x6F)

		assert this.cs.GetWordSum() == 41104
	}

	/**
	 * Checks that a second stream cannot retain a carry or byte from the first stream.
	 */
	@Test
	fn ResetAllowsReuse()
	{
		this.cs.Reset()
		this.cs.AddByte(0xFF)
		this.cs.AddByte(0xFF)
		this.cs.Reset()
		this.cs.AddByte(0x3C)
		this.cs.AddByte(0x21)

		assert this.cs.GetWordSum() == 15393
	}

	/**
	 * Compares the finalized checksum to an independently inverted word sum.
	 */
	@Test
	fn GetChecksumFoldsAndInverts()
	{
		// 0x45, 0x00 -> word sum = 17664; invert: 0xFFFF - 17664 = 47871
		this.cs.Reset()
		this.cs.AddByte(0x45)
		this.cs.AddByte(0x00)

		assert this.cs.GetChecksum() == 47871
	}

	/**
	 * Feeds a known word into the stream and extracts octets from its actual accumulated sum.
	 */
	@Test
	fn ExtractsNetworkOrderOctets()
	{
		this.cs.Reset()
		this.cs.AddByte(0x3C)
		this.cs.AddByte(0x21)

		var sum = this.cs.GetWordSum()
		assert NetworkOrder.HighByte(sum) == 60
		assert NetworkOrder.LowByte(sum) == 33
	}
}