blob: 9ee70644399c1960beb9294d1487ed13adb59f18 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
Ray Milkey269ffb92014-04-03 14:43:30 -07002 * 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 Krishnaswamy345ee992012-12-13 20:29:48 -080017
18/**
Ray Milkey269ffb92014-04-03 14:43:30 -070019 *
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080020 */
Jonathan Hartdeda0ba2014-04-03 11:14:12 -070021package net.onrc.onos.core.packet;
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080022
23import static org.junit.Assert.assertEquals;
24import static org.junit.Assert.assertTrue;
25
26import java.util.Arrays;
27
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080028import org.junit.Test;
29
30/**
31 * @author David Erickson (daviderickson@cs.stanford.edu)
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080032 */
33public class EthernetTest {
34 @Test
35 public void testToMACAddress() {
Ray Milkey269ffb92014-04-03 14:43:30 -070036 byte[] address = new byte[]{0x0, 0x11, 0x22, (byte) 0xff,
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080037 (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 Milkey269ffb92014-04-03 14:43:30 -070047 .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 Krishnaswamy345ee992012-12-13 20:29:48 -080051 (byte) 0xef, (byte) 0xde, (byte) 0xad, (byte) 0xbe,
52 (byte) 0xef, (byte) 0xde, (byte) 0xad, (byte) 0xbe,
Ray Milkey269ffb92014-04-03 14:43:30 -070053 (byte) 0xef, 0x0, 0x0};
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080054 assertTrue(Arrays.equals(expected, ethernet.serialize()));
55 }
56
57 @Test
58 public void testToLong() {
59 assertEquals(
60 281474976710655L,
Ray Milkey269ffb92014-04-03 14:43:30 -070061 Ethernet.toLong(new byte[]{(byte) 0xff, (byte) 0xff,
62 (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff}));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080063
64 assertEquals(
65 1103823438081L,
Ray Milkey269ffb92014-04-03 14:43:30 -070066 Ethernet.toLong(new byte[]{(byte) 0x01, (byte) 0x01,
67 (byte) 0x01, (byte) 0x01, (byte) 0x01, (byte) 0x01}));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080068
69 assertEquals(
70 141289400074368L,
Ray Milkey269ffb92014-04-03 14:43:30 -070071 Ethernet.toLong(new byte[]{(byte) 0x80, (byte) 0x80,
72 (byte) 0x80, (byte) 0x80, (byte) 0x80, (byte) 0x80}));
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -080073 }
74}