1 // Copyright (c) 2020 Cisco and/or its affiliates.
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:
7 // http://www.apache.org/licenses/LICENSE-2.0
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.
21 . "github.com/onsi/gomega"
23 "git.fd.io/govpp.git/binapi/ethernet_types"
24 "git.fd.io/govpp.git/binapi/ip_types"
27 func TestGeneratedParseAddress(t *testing.T) {
32 result ip_types.Address
34 {"192.168.0.1", ip_types.Address{
35 Af: ip_types.ADDRESS_IP4,
36 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 0, 1}),
38 {"aac1:0:ab45::", ip_types.Address{
39 Af: ip_types.ADDRESS_IP6,
40 Un: ip_types.AddressUnionIP6(ip_types.IP6Address{170, 193, 0, 0, 171, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
44 for _, entry := range data {
45 t.Run(entry.input, func(t *testing.T) {
46 parsedAddress, err := ip_types.ParseAddress(entry.input)
47 Expect(err).ShouldNot(HaveOccurred())
48 Expect(parsedAddress).To(Equal(entry.result))
50 originAddress := parsedAddress.String()
51 Expect(originAddress).To(Equal(entry.input))
56 func TestGeneratedParseAddressError(t *testing.T) {
59 _, err := ip_types.ParseAddress("malformed_ip")
60 Expect(err).Should(HaveOccurred())
63 func TestGeneratedParsePrefix(t *testing.T) {
68 result ip_types.Prefix
70 {"192.168.0.1/24", ip_types.Prefix{
71 Address: ip_types.Address{
72 Af: ip_types.ADDRESS_IP4,
73 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 0, 1}),
77 {"192.168.0.1", ip_types.Prefix{
78 Address: ip_types.Address{
79 Af: ip_types.ADDRESS_IP4,
80 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 0, 1}),
84 {"aac1:0:ab45::/96", ip_types.Prefix{
85 Address: ip_types.Address{
86 Af: ip_types.ADDRESS_IP6,
87 Un: ip_types.AddressUnionIP6(ip_types.IP6Address{170, 193, 0, 0, 171, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
91 {"aac1:0:ab45::", ip_types.Prefix{
92 Address: ip_types.Address{
93 Af: ip_types.ADDRESS_IP6,
94 Un: ip_types.AddressUnionIP6(ip_types.IP6Address{170, 193, 0, 0, 171, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
100 for _, entry := range data {
101 t.Run(entry.input, func(t *testing.T) {
102 parsedAddress, err := ip_types.ParsePrefix(entry.input)
103 Expect(err).ShouldNot(HaveOccurred())
104 Expect(parsedAddress).To(Equal(entry.result))
106 // Parsed IP without prefix receives a default one
107 // so the input data must be adjusted
108 if entry.result.Address.Af == ip_types.ADDRESS_IP4 && !strings.Contains(entry.input, "/") {
109 entry.input = entry.input + "/32"
111 if entry.result.Address.Af == ip_types.ADDRESS_IP6 && !strings.Contains(entry.input, "/") {
112 entry.input = entry.input + "/128"
114 originAddress := parsedAddress.String()
115 Expect(originAddress).To(Equal(entry.input))
120 func TestGeneratedParsePrefixError(t *testing.T) {
123 _, err := ip_types.ParsePrefix("malformed_ip")
124 Expect(err).Should(HaveOccurred())
127 func TestGeneratedParseMAC(t *testing.T) {
130 var data = []struct {
132 result ethernet_types.MacAddress
134 {"b7:b9:bb:a1:5c:af", ethernet_types.MacAddress{183, 185, 187, 161, 92, 175}},
135 {"47:4b:c7:3e:06:c8", ethernet_types.MacAddress{71, 75, 199, 62, 6, 200}},
136 {"a7:cc:9f:10:18:e3", ethernet_types.MacAddress{167, 204, 159, 16, 24, 227}},
139 for _, entry := range data {
140 t.Run(entry.input, func(t *testing.T) {
141 parsedMac, err := ethernet_types.ParseMacAddress(entry.input)
142 Expect(err).ShouldNot(HaveOccurred())
143 Expect(parsedMac).To(Equal(entry.result))
145 originAddress := parsedMac.String()
146 Expect(originAddress).To(Equal(entry.input))
151 func TestGeneratedParseMACError(t *testing.T) {
154 _, err := ethernet_types.ParseMacAddress("malformed_mac")
155 Expect(err).Should(HaveOccurred())