binapigen: fix union size
[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
21         . "github.com/onsi/gomega"
22
23         "git.fd.io/govpp.git/binapi/ethernet_types"
24         "git.fd.io/govpp.git/binapi/ip_types"
25 )
26
27 func TestGeneratedParseAddress(t *testing.T) {
28         RegisterTestingT(t)
29
30         var data = []struct {
31                 input  string
32                 result ip_types.Address
33         }{
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}),
37                 }},
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}),
41                 }},
42         }
43
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))
49
50                         originAddress := parsedAddress.String()
51                         Expect(originAddress).To(Equal(entry.input))
52                 })
53         }
54 }
55
56 func TestGeneratedParseAddressError(t *testing.T) {
57         RegisterTestingT(t)
58
59         _, err := ip_types.ParseAddress("malformed_ip")
60         Expect(err).Should(HaveOccurred())
61 }
62
63 func TestGeneratedParsePrefix(t *testing.T) {
64         RegisterTestingT(t)
65
66         var data = []struct {
67                 input  string
68                 result ip_types.Prefix
69         }{
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}),
74                         },
75                         Len: 24,
76                 }},
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}),
81                         },
82                         Len: 32,
83                 }},
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}),
88                         },
89                         Len: 96,
90                 }},
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}),
95                         },
96                         Len: 128,
97                 }},
98         }
99
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))
105
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"
110                         }
111                         if entry.result.Address.Af == ip_types.ADDRESS_IP6 && !strings.Contains(entry.input, "/") {
112                                 entry.input = entry.input + "/128"
113                         }
114                         originAddress := parsedAddress.String()
115                         Expect(originAddress).To(Equal(entry.input))
116                 })
117         }
118 }
119
120 func TestGeneratedParsePrefixError(t *testing.T) {
121         RegisterTestingT(t)
122
123         _, err := ip_types.ParsePrefix("malformed_ip")
124         Expect(err).Should(HaveOccurred())
125 }
126
127 func TestGeneratedParseMAC(t *testing.T) {
128         RegisterTestingT(t)
129
130         var data = []struct {
131                 input  string
132                 result ethernet_types.MacAddress
133         }{
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}},
137         }
138
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))
144
145                         originAddress := parsedMac.String()
146                         Expect(originAddress).To(Equal(entry.input))
147                 })
148         }
149 }
150
151 func TestGeneratedParseMACError(t *testing.T) {
152         RegisterTestingT(t)
153
154         _, err := ethernet_types.ParseMacAddress("malformed_mac")
155         Expect(err).Should(HaveOccurred())
156 }