blob: 7fba3cd722ea1d3483218eba86c1fc4e788671dd [file] [log] [blame]
Rusty Eddy80f12522015-09-03 22:42:02 +00001/*
2 * Copyright 2015 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.packet;
17
18import org.junit.Before;
19import org.junit.Test;
20import org.onlab.packet.pim.PIMAddrUnicast;
21import org.onlab.packet.pim.PIMHello;
22import org.onlab.packet.pim.PIMJoinPrune;
23
24import static junit.framework.Assert.assertTrue;
25
26public final class PIMTest {
27
28 public static final String SADDR = "10.2.1.2";
29 public static final String PIMADDR = "224.0.0.13";
30 public static final String PIMUADDR = "10.23.3.5";
31
32 public static final String SADDR1 = "10.1.1.1/32";
33 public static final String SADDR2 = "10.1.2.1/32";
34 public static final String GADDR1 = "232.1.1.1/32";
35 public static final String GADDR2 = "232.1.2.1/32";
36
37 public static final String CPSTR1 = "of:deadbeefball/8";
38 public static final String CPSTR2 = "of:deadbeefcafe/3";
39 public static final String CPSTR3 = "of:2badcafef00d/3";
40
41 private Deserializer<PIM> deserializer;
42
43 private PIM pimHello;
44 private PIMHello hello;
45
46 private PIM pimJoinPrune;
47 private PIMJoinPrune joinPrune;
48
Rusty Eddy9cbc0952015-09-14 22:29:07 +000049 /**
50 * Create PIM Hello and Join/Prune packets to be used in testing.
51 *
52 * @throws Exception if packet creation fails
53 */
Rusty Eddy80f12522015-09-03 22:42:02 +000054 @Before
55 public void setUp() throws Exception {
56
57 // Create a PIM Hello
58 pimHello = new PIM();
59 pimHello.setVersion((byte) 2);
60 pimHello.setPIMType((byte) PIM.TYPE_HELLO);
61 pimHello.setChecksum((short) 0);
62
63 hello = new PIMHello();
Rusty Eddy9cbc0952015-09-14 22:29:07 +000064 hello.createDefaultOptions();
Rusty Eddy80f12522015-09-03 22:42:02 +000065 pimHello.setPayload(hello);
66 hello.setParent(pimHello);
67
68 // Create PIM Join Prune
69 pimJoinPrune = new PIM();
70 pimJoinPrune.setVersion((byte) 2);
71 pimJoinPrune.setPIMType((byte) PIM.TYPE_JOIN_PRUNE_REQUEST);
72 pimJoinPrune.setChecksum((short) 0);
73
74 joinPrune = new PIMJoinPrune();
75 joinPrune.setUpstreamAddr(new PIMAddrUnicast(SADDR));
76 joinPrune.addJoin(GADDR1, SADDR1);
77 joinPrune.addJoin(GADDR2, SADDR2);
78 joinPrune.addPrune(GADDR1, SADDR2);
79 joinPrune.addPrune(GADDR2, SADDR1);
80
81 pimJoinPrune.setPayload(joinPrune);
82 joinPrune.setParent(pimJoinPrune);
83
84 deserializer = PIM.deserializer();
85 }
86
Rusty Eddy9cbc0952015-09-14 22:29:07 +000087 /**
88 * Make sure our deserializer throws an exception if we recieve bad input.
89 *
90 * @throws Exception if we are given bad input.
91 */
Rusty Eddy80f12522015-09-03 22:42:02 +000092 @Test
Rusty Eddy9cbc0952015-09-14 22:29:07 +000093 public void testDeserializeBadInput() throws Exception {
Rusty Eddy80f12522015-09-03 22:42:02 +000094 PacketTestUtils.testDeserializeBadInput(deserializer);
95 }
96
Rusty Eddy9cbc0952015-09-14 22:29:07 +000097 /**
98 * Verify we throw an exception if we receive a truncated Join/Prune message.
99 *
100 * @throws Exception if we receive a truncated Join/Prune message.
101 */
Rusty Eddy80f12522015-09-03 22:42:02 +0000102 @Test
103 public void testDeserializeTruncated() throws Exception {
Rusty Eddy80f12522015-09-03 22:42:02 +0000104 byte [] bits = pimJoinPrune.serialize();
105 PacketTestUtils.testDeserializeTruncated(deserializer, bits);
106 }
107
Rusty Eddy9cbc0952015-09-14 22:29:07 +0000108 /**
109 * Verify that we correctly deserialize hello messages.
110 *
111 * @throws Exception if our input is bad or truncated.
112 */
Rusty Eddy80f12522015-09-03 22:42:02 +0000113 @Test
114 public void testDeserializeHello() throws Exception {
115 byte [] data = pimHello.serialize();
116 PIM pim = deserializer.deserialize(data, 0, data.length);
117 assertTrue(pim.equals(pimHello));
118 }
119
Rusty Eddy9cbc0952015-09-14 22:29:07 +0000120 /**
121 * Verify that we correctly deserialize Join/Prune messages.
122 *
123 * @throws Exception if our input is bad or truncated.
124 */
Rusty Eddy80f12522015-09-03 22:42:02 +0000125 @Test
126 public void testDeserializeJoinPrune() throws Exception {
127 byte [] data = pimJoinPrune.serialize();
128 PIM pim = deserializer.deserialize(data, 0, data.length);
129 assertTrue(pim.equals(pimJoinPrune));
130 }
131
132}