File: rmcp_test.go

package info (click to toggle)
golang-github-gopacket-gopacket 1.3.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 6,004 kB
  • sloc: sh: 301; python: 76; makefile: 10
file content (86 lines) | stat: -rw-r--r-- 2,077 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
// Copyright 2019 The GoPacket Authors. All rights reserved.
//
// Use of this source code is governed by a BSD-style license that can be found
// in the LICENSE file in the root of the source tree.

package layers

import (
	"bytes"
	"encoding/hex"
	"testing"

	"github.com/gopacket/gopacket"
)

func TestRMCPDecodeFromBytes(t *testing.T) {
	b, err := hex.DecodeString("0600ff06")
	if err != nil {
		t.Fatalf("Failed to decode RMCP message")
	}

	rmcp := &RMCP{}
	if err := rmcp.DecodeFromBytes(b, gopacket.NilDecodeFeedback); err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	if !bytes.Equal(rmcp.BaseLayer.Payload, []byte{}) {
		t.Errorf("payload is %v, want %v", rmcp.BaseLayer.Payload, b)
	}
	if !bytes.Equal(rmcp.BaseLayer.Contents, b) {
		t.Errorf("contents is %v, want %v", rmcp.BaseLayer.Contents, b)
	}
	if rmcp.Version != RMCPVersion1 {
		t.Errorf("version is %v, want %v", rmcp.Version, RMCPVersion1)
	}
	if rmcp.Sequence != 0xFF {
		t.Errorf("sequence is %v, want %v", rmcp.Sequence, 0xFF)
	}
	if rmcp.Ack {
		t.Errorf("ack is true, want false")
	}
	if rmcp.Class != RMCPClassASF {
		t.Errorf("class is %v, want %v", rmcp.Class, RMCPClassASF)
	}
}

func serializeRMCP(rmcp *RMCP) ([]byte, error) {
	sb := gopacket.NewSerializeBuffer()
	err := rmcp.SerializeTo(sb, gopacket.SerializeOptions{})
	return sb.Bytes(), err
}

func TestRMCPTestSerializeTo(t *testing.T) {
	table := []struct {
		layer *RMCP
		want  []byte
	}{
		{
			&RMCP{
				Version:  RMCPVersion1,
				Sequence: 1,
				Ack:      false,
				Class:    RMCPClassASF,
			},
			[]byte{0x6, 0x0, 0x1, 0x6},
		},
		{
			&RMCP{
				Version:  RMCPVersion1,
				Sequence: 0xFF,
				Ack:      true,
				Class:    RMCPClassIPMI,
			},
			[]byte{0x6, 0x0, 0xFF, 0x87},
		},
	}
	for _, test := range table {
		b, err := serializeRMCP(test.layer)
		switch {
		case err != nil && test.want != nil:
			t.Errorf("serialize %v failed with %v, wanted %v", test.layer,
				err, test.want)
		case err == nil && !bytes.Equal(b, test.want):
			t.Errorf("serialize %v = %v, want %v", test.layer, b, test.want)
		}
	}
}