package radius

import (
	"bytes"
	"fmt"
	"net"
	"testing"
)

func TestPacket(ot *testing.T) {
	inBytes := []byte{0x1, 0xef, 0x0, 0x8e, 0x94, 0xb, 0x18, 0xaf, 0xa, 0xb6, 0x12, 0xf5, 0x24, 0x4, 0x94, 0xbe, 0x18, 0xbc, 0x7, 0x4d,
		0x1, 0x4, 0x72, 0x48, 0x3d, 0x6, 0x0, 0x0, 0x0, 0x5, 0x6, 0x6, 0x0, 0x0, 0x0, 0x2, 0x5, 0x6, 0x0, 0x0, 0x0, 0x10, 0x57,
		0xf, 0x69, 0x6f, 0x73, 0x5f, 0x78, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x70, 0x73, 0x6b, 0x4, 0x6, 0xa, 0x1, 0x1, 0x5, 0x1e,
		0xf, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x35, 0x5b, 0x35, 0x30, 0x30, 0x5d, 0x1f, 0x10, 0x31, 0x30, 0x2e, 0x31,
		0x2e, 0x31, 0x2e, 0x37, 0x30, 0x5b, 0x35, 0x30, 0x30, 0x5d, 0x20, 0xc, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x53, 0x77,
		0x61, 0x6e, 0x2, 0x12, 0x8d, 0x7, 0xc2, 0xc0, 0xa4, 0x2, 0x2c, 0xed, 0x8e, 0x69, 0x5b, 0x9e, 0x25, 0x77, 0xe5, 0xd, 0x50,
		0x12, 0x39, 0x5c, 0xaa, 0x3e, 0x6d, 0x23, 0xea, 0xb5, 0x86, 0xc1, 0x3, 0x2d, 0x9d, 0x5c, 0x19, 0xca}
	pac, err := DecodePacket("sEcReT", inBytes)
	fmt.Println(err)
	ok(err == nil)
	ok(pac.GetUsername() == "rH")
	ok(pac.GetAVP(NASPortType).Decode(pac).(NASPortTypeEnum) == NASPortTypeEnumVirtual)
	ok(pac.GetAVP(ServiceType).Decode(pac).(ServiceTypeEnum) == ServiceTypeEnumFramed)
	ok(pac.GetAVP(NASPort).Decode(pac).(uint32) == 16)
	ok(pac.GetAVP(NASPortId).Decode(pac).(string) == "ios_xauth_psk")
	ok(pac.GetNasIpAddress().Equal(net.ParseIP("10.1.1.5")))
	ok(pac.GetAVP(CalledStationId).Decode(pac).(string) == "10.1.1.5[500]")
	ok(pac.GetAVP(CallingStationId).Decode(pac).(string) == "10.1.1.70[500]")
	ok(pac.GetAVP(NASIdentifier).Decode(pac).(string) == "strongSwan")
	ok(bytes.Equal(pac.GetAVP(MessageAuthenticator).Decode(pac).([]byte), []byte{0x39, 0x5c, 0xaa, 0x3e, 0x6d, 0x23, 0xea, 0xb5,
		0x86, 0xc1, 0x3, 0x2d, 0x9d, 0x5c, 0x19, 0xca}))
	ok(pac.GetPassword() == "O6")
	ok(pac.GetPassword() == "O6")
	outBytes, err := pac.Encode()
	fmt.Println(err)
	ok(err == nil)
	ok(bytes.Equal(pac.GetAVP(MessageAuthenticator).Decode(pac).([]byte), []byte{0x39, 0x5c, 0xaa, 0x3e, 0x6d, 0x23, 0xea, 0xb5,
		0x86, 0xc1, 0x3, 0x2d, 0x9d, 0x5c, 0x19, 0xca}))

	pac2, err := DecodePacket("sEcReT", outBytes)
	fmt.Println(err)
	ok(err == nil)
	ok(pac2.GetUsername() == "rH")
	//ok(pac2.GetPassword()=="O6") TODO fix this.

	outPac := pac.Reply()
	outPac.Code = AccessAccept
	outBytes, err = outPac.Encode()
	ok(err == nil)
	expectBytes := []byte{0x2, 0xef, 0x0, 0x26, 0x54, 0x2a, 0x19, 0xab, 0xcb, 0x8e, 0xe6, 0x5f, 0x42, 0x44, 0x15, 0x3c,
		0x8a, 0xb8, 0x23, 0x51, 0x50, 0x12, 0x53, 0x4e, 0xee, 0x31, 0x43, 0x5c, 0x93, 0x2f, 0xfd, 0x11, 0x67, 0xb9,
		0xe8, 0xfb, 0x69, 0x60}

	ok(bytes.Equal(expectBytes, outBytes))
}

func TestPacket2(ot *testing.T) {
	inBytes := []byte{0x1, 0xe6, 0x0, 0xa5, 0xe6, 0x17, 0x46, 0x35, 0xe4, 0xba, 0x8f, 0xe5, 0x15, 0x90, 0x96, 0x33, 0xd0,
		0xb3, 0x61, 0x34, 0x1, 0x12, 0x63, 0x62, 0x46, 0x69, 0x42, 0x6f, 0x6f, 0x52, 0x6e, 0x5a, 0x73, 0x58, 0x6e,
		0x5a, 0x4a, 0x33, 0x3d, 0x6, 0x0, 0x0, 0x0, 0x5, 0x6, 0x6, 0x0, 0x0, 0x0, 0x2, 0x5, 0x6, 0x0, 0x0, 0x0, 0xa,
		0x57, 0x13, 0x69, 0x6f, 0x73, 0x5f, 0x69, 0x6b, 0x65, 0x76, 0x32, 0x5f, 0x65, 0x61, 0x70, 0x5f, 0x70, 0x73,
		0x6b, 0x4, 0x6, 0xa, 0x1, 0x1, 0x5, 0x1e, 0xf, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x35, 0x5b, 0x35,
		0x30, 0x30, 0x5d, 0x1f, 0x10, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x36, 0x36, 0x5b, 0x35, 0x30, 0x30,
		0x5d, 0x4f, 0x17, 0x2, 0x0, 0x0, 0x15, 0x1, 0x63, 0x62, 0x46, 0x69, 0x42, 0x6f, 0x6f, 0x52, 0x6e, 0x5a, 0x73,
		0x58, 0x6e, 0x5a, 0x4a, 0x33, 0x20, 0xc, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x53, 0x77, 0x61, 0x6e, 0x50,
		0x12, 0x48, 0x14, 0x43, 0xf2, 0x6, 0xa4, 0x8b, 0x7a, 0xd8, 0xc0, 0xdd, 0xe9, 0xb3, 0x3, 0x7d, 0x84}
	pac, err := DecodePacket("sEcReT", inBytes)
	ok(err == nil)
	ok(pac.GetUsername() == "cbFiBooRnZsXnZJ3")
	eap := pac.GetEAPMessage()
	ok(eap != nil)
	ok(eap.Code == EapCodeResponse)
	ok(eap.Identifier == 0)
	ok(eap.Type == EapTypeIdentity)
	ok(string(eap.Data) == "cbFiBooRnZsXnZJ3")
}
func ok(t bool) {
	if !t {
		panic("not ok")
	}
}
