blob: dc25b124bb3bdf2cec58cfd113da60002bf8d522 [file] [log] [blame]
Yotam Harcholf3f11152013-09-05 16:47:16 -07001package org.projectfloodlight.openflow.types;
2
3import static org.junit.Assert.assertArrayEquals;
4import static org.junit.Assert.assertEquals;
5import static org.junit.Assert.fail;
6
7import java.util.Arrays;
8
9import org.jboss.netty.buffer.ChannelBuffers;
10import org.junit.Test;
11import org.projectfloodlight.openflow.exceptions.OFParseError;
12
13public class MacAddressTest {
14 byte[][] testAddresses = new byte[][] {
15 {0x01, 0x02, 0x03, 0x04, 0x05, 0x06 },
16 {(byte) 0x80, 0x0, 0x0, 0x0, 0x0, 0x01},
17 {(byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255, (byte) 255 }
18 };
19
20 String[] testStrings = {
21 "01:02:03:04:05:06",
22 "80:00:00:00:00:01",
23 "ff:ff:ff:ff:ff:ff"
24 };
25
26 long[] testInts = {
27 0x00010203040506L,
28 0x00800000000001L,
29 0x00ffffffffffffL
30 };
31
32 String[] invalidMacs = {
33 "",
34 "1.2.3.4",
35 "00:ff:ef:12:12:ff:",
36 "00:fff:ef:12:12:ff",
37 "01:02:03:04:05;06",
38 "0:1:2:3:4:5:6",
39 "01:02:03:04"
40 };
41
42
43 @Test
44 public void testOfString() {
45 for(int i=0; i < testAddresses.length; i++ ) {
46 MacAddress ip = MacAddress.of(testStrings[i]);
47 assertEquals(testInts[i], ip.getLong());
48 assertArrayEquals(testAddresses[i], ip.getBytes());
49 assertEquals(testStrings[i], ip.toString());
50 }
51 }
52
53 @Test
54 public void testOfByteArray() {
55 for(int i=0; i < testAddresses.length; i++ ) {
56 MacAddress ip = MacAddress.of(testAddresses[i]);
57 assertEquals("error checking long representation of "+Arrays.toString(testAddresses[i]) + "(should be "+Long.toHexString(testInts[i]) +")", testInts[i], ip.getLong());
58 assertArrayEquals(testAddresses[i], ip.getBytes());
59 assertEquals(testStrings[i], ip.toString());
60 }
61 }
62
63 @Test
64 public void testReadFrom() throws OFParseError {
65 for(int i=0; i < testAddresses.length; i++ ) {
66 MacAddress ip = MacAddress.read6Bytes(ChannelBuffers.copiedBuffer(testAddresses[i]));
67 assertEquals(testInts[i], ip.getLong());
68 assertArrayEquals(testAddresses[i], ip.getBytes());
69 assertEquals(testStrings[i], ip.toString());
70 }
71 }
72
73
74 @Test
75 public void testInvalidMacss() throws OFParseError {
76 for(String invalid : invalidMacs) {
77 try {
78 MacAddress.of(invalid);
79 fail("Invalid IP "+invalid+ " should have raised IllegalArgumentException");
80 } catch(IllegalArgumentException e) {
81 // ok
82 }
83 }
84 }
85}