Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016 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 | */ |
| 16 | package org.onosproject.bgpio.types; |
| 17 | |
| 18 | import static org.hamcrest.MatcherAssert.assertThat; |
| 19 | import static org.hamcrest.Matchers.instanceOf; |
| 20 | import static org.hamcrest.core.Is.is; |
| 21 | |
| 22 | import org.jboss.netty.buffer.ChannelBuffer; |
| 23 | import org.jboss.netty.buffer.ChannelBuffers; |
| 24 | import org.junit.Test; |
| 25 | import org.onosproject.bgpio.exceptions.BgpParseException; |
| 26 | import org.onosproject.bgpio.protocol.BgpFactories; |
| 27 | import org.onosproject.bgpio.protocol.BgpMessage; |
| 28 | import org.onosproject.bgpio.protocol.BgpMessageReader; |
| 29 | import org.onosproject.bgpio.protocol.ver4.BgpUpdateMsgVer4; |
| 30 | |
| 31 | /** |
| 32 | * Test for MP reach NLRI encoding and decoding. |
| 33 | */ |
| 34 | public class MpReachNlriTest { |
| 35 | |
| 36 | /** |
| 37 | * This testcase checks BGP update message. |
| 38 | */ |
| 39 | @Test |
| 40 | public void mpReachNlriTest1() throws BgpParseException { |
| 41 | |
Shashikanth VH | 03205d2 | 2016-02-19 22:48:08 +0530 | [diff] [blame] | 42 | // BGP flow spec Message |
| 43 | byte[] flowSpecMsg = new byte[] {(byte) 0xff, (byte) 0xff, |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 44 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 45 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 46 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
Shashikanth VH | 03205d2 | 2016-02-19 22:48:08 +0530 | [diff] [blame] | 47 | (byte) 0xff, (byte) 0xff, 0x00, 0x4a, 0x02, 0x00, 0x00, 0x00, |
| 48 | 0x33, 0x40, 0x01, 0x01, 0x00, 0x40, 0x02, 0x04, 0x02, 0x01, |
| 49 | 0x00, 0x64, (byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00, 0x00, |
| 50 | (byte) 0xc0, 0x10, 0x08, (byte) 0x80, 0x06, 0x00, 0x00, 0x00, |
| 51 | 0x00, 0x00, 0x00, (byte) 0x90, 0x0e, 0x00, 0x12, 0x00, 0x01, |
| 52 | (byte) 0x85, 0x00, 0x00, 0x0c, 0x02, 0x20, (byte) 0xc0, |
| 53 | (byte) 0xa8, 0x07, 0x36, 0x03, (byte) 0x81, 0x67, 0x04, |
| 54 | (byte) 0x81, 0x01 }; |
| 55 | |
| 56 | byte[] testFsMsg; |
| 57 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 58 | buffer.writeBytes(flowSpecMsg); |
| 59 | |
| 60 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 61 | BgpMessage message; |
| 62 | BgpHeader bgpHeader = new BgpHeader(); |
| 63 | |
| 64 | message = reader.readFrom(buffer, bgpHeader); |
| 65 | |
| 66 | assertThat(message, instanceOf(BgpUpdateMsgVer4.class)); |
| 67 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 68 | message.writeTo(buf); |
| 69 | |
| 70 | int readLen = buf.writerIndex(); |
| 71 | testFsMsg = new byte[readLen]; |
| 72 | buf.readBytes(testFsMsg, 0, readLen); |
| 73 | |
| 74 | assertThat(testFsMsg, is(flowSpecMsg)); |
| 75 | } |
| 76 | |
| 77 | /** |
| 78 | * This testcase checks BGP update message. |
| 79 | */ |
| 80 | @Test |
| 81 | public void mpReachNlriTest2() throws BgpParseException { |
| 82 | |
| 83 | // BGP flow spec Message |
| 84 | byte[] flowSpecMsg = new byte[] {(byte) 0xff, (byte) 0xff, |
| 85 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 86 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 87 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 88 | (byte) 0xff, (byte) 0xff, 0x00, 0x52, 0x02, 0x00, 0x00, 0x00, |
| 89 | 0x3b, 0x40, 0x01, 0x01, 0x01, 0x40, 0x02, 0x04, 0x02, 0x01, |
| 90 | 0x00, (byte) 0xc8, (byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00, |
| 91 | 0x00, (byte) 0xc0, 0x10, 0x10, (byte) 0x80, 0x06, 0x00, 0x7b, |
| 92 | 0x40, 0x60, 0x00, 0x00, (byte) 0x80, 0x09, 0x00, 0x00, 0x00, |
| 93 | 0x00, 0x00, 0x0f, (byte) 0x90, 0x0e, 0x00, 0x12, 0x00, 0x01, |
| 94 | (byte) 0x85, 0x00, 0x00, 0x0c, 0x01, 0x1e, (byte) 0xc0, |
| 95 | (byte) 0xa8, 0x02, 0x00, 0x02, 0x1e, (byte) 0xc0, (byte) 0xa8, |
| 96 | 0x01, 0x00 }; |
| 97 | |
| 98 | byte[] testFsMsg; |
| 99 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 100 | buffer.writeBytes(flowSpecMsg); |
| 101 | |
| 102 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 103 | BgpMessage message; |
| 104 | BgpHeader bgpHeader = new BgpHeader(); |
| 105 | |
| 106 | message = reader.readFrom(buffer, bgpHeader); |
| 107 | |
| 108 | assertThat(message, instanceOf(BgpUpdateMsgVer4.class)); |
| 109 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 110 | message.writeTo(buf); |
| 111 | |
| 112 | int readLen = buf.writerIndex(); |
| 113 | testFsMsg = new byte[readLen]; |
| 114 | buf.readBytes(testFsMsg, 0, readLen); |
| 115 | |
| 116 | assertThat(testFsMsg, is(flowSpecMsg)); |
| 117 | } |
| 118 | |
| 119 | /** |
| 120 | * This testcase checks BGP update message. |
| 121 | */ |
| 122 | @Test |
| 123 | public void mpReachNlriTest3() throws BgpParseException { |
| 124 | |
| 125 | // BGP flow spec Message |
| 126 | byte[] flowSpecMsg = new byte[] {(byte) 0xff, (byte) 0xff, |
| 127 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 128 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 129 | (byte) 0xff, (byte) 0xff, (byte) 0xff, (byte) 0xff, |
| 130 | (byte) 0xff, (byte) 0xff, 0x00, 0x71, 0x02, 0x00, 0x00, 0x00, |
| 131 | 0x5a, 0x40, 0x01, 0x01, 0x01, 0x40, 0x02, 0x04, 0x02, 0x01, |
| 132 | 0x00, (byte) 0xc8, (byte) 0x80, 0x04, 0x04, 0x00, 0x00, 0x00, |
| 133 | 0x00, (byte) 0xc0, 0x10, 0x10, (byte) 0x80, 0x06, 0x00, 0x7b, |
| 134 | 0x40, 0x60, 0x00, 0x00, (byte) 0x80, 0x09, 0x00, 0x00, 0x00, |
| 135 | 0x00, 0x00, 0x0f, (byte) 0x90, 0x0e, 0x00, 0x31, 0x00, 0x01, |
| 136 | (byte) 0x85, 0x00, 0x00, 0x2b, 0x01, 0x1e, (byte) 0xc0, |
| 137 | (byte) 0xa8, 0x02, 0x00, 0x02, 0x1e, (byte) 0xc0, (byte) 0xa8, |
| 138 | 0x01, 0x00, 0x03, (byte) 0x80, 0x04, 0x04, (byte) 0x80, |
| 139 | (byte) 0xb3, 0x05, (byte) 0x80, (byte) 0xc8, 0x06, (byte) 0x80, |
| 140 | 0x64, 0x07, (byte) 0x80, 0x7b, 0x08, (byte) 0x80, (byte) 0xea, |
| 141 | 0x09, (byte) 0x80, 0x7b, 0x0a, (byte) 0x90, 0x03, (byte) 0xe8, |
| 142 | 0x0b, (byte) 0x80, 0x7b, 0x0c, (byte) 0x80, 0x02 }; |
Shashikanth VH | 5826066 | 2016-02-13 01:12:02 +0530 | [diff] [blame] | 143 | |
| 144 | byte[] testFsMsg; |
| 145 | ChannelBuffer buffer = ChannelBuffers.dynamicBuffer(); |
| 146 | buffer.writeBytes(flowSpecMsg); |
| 147 | |
| 148 | BgpMessageReader<BgpMessage> reader = BgpFactories.getGenericReader(); |
| 149 | BgpMessage message; |
| 150 | BgpHeader bgpHeader = new BgpHeader(); |
| 151 | |
| 152 | message = reader.readFrom(buffer, bgpHeader); |
| 153 | |
| 154 | assertThat(message, instanceOf(BgpUpdateMsgVer4.class)); |
| 155 | ChannelBuffer buf = ChannelBuffers.dynamicBuffer(); |
| 156 | message.writeTo(buf); |
| 157 | |
| 158 | int readLen = buf.writerIndex(); |
| 159 | testFsMsg = new byte[readLen]; |
| 160 | buf.readBytes(testFsMsg, 0, readLen); |
| 161 | |
| 162 | assertThat(testFsMsg, is(flowSpecMsg)); |
| 163 | } |
| 164 | } |