Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 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 | */ |
| 16 | package org.onlab.packet; |
| 17 | |
| 18 | import org.junit.Before; |
| 19 | import org.junit.Test; |
| 20 | import org.onlab.packet.pim.PIMAddrUnicast; |
| 21 | import org.onlab.packet.pim.PIMHello; |
| 22 | import org.onlab.packet.pim.PIMJoinPrune; |
| 23 | |
| 24 | import static junit.framework.Assert.assertTrue; |
| 25 | |
| 26 | public 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 Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 49 | /** |
| 50 | * Create PIM Hello and Join/Prune packets to be used in testing. |
| 51 | * |
| 52 | * @throws Exception if packet creation fails |
| 53 | */ |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 54 | @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 Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 64 | hello.createDefaultOptions(); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 65 | 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 Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 87 | /** |
| 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 Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 92 | @Test |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 93 | public void testDeserializeBadInput() throws Exception { |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 94 | PacketTestUtils.testDeserializeBadInput(deserializer); |
| 95 | } |
| 96 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 97 | /** |
| 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 Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 102 | @Test |
| 103 | public void testDeserializeTruncated() throws Exception { |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 104 | byte[] bits = pimJoinPrune.serialize(); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 105 | PacketTestUtils.testDeserializeTruncated(deserializer, bits); |
| 106 | } |
| 107 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 108 | /** |
| 109 | * Verify that we correctly deserialize hello messages. |
| 110 | * |
| 111 | * @throws Exception if our input is bad or truncated. |
| 112 | */ |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 113 | @Test |
| 114 | public void testDeserializeHello() throws Exception { |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 115 | byte[] data = pimHello.serialize(); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 116 | PIM pim = deserializer.deserialize(data, 0, data.length); |
| 117 | assertTrue(pim.equals(pimHello)); |
| 118 | } |
| 119 | |
Rusty Eddy | 9cbc095 | 2015-09-14 22:29:07 +0000 | [diff] [blame] | 120 | /** |
| 121 | * Verify that we correctly deserialize Join/Prune messages. |
| 122 | * |
| 123 | * @throws Exception if our input is bad or truncated. |
| 124 | */ |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 125 | @Test |
| 126 | public void testDeserializeJoinPrune() throws Exception { |
Jian Li | 68c4fc4 | 2016-01-11 16:07:03 -0800 | [diff] [blame] | 127 | byte[] data = pimJoinPrune.serialize(); |
Rusty Eddy | 80f1252 | 2015-09-03 22:42:02 +0000 | [diff] [blame] | 128 | PIM pim = deserializer.deserialize(data, 0, data.length); |
| 129 | assertTrue(pim.equals(pimJoinPrune)); |
| 130 | } |
| 131 | |
| 132 | } |