2b5df66fd88514afcb4b50860914008c2b73862e
[honeycomb.git] / vpp-common / vpp-translate-utils / src / test / java / io / fd / honeycomb / translate / vpp / util / MacTranslatorTest.java
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at:
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16
17 package io.fd.honeycomb.translate.vpp.util;
18
19 import static org.junit.Assert.assertEquals;
20
21 import org.junit.Test;
22
23 public class MacTranslatorTest implements MacTranslator {
24
25     @Test
26     public void testParseMac() throws Exception {
27         byte[] bytes = parseMac("00:fF:7f:15:5e:A9");
28         assertMac(bytes);
29     }
30
31     private void assertMac(final byte[] bytes) {
32         assertEquals(6, bytes.length);
33         assertEquals((byte) 0, bytes[0]);
34         assertEquals((byte) 255, bytes[1]);
35         assertEquals((byte) 127, bytes[2]);
36         assertEquals((byte) 21, bytes[3]);
37         assertEquals((byte) 94, bytes[4]);
38         assertEquals((byte) 169, bytes[5]);
39     }
40
41     @Test(expected = IllegalArgumentException.class)
42     public void testParseMacLonger() throws Exception {
43         byte[] bytes = parseMac("00:fF:7f:15:5e:A9:88:77");
44         assertMac(bytes);
45     }
46
47     @Test(expected = IllegalArgumentException.class)
48     public void testParseMacShorter() throws Exception {
49         parseMac("00:fF:7f");
50     }
51
52     @Test(expected = IllegalArgumentException.class)
53     public void testParseRandomString() throws Exception {
54         parseMac("random{}}@$*&*!");
55     }
56
57     @Test(expected = NumberFormatException.class)
58     public void testParseMacNumberFormatEx() throws Exception {
59         parseMac("00:XX:7f:15:5e:77\"");
60     }
61
62     @Test
63     public void testByteArrayToMacUnseparated() {
64         byte[] address = parseMac("aa:bb:cc:dd:ee:ff");
65
66         String converted = byteArrayToMacUnseparated(address);
67
68         assertEquals("aabbccddeeff", converted);
69     }
70
71     @Test
72     public void testByteArrayToMacSeparated() {
73         byte[] address = parseMac("aa:bb:cc:dd:ee:ff");
74
75         String converted = byteArrayToMacSeparated(address);
76
77         assertEquals("aa:bb:cc:dd:ee:ff", converted);
78     }
79
80     @Test(expected = IllegalArgumentException.class)
81     public void testByteArrayToMacUnseparatedIllegal() {
82         byteArrayToMacUnseparated(new byte[]{54, 26, 87, 32, 14});
83     }
84
85     @Test(expected = IllegalArgumentException.class)
86     public void testByteArrayToMacSeparatedIllegal() {
87         byteArrayToMacSeparated(new byte[]{54, 26, 87, 32, 14});
88     }
89 }