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