blob: f4884e959e9c1e8fb66491b4cac97778f095c3dd [file] [log] [blame]
Ray Milkeyafa00d22014-11-13 19:25:15 -08001/*
2 * Copyright 2014 Open Networking Laboratory
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onlab.onos.net.packet;
17
18import java.nio.ByteBuffer;
19
20import org.junit.Test;
21import org.onlab.packet.Ethernet;
22import org.onlab.packet.MacAddress;
23
24import com.google.common.testing.EqualsTester;
25
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.equalTo;
28import static org.hamcrest.Matchers.notNullValue;
29import static org.onlab.junit.ImmutableClassChecker.assertThatClassIsImmutable;
30import static org.onlab.onos.net.NetTestTools.connectPoint;
31
32/**
33 * Unit tests for the DefaultInboundPacket class.
34 */
35public class DefaultInboundPacketTest {
36
37 final Ethernet eth = new Ethernet()
38 .setDestinationMACAddress(MacAddress.BROADCAST)
39 .setSourceMACAddress(MacAddress.BROADCAST);
40 final ByteBuffer byteBuffer = ByteBuffer.wrap(eth.serialize());
41 final DefaultInboundPacket packet1 =
42 new DefaultInboundPacket(connectPoint("d1", 1),
43 eth,
44 byteBuffer);
45 final DefaultInboundPacket sameAsPacket1 =
46 new DefaultInboundPacket(connectPoint("d1", 1),
47 eth,
48 byteBuffer);
49 final DefaultInboundPacket packet2 =
50 new DefaultInboundPacket(connectPoint("d2", 1),
51 eth,
52 byteBuffer);
53 /**
54 * Checks that the DefaultInboundPacket class is immutable.
55 */
56 @Test
57 public void testImmutability() {
58 assertThatClassIsImmutable(DefaultInboundPacket.class);
59 }
60
61 /**
62 * Tests the equals(), hashCode() and toString() methods.
63 */
64 @Test
65 public void testEquals() {
66 new EqualsTester()
67 .addEqualityGroup(packet1, sameAsPacket1)
68 .addEqualityGroup(packet2)
69 .testEquals();
70 }
71
72 /**
73 * Tests the object creation through the constructor.
74 */
75 @Test
76 public void testConstruction() {
77 assertThat(packet1.receivedFrom(), equalTo(connectPoint("d1", 1)));
78 assertThat(packet1.parsed(), equalTo(eth));
79 assertThat(packet1.unparsed(), notNullValue());
80 }
81}