// Copyright (c) 2023-2024 Canonical Ltd
//
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU Affero General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU Affero General Public License for more details.
//
// You should have received a copy of the GNU Affero General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.

package ethernet

import (
	"io"
	"net"
	"net/netip"
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestVLANUnmarshal(t *testing.T) {
	t.Parallel()

	testcases := map[string]struct {
		in  []byte
		out *VLAN
		err error
	}{
		"valid VLAN tag": {
			in: []byte{0x00, 0x02, 0x08, 0x06},
			out: &VLAN{
				Priority:     0,
				DropEligible: false,
				ID:           2,
				EthernetType: EthernetTypeARP,
			},
		},
		"invalid VLAN tag": {
			in:  []byte{0x00, 0x02},
			err: ErrMalformedVLAN,
		},
	}

	for name, tc := range testcases {
		tc := tc

		t.Run(name, func(t *testing.T) {
			t.Parallel()

			res := &VLAN{}

			err := res.UnmarshalBinary(tc.in)
			assert.ErrorIs(t, err, tc.err)

			if err == nil {
				assert.Equal(t, tc.out, res)
			}
		})
	}
}

func TestEthernetUnmarshal(t *testing.T) {
	t.Parallel()

	testcases := map[string]struct {
		in  []byte
		out *EthernetFrame
		err error
	}{
		"valid ethernet frame without VLAN": {
			in: []byte{
				0x24, 0x4b, 0xfe, 0xe1, 0xea, 0x26, 0x80, 0x61, 0x5f, 0x08, 0xfc, 0x16, 0x08, 0x06, 0x00, 0x01,
				0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x80, 0x61, 0x5f, 0x08, 0xfc, 0x16, 0xc0, 0xa8, 0x01, 0x6c,
				0x24, 0x4b, 0xfe, 0xe1, 0xea, 0x26, 0xc0, 0xa8, 0x01, 0x50,
			},
			out: &EthernetFrame{
				SrcMAC:       []byte{0x80, 0x61, 0x5f, 0x08, 0xfc, 0x16},
				DstMAC:       []byte{0x24, 0x4b, 0xfe, 0xe1, 0xea, 0x26},
				EthernetType: EthernetTypeARP,
				Payload: []byte{
					0x00, 0x01,
					0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x80, 0x61, 0x5f, 0x08, 0xfc, 0x16, 0xc0, 0xa8, 0x01, 0x6c,
					0x24, 0x4b, 0xfe, 0xe1, 0xea, 0x26, 0xc0, 0xa8, 0x01, 0x50,
				},
			},
		},
		"valid ethernet frame with VLAN": {
			in: []byte{
				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25, 0x81, 0x00, 0x00, 0x02,
				0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25,
				0xc0, 0xa8, 0x0a, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x0a, 0x19, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			},
			out: &EthernetFrame{
				SrcMAC:       []byte{0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25},
				DstMAC:       []byte{0xff, 0xff, 0xff, 0xff, 0xff, 0xff},
				EthernetType: EthernetTypeVLAN,
				Payload: []byte{
					0x00, 0x02,
					0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25,
					0xc0, 0xa8, 0x0a, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x0a, 0x19, 0x00, 0x00,
					0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
				},
			},
		},
		"no ethernet frame": {err: io.ErrUnexpectedEOF},
		"invalid ethernet frame": {
			in: []byte{
				0xff, 0xff, 0xff, 0xff, 0xff, 0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25, 0x00, 0x02,
				0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25,
				0xc0, 0xa8, 0x0a, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x0a, 0x19,
			},

			err: ErrMalformedFrame},
	}

	for name, tc := range testcases {
		tc := tc

		t.Run(name, func(t *testing.T) {
			t.Parallel()

			res := &EthernetFrame{}
			err := res.UnmarshalBinary(tc.in)
			assert.ErrorIs(t, err, tc.err)

			if err == nil {
				assert.Equal(t, tc.out, res)
			}
		})
	}
}

func TestEthernetFrameExtractVLAN(t *testing.T) {
	t.Parallel()

	testcases := map[string]struct {
		in  []byte
		out *VLAN
		err error
	}{
		"ethernet frame is VLAN": {
			in: []byte{
				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25, 0x81, 0x00, 0x00, 0x02,
				0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25,
				0xc0, 0xa8, 0x0a, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x0a, 0x19, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			},
			out: &VLAN{
				ID:           2,
				EthernetType: EthernetTypeARP,
			}},
		"ethernet frame is not VLAN": {
			in: []byte{
				0x24, 0x4b, 0xfe, 0xe1, 0xea, 0x26, 0x80, 0x61, 0x5f, 0x08, 0xfc, 0x16, 0x08, 0x06, 0x00, 0x01,
				0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x80, 0x61, 0x5f, 0x08, 0xfc, 0x16, 0xc0, 0xa8, 0x01, 0x6c,
				0x24, 0x4b, 0xfe, 0xe1, 0xea, 0x26, 0xc0, 0xa8, 0x01, 0x50,
			},
			err: ErrNotVLAN,
		},
	}

	for name, tc := range testcases {
		tc := tc

		t.Run(name, func(t *testing.T) {
			t.Parallel()

			eth := &EthernetFrame{}

			err := eth.UnmarshalBinary(tc.in)
			if err != nil {
				t.Fatal(err)
			}

			vlan, err := eth.ExtractVLAN()
			assert.ErrorIs(t, err, tc.err)

			if err == nil {
				assert.Equal(t, tc.out, vlan)
			}
		})
	}
}

func TestEthernetFrameExtractARP(t *testing.T) {
	t.Parallel()

	testcases := map[string]struct {
		in  []byte
		out *ARPPacket
		err error
	}{
		"ethernet frame is VLAN": {
			in: []byte{
				0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25, 0x81, 0x00, 0x00, 0x02,
				0x08, 0x06, 0x00, 0x01, 0x08, 0x00, 0x06, 0x04, 0x00, 0x01, 0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25,
				0xc0, 0xa8, 0x0a, 0x1a, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xc0, 0xa8, 0x0a, 0x19, 0x00, 0x00,
				0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
			},
			out: &ARPPacket{
				HardwareType:    HardwareTypeEthernet,
				ProtocolType:    ProtocolTypeIPv4,
				HardwareAddrLen: 6,
				ProtocolAddrLen: 4,
				OpCode:          OpRequest,
				SendHwAddr:      []byte{0x84, 0x39, 0xc0, 0x0b, 0x22, 0x25},
				SendIPAddr:      netip.MustParseAddr("192.168.10.26"),
				TgtHwAddr:       []byte{0x00, 0x00, 0x00, 0x00, 0x00, 0x00},
				TgtIPAddr:       netip.MustParseAddr("192.168.10.25"),
			},
		},
		"ethernet frame is not VLAN": {
			in: []byte{
				0x24, 0x4b, 0xfe, 0xe1, 0xea, 0x26, 0x80, 0x61, 0x5f, 0x08, 0xfc, 0x16, 0x08, 0x06, 0x00, 0x01,
				0x08, 0x00, 0x06, 0x04, 0x00, 0x02, 0x80, 0x61, 0x5f, 0x08, 0xfc, 0x16, 0xc0, 0xa8, 0x01, 0x6c,
				0x24, 0x4b, 0xfe, 0xe1, 0xea, 0x26, 0xc0, 0xa8, 0x01, 0x50,
			},
			out: &ARPPacket{
				HardwareType:    HardwareTypeEthernet,
				ProtocolType:    ProtocolTypeIPv4,
				HardwareAddrLen: 6,
				ProtocolAddrLen: 4,
				OpCode:          OpReply,
				SendHwAddr:      []byte{0x80, 0x61, 0x5f, 0x08, 0xfc, 0x16},
				SendIPAddr:      netip.MustParseAddr("192.168.1.108"),
				TgtHwAddr:       []byte{0x24, 0x4b, 0xfe, 0xe1, 0xea, 0x26},
				TgtIPAddr:       netip.MustParseAddr("192.168.1.80"),
			},
		},
	}

	for name, tc := range testcases {
		tc := tc

		t.Run(name, func(t *testing.T) {
			t.Parallel()

			eth := &EthernetFrame{}

			err := eth.UnmarshalBinary(tc.in)
			if err != nil {
				t.Fatal(err)
			}

			pkt, err := eth.ExtractARPPacket()
			assert.ErrorIs(t, err, tc.err)

			if err == nil {
				assert.Equal(t, tc.out, pkt)
			}
		})
	}
}

func TestEthernetFrameMarshalBinary(t *testing.T) {
	testcases := map[string]struct {
		in  *EthernetFrame
		out []byte
		err error
	}{
		"non-special type": {
			in: &EthernetFrame{
				DstMAC:       net.HardwareAddr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
				SrcMAC:       net.HardwareAddr{0xab, 0xcd, 0xef, 0x11, 0x22, 0x33},
				EthernetType: EthernetTypeIPv4,
			},
			out: []byte{
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xab, 0xcd, 0xef, 0x11, 0x22, 0x33,
				0x08, 0x00,
			},
		},
		"LLC type": {
			in: &EthernetFrame{
				DstMAC:       net.HardwareAddr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
				SrcMAC:       net.HardwareAddr{0xab, 0xcd, 0xef, 0x11, 0x22, 0x33},
				EthernetType: EthernetTypeLLC,
				Len:          10,
			},
			out: []byte{
				0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0xab, 0xcd, 0xef, 0x11, 0x22, 0x33,
				0x00, 0x0a,
			},
		},
		"LLC type no length": {
			in: &EthernetFrame{
				DstMAC:       net.HardwareAddr{0x11, 0x22, 0x33, 0x44, 0x55, 0x66},
				SrcMAC:       net.HardwareAddr{0xab, 0xcd, 0xef, 0x11, 0x22, 0x33},
				EthernetType: EthernetTypeLLC,
			},
			err: ErrMalformedFrame,
		},
	}

	for tname, tc := range testcases {
		t.Run(tname, func(t *testing.T) {
			out, err := tc.in.MarshalBinary()
			if err != nil {
				if tc.err != nil {
					assert.ErrorIs(t, err, tc.err)
					return
				}

				t.Fatal(err)
			}

			assert.Equal(t, out, tc.out)
		})
	}
}
