github.com/gopacket/gopacket@v1.1.0/layers/eapol_test.go (about)

     1  // Copyright placeholder
     2  //
     3  // Use of this source code is governed by a BSD-style license
     4  // that can be found in the LICENSE file in the root of the source
     5  // tree.
     6  package layers
     7  
     8  import (
     9  	"reflect"
    10  	"testing"
    11  
    12  	"github.com/gopacket/gopacket"
    13  )
    14  
    15  const eapolErrFmt = "%s packet processing failed:\ngot  :\n%#v\n\nwant :\n%#v\n\n"
    16  
    17  // testPacketEAPOLKey is frame 87 in the capture:
    18  // https://wiki.wireshark.org/SampleCaptures?action=AttachFile&do=get&target=wpa-Induction.pcap
    19  // It's the first EAPOL-Key frame in the WPA 4-way handshake:
    20  // 0x0000   02 03 00 75 02 00 8a 00 10 00 00 00 00 00 00 00  ...u............
    21  // 0x0010   00 3e 8e 96 7d ac d9 60 32 4c ac 5b 6a a7 21 23  .>..}..`2L.[j.!#
    22  // 0x0020   5b f5 7b 94 97 71 c8 67 98 9f 49 d0 4e d4 7c 69  [.{..q.g..I.N.|i
    23  // 0x0030   33 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  3...............
    24  // 0x0040   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    25  // 0x0050   00 00 00 00 00 00 00 00 00 00 00 00 00 00 00 00  ................
    26  // 0x0060   00 00 16 dd 14 00 0f ac 04 59 2d a8 80 96 c4 61  .........Y-....a
    27  // 0x0070   da 24 6c 69 00 1e 87 7f 3d                       .$li....=
    28  
    29  var testPacketEAPOLKey = []byte{
    30  	0x02, 0x03, 0x00, 0x75, 0x02, 0x00, 0x8a, 0x00,
    31  	0x10, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    32  	0x00, 0x3e, 0x8e, 0x96, 0x7d, 0xac, 0xd9, 0x60,
    33  	0x32, 0x4c, 0xac, 0x5b, 0x6a, 0xa7, 0x21, 0x23,
    34  	0x5b, 0xf5, 0x7b, 0x94, 0x97, 0x71, 0xc8, 0x67,
    35  	0x98, 0x9f, 0x49, 0xd0, 0x4e, 0xd4, 0x7c, 0x69,
    36  	0x33, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    37  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    38  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    39  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    40  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    41  	0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
    42  	0x00, 0x00, 0x16, 0xdd, 0x14, 0x00, 0x0f, 0xac,
    43  	0x04, 0x59, 0x2d, 0xa8, 0x80, 0x96, 0xc4, 0x61,
    44  	0xda, 0x24, 0x6c, 0x69, 0x00, 0x1e, 0x87, 0x7f,
    45  	0x3d,
    46  }
    47  
    48  func TestPacketEAPOLKey(t *testing.T) {
    49  	p := gopacket.NewPacket(testPacketEAPOLKey, LayerTypeEAPOL, gopacket.Default)
    50  	if p.ErrorLayer() != nil {
    51  		t.Error("Failed to decode packet:", p.ErrorLayer().Error())
    52  	}
    53  	checkLayers(p, []gopacket.LayerType{LayerTypeEAPOL, LayerTypeEAPOLKey,
    54  		LayerTypeDot11InformationElement}, t)
    55  
    56  	{
    57  		got := p.Layer(LayerTypeEAPOL).(*EAPOL)
    58  		want := &EAPOL{
    59  			BaseLayer: BaseLayer{
    60  				Contents: testPacketEAPOLKey[:4],
    61  				Payload:  testPacketEAPOLKey[4:],
    62  			},
    63  			Version: 2,
    64  			Type:    EAPOLTypeKey,
    65  			Length:  117,
    66  		}
    67  		if !reflect.DeepEqual(got, want) {
    68  			t.Errorf(eapolErrFmt, "EAPOL", got, want)
    69  		}
    70  	}
    71  
    72  	{
    73  		got := p.Layer(LayerTypeEAPOLKey).(*EAPOLKey)
    74  		want := &EAPOLKey{
    75  			BaseLayer: BaseLayer{
    76  				Contents: testPacketEAPOLKey[4 : 4+eapolKeyFrameLen],
    77  				Payload:  testPacketEAPOLKey[4+eapolKeyFrameLen:],
    78  			},
    79  			KeyDescriptorType:    EAPOLKeyDescriptorTypeDot11,
    80  			KeyDescriptorVersion: EAPOLKeyDescriptorVersionAESHMACSHA1,
    81  			KeyType:              EAPOLKeyTypePairwise,
    82  			KeyACK:               true,
    83  			KeyLength:            16,
    84  			Nonce: []byte{
    85  				0x3e, 0x8e, 0x96, 0x7d, 0xac, 0xd9, 0x60, 0x32,
    86  				0x4c, 0xac, 0x5b, 0x6a, 0xa7, 0x21, 0x23, 0x5b,
    87  				0xf5, 0x7b, 0x94, 0x97, 0x71, 0xc8, 0x67, 0x98,
    88  				0x9f, 0x49, 0xd0, 0x4e, 0xd4, 0x7c, 0x69, 0x33,
    89  			},
    90  			IV:            make([]byte, 16),
    91  			MIC:           make([]byte, 16),
    92  			KeyDataLength: 22,
    93  		}
    94  		if !reflect.DeepEqual(got, want) {
    95  			t.Errorf(eapolErrFmt, "EAPOLKey", got, want)
    96  		}
    97  	}
    98  	{
    99  		got := p.Layer(LayerTypeDot11InformationElement).(*Dot11InformationElement)
   100  		want := &Dot11InformationElement{
   101  			BaseLayer: BaseLayer{
   102  				Contents: testPacketEAPOLKey[4+eapolKeyFrameLen:],
   103  				Payload:  []byte{},
   104  			},
   105  			ID:     Dot11InformationElementIDVendor,
   106  			Length: 20,
   107  			OUI:    []byte{0x00, 0x0f, 0xac, 0x04},
   108  			Info: []byte{
   109  				0x59, 0x2d, 0xa8, 0x80, 0x96, 0xc4, 0x61, 0xda,
   110  				0x24, 0x6c, 0x69, 0x00, 0x1e, 0x87, 0x7f, 0x3d,
   111  			},
   112  		}
   113  		if !reflect.DeepEqual(got, want) {
   114  			t.Errorf(eapolErrFmt, "Dot11InformationElement", got, want)
   115  		}
   116  	}
   117  }
   118  
   119  func BenchmarkDecodePacketEAPOLKey(b *testing.B) {
   120  	for i := 0; i < b.N; i++ {
   121  		gopacket.NewPacket(testPacketEAPOLKey, nil, gopacket.NoCopy)
   122  	}
   123  }