075dd8e65ae78ee3b56aa6930a8b30457e037f01
[govpp.git] / binapigen / gen_helpers_test.go
1 //  Copyright (c) 2020 Cisco and/or its affiliates.
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 binapigen
16
17 import (
18         "strings"
19         "testing"
20         "time"
21
22         . "github.com/onsi/gomega"
23
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"
27 )
28
29 func TestGeneratedParseAddress(t *testing.T) {
30         RegisterTestingT(t)
31
32         var data = []struct {
33                 input  string
34                 result ip_types.Address
35         }{
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}),
39                 }},
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}),
43                 }},
44         }
45
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))
51
52                         originAddress := parsedAddress.String()
53                         Expect(originAddress).To(Equal(entry.input))
54                 })
55         }
56 }
57
58 func TestGeneratedParseAddressError(t *testing.T) {
59         RegisterTestingT(t)
60
61         _, err := ip_types.ParseAddress("malformed_ip")
62         Expect(err).Should(HaveOccurred())
63 }
64
65 func TestGeneratedParsePrefix(t *testing.T) {
66         RegisterTestingT(t)
67
68         var data = []struct {
69                 input  string
70                 result ip_types.Prefix
71         }{
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}),
76                         },
77                         Len: 24,
78                 }},
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}),
83                         },
84                         Len: 32,
85                 }},
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}),
90                         },
91                         Len: 96,
92                 }},
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}),
97                         },
98                         Len: 128,
99                 }},
100         }
101
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))
107
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"
112                         }
113                         if entry.result.Address.Af == ip_types.ADDRESS_IP6 && !strings.Contains(entry.input, "/") {
114                                 entry.input = entry.input + "/128"
115                         }
116                         originAddress := parsedAddress.String()
117                         Expect(originAddress).To(Equal(entry.input))
118                 })
119         }
120 }
121
122 func TestGeneratedParsePrefixError(t *testing.T) {
123         RegisterTestingT(t)
124
125         _, err := ip_types.ParsePrefix("malformed_ip")
126         Expect(err).Should(HaveOccurred())
127 }
128
129 func TestGeneratedParseMAC(t *testing.T) {
130         RegisterTestingT(t)
131
132         var data = []struct {
133                 input  string
134                 result ethernet_types.MacAddress
135         }{
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}},
139         }
140
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))
146
147                         originAddress := parsedMac.String()
148                         Expect(originAddress).To(Equal(entry.input))
149                 })
150         }
151 }
152
153 func TestGeneratedParseMACError(t *testing.T) {
154         RegisterTestingT(t)
155
156         _, err := ethernet_types.ParseMacAddress("malformed_mac")
157         Expect(err).Should(HaveOccurred())
158 }
159
160 func TestGeneratedParseTimestamp(t *testing.T) {
161         RegisterTestingT(t)
162
163         var data = []struct {
164                 input  time.Time
165                 result vpe_types.Timestamp
166         }{
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)},
170         }
171
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))
176
177                         Expect(entry.input.Equal(ts.ToTime())).To(BeTrue())
178
179                         originTime := ts.String()
180                         Expect(originTime).To(Equal(entry.input.Local().String()))
181                 })
182         }
183 }