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

     1  package MSCHAPV2
     2  
     3  import (
     4  	"testing"
     5  
     6  	. "github.com/bronze1man/kmg/kmgTest"
     7  	//"fmt"
     8  )
     9  
    10  func TestChallengePacket(ot *testing.T) {
    11  	p1in := &ChallengePacket{
    12  		Identifier: 100,
    13  		Challenge:  [16]byte{0x24, 0xdc, 0x3d, 0x6d, 0xb5, 0x66, 0xed, 0x25, 0xe4, 0x90, 0x49, 0x2c, 0x6e, 0xa2, 0x65, 0xcd},
    14  		Name:       "test name",
    15  	}
    16  	p1Byte := p1in.Encode()
    17  	p1outI, err := Decode(p1Byte)
    18  	Equal(err, nil)
    19  	p1out, ok := p1outI.(*ChallengePacket)
    20  	Equal(ok, true)
    21  	Equal(*p1in, *p1out)
    22  }
    23  
    24  func TestResponsePacket(ot *testing.T) {
    25  	p1in := &ResponsePacket{
    26  		Identifier:    100,
    27  		PeerChallenge: [16]byte{0x24, 0xdc, 0x3d, 0x6d, 0xb5, 0x66, 0xed, 0x25, 0xe4, 0x90, 0x49, 0x2c, 0x6e, 0xa2, 0x65, 0xcd},
    28  		NTResponse: [24]byte{0x24, 0xdc, 0x3d, 0x6d, 0xb5, 0x66, 0xed, 0x25, 0xe4, 0x90, 0x49, 0x2c, 0x6e, 0xa2, 0x65, 0xcd,
    29  			0x24, 0xdc, 0x3d, 0x6d, 0xb5, 0x66, 0xed, 0x25},
    30  		Name: "test name",
    31  	}
    32  	p1Byte := p1in.Encode()
    33  	Equal(p1Byte, []byte{0x2, 0x64, 0x0, 0x3f, 0x31, 0x24, 0xdc, 0x3d, 0x6d, 0xb5, 0x66, 0xed, 0x25, 0xe4, 0x90, 0x49,
    34  		0x2c, 0x6e, 0xa2, 0x65, 0xcd, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x24, 0xdc, 0x3d, 0x6d, 0xb5, 0x66,
    35  		0xed, 0x25, 0xe4, 0x90, 0x49, 0x2c, 0x6e, 0xa2, 0x65, 0xcd, 0x24, 0xdc, 0x3d, 0x6d, 0xb5, 0x66, 0xed, 0x25,
    36  		0x0, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6e, 0x61, 0x6d, 0x65})
    37  	p1outI, err := Decode(p1Byte)
    38  	Equal(err, nil)
    39  	p1out, ok := p1outI.(*ResponsePacket)
    40  	Equal(ok, true)
    41  	Equal(*p1in, *p1out)
    42  }
    43  
    44  func TestSuccessPacket(ot *testing.T) {
    45  	p1in := &SuccessPacket{
    46  		Identifier: 100,
    47  		Auth: [20]byte{0x24, 0xdc, 0x3d, 0x6d, 0xb5, 0x66, 0xed, 0x25, 0xe4, 0x90, 0x49, 0x2c, 0x6e, 0xa2,
    48  			0x65, 0xcd, 0x24, 0xdc, 0x3d, 0x6d},
    49  		Message: "test name",
    50  	}
    51  	p1Byte := p1in.Encode()
    52  	Equal(p1Byte, []byte{0x3, //OpCodeSuccess
    53  		0x64,      //Identifier
    54  		0x0, 0x3a, //length
    55  		0x53, 0x3d, //S=
    56  		0x32, 0x34, 0x44, 0x43, 0x33, 0x44, 0x36, 0x44, 0x42, 0x35, 0x36, 0x36, 0x45, 0x44, 0x32, 0x35, 0x45, 0x34, 0x39, 0x30, 0x34, 0x39, 0x32, 0x43, 0x36, 0x45, 0x41, 0x32, 0x36, 0x35, 0x43, 0x44, 0x32, 0x34, 0x44, 0x43, 0x33, 0x44, 0x36, 0x44, 0x20, 0x4d, 0x3d, 0x74, 0x65, 0x73, 0x74, 0x20, 0x6e, 0x61, 0x6d, 0x65})
    57  	p1outI, err := Decode(p1Byte)
    58  	Equal(err, nil)
    59  	p1out, ok := p1outI.(*SuccessPacket)
    60  	Equal(ok, true)
    61  	Equal(*p1in, *p1out)
    62  }
    63  
    64  func TestLogFromStrongswan(ot *testing.T) {
    65  	//step 1. server to client ChallengePacket
    66  	/*
    67  		inByte := []byte{0x01, 0xED, 0x00, 0x1F, 0x10, 0x24, 0xDC, 0x3D, 0x6D, 0xB5, 0x66, 0xED, 0x25, 0xE4,
    68  			0x90, 0x49, 0x2C, 0x6E, 0xA2, 0x65, 0xCD, 0x73, 0x74, 0x72, 0x6F, 0x6E, 0x67, 0x53, 0x77, 0x61, 0x6E}
    69  		p2outI, err := Decode(inByte)
    70  		Equal(err, nil)
    71  		p2out, ok := p2outI.(*ChallengePacket)
    72  		Equal(ok, true)
    73  		Equal(p2out.Identifier, uint8(0xED))
    74  		Equal(p2out.Challenge, [16]byte{0x24, 0xdc, 0x3d, 0x6d, 0xb5, 0x66, 0xed, 0x25, 0xe4, 0x90, 0x49, 0x2c, 0x6e, 0xa2, 0x65, 0xcd})
    75  		Equal(p2out.Name, "strongSwan")
    76  	*/
    77  	inByte := []byte{0x01, 0x22, 0x00, 0x1F, 0x10, 0xF2, 0x44, 0xDD, 0xC7, 0xBE, 0x70, 0x3F, 0x10, 0x2A,
    78  		0x8D, 0xC6, 0xAF, 0xAB, 0xA7, 0x9E, 0x2E, 0x73, 0x74, 0x72, 0x6F, 0x6E, 0x67, 0x53, 0x77, 0x61, 0x6E}
    79  	p1outI, err := Decode(inByte)
    80  	Equal(err, nil)
    81  	p1out, ok := p1outI.(*ChallengePacket)
    82  	Equal(ok, true)
    83  	Equal(p1out.Identifier, uint8(0x22))
    84  	Equal(p1out.Challenge, [16]byte{0xf2, 0x44, 0xdd, 0xc7, 0xbe, 0x70, 0x3f, 0x10, 0x2a, 0x8d, 0xc6, 0xaf, 0xab, 0xa7, 0x9e, 0x2e})
    85  	Equal(p1out.Name, "strongSwan")
    86  	Equal(p1out.Encode(), inByte)
    87  
    88  	//step 2. client to server response packet
    89  	inByte2 := []byte{0x02, 0x22, 0x00, 0x46, 0x31, 0x84, 0xF5, 0xF7, 0xFD, 0xC8, 0x3A, 0x25, 0x22, 0x31,
    90  		0x18, 0x12, 0x17, 0x02, 0x8F, 0xA4, 0xF3, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xA3, 0x0C, 0x3A, 0xD4, 0xE9, 0x13,
    91  		0x69, 0xBF, 0x6E, 0x66, 0x6E, 0x13, 0xBD, 0x19, 0x7F, 0x05, 0xF0, 0xF2, 0x7A, 0x4D, 0xC2, 0x9A, 0xCA, 0x17, 0x00, 0x51, 0x36,
    92  		0x56, 0x67, 0x74, 0x71, 0x64, 0x55, 0x34, 0x58, 0x4F, 0x57, 0x38, 0x72, 0x4A, 0x70}
    93  	p2outI, err := Decode(inByte2)
    94  	Equal(err, nil)
    95  	p2out, ok := p2outI.(*ResponsePacket)
    96  	Equal(ok, true)
    97  	Equal(p2out.Identifier, uint8(0x22))
    98  	Equal(p2out.NTResponse, [24]uint8{0xa3, 0xc, 0x3a, 0xd4, 0xe9, 0x13, 0x69, 0xbf, 0x6e, 0x66, 0x6e, 0x13, 0xbd, 0x19, 0x7f, 0x5, 0xf0, 0xf2, 0x7a, 0x4d, 0xc2, 0x9a, 0xca, 0x17})
    99  	Equal(p2out.PeerChallenge, [16]uint8{0x84, 0xf5, 0xf7, 0xfd, 0xc8, 0x3a, 0x25, 0x22, 0x31, 0x18, 0x12, 0x17, 0x2, 0x8f, 0xa4, 0xf3})
   100  	Equal(p2out.Name, "Q6VgtqdU4XOW8rJp")
   101  
   102  	//step 3. server to client success packet
   103  	inByte3 := []byte{0x03, 0x22, 0x00, 0x44, 0x53, 0x3D, 0x34, 0x36, 0x41, 0x35, 0x43, 0x30, 0x46, 0x41,
   104  		0x43, 0x35, 0x39, 0x44, 0x43, 0x41, 0x43, 0x31, 0x45, 0x46, 0x41, 0x41, 0x39, 0x45, 0x31, 0x46, 0x45, 0x42, 0x37, 0x36, 0x36, 0x30,
   105  		0x42, 0x36, 0x42, 0x41, 0x33, 0x30, 0x38, 0x44, 0x33, 0x39, 0x20, 0x4D, 0x3D, 0x57, 0x65, 0x6C, 0x63, 0x6F, 0x6D, 0x65, 0x32, 0x73,
   106  		0x74, 0x72, 0x6F, 0x6E, 0x67, 0x53, 0x77, 0x61, 0x6E, 0x00}
   107  	p3outI, err := Decode(inByte3)
   108  	Equal(err, nil)
   109  	p3out, ok := p3outI.(*SuccessPacket)
   110  	Equal(ok, true)
   111  	Equal(p3out.Identifier, uint8(0x22))
   112  	Equal(p3out.Auth, [20]byte{0x46, 0xa5, 0xc0, 0xfa, 0xc5, 0x9d, 0xca, 0xc1, 0xef, 0xaa, 0x9e, 0x1f, 0xeb, 0x76, 0x60,
   113  		0xb6, 0xba, 0x30, 0x8d, 0x39})
   114  	Equal(p3out.Message, "Welcome2strongSwan\x00")
   115  	Equal(p3out.Encode(), inByte3)
   116  
   117  	//verfiy we crypto correct
   118  	p3outget := ReplySuccessPacket(&ReplySuccessPacketRequest{
   119  		AuthenticatorChallenge: p1out.Challenge,
   120  		Response:               p2out,
   121  		Username:               []byte("Q6VgtqdU4XOW8rJp"),
   122  		Password:               []byte("GSxy6JM2t4vZYKXO"),
   123  		Message:                "Welcome2strongSwan\x00",
   124  	})
   125  	Equal(p3outget.Encode(), inByte3)
   126  	//step 4. client to server sucess packet
   127  
   128  }