summaryrefslogtreecommitdiffhomepage
path: root/dhcpv4/options_test.go
blob: 07be9bf08fa75d1f8b08292d55aeff4079f82582 (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
142
143
144
145
146
147
148
package dhcpv4

import (
	"bytes"
	"testing"
)

func TestParseOption(t *testing.T) {
	option := []byte{5, 4, 192, 168, 1, 254} // DNS option
	opt, err := ParseOption(option)
	if err != nil {
		t.Fatal(err)
	}
	if opt.Code != OptionNameServer {
		t.Fatalf("Invalid option code. Expected 5, got %v", opt.Code)
	}
	if !bytes.Equal(opt.Data, option[2:]) {
		t.Fatalf("Invalid option data. Expected %v, got %v", option[2:], opt.Data)
	}
}

func TestParseOptionPad(t *testing.T) {
	option := []byte{0}
	opt, err := ParseOption(option)
	if err != nil {
		t.Fatal(err)
	}
	if opt.Code != OptionPad {
		t.Fatalf("Invalid option code. Expected %v, got %v", OptionPad, opt.Code)
	}
	if len(opt.Data) != 0 {
		t.Fatalf("Invalid option data. Expected empty slice, got %v", opt.Data)
	}
}

func TestParseOptionZeroLength(t *testing.T) {
	option := []byte{}
	_, err := ParseOption(option)
	if err == nil {
		t.Fatal("Expected an error, got none")
	}
}

func TestParseOptionShortOption(t *testing.T) {
	option := []byte{53, 1}
	_, err := ParseOption(option)
	if err == nil {
		t.Fatal(err)
	}
}

func TestOptionsFromBytes(t *testing.T) {
	options := []byte{
		99, 130, 83, 99, // Magic Cookie
		5, 4, 192, 168, 1, 1, // DNS
		255,     // end
		0, 0, 0, //padding
	}
	opts, err := OptionsFromBytes(options)
	if err != nil {
		t.Fatal(err)
	}
	// each padding byte counts as an option. Magic Cookie doesn't add up
	if len(opts) != 5 {
		t.Fatal("Invalid options length. Expected 5, got %v", len(opts))
	}
	if opts[0].Code != OptionNameServer {
		t.Fatal("Invalid option code. Expected %v, got %v", OptionNameServer, opts[0].Code)
	}
	if !bytes.Equal(opts[0].Data, options[6:10]) {
		t.Fatal("Invalid option data. Expected %v, got %v", options[6:10], opts[0].Data)
	}
	if opts[1].Code != OptionEnd {
		t.Fatalf("Invalid option code. Expected %v, got %v", OptionEnd, opts[1].Code)
	}
	if opts[2].Code != OptionPad {
		t.Fatalf("Invalid option code. Expected %v, got %v", OptionPad, opts[2].Code)
	}
}

func TestOptionsFromBytesZeroLength(t *testing.T) {
	options := []byte{}
	_, err := OptionsFromBytes(options)
	if err == nil {
		t.Fatal("Expected an error, got none")
	}
}

func TestOptionsFromBytesBadMagicCookie(t *testing.T) {
	options := []byte{1, 2, 3, 4}
	_, err := OptionsFromBytes(options)
	if err == nil {
		t.Fatal("Expected an error, got none")
	}
}

func TestOptionsToBytes(t *testing.T) {
	originalOptions := []byte{
		99, 130, 83, 99, // Magic Cookie
		5, 4, 192, 168, 1, 1, // DNS
		255,     // end
		0, 0, 0, //padding
	}
	options, err := OptionsFromBytes(originalOptions)
	if err != nil {
		t.Fatal(err)
	}
	finalOptions := OptionsToBytes(options)
	if !bytes.Equal(originalOptions, finalOptions) {
		t.Fatalf("Invalid options. Expected %v, got %v", originalOptions, finalOptions)
	}
}

func TestOptionsToBytesEmpty(t *testing.T) {
	originalOptions := []byte{99, 130, 83, 99}
	options, err := OptionsFromBytes(originalOptions)
	if err != nil {
		t.Fatal(err)
	}
	finalOptions := OptionsToBytes(options)
	if !bytes.Equal(originalOptions, finalOptions) {
		t.Fatalf("Invalid options. Expected %v, got %v", originalOptions, finalOptions)
	}
}

func TestOptionsToStringPad(t *testing.T) {
	option := []byte{0}
	opt, err := ParseOption(option)
	if err != nil {
		t.Fatal(err)
	}
	stropt := opt.String()
	if stropt != "Pad -> []" {
		t.Fatalf("Invalid string representation: %v", stropt)
	}
}

func TestOptionsToStringDHCPMessageType(t *testing.T) {
	option := []byte{53, 1, 5}
	opt, err := ParseOption(option)
	if err != nil {
		t.Fatal(err)
	}
	stropt := opt.String()
	if stropt != "DHCP Message Type -> [5]" {
		t.Fatalf("Invalid string representation: %v", stropt)
	}
}