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

     1  // Copyright 2019 The GoPacket Authors. All rights reserved.
     2  //
     3  // Use of this source code is governed by a BSD-style license that can be found
     4  // in the LICENSE file in the root of the source tree.
     5  
     6  package layers
     7  
     8  import (
     9  	"bytes"
    10  	"encoding/hex"
    11  	"testing"
    12  
    13  	"github.com/gopacket/gopacket"
    14  )
    15  
    16  func TestRMCPDecodeFromBytes(t *testing.T) {
    17  	b, err := hex.DecodeString("0600ff06")
    18  	if err != nil {
    19  		t.Fatalf("Failed to decode RMCP message")
    20  	}
    21  
    22  	rmcp := &RMCP{}
    23  	if err := rmcp.DecodeFromBytes(b, gopacket.NilDecodeFeedback); err != nil {
    24  		t.Fatalf("Unexpected error: %v", err)
    25  	}
    26  	if !bytes.Equal(rmcp.BaseLayer.Payload, []byte{}) {
    27  		t.Errorf("payload is %v, want %v", rmcp.BaseLayer.Payload, b)
    28  	}
    29  	if !bytes.Equal(rmcp.BaseLayer.Contents, b) {
    30  		t.Errorf("contents is %v, want %v", rmcp.BaseLayer.Contents, b)
    31  	}
    32  	if rmcp.Version != RMCPVersion1 {
    33  		t.Errorf("version is %v, want %v", rmcp.Version, RMCPVersion1)
    34  	}
    35  	if rmcp.Sequence != 0xFF {
    36  		t.Errorf("sequence is %v, want %v", rmcp.Sequence, 0xFF)
    37  	}
    38  	if rmcp.Ack {
    39  		t.Errorf("ack is true, want false")
    40  	}
    41  	if rmcp.Class != RMCPClassASF {
    42  		t.Errorf("class is %v, want %v", rmcp.Class, RMCPClassASF)
    43  	}
    44  }
    45  
    46  func serializeRMCP(rmcp *RMCP) ([]byte, error) {
    47  	sb := gopacket.NewSerializeBuffer()
    48  	err := rmcp.SerializeTo(sb, gopacket.SerializeOptions{})
    49  	return sb.Bytes(), err
    50  }
    51  
    52  func TestRMCPTestSerializeTo(t *testing.T) {
    53  	table := []struct {
    54  		layer *RMCP
    55  		want  []byte
    56  	}{
    57  		{
    58  			&RMCP{
    59  				Version:  RMCPVersion1,
    60  				Sequence: 1,
    61  				Ack:      false,
    62  				Class:    RMCPClassASF,
    63  			},
    64  			[]byte{0x6, 0x0, 0x1, 0x6},
    65  		},
    66  		{
    67  			&RMCP{
    68  				Version:  RMCPVersion1,
    69  				Sequence: 0xFF,
    70  				Ack:      true,
    71  				Class:    RMCPClassIPMI,
    72  			},
    73  			[]byte{0x6, 0x0, 0xFF, 0x87},
    74  		},
    75  	}
    76  	for _, test := range table {
    77  		b, err := serializeRMCP(test.layer)
    78  		switch {
    79  		case err != nil && test.want != nil:
    80  			t.Errorf("serialize %v failed with %v, wanted %v", test.layer,
    81  				err, test.want)
    82  		case err == nil && !bytes.Equal(b, test.want):
    83  			t.Errorf("serialize %v = %v, want %v", test.layer, b, test.want)
    84  		}
    85  	}
    86  }