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

namespace Livt.Net.Tests

using Livt.Net

/**
 * Deterministic Ethernet provider with observable reads and injected failures.
 */
component CountingPacketData : IPacketData
{
	reads: int
	failureIndex: int
	ready: bool

	/**
	 * Starts a new test lifetime after callers invalidate their views.
	 */
	public fn Initialize(failureIndex: int)
	{
		this.failureIndex = failureIndex
		reads = 0
		ready = true
	}

	/**
	 * Simulates source unavailability.
	 */
	public fn Release()
	{
		ready = false
	}

	/**
	 * Returns the number of scheduled byte reads.
	 */
	public fn GetReadCount() int
	{
		return reads
	}

	/**
	 * Reports publication state.
	 */
	override fn IsReady() bool
	{
		return ready
	}

	/**
	 * Publishes a header and one payload byte.
	 */
	override fn GetAvailableLength() int
	{
		if (!ready) {
			return 0
		}

		return 15
	}

	/**
	 * Supplies IPv4 EtherType and fails exactly at the configured index.
	 */
	override fn TryRead(index: int, value: out byte) PacketDataResult
	{
		value = 0x00

		if (!ready) {
			return PacketDataResult.NotReady
		}

		if (index < 0 || index >= 15 || index == failureIndex) {
			return PacketDataResult.Invalid
		}

		reads = reads + 1

		if (index == 12) {
			value = 0x08
		}

		if (index == 14) {
			value = 0xAB
		}

		return PacketDataResult.Success
	}
}