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.
22 . "github.com/onsi/gomega"
24 "git.fd.io/govpp.git/binapi/ethernet_types"
25 "git.fd.io/govpp.git/binapi/ip_types"
26 "git.fd.io/govpp.git/binapi/vpe_types"
29 func TestGeneratedParseAddress(t *testing.T) {
34 result ip_types.Address
36 {"192.168.0.1", ip_types.Address{
37 Af: ip_types.ADDRESS_IP4,
38 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 0, 1}),
40 {"aac1:0:ab45::", ip_types.Address{
41 Af: ip_types.ADDRESS_IP6,
42 Un: ip_types.AddressUnionIP6(ip_types.IP6Address{170, 193, 0, 0, 171, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
46 for _, entry := range data {
47 t.Run(entry.input, func(t *testing.T) {
48 parsedAddress, err := ip_types.ParseAddress(entry.input)
49 Expect(err).ShouldNot(HaveOccurred())
50 Expect(parsedAddress).To(Equal(entry.result))
52 originAddress := parsedAddress.String()
53 Expect(originAddress).To(Equal(entry.input))
58 func TestGeneratedParseAddressError(t *testing.T) {
61 _, err := ip_types.ParseAddress("malformed_ip")
62 Expect(err).Should(HaveOccurred())
65 func TestGeneratedParsePrefix(t *testing.T) {
70 result ip_types.Prefix
72 {"192.168.0.1/24", ip_types.Prefix{
73 Address: ip_types.Address{
74 Af: ip_types.ADDRESS_IP4,
75 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 0, 1}),
79 {"192.168.0.1", ip_types.Prefix{
80 Address: ip_types.Address{
81 Af: ip_types.ADDRESS_IP4,
82 Un: ip_types.AddressUnionIP4(ip_types.IP4Address{192, 168, 0, 1}),
86 {"aac1:0:ab45::/96", ip_types.Prefix{
87 Address: ip_types.Address{
88 Af: ip_types.ADDRESS_IP6,
89 Un: ip_types.AddressUnionIP6(ip_types.IP6Address{170, 193, 0, 0, 171, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
93 {"aac1:0:ab45::", ip_types.Prefix{
94 Address: ip_types.Address{
95 Af: ip_types.ADDRESS_IP6,
96 Un: ip_types.AddressUnionIP6(ip_types.IP6Address{170, 193, 0, 0, 171, 69, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0}),
102 for _, entry := range data {
103 t.Run(entry.input, func(t *testing.T) {
104 parsedAddress, err := ip_types.ParsePrefix(entry.input)
105 Expect(err).ShouldNot(HaveOccurred())
106 Expect(parsedAddress).To(Equal(entry.result))
108 // Parsed IP without prefix receives a default one
109 // so the input data must be adjusted
110 if entry.result.Address.Af == ip_types.ADDRESS_IP4 && !strings.Contains(entry.input, "/") {
111 entry.input = entry.input + "/32"
113 if entry.result.Address.Af == ip_types.ADDRESS_IP6 && !strings.Contains(entry.input, "/") {
114 entry.input = entry.input + "/128"
116 originAddress := parsedAddress.String()
117 Expect(originAddress).To(Equal(entry.input))
122 func TestGeneratedParsePrefixError(t *testing.T) {
125 _, err := ip_types.ParsePrefix("malformed_ip")
126 Expect(err).Should(HaveOccurred())
129 func TestGeneratedParseMAC(t *testing.T) {
132 var data = []struct {
134 result ethernet_types.MacAddress
136 {"b7:b9:bb:a1:5c:af", ethernet_types.MacAddress{183, 185, 187, 161, 92, 175}},
137 {"47:4b:c7:3e:06:c8", ethernet_types.MacAddress{71, 75, 199, 62, 6, 200}},
138 {"a7:cc:9f:10:18:e3", ethernet_types.MacAddress{167, 204, 159, 16, 24, 227}},
141 for _, entry := range data {
142 t.Run(entry.input, func(t *testing.T) {
143 parsedMac, err := ethernet_types.ParseMacAddress(entry.input)
144 Expect(err).ShouldNot(HaveOccurred())
145 Expect(parsedMac).To(Equal(entry.result))
147 originAddress := parsedMac.String()
148 Expect(originAddress).To(Equal(entry.input))
153 func TestGeneratedParseMACError(t *testing.T) {
156 _, err := ethernet_types.ParseMacAddress("malformed_mac")
157 Expect(err).Should(HaveOccurred())
160 func TestGeneratedParseTimestamp(t *testing.T) {
163 var data = []struct {
165 result vpe_types.Timestamp
167 {time.Unix(0, 0), vpe_types.Timestamp(0)},
168 {time.Date(2000, 1, 1, 0, 0, 0, 0, time.UTC),
169 vpe_types.Timestamp(9.466848e+08)},
172 for _, entry := range data {
173 t.Run(entry.input.String(), func(t *testing.T) {
174 ts := vpe_types.NewTimestamp(entry.input)
175 Expect(ts).To(Equal(entry.result))
177 Expect(entry.input.Equal(ts.ToTime())).To(BeTrue())
179 originTime := ts.String()
180 Expect(originTime).To(Equal(entry.input.Local().String()))