Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
| 2 | * Copyright 2011, Big Switch Networks, Inc. |
| 3 | * Originally created by David Erickson, Stanford University |
| 4 | * |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); you may |
| 6 | * not use this file except in compliance with the License. You may obtain |
| 7 | * a copy of the License at |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT |
| 13 | * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the |
| 14 | * License for the specific language governing permissions and limitations |
| 15 | * under the License. |
| 16 | **/ |
| 17 | |
| 18 | /** |
| 19 | * |
| 20 | */ |
Jonathan Hart | 96892d1 | 2014-03-26 20:21:29 -0700 | [diff] [blame] | 21 | package net.onrc.onos.packet; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 22 | |
| 23 | import static org.junit.Assert.assertEquals; |
| 24 | import static org.junit.Assert.assertTrue; |
| 25 | |
| 26 | import java.util.Arrays; |
| 27 | |
Jonathan Hart | 96892d1 | 2014-03-26 20:21:29 -0700 | [diff] [blame] | 28 | import net.onrc.onos.packet.Ethernet; |
| 29 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 30 | import org.junit.Test; |
| 31 | |
| 32 | /** |
| 33 | * @author David Erickson (daviderickson@cs.stanford.edu) |
| 34 | * |
| 35 | */ |
| 36 | public class EthernetTest { |
| 37 | @Test |
| 38 | public void testToMACAddress() { |
| 39 | byte[] address = new byte[] { 0x0, 0x11, 0x22, (byte) 0xff, |
| 40 | (byte) 0xee, (byte) 0xdd}; |
| 41 | assertTrue(Arrays.equals(address, Ethernet |
| 42 | .toMACAddress("00:11:22:ff:ee:dd"))); |
| 43 | assertTrue(Arrays.equals(address, Ethernet |
| 44 | .toMACAddress("00:11:22:FF:EE:DD"))); |
| 45 | } |
| 46 | |
| 47 | @Test |
| 48 | public void testSerialize() { |
| 49 | Ethernet ethernet = new Ethernet() |
| 50 | .setDestinationMACAddress("de:ad:be:ef:de:ad") |
| 51 | .setSourceMACAddress("be:ef:de:ad:be:ef") |
| 52 | .setEtherType((short) 0); |
| 53 | byte[] expected = new byte[] { (byte) 0xde, (byte) 0xad, (byte) 0xbe, |
| 54 | (byte) 0xef, (byte) 0xde, (byte) 0xad, (byte) 0xbe, |
| 55 | (byte) 0xef, (byte) 0xde, (byte) 0xad, (byte) 0xbe, |
| 56 | (byte) 0xef, 0x0, 0x0 }; |
| 57 | assertTrue(Arrays.equals(expected, ethernet.serialize())); |
| 58 | } |
| 59 | |
| 60 | @Test |
| 61 | public void testToLong() { |
| 62 | assertEquals( |
| 63 | 281474976710655L, |
| 64 | Ethernet.toLong(new byte[] { (byte) 0xff, (byte) 0xff, |
| 65 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff })); |
| 66 | |
| 67 | assertEquals( |
| 68 | 1103823438081L, |
| 69 | Ethernet.toLong(new byte[] { (byte) 0x01, (byte) 0x01, |
| 70 | (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01 })); |
| 71 | |
| 72 | assertEquals( |
| 73 | 141289400074368L, |
| 74 | Ethernet.toLong(new byte[] { (byte) 0x80, (byte) 0x80, |
| 75 | (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80 })); |
| 76 | } |
| 77 | } |