summaryrefslogtreecommitdiffhomepage
path: root/rfc1035label/label_test.go
blob: 3a69f2bb15a57af16b159d247c333cb18c1a8663 (plain)
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
package rfc1035label

import (
	"testing"

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

func TestLabelsFromBytes(t *testing.T) {
	labels, err := LabelsFromBytes([]byte{
		0x9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e',
		0x2, 'i', 't',
		0x0,
	})
	require.NoError(t, err)
	require.Equal(t, 1, len(labels))
	require.Equal(t, "slackware.it", labels[0])
}

func TestLabelsFromBytesZeroLength(t *testing.T) {
	labels, err := LabelsFromBytes([]byte{})
	require.NoError(t, err)
	require.Equal(t, 0, len(labels))
}

func TestLabelsFromBytesInvalidLength(t *testing.T) {
	labels, err := LabelsFromBytes([]byte{0x5, 0xaa, 0xbb}) // short length
	require.Error(t, err)
	require.Equal(t, 0, len(labels))
}

func TestLabelsFromBytesInvalidLengthOffByOne(t *testing.T) {
	labels, err := LabelsFromBytes([]byte{0x3, 0xaa, 0xbb}) // short length
	require.Error(t, err)
	require.Equal(t, 0, len(labels))
}

func TestLabelToBytes(t *testing.T) {
	encodedLabel := LabelToBytes("slackware.it")
	expected := []byte{
		0x9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e',
		0x2, 'i', 't',
		0x0,
	}
	require.Equal(t, expected, encodedLabel)
}

func TestLabelsToBytes(t *testing.T) {
	expected := []byte{
		9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e',
		2, 'i', 't',
		0,
		9, 'i', 'n', 's', 'o', 'm', 'n', 'i', 'a', 'c',
		9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e',
		2, 'i', 't',
		0,
	}
	encodedLabels := LabelsToBytes([]string{"slackware.it", "insomniac.slackware.it"})
	require.Equal(t, expected, encodedLabels)
}

func TestLabelToBytesZeroLength(t *testing.T) {
	encodedLabel := LabelToBytes("")
	expected := []byte{0}
	require.Equal(t, expected, encodedLabel)
}

func TestCompressedLabel(t *testing.T) {
	data := []byte{
		// slackware.it
		9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e',
		2, 'i', 't',
		0,
		// insomniac.slackware.it
		9, 'i', 'n', 's', 'o', 'm', 'n', 'i', 'a', 'c',
		192, 0,
		// mail.systemboot.org
		4, 'm', 'a', 'i', 'l',
		10, 's', 'y', 's', 't', 'e', 'm', 'b', 'o', 'o', 't',
		3, 'o', 'r', 'g',
		0,
		// systemboot.org
		192, 31,
	}
	expected := []string{
		"slackware.it",
		"insomniac.slackware.it",
		"mail.systemboot.org",
		"systemboot.org",
	}

	labels, err := LabelsFromBytes(data)
	require.NoError(t, err)
	require.Equal(t, expected, labels)
}

func TestShortCompressedLabel(t *testing.T) {
	data := []byte{
		// slackware.it
		9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e',
		2, 'i', 't',
		0,
		// insomniac.slackware.it
		9, 'i', 'n', 's', 'o', 'm', 'n', 'i', 'a', 'c',
		192,
	}

	_, err := LabelsFromBytes(data)
	require.Error(t, err)
}

func TestNestedCompressedLabel(t *testing.T) {
	data := []byte{
		// it
		3, 'i', 't',
		0,
		// slackware.it
		9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e',
		192, 0,
		// insomniac.slackware.it
		9, 'i', 'n', 's', 'o', 'm', 'n', 'i', 'a', 'c',
		192, 5,
	}
	_, err := LabelsFromBytes(data)
	require.Error(t, err)
}