Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2016-present Open Networking Laboratory |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [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.onosproject.ospf.protocol.ospfpacket; |
| 17 | |
| 18 | import org.jboss.netty.buffer.ChannelBuffer; |
| 19 | import org.jboss.netty.buffer.ChannelBuffers; |
| 20 | import org.junit.After; |
| 21 | import org.junit.Before; |
| 22 | import org.junit.Test; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 23 | import org.onosproject.ospf.protocol.util.OspfUtil; |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 24 | |
| 25 | import static org.hamcrest.CoreMatchers.is; |
| 26 | import static org.hamcrest.CoreMatchers.notNullValue; |
| 27 | import static org.hamcrest.MatcherAssert.assertThat; |
| 28 | |
| 29 | /** |
| 30 | * Unit test class for OspfMessageReader. |
| 31 | */ |
| 32 | |
| 33 | public class OspfMessageReaderTest { |
| 34 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 35 | private final byte[] packet1 = {2, 1, 0, 44, -64, -88, -86, 8, |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 36 | 0, 0, 0, 1, 39, 59, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, -1, -1, -1, 0, 0, |
| 37 | 10, 2, 1, 0, 0, 0, 40, -64, -88, -86, 8, 0, 0, 0, 0}; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 38 | private final byte[] packet2 = {2, 2, 0, 52, -64, -88, -86, 8, 0, |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 39 | 0, 0, 1, -96, 82, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 5, -36, 2, 7, 65, 119, |
| 40 | -87, 126, 0, 23, 2, 1, 10, 10, 10, 10, 10, 10, 10, 10, -128, 0, 0, 6, |
| 41 | -69, 26, 0, 36}; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 42 | private final byte[] packet3 = {2, 3, 0, 36, -64, -88, -86, 3, 0, |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 43 | 0, 0, 1, -67, -57, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, -64, -88, |
| 44 | -86, 8, -64, -88, -86, 8}; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 45 | private final byte[] packet4 = {2, 4, 1, 36, -64, -88, -86, 3, 0, |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 46 | 0, 0, 1, 54, 107, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 7, 0, |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 47 | 2, 2, 1, -64, -88, -86, 3, -64, -88, -86, 3, -128, 0, |
| 48 | 0, 1, 58, -100, 0, 48, 2, 0, 0, 2, -64, -88, -86, |
| 49 | 0, -1, -1, -1, 0, 3, 0, 0, 10, -64, -88, -86, 0, |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 50 | -1, -1, -1, 0, 3, 0, 0, 10, 0, 3, 2, 5, 80, -44, |
| 51 | 16, 0, -64, -88, -86, 2, -128, 0, 0, 1, 42, 73, 0, |
| 52 | 36, -1, -1, -1, -1, -128, 0, 0, 20, 0, 0, 0, 0, 0, |
| 53 | 0, 0, 0, 0, 3, 2, 5, -108, 121, -85, 0, -64, -88, |
| 54 | -86, 2, -128, 0, 0, 1, 52, -91, 0, 36, -1, -1, -1, |
| 55 | 0, -128, 0, 0, 20, -64, -88, -86, 1, 0, 0, 0, 0, 0, |
| 56 | 3, 2, 5, -64, -126, 120, 0, -64, -88, -86, 2, -128, 0, |
| 57 | 0, 1, -45, 25, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, |
| 58 | 0, 0, 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, 0, 0, |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 59 | -64, -88, -86, 2, -128, 0, 0, 1, 55, 8, 0, 36, -1, -1, |
| 60 | -1, 0, -128, 0, 0, 20, 0, 0, 0, 0, 0, 0, 0, 0, 0, |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 61 | 3, 2, 5, -64, -88, 1, 0, -64, -88, -86, 2, -128, 0, 0, |
| 62 | 1, 44, 18, 0, 36, -1, -1, -1, 0, -128, 0, 0, 20, 0, 0, |
| 63 | 0, 0, 0, 0, 0, 0, 0, 3, 2, 5, -64, -88, -84, 0, -64, |
| 64 | -88, -86, 2, -128, 0, 0, 1, 51, 65, 0, 36, -1, -1, -1, 0, |
| 65 | -128, 0, 0, 20, -64, -88, -86, 10, 0, 0, 0, 0}; |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 66 | private final byte[] packet5 = {2, 5, 0, 44, -64, -88, -86, 8, 0, 0, |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 67 | 0, 1, -30, -12, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 14, 16, 2, 1, -64, -88, -86, |
| 68 | 2, -64, -88, -86, 2, -128, 0, 0, 1, 74, -114, 0, 48}; |
| 69 | private OspfMessageReader ospfMessageReader; |
| 70 | private ChannelBuffer channelBuffer; |
| 71 | |
| 72 | @Before |
| 73 | public void setUp() throws Exception { |
| 74 | ospfMessageReader = new OspfMessageReader(); |
| 75 | } |
| 76 | |
| 77 | @After |
| 78 | public void tearDown() throws Exception { |
| 79 | ospfMessageReader = null; |
| 80 | channelBuffer = null; |
| 81 | } |
| 82 | |
| 83 | /** |
| 84 | * Tests readFromBuffer() method. |
| 85 | */ |
| 86 | @Test |
| 87 | public void testReadFromBuffer() throws Exception { |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 88 | channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet1)); |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 89 | ospfMessageReader.readFromBuffer(channelBuffer); |
| 90 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 91 | channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet2)); |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 92 | ospfMessageReader.readFromBuffer(channelBuffer); |
| 93 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 94 | channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet3)); |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 95 | ospfMessageReader.readFromBuffer(channelBuffer); |
| 96 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 97 | channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet4)); |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 98 | ospfMessageReader.readFromBuffer(channelBuffer); |
| 99 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 100 | channelBuffer = ChannelBuffers.copiedBuffer(framePacket(packet5)); |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 101 | ospfMessageReader.readFromBuffer(channelBuffer); |
| 102 | assertThat(ospfMessageReader, is(notNullValue())); |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 103 | } |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 104 | |
sunishvk | f7c5655 | 2016-07-18 16:02:39 +0530 | [diff] [blame^] | 105 | /** |
| 106 | * Frames the packet to min frame length. |
| 107 | * |
| 108 | * @param ospfPacket OSPF packet |
| 109 | * @return OSPF packet as byte array |
| 110 | */ |
| 111 | private byte[] framePacket(byte[] ospfPacket) { |
| 112 | //Set the length of the packet |
| 113 | //Get the total length of the packet |
| 114 | int length = ospfPacket.length; |
| 115 | //PDU_LENGTH + 1 byte for interface index |
| 116 | if (length < OspfUtil.MINIMUM_FRAME_LEN) { |
| 117 | byte[] bytes = new byte[OspfUtil.MINIMUM_FRAME_LEN + 5]; |
| 118 | System.arraycopy(ospfPacket, 0, bytes, 0, length); |
| 119 | return bytes; |
| 120 | } |
| 121 | return ospfPacket; |
Dhruv Dhody | 43f3ce6 | 2016-02-16 22:44:21 +0530 | [diff] [blame] | 122 | } |
sunish vk | aa48da8 | 2016-03-02 23:17:06 +0530 | [diff] [blame] | 123 | } |