summaryrefslogtreecommitdiffhomepage
path: root/rfc1035label/label_test.go
blob: 865b41ac26d65f66128a0d3ddaa7aa12f128657f (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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package rfc1035label

import (
	"testing"

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

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

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

func TestLabelsFromBytesPartialDomainName(t *testing.T) {
	// Partial domain name without trailing zero-length byte as per RFC 4704
	// Section 4.2
	expected := []byte{
		0x8, 'h', 'o', 's', 't', 'n', 'a', 'm', 'e',
	}
	labels, err := FromBytes(expected)
	require.NoError(t, err)
	require.Equal(t, 1, len(labels.Labels))
	require.Equal(t, len(expected), labels.Length())
	require.Equal(t, expected, labels.ToBytes())
	require.Equal(t, "hostname", labels.Labels[0])
}

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

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

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,
	}
	labels := Labels{
		Labels: []string{
			"slackware.it",
			"insomniac.slackware.it",
		},
	}
	require.Equal(t, expected, labels.ToBytes())
}

func TestLabelToBytesZeroLength(t *testing.T) {
	labels := Labels{
		Labels: []string{""},
	}
	require.Equal(t, []byte{0}, labels.ToBytes())
}

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 := FromBytes(data)
	require.NoError(t, err)
	require.Equal(t, 4, len(labels.Labels))
	require.Equal(t, expected, labels.Labels)
	require.Equal(t, len(data), labels.Length())
}

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 := FromBytes(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 := FromBytes(data)
	require.Error(t, err)
}