Debian Codegraph @

plugins/ipam/dhcp/options_test.go

raw symbols SCIP
1 // Copyright 2015 CNI authors
2 //
3 // Licensed under the Apache License, Version 2.0 (the "License");
4 // you may not use this file except in compliance with the License.
5 // You may obtain a copy of the License at
6 //
7 // http://www.apache.org/licenses/LICENSE-2.0
8 //
9 // Unless required by applicable law or agreed to in writing, software
10 // distributed under the License is distributed on an "AS IS" BASIS,
11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 // See the License for the specific language governing permissions and
13 // limitations under the License.
14
15 package main
16
17 import (
18 "net"
19 "reflect"
20 "testing"
21
22 dhcp4 "github.com/insomniacslk/dhcp/dhcpv4"
23
24 "github.com/containernetworking/cni/pkg/types"
25 )
26
27 func validateRoutes(t *testing.T, routes []*types.Route) {
28 expected := []*types.Route{
29 {
30 Dst: net.IPNet{
31 IP: net.IPv4(10, 0, 0, 0),
32 Mask: net.CIDRMask(8, 32),
33 },
34 GW: net.IPv4(10, 1, 2, 3),
35 },
36 {
37 Dst: net.IPNet{
38 IP: net.IPv4(192, 168, 1, 0),
39 Mask: net.CIDRMask(24, 32),
40 },
41 GW: net.IPv4(192, 168, 2, 3),
42 },
43 }
44
45 if len(routes) != len(expected) {
46 t.Fatalf("wrong length slice; expected %v, got %v", len(expected), len(routes))
47 }
48
49 for i := 0; i < len(routes); i++ {
50 a := routes[i]
51 e := expected[i]
52
53 if a.Dst.String() != e.Dst.String() {
54 t.Errorf("route.Dst mismatch: expected %v, got %v", e.Dst, a.Dst)
55 }
56
57 if !a.GW.Equal(e.GW) {
58 t.Errorf("route.GW mismatch: expected %v, got %v", e.GW, a.GW)
59 }
60 }
61 }
62
63 func TestParseRoutes(t *testing.T) {
64 data := []byte{10, 0, 0, 0, 10, 1, 2, 3, 192, 168, 1, 0, 192, 168, 2, 3}
65 routes := parseRoutes(data)
66
67 validateRoutes(t, routes)
68 }
69
70 func TestParseOptionName(t *testing.T) {
71 tests := []struct {
72 name string
73 option string
74 want dhcp4.OptionCode
75 wantErr bool
76 }{
77 {
78 "hostname", "host-name", dhcp4.OptionHostName, false,
79 },
80 {
81 "hostname in number", "12", dhcp4.GenericOptionCode(12), false,
82 },
83 {
84 "random string", "doNotparseMe", dhcp4.OptionPad, true,
85 },
86 }
87 for _, tt := range tests {
88 t.Run(tt.name, func(t *testing.T) {
89 got, err := parseOptionName(tt.option)
90 if (err != nil) != tt.wantErr {
91 t.Errorf("parseOptionName() error = %v, wantErr %v", err, tt.wantErr)
92 return
93 }
94 if !reflect.DeepEqual(got, tt.want) {
95 t.Errorf("parseOptionName() = %v, want %v", got, tt.want)
96 }
97 })
98 }
99 }