File: asf_presencepong_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 (138 lines) | stat: -rw-r--r-- 3,237 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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
// 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 TestASFPresencePongDecodeFromBytes(t *testing.T) {
	b, err := hex.DecodeString("000011be000000008100000000000000")
	if err != nil {
		t.Fatalf("Failed to decode ASF Presence Pong message")
	}

	pp := &ASFPresencePong{}
	if err := pp.DecodeFromBytes(b, gopacket.NilDecodeFeedback); err != nil {
		t.Fatalf("Unexpected error: %v", err)
	}
	if !bytes.Equal(pp.BaseLayer.Payload, []byte{}) {
		t.Errorf("payload is %v, want %v", pp.BaseLayer.Payload, b)
	}
	if !bytes.Equal(pp.BaseLayer.Contents, b) {
		t.Errorf("contents is %v, want %v", pp.BaseLayer.Contents, b)
	}
	if pp.Enterprise != ASFRMCPEnterprise {
		t.Errorf("want enterprise %v, got %v", ASFRMCPEnterprise, pp.Enterprise)
	}
	if !bytes.Equal(pp.OEM[:], make([]byte, 4)) {
		t.Errorf("want null OEM, got %v", pp.OEM[:])
	}
	if !pp.IPMI {
		t.Errorf("want IPMI, got false")
	}
	if !pp.ASFv1 {
		t.Errorf("want ASFv1, got false")
	}
	if pp.SecurityExtensions {
		t.Errorf("do not want security extensions, got true")
	}
	if pp.DASH {
		t.Errorf("do not want DASH, got true")
	}
}

func TestASFPresencePongSupportsDCMI(t *testing.T) {
	table := []struct {
		layer *ASFPresencePong
		want  bool
	}{
		{
			&ASFPresencePong{
				Enterprise: ASFRMCPEnterprise,
				IPMI:       true,
				ASFv1:      true,
			},
			false,
		},
		{
			&ASFPresencePong{
				Enterprise: ASFDCMIEnterprise,
				IPMI:       false,
				ASFv1:      true,
			},
			false,
		},
		{
			&ASFPresencePong{
				Enterprise: ASFDCMIEnterprise,
				IPMI:       true,
				ASFv1:      false,
			},
			false,
		},
		{
			&ASFPresencePong{
				Enterprise: ASFDCMIEnterprise,
				IPMI:       true,
				ASFv1:      true,
			},
			true,
		},
	}
	for _, test := range table {
		got := test.layer.SupportsDCMI()
		if got != test.want {
			t.Errorf("%v SupportsDCMI() = %v, want %v", test.layer, got, test.want)
		}
	}
}

func serializeASFPresencePong(pp *ASFPresencePong) ([]byte, error) {
	sb := gopacket.NewSerializeBuffer()
	err := pp.SerializeTo(sb, gopacket.SerializeOptions{})
	return sb.Bytes(), err
}

func TestASFPresencePongSerializeTo(t *testing.T) {
	table := []struct {
		layer *ASFPresencePong
		want  []byte
	}{
		{
			&ASFPresencePong{
				Enterprise: ASFRMCPEnterprise,
				IPMI:       true,
				ASFv1:      true,
			},
			[]byte{0, 0, 0x11, 0xbe, 0, 0, 0, 0, 0x81, 0, 0, 0, 0, 0, 0, 0},
		},
		{
			&ASFPresencePong{
				Enterprise:         1234,
				OEM:                [4]byte{1, 2, 3, 4},
				ASFv1:              true,
				SecurityExtensions: true,
				DASH:               true,
			},
			[]byte{0, 0, 0x4, 0xd2, 1, 2, 3, 4, 0x01, 0xa0, 0, 0, 0, 0, 0, 0},
		},
	}
	for _, test := range table {
		b, err := serializeASFPresencePong(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)
		}
	}
}