Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 1 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 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 | **/ |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 17 | |
| 18 | /** |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 19 | * |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 20 | */ |
Jonathan Hart | deda0ba | 2014-04-03 11:14:12 -0700 | [diff] [blame] | 21 | package net.onrc.onos.core.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 | |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 28 | import org.junit.Test; |
| 29 | |
| 30 | /** |
| 31 | * @author David Erickson (daviderickson@cs.stanford.edu) |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 32 | */ |
| 33 | public class EthernetTest { |
| 34 | @Test |
| 35 | public void testToMACAddress() { |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 36 | byte[] address = new byte[]{0x0, 0x11, 0x22, (byte) 0xff, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 37 | (byte) 0xee, (byte) 0xdd}; |
| 38 | assertTrue(Arrays.equals(address, Ethernet |
| 39 | .toMACAddress("00:11:22:ff:ee:dd"))); |
| 40 | assertTrue(Arrays.equals(address, Ethernet |
| 41 | .toMACAddress("00:11:22:FF:EE:DD"))); |
| 42 | } |
| 43 | |
| 44 | @Test |
| 45 | public void testSerialize() { |
| 46 | Ethernet ethernet = new Ethernet() |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 47 | .setDestinationMACAddress("de:ad:be:ef:de:ad") |
| 48 | .setSourceMACAddress("be:ef:de:ad:be:ef") |
| 49 | .setEtherType((short) 0); |
| 50 | byte[] expected = new byte[]{(byte) 0xde, (byte) 0xad, (byte) 0xbe, |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 51 | (byte) 0xef, (byte) 0xde, (byte) 0xad, (byte) 0xbe, |
| 52 | (byte) 0xef, (byte) 0xde, (byte) 0xad, (byte) 0xbe, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 53 | (byte) 0xef, 0x0, 0x0}; |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 54 | assertTrue(Arrays.equals(expected, ethernet.serialize())); |
| 55 | } |
| 56 | |
| 57 | @Test |
| 58 | public void testToLong() { |
| 59 | assertEquals( |
| 60 | 281474976710655L, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 61 | Ethernet.toLong(new byte[]{(byte) 0xff, (byte) 0xff, |
| 62 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff})); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 63 | |
| 64 | assertEquals( |
| 65 | 1103823438081L, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 66 | Ethernet.toLong(new byte[]{(byte) 0x01, (byte) 0x01, |
| 67 | (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01})); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 68 | |
| 69 | assertEquals( |
| 70 | 141289400074368L, |
Ray Milkey | 269ffb9 | 2014-04-03 14:43:30 -0700 | [diff] [blame] | 71 | Ethernet.toLong(new byte[]{(byte) 0x80, (byte) 0x80, |
| 72 | (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80})); |
Umesh Krishnaswamy | 345ee99 | 2012-12-13 20:29:48 -0800 | [diff] [blame] | 73 | } |
| 74 | } |