File: message_test.go

package info (click to toggle)
golang-github-pion-datachannel 1.5.5-1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm, bookworm-backports, forky, sid, trixie
  • size: 232 kB
  • sloc: makefile: 2
file content (94 lines) | stat: -rw-r--r-- 2,152 bytes parent folder | download
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
package datachannel

import (
	"testing"

	"github.com/stretchr/testify/assert"
)

func TestChannelOpenMarshal(t *testing.T) {
	msg := channelOpen{
		ChannelType:          ChannelTypeReliable,
		Priority:             0,
		ReliabilityParameter: 0,

		Label:    []byte("foo"),
		Protocol: []byte("bar"),
	}

	rawMsg, err := msg.Marshal()
	if err != nil {
		t.Errorf("Failed to marshal: %v", err)
		return
	}

	result := []byte{
		0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x03, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72,
	}

	if len(rawMsg) != len(result) {
		t.Errorf("%q != %q", rawMsg, result)
		return
	}

	for i, v := range rawMsg {
		if v != result[i] {
			t.Errorf("%q != %q", rawMsg, result)
			break
		}
	}
}

func TestChannelAckMarshal(t *testing.T) {
	msg := channelAck{}
	rawMsg, err := msg.Marshal()
	if err != nil {
		t.Errorf("Failed to marshal: %v", err)
		return
	}
	result := []byte{0x02, 0x00, 0x00, 0x00}

	if len(rawMsg) != len(result) {
		t.Errorf("%q != %q", rawMsg, result)
		return
	}

	for i, v := range rawMsg {
		if v != result[i] {
			t.Errorf("%q != %q", rawMsg, result)
			break
		}
	}
}

func TestChannelOpenUnmarshal(t *testing.T) {
	rawMsg := []byte{
		0x03, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
		0x03, 0x00, 0x03, 0x66, 0x6f, 0x6f, 0x62, 0x61, 0x72,
	}

	msgUncast, err := parse(rawMsg)

	msg, ok := msgUncast.(*channelOpen)
	assert.True(t, ok, "Failed to cast to ChannelOpen")

	assert.NoError(t, err, "Unmarshal failed, ChannelOpen")
	assert.Equal(t, msg.ChannelType, ChannelTypeReliable, "ChannelType should be 0")
	assert.Equal(t, msg.Priority, uint16(0), "Priority should be 0")
	assert.Equal(t, msg.ReliabilityParameter, uint32(0), "ReliabilityParameter should be 0")
	assert.Equal(t, msg.Label, []uint8("foo"), "msg Label should be 'foo'")
	assert.Equal(t, msg.Protocol, []uint8("bar"), "msg protocol should be 'bar'")
}

func TestChannelAckUnmarshal(t *testing.T) {
	rawMsg := []byte{0x02}
	msgUncast, err := parse(rawMsg)
	if err != nil {
		t.Errorf("Failed to parse: %v", err)
		return
	}

	_, ok := msgUncast.(*channelAck)
	assert.True(t, ok, "Failed to cast to ChannelAck")
}