github.com/keysonZZZ/kmg@v0.0.0-20151121023212-05317bfd7d39/third/kmgRadius/packet_test.go (about)

     1  package kmgRadius
     2  
     3  import (
     4  	"bytes"
     5  	"fmt"
     6  	"net"
     7  	"testing"
     8  
     9  	. "github.com/bronze1man/kmg/kmgTest"
    10  	"github.com/bronze1man/kmg/third/kmgRadius/MSCHAPV2"
    11  	"github.com/bronze1man/kmg/third/kmgRadius/eap"
    12  )
    13  
    14  func TestAccessRequestPacket(ot *testing.T) {
    15  	inBytes := []byte{0x1, 0xef, 0x0, 0x8e, 0x94, 0xb, 0x18, 0xaf, 0xa, 0xb6, 0x12, 0xf5, 0x24, 0x4, 0x94, 0xbe, 0x18, 0xbc, 0x7, 0x4d,
    16  		0x1, 0x4, 0x72, 0x48, 0x3d, 0x6, 0x0, 0x0, 0x0, 0x5, 0x6, 0x6, 0x0, 0x0, 0x0, 0x2, 0x5, 0x6, 0x0, 0x0, 0x0, 0x10, 0x57,
    17  		0xf, 0x69, 0x6f, 0x73, 0x5f, 0x78, 0x61, 0x75, 0x74, 0x68, 0x5f, 0x70, 0x73, 0x6b, 0x4, 0x6, 0xa, 0x1, 0x1, 0x5, 0x1e,
    18  		0xf, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x35, 0x5b, 0x35, 0x30, 0x30, 0x5d, 0x1f, 0x10, 0x31, 0x30, 0x2e, 0x31,
    19  		0x2e, 0x31, 0x2e, 0x37, 0x30, 0x5b, 0x35, 0x30, 0x30, 0x5d, 0x20, 0xc, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x53, 0x77,
    20  		0x61, 0x6e, 0x2, 0x12, 0x8d, 0x7, 0xc2, 0xc0, 0xa4, 0x2, 0x2c, 0xed, 0x8e, 0x69, 0x5b, 0x9e, 0x25, 0x77, 0xe5, 0xd, 0x50,
    21  		0x12, 0x39, 0x5c, 0xaa, 0x3e, 0x6d, 0x23, 0xea, 0xb5, 0x86, 0xc1, 0x3, 0x2d, 0x9d, 0x5c, 0x19, 0xca}
    22  	pac, err := DecodeRequestPacket([]byte("sEcReT"), inBytes)
    23  	fmt.Println(err)
    24  	Ok(err == nil)
    25  	Equal(pac.Code, CodeAccessRequest)
    26  	Ok(pac.GetUsername() == "rH")
    27  	Ok(pac.GetAVP(AVPTypeNASPortType).GetValue().(NASPortTypeEnum) == NASPortTypeEnumVirtual)
    28  	Ok(pac.GetAVP(AVPTypeServiceType).GetValue().(ServiceTypeEnum) == ServiceTypeEnumFramed)
    29  	Ok(pac.GetAVP(AVPTypeNASPort).GetValue().(uint32) == 16)
    30  	Ok(pac.GetAVP(AVPTypeNASPortId).GetValue().(string) == "ios_xauth_psk")
    31  	Ok(pac.GetNasIpAddress().Equal(net.ParseIP("10.1.1.5")))
    32  	Ok(pac.GetAVP(AVPTypeCalledStationId).GetValue().(string) == "10.1.1.5[500]")
    33  	Ok(pac.GetAVP(AVPTypeCallingStationId).GetValue().(string) == "10.1.1.70[500]")
    34  	Ok(pac.GetAVP(AVPTypeNASIdentifier).GetValue().(string) == "strongSwan")
    35  	Ok(bytes.Equal(pac.GetAVP(AVPTypeMessageAuthenticator).GetValue().([]byte), []byte{0x39, 0x5c, 0xaa, 0x3e, 0x6d, 0x23, 0xea, 0xb5,
    36  		0x86, 0xc1, 0x3, 0x2d, 0x9d, 0x5c, 0x19, 0xca}))
    37  	Ok(pac.GetPassword() == "O6")
    38  	Ok(pac.GetPassword() == "O6")
    39  	outBytes, err := pac.Encode()
    40  	fmt.Println(err)
    41  	Ok(err == nil)
    42  	Ok(bytes.Equal(pac.GetAVP(AVPTypeMessageAuthenticator).GetValue().([]byte), []byte{0x39, 0x5c, 0xaa, 0x3e, 0x6d, 0x23, 0xea, 0xb5,
    43  		0x86, 0xc1, 0x3, 0x2d, 0x9d, 0x5c, 0x19, 0xca}))
    44  
    45  	pac2, err := DecodeRequestPacket([]byte("sEcReT"), outBytes)
    46  	fmt.Println(err)
    47  	Ok(err == nil)
    48  	Ok(pac2.GetUsername() == "rH")
    49  	//Ok(pac2.GetPassword()=="O6") TODO fix this.
    50  
    51  	outPac := pac.Reply()
    52  	outPac.Code = CodeAccessAccept
    53  	outBytes, err = outPac.Encode()
    54  	Ok(err == nil)
    55  	expectBytes := []byte{0x2, 0xef, 0x0, 0x26, 0x54, 0x2a, 0x19, 0xab, 0xcb, 0x8e, 0xe6, 0x5f, 0x42, 0x44, 0x15, 0x3c,
    56  		0x8a, 0xb8, 0x23, 0x51, 0x50, 0x12, 0x53, 0x4e, 0xee, 0x31, 0x43, 0x5c, 0x93, 0x2f, 0xfd, 0x11, 0x67, 0xb9,
    57  		0xe8, 0xfb, 0x69, 0x60}
    58  
    59  	Ok(bytes.Equal(expectBytes, outBytes))
    60  }
    61  
    62  func TestEapIdentityPacket(ot *testing.T) {
    63  	inBytes := []byte{0x1, 0xe6, 0x0, 0xa5, 0xe6, 0x17, 0x46, 0x35, 0xe4, 0xba, 0x8f, 0xe5, 0x15, 0x90, 0x96, 0x33, 0xd0,
    64  		0xb3, 0x61, 0x34, 0x1, 0x12, 0x63, 0x62, 0x46, 0x69, 0x42, 0x6f, 0x6f, 0x52, 0x6e, 0x5a, 0x73, 0x58, 0x6e,
    65  		0x5a, 0x4a, 0x33, 0x3d, 0x6, 0x0, 0x0, 0x0, 0x5, 0x6, 0x6, 0x0, 0x0, 0x0, 0x2, 0x5, 0x6, 0x0, 0x0, 0x0, 0xa,
    66  		0x57, 0x13, 0x69, 0x6f, 0x73, 0x5f, 0x69, 0x6b, 0x65, 0x76, 0x32, 0x5f, 0x65, 0x61, 0x70, 0x5f, 0x70, 0x73,
    67  		0x6b, 0x4, 0x6, 0xa, 0x1, 0x1, 0x5, 0x1e, 0xf, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x35, 0x5b, 0x35,
    68  		0x30, 0x30, 0x5d, 0x1f, 0x10, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x36, 0x36, 0x5b, 0x35, 0x30, 0x30,
    69  		0x5d, 0x4f, 0x17, 0x2, 0x0, 0x0, 0x15, 0x1, 0x63, 0x62, 0x46, 0x69, 0x42, 0x6f, 0x6f, 0x52, 0x6e, 0x5a, 0x73,
    70  		0x58, 0x6e, 0x5a, 0x4a, 0x33, 0x20, 0xc, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x53, 0x77, 0x61, 0x6e, 0x50,
    71  		0x12, 0x48, 0x14, 0x43, 0xf2, 0x6, 0xa4, 0x8b, 0x7a, 0xd8, 0xc0, 0xdd, 0xe9, 0xb3, 0x3, 0x7d, 0x84}
    72  	pac, err := DecodeRequestPacket([]byte("sEcReT"), inBytes)
    73  	Ok(err == nil)
    74  	Equal(pac.Code, CodeAccessRequest)
    75  	Ok(pac.GetUsername() == "cbFiBooRnZsXnZJ3")
    76  	e := pac.GetEAPMessage()
    77  	Ok(e != nil)
    78  	Ok(e.Header().Code == eap.CodeResponse)
    79  	Ok(e.Header().Identifier == 0)
    80  	Ok(e.Header().Type == eap.TypeIdentity)
    81  	Ok(e.(*eap.IdentityPacket).Identity == "cbFiBooRnZsXnZJ3")
    82  }
    83  
    84  func TestMSCHAPV2ResponsePacket(ot *testing.T) {
    85  	inBytes := []byte{0x1, 0xa6, 0x0, 0xdb, 0x97, 0x4f, 0xaa, 0x63, 0xd3, 0x4a, 0x58, 0x1d, 0x5b, 0x49, 0x20, 0x67,
    86  		0x53, 0x92, 0x4c, 0xab, 0x1, 0x12, 0x51, 0x36, 0x56, 0x67, 0x74, 0x71, 0x64, 0x55, 0x34, 0x58, 0x4f, 0x57,
    87  		0x38, 0x72, 0x4a, 0x70, 0x3d, 0x6, 0x0, 0x0, 0x0, 0x5, 0x6, 0x6, 0x0, 0x0, 0x0, 0x2, 0x5, 0x6, 0x0, 0x0, 0x0,
    88  		0x4f, 0x57, 0x13, 0x69, 0x6f, 0x73, 0x5f, 0x69, 0x6b, 0x65, 0x76, 0x32, 0x5f, 0x65, 0x61, 0x70, 0x5f, 0x70,
    89  		0x73, 0x6b, 0x4, 0x6, 0xa, 0x1, 0x1, 0x5, 0x1e, 0xf, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x35, 0x5b,
    90  		0x35, 0x30, 0x30, 0x5d, 0x1f, 0x10, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x36, 0x36, 0x5b, 0x35, 0x30,
    91  		0x30, 0x5d, 0x4f, 0x4d, 0x2, 0x0, 0x0, 0x4b, 0x1a, 0x2, 0x0, 0x0, 0x46, 0x31, 0x86, 0xc5, 0xfc, 0xe0, 0x56,
    92  		0x2c, 0xd, 0xea, 0xe9, 0xc8, 0x86, 0xf5, 0x2d, 0xf8, 0x78, 0xe2, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0,
    93  		0x6e, 0x90, 0x4f, 0xdd, 0x97, 0xbf, 0x69, 0xad, 0x7a, 0x7d, 0x39, 0x2c, 0xb4, 0x8e, 0x0, 0x98, 0x6f, 0x8f,
    94  		0xed, 0x43, 0x6d, 0x5b, 0x20, 0xa9, 0x0, 0x51, 0x36, 0x56, 0x67, 0x74, 0x71, 0x64, 0x55, 0x34, 0x58, 0x4f,
    95  		0x57, 0x38, 0x72, 0x4a, 0x70, 0x20, 0xc, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x53, 0x77, 0x61, 0x6e, 0x50,
    96  		0x12, 0x48, 0x10, 0x63, 0xa3, 0x1c, 0xe3, 0xa6, 0xb4, 0xb7, 0xfb, 0xad, 0xf8, 0x6d, 0x5c, 0x66, 0xae}
    97  	pac, err := DecodeRequestPacket([]byte("sEcReT"), inBytes)
    98  	Equal(err, nil)
    99  	Equal(pac.Code, CodeAccessRequest)
   100  	Equal(pac.GetUsername(), "Q6VgtqdU4XOW8rJp")
   101  	e := pac.GetEAPMessage()
   102  	Equal(e.Header().Code, eap.CodeResponse)
   103  	Equal(e.Header().Type, eap.TypeMSCHAPV2)
   104  	mschapv2 := e.(*eap.MSCHAPV2Packet).MSCHAPV2.(*MSCHAPV2.ResponsePacket)
   105  	Equal(err, nil)
   106  	Equal(mschapv2.OpCode(), MSCHAPV2.OpCodeResponse)
   107  	Equal(mschapv2.Name, "Q6VgtqdU4XOW8rJp")
   108  	Equal(mschapv2.NTResponse, [24]byte{0x6e, 0x90, 0x4f, 0xdd, 0x97, 0xbf, 0x69, 0xad, 0x7a, 0x7d, 0x39, 0x2c, 0xb4,
   109  		0x8e, 0x0, 0x98, 0x6f, 0x8f, 0xed, 0x43, 0x6d, 0x5b, 0x20, 0xa9})
   110  	Equal(mschapv2.PeerChallenge, [16]byte{0x86, 0xc5, 0xfc, 0xe0, 0x56, 0x2c, 0xd, 0xea, 0xe9, 0xc8, 0x86, 0xf5,
   111  		0x2d, 0xf8, 0x78, 0xe2})
   112  }
   113  
   114  func TestAccountingRequestPacket(ot *testing.T) {
   115  	inBytes := []byte{0x4, 0x16, 0x0, 0x96, 0x67, 0x80, 0x6e, 0x54, 0x9c, 0x98, 0xe4, 0x56, 0x68, 0xfc, 0xa5, 0x8f,
   116  		0x83, 0x5f, 0x37, 0x1e, 0x28, 0x6, 0x0, 0x0, 0x0, 0x1, 0x2c, 0xe, 0x31, 0x34, 0x32, 0x33, 0x33, 0x31, 0x36,
   117  		0x37, 0x32, 0x31, 0x2d, 0x34, 0x3d, 0x6, 0x0, 0x0, 0x0, 0x5, 0x6, 0x6, 0x0, 0x0, 0x0, 0x2, 0x5, 0x6, 0x0,
   118  		0x0, 0x0, 0x4, 0x57, 0x13, 0x69, 0x6f, 0x73, 0x5f, 0x69, 0x6b, 0x65, 0x76, 0x32, 0x5f, 0x65, 0x61, 0x70,
   119  		0x5f, 0x70, 0x73, 0x6b, 0x4, 0x6, 0xa, 0x1, 0x1, 0x5, 0x1e, 0xf, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e,
   120  		0x35, 0x5b, 0x35, 0x30, 0x30, 0x5d, 0x1f, 0x10, 0x31, 0x30, 0x2e, 0x31, 0x2e, 0x31, 0x2e, 0x36, 0x36, 0x5b,
   121  		0x35, 0x30, 0x30, 0x5d, 0x1, 0x12, 0x51, 0x36, 0x56, 0x67, 0x74, 0x71, 0x64, 0x55, 0x34, 0x58, 0x4f, 0x57,
   122  		0x38, 0x72, 0x4a, 0x70, 0x8, 0x6, 0xac, 0x14, 0x1, 0x1, 0x20, 0xc, 0x73, 0x74, 0x72, 0x6f, 0x6e, 0x67, 0x53,
   123  		0x77, 0x61, 0x6e}
   124  	pac, err := DecodeRequestPacket([]byte("sEcReT"), inBytes)
   125  	Equal(err, nil)
   126  	Equal(pac.Code, CodeAccountingRequest)
   127  	Equal(pac.GetUsername(), "Q6VgtqdU4XOW8rJp")
   128  	Equal(pac.GetNASPort(), uint32(4))
   129  }