package main

import (
	"testing"

	"github.com/soypat/lneto/ethernet"
	"github.com/soypat/lneto/ipv4"
	"github.com/soypat/lneto/tcp"
)

func TestChecksum(t *testing.T) {
	for _, epacket := range ethpackets {
		efrm, _ := ethernet.NewFrame(epacket)
		ifrm, _ := ipv4.NewFrame(efrm.Payload())
		ipPayload := ifrm.Payload()
		tfrm, _ := tcp.NewFrame(ipPayload)

		crc := tcpChecksum(ifrm.RawData(), len(ipPayload))
		wantCRC := tfrm.CRC()
		if crc != wantCRC {
			t.Fatalf("crc mismatch, got %x, want %x", crc, wantCRC)
		}
	}
}

var ethpackets = [][]byte{

	{
		0xc0, 0xff, 0xee, 0x00, 0xde, 0xad, 0x3a, 0xd1, 0x6d, 0x82, 0x6b, 0x1a, 0x08, 0x00, 0x45, 0x00,
		0x00, 0x3c, 0xe3, 0xc6, 0x40, 0x00, 0x40, 0x06, 0xc1, 0xa1, 0xc0, 0xa8, 0x0a, 0x01, 0xc0, 0xa8,
		0x0a, 0x02, 0xd5, 0x70, 0x00, 0x50, 0xc4, 0x10, 0x30, 0x49, 0x00, 0x00, 0x00, 0x00, 0xa0, 0x02,
		0xfa, 0xf0, 0x2c, 0x80, 0x00, 0x00, 0x02, 0x04, 0x05, 0xb4, 0x04, 0x02, 0x08, 0x0a, 0xe8, 0x22,
		0xd8, 0xfd, 0x00, 0x00, 0x00, 0x00, 0x01, 0x03, 0x03, 0x07,
	},
	{
		0xc0, 0xff, 0xee, 0x00, 0xde, 0xad, 0xc0, 0xff, 0xee, 0x00, 0xde, 0xad, 0x08, 0x00, 0x45, 0x00,
		0x00, 0x28, 0x00, 0x00, 0x40, 0x00, 0x40, 0x06, 0x70, 0x26, 0xc0, 0xa8, 0x0a, 0x02, 0x00, 0x00,
		0x00, 0x00, 0x00, 0x50, 0xa5, 0xb8, 0x00, 0x00, 0x00, 0x64, 0x2f, 0x46, 0x4c, 0xe5, 0x50, 0x12,
		0x08, 0x00, 0xBA, 0x90, 0x00, 0x00,
	},
}
