File: lcm_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 (156 lines) | stat: -rw-r--r-- 3,989 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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
// Copyright 2018 Google, Inc. 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 (
	"encoding/hex"
	"testing"

	"github.com/gopacket/gopacket"
)

var (
	fingerprint uint64 = 0x6c636d2073656c66

	shortPacket = []byte{
		0x4c, 0x43, 0x30, 0x32, 0x00, 0x00, 0x00, 0x00, 0x4c, 0x43, 0x4d, 0x5f,
		0x53, 0x45, 0x4c, 0x46, 0x5f, 0x54, 0x45, 0x53, 0x54, 0x00, 0x6c, 0x63,
		0x6d, 0x20, 0x73, 0x65, 0x6c, 0x66, 0x20, 0x74, 0x65, 0x73, 0x74,
	}

	fragmentedPacket = []byte{
		0x4c, 0x43, 0x30, 0x33, 0x00, 0x00, 0x00, 0x01, 0x00, 0x00, 0x00, 0x0d,
		0x00, 0x00, 0x00, 0x2d, 0x00, 0x00, 0x00, 0x02, 0x4c, 0x43, 0x4d, 0x5f,
		0x53, 0x45, 0x4c, 0x46, 0x5f, 0x54, 0x45, 0x53, 0x54, 0x00, 0x6c, 0x63,
		0x6d, 0x20, 0x73, 0x65, 0x6c, 0x66, 0x20, 0x74, 0x65, 0x73, 0x74,
	}

	invalidPacket = []byte{
		0x4c, 0x43, 0x30, 0x34, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
	}

	expectedChannel = "LCM_SELF_TEST"
)

func TestLCMDecode(t *testing.T) {
	testShortLCM(t)
	testFragmentedLCM(t)
	testInvalidLCM(t)
}

func testShortLCM(t *testing.T) {
	lcm := &LCM{}

	err := lcm.DecodeFromBytes(shortPacket, gopacket.NilDecodeFeedback)
	if err != nil {
		t.Fatal(err)
	}

	if lcm.Magic != LCMShortHeaderMagic {
		t.Errorf("Expected LCM Magic %x, but decoded %x.\n",
			LCMShortHeaderMagic, lcm.Magic)
	}

	if lcm.SequenceNumber != 0x00 {
		t.Errorf("Expected an LCM Sequence Number of %x, but decoded %x.\n",
			0x00, lcm.SequenceNumber)
	}

	if lcm.ChannelName != expectedChannel {
		t.Errorf("Expected channel name %s but received %s\n",
			expectedChannel, lcm.ChannelName)
	}

	if lcm.Fragmented {
		t.Errorf("Misinterpreted non-fragmented packet as fragmented.")
	}

	for i, val := range lcm.LayerContents() {
		if val != shortPacket[i] {
			t.Errorf("\nLCM Payload: expected\n%sbut received\n%s",
				hex.Dump(shortPacket[:22]), hex.Dump(lcm.Payload()))
		}
	}

	for i, val := range lcm.Payload() {
		if val != shortPacket[i+22] {
			t.Errorf("\nLCM Payload: expected\n%sbut received\n%s",
				hex.Dump(shortPacket[22:]), hex.Dump(lcm.Payload()))
		}
	}
}

func testFragmentedLCM(t *testing.T) {
	lcm := LCM{}

	err := lcm.DecodeFromBytes(fragmentedPacket, gopacket.NilDecodeFeedback)
	if err != nil {
		t.Fatal(err)
	}

	if lcm.Magic != LCMFragmentedHeaderMagic {
		t.Errorf("Expected LCM Magic %x, but decoded %x.\n",
			LCMFragmentedHeaderMagic, lcm.Magic)
	}

	if lcm.SequenceNumber != 0x01 {
		t.Errorf("Expected an LCM Sequence Number of %x, but decoded %x.\n",
			0x01, lcm.SequenceNumber)
	}

	if lcm.PayloadSize != 0x0d {
		t.Errorf("Expected an LCM Payload Size of %x, but decoded %x.\n", 0x0d,
			lcm.PayloadSize)
	}

	if lcm.FragmentOffset != 0x2d {
		t.Errorf("Expected an LCM Fragment Offset of %x, but decoded %x.\n",
			0x2d, lcm.FragmentOffset)
	}

	if lcm.FragmentNumber != 0x00 {
		t.Errorf("Expected the first LCM fragment (%x), but decoded %x.\n",
			0x00, lcm.FragmentNumber)
	}

	if lcm.TotalFragments != 0x02 {
		t.Errorf("Expected two LCM fragments (%x), but decoded %x.\n", 0x02,
			lcm.TotalFragments)
	}

	if lcm.ChannelName != expectedChannel {
		t.Errorf("Expected LCM Channel Name %s but decoded %s\n",
			expectedChannel, lcm.ChannelName)
	}

	if !lcm.Fragmented {
		t.Errorf("Misinterpreted fragmented packet as non-fragmented.")
	}

	for i, val := range lcm.LayerContents() {
		if val != fragmentedPacket[i] {
			t.Errorf("\nLCM Payload: expected\n%sbut received\n%s",
				hex.Dump(fragmentedPacket[:22]), hex.Dump(lcm.Payload()))
		}
	}

	for i, val := range lcm.Payload() {
		if val != fragmentedPacket[i+34] {
			t.Errorf("\nLCM Payload: expected\n%sbut received\n%s",
				hex.Dump(fragmentedPacket[34:]), hex.Dump(lcm.Payload()))
		}
	}
}

func testInvalidLCM(t *testing.T) {
	lcm := LCM{}

	err := lcm.DecodeFromBytes(invalidPacket, gopacket.NilDecodeFeedback)
	if err == nil {
		t.Fatal("Did not detect LCM decode error.")
	}
}