3b7efbfc5a35ad23c0040be583d3948a67f5a170
[hc2vpp.git] /
1 package io.fd.honeycomb.translate.v3po.util;
2
3 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.reverseAddress;
4 import static io.fd.honeycomb.translate.v3po.util.TranslateUtils.reverseBytes;
5 import static org.junit.Assert.assertArrayEquals;
6 import static org.junit.Assert.assertEquals;
7 import static org.junit.Assert.assertTrue;
8 import static org.junit.Assert.fail;
9 import static org.mockito.Matchers.anyLong;
10 import static org.mockito.Matchers.eq;
11 import static org.mockito.Mockito.mock;
12 import static org.mockito.Mockito.when;
13
14 import java.util.concurrent.Future;
15 import java.util.concurrent.TimeUnit;
16 import java.util.concurrent.TimeoutException;
17 import org.junit.Test;
18 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.IpAddress;
19 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Address;
20 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4AddressNoZone;
21 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv4Prefix;
22 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Address;
23 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6AddressNoZone;
24 import org.opendaylight.yang.gen.v1.urn.ietf.params.xml.ns.yang.ietf.inet.types.rev130715.Ipv6Prefix;
25 import org.opendaylight.yangtools.yang.binding.DataContainer;
26 import org.opendaylight.yangtools.yang.binding.DataObject;
27 import org.opendaylight.yangtools.yang.binding.InstanceIdentifier;
28 import org.openvpp.jvpp.dto.JVppReply;
29
30 public class TranslateUtilsTest {
31
32     private static class AnDataObject implements DataObject {
33         @Override
34         public Class<? extends DataContainer> getImplementedInterface() {
35             return null;
36         }
37     }
38
39     @Test
40     public void testGetReplyForWriteTimeout() throws Exception {
41         final Future<JVppReply<?>> future = mock(Future.class);
42         when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
43         final InstanceIdentifier<AnDataObject> replyType = InstanceIdentifier.create(AnDataObject.class);
44         try {
45             TranslateUtils.getReplyForWrite(future, replyType);
46         } catch (WriteTimeoutException e) {
47             assertTrue(e.getCause() instanceof TimeoutException);
48             assertEquals(replyType, e.getFailedId());
49             return;
50         }
51         fail("WriteTimeoutException was expected");
52     }
53
54     @Test
55     public void testGetReplyForReadTimeout() throws Exception {
56         final Future<JVppReply<?>> future = mock(Future.class);
57         final InstanceIdentifier<AnDataObject> replyType = InstanceIdentifier.create(AnDataObject.class);
58         when(future.get(anyLong(), eq(TimeUnit.SECONDS))).thenThrow(TimeoutException.class);
59         try {
60             TranslateUtils.getReplyForRead(future, replyType);
61         } catch (ReadTimeoutException e) {
62             assertTrue(e.getCause() instanceof TimeoutException);
63             assertEquals(replyType, e.getFailedId());
64             return;
65         }
66         fail("ReadTimeoutException was expected");
67     }
68
69     @Test
70     public void testIpv4NoZone() throws Exception {
71         final Ipv4AddressNoZone ipv4Addr = new Ipv4AddressNoZone("192.168.1.1");
72         byte[] bytes = TranslateUtils.ipv4AddressNoZoneToArray(ipv4Addr);
73         assertEquals((byte) 192, bytes[0]);
74         // Simulating the magic of VPP
75         bytes = reverseBytes(bytes);
76         final Ipv4AddressNoZone ipv4AddressNoZone = TranslateUtils.arrayToIpv4AddressNoZone(bytes);
77         assertEquals(ipv4Addr, ipv4AddressNoZone);
78     }
79
80     @Test
81     public void testToString() {
82         final byte[] expected = "test".getBytes();
83         final byte[] cString = new byte[expected.length + 10];
84         System.arraycopy(expected, 0, cString, 0, expected.length);
85         final String jString = TranslateUtils.toString(cString);
86         assertArrayEquals(expected, jString.getBytes());
87     }
88
89     @Test
90     public void testParseMac() throws Exception {
91         byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9");
92         assertMac(bytes);
93     }
94
95     private void assertMac(final byte[] bytes) {
96         assertEquals(6, bytes.length);
97         assertEquals((byte) 0, bytes[0]);
98         assertEquals((byte) 255, bytes[1]);
99         assertEquals((byte) 127, bytes[2]);
100         assertEquals((byte) 21, bytes[3]);
101         assertEquals((byte) 94, bytes[4]);
102         assertEquals((byte) 169, bytes[5]);
103     }
104
105     @Test(expected = IllegalArgumentException.class)
106     public void testParseMacLonger() throws Exception {
107         byte[] bytes = TranslateUtils.parseMac("00:fF:7f:15:5e:A9:88:77");
108         assertMac(bytes);
109     }
110
111     @Test(expected = IllegalArgumentException.class)
112     public void testParseMacShorter() throws Exception {
113         TranslateUtils.parseMac("00:fF:7f");
114     }
115
116     @Test(expected = IllegalArgumentException.class)
117     public void testParseRandomString() throws Exception {
118         TranslateUtils.parseMac("random{}}@$*&*!");
119     }
120
121     @Test(expected = NumberFormatException.class)
122     public void testParseMacNumberFormatEx() throws Exception {
123         TranslateUtils.parseMac("00:XX:7f:15:5e:77\"");
124     }
125
126     @Test
127     public void testBooleanToByte() {
128         assertEquals(0, TranslateUtils.booleanToByte(null));
129         assertEquals(0, TranslateUtils.booleanToByte(false));
130         assertEquals(1, TranslateUtils.booleanToByte(true));
131     }
132
133     @Test
134     public void testByteToBoolean() {
135         assertEquals(Boolean.FALSE, TranslateUtils.byteToBoolean((byte) 0));
136         assertEquals(Boolean.TRUE, TranslateUtils.byteToBoolean((byte) 1));
137     }
138
139     @Test(expected = IllegalArgumentException.class)
140     public void testByteToBooleanFailed() {
141         TranslateUtils.byteToBoolean((byte) 123);
142     }
143
144     @Test
145     public void testIpv6NoZone() {
146         final Ipv6AddressNoZone ipv6Addr = new Ipv6AddressNoZone("3ffe:1900:4545:3:200:f8ff:fe21:67cf");
147         byte[] bytes = TranslateUtils.ipv6AddressNoZoneToArray(ipv6Addr);
148         assertEquals((byte) 63, bytes[0]);
149
150         bytes = reverseBytes(bytes);
151         final Ipv6AddressNoZone ivp6AddressNoZone = TranslateUtils.arrayToIpv6AddressNoZone(bytes);
152         assertEquals(ipv6Addr, ivp6AddressNoZone);
153     }
154
155     @Test
156     public void testByteArrayToMacUnseparated() {
157         byte[] address = TranslateUtils.parseMac("aa:bb:cc:dd:ee:ff");
158
159         String converted = TranslateUtils.byteArrayToMacUnseparated(address);
160
161         assertEquals("aabbccddeeff", converted);
162     }
163
164     @Test
165     public void testByteArrayToMacSeparated() {
166         byte[] address = TranslateUtils.parseMac("aa:bb:cc:dd:ee:ff");
167
168         String converted = TranslateUtils.byteArrayToMacSeparated(address);
169
170         assertEquals("aa:bb:cc:dd:ee:ff", converted);
171     }
172
173     @Test(expected = IllegalArgumentException.class)
174     public void testByteArrayToMacUnseparatedIllegal() {
175         TranslateUtils.byteArrayToMacUnseparated(new byte[]{54, 26, 87, 32, 14});
176     }
177
178     @Test(expected = IllegalArgumentException.class)
179     public void testByteArrayToMacSeparatedIllegal() {
180         TranslateUtils.byteArrayToMacSeparated(new byte[]{54, 26, 87, 32, 14});
181     }
182
183     @Test
184     public void testIpv4AddressPrefixToArray() {
185         byte[] ip = TranslateUtils.ipv4AddressPrefixToArray(new Ipv4Prefix("192.168.2.1/24"));
186
187         assertEquals("1.2.168.192", TranslateUtils.arrayToIpv4AddressNoZone(ip).getValue());
188     }
189
190     @Test
191     public void testIpv6AddressPrefixToArray() {
192         byte[] ip = TranslateUtils.ipv6AddressPrefixToArray(new Ipv6Prefix("3ffe:1900:4545:3:200:f8ff:fe21:67cf/48"));
193
194         assertEquals("cf67:21fe:fff8:2:300:4545:19:fe3f", TranslateUtils.arrayToIpv6AddressNoZone(ip).getValue());
195     }
196
197     @Test
198     public void testExtractPrefix() {
199         assertEquals(24, TranslateUtils.extractPrefix(new Ipv4Prefix("192.168.2.1/24")));
200         assertEquals(48, TranslateUtils.extractPrefix(new Ipv6Prefix("3ffe:1900:4545:3:200:f8ff:fe21:67cf/48")));
201     }
202
203     @Test
204     public void testRevertAddress() {
205         assertEquals("1.2.168.192",
206                 reverseAddress(new IpAddress(new Ipv4Address("192.168.2.1"))).getIpv4Address().getValue());
207         assertEquals("3473:7003:2e8a::a385:b80d:120",
208                 reverseAddress(new IpAddress(new Ipv6Address("2001:db8:85a3:0:0:8a2e:370:7334"))).getIpv6Address()
209                         .getValue());
210     }
211 }