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

import (
	"bytes"
	"testing"
)

func TestLabelsFromBytes(t *testing.T) {
	labels, err := LabelsFromBytes([]byte{
		0x9, 's', 'l', 'a', 'c', 'k', 'w', 'a', 'r', 'e',
		0x2, 'i', 't',
		0x0,
	})
	if err != nil {
		t.Fatal(err)
	}
	if len(labels) != 1 {
		t.Fatalf("Invalid labels length. Expected: 1, got: %v", len(labels))
	}
	if labels[0] != "slackware.it" {
		t.Fatalf("Invalid label. Expected: %v, got: %v'", "slackware.it", labels[0])
	}
}

func TestLabelsFromBytesZeroLength(t *testing.T) {
	labels, err := LabelsFromBytes([]byte{})
	if err != nil {
		t.Fatal(err)
	}
	if len(labels) != 0 {
		t.Fatalf("Invalid labels length. Expected: 0, got: %v", len(labels))
	}
}

func TestLabelsFromBytesInvalidLength(t *testing.T) {
	labels, err := LabelsFromBytes([]byte{0x3, 0xaa, 0xbb}) // short length
	if err == nil {
		t.Fatal("Expected error, got nil")
	}
	if len(labels) != 0 {
		t.Fatalf("Invalid labels length. Expected: 0, got: %v", len(labels))
	}
	if labels != nil {
		t.Fatalf("Invalid label. Expected nil, got %v", 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,
	}
	if !bytes.Equal(encodedLabel, expected) {
		t.Fatalf("Invalid label. Expected: %v, got: %v", expected, encodedLabel)
	}
}

func TestLabelToBytesZeroLength(t *testing.T) {
	encodedLabel := LabelToBytes("")
	expected := []byte{0}
	if !bytes.Equal(encodedLabel, expected) {
		t.Fatalf("Invalid label. Expected: %v, got: %v", expected, encodedLabel)
	}
}