Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2016-present 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.lisp.ctl; |
| 17 | |
| 18 | import com.google.common.collect.Lists; |
| 19 | import io.netty.buffer.ByteBuf; |
| 20 | import io.netty.buffer.Unpooled; |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 21 | import io.netty.channel.socket.DatagramPacket; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 22 | import org.junit.Test; |
| 23 | import org.onosproject.lisp.msg.protocols.LispMapNotify; |
| 24 | import org.onosproject.lisp.msg.protocols.LispMapRegister; |
| 25 | import org.onosproject.lisp.msg.protocols.LispMapReply; |
| 26 | import org.onosproject.lisp.msg.protocols.LispMapRequest; |
| 27 | |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 28 | import java.net.InetSocketAddress; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 29 | import java.util.List; |
| 30 | |
| 31 | import static org.hamcrest.MatcherAssert.assertThat; |
| 32 | import static org.hamcrest.Matchers.instanceOf; |
| 33 | import static org.hamcrest.Matchers.is; |
| 34 | |
| 35 | /** |
| 36 | * Tests for LISP message decoder. |
| 37 | */ |
| 38 | public class LispMessageDecoderTest { |
| 39 | |
| 40 | private static final int TYPE_SHIFT_BIT = 4; |
| 41 | private static final byte MAP_REQUEST = 1; |
| 42 | private static final byte MAP_REPLY = 2; |
| 43 | private static final byte MAP_REGISTER = 3; |
| 44 | private static final byte MAP_NOTIFY = 4; |
| 45 | |
| 46 | |
| 47 | private ByteBuf getLispMapRequestBuffer() { |
| 48 | ByteBuf buffer = Unpooled.buffer(); |
| 49 | |
| 50 | // specify message type |
| 51 | buffer.writeByte(MAP_REQUEST << TYPE_SHIFT_BIT); |
| 52 | |
| 53 | // fill up message payload |
| 54 | // second byte denotes the number of RLOCs |
Jian Li | f11594a | 2016-10-31 18:16:02 +0900 | [diff] [blame] | 55 | byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 56 | byte[] eidData = {0x0, 0x1, 0x0, 0x0, 0x0, 0x0}; |
| 57 | byte[] rlocData = {0x0, 0x1, 0x0, 0x0, 0x0, 0x0}; |
| 58 | buffer.writeBytes(messageData); |
| 59 | buffer.writeBytes(eidData); |
| 60 | buffer.writeBytes(rlocData); |
| 61 | return buffer; |
| 62 | } |
| 63 | |
| 64 | private ByteBuf getLispMapReplyBuffer() { |
| 65 | ByteBuf buffer = Unpooled.buffer(); |
| 66 | |
| 67 | // specify message type |
| 68 | buffer.writeByte(MAP_REPLY << TYPE_SHIFT_BIT); |
| 69 | |
| 70 | // fill up message payload |
| 71 | byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 72 | buffer.writeBytes(messageData); |
| 73 | return buffer; |
| 74 | } |
| 75 | |
| 76 | private ByteBuf getLispMapRegisterBuffer() { |
| 77 | ByteBuf buffer = Unpooled.buffer(); |
| 78 | |
| 79 | // specify message type |
| 80 | buffer.writeByte(MAP_REGISTER << TYPE_SHIFT_BIT); |
| 81 | |
| 82 | // fill up message payload |
| 83 | byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 84 | byte[] keyId = {0x0, 0x1}; |
| 85 | |
| 86 | // assume that we have auth data which has 2 bytes size |
| 87 | byte[] authDataLength = {0x0, 0x2}; |
| 88 | byte[] authData = {0x0, 0x0}; |
| 89 | |
| 90 | buffer.writeBytes(messageData); |
| 91 | buffer.writeBytes(keyId); |
| 92 | buffer.writeBytes(authDataLength); |
| 93 | buffer.writeBytes(authData); |
| 94 | return buffer; |
| 95 | } |
| 96 | |
| 97 | private ByteBuf getLispMapNotifyBuffer() { |
| 98 | ByteBuf buffer = Unpooled.buffer(); |
| 99 | |
| 100 | // specify message type |
| 101 | buffer.writeByte(MAP_NOTIFY << TYPE_SHIFT_BIT); |
| 102 | |
| 103 | // fill up message payload |
| 104 | byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0}; |
| 105 | byte[] keyId = {0x0, 0x1}; |
| 106 | |
| 107 | // assume that we have auth data which has 2 bytes size |
| 108 | byte[] authDataLength = {0x0, 0x2}; |
| 109 | byte[] authData = {0x0, 0x0}; |
| 110 | |
| 111 | buffer.writeBytes(messageData); |
| 112 | buffer.writeBytes(keyId); |
| 113 | buffer.writeBytes(authDataLength); |
| 114 | buffer.writeBytes(authData); |
| 115 | |
| 116 | return buffer; |
| 117 | } |
| 118 | |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 119 | private DatagramPacket convToDatagram(ByteBuf byteBuf) { |
| 120 | InetSocketAddress source = new InetSocketAddress(0); |
| 121 | return new DatagramPacket(byteBuf, source); |
| 122 | } |
| 123 | |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 124 | @Test(expected = IllegalArgumentException.class) |
| 125 | public void testDecodeNoChannel() throws Exception { |
| 126 | LispMessageDecoder decoder = new LispMessageDecoder(); |
| 127 | |
| 128 | List<Object> list = Lists.newArrayList(); |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 129 | decoder.decode(new ChannelHandlerContextAdapter(), |
| 130 | convToDatagram(Unpooled.buffer()), list); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | @Test |
| 134 | public void testDecode() throws Exception { |
| 135 | LispMessageDecoder decoder = new LispMessageDecoder(); |
Jian Li | 6322a36 | 2016-10-31 00:57:19 +0900 | [diff] [blame] | 136 | DatagramPacket requestBuff = convToDatagram(getLispMapRequestBuffer()); |
| 137 | DatagramPacket replyBuff = convToDatagram(getLispMapReplyBuffer()); |
| 138 | DatagramPacket registerBuff = convToDatagram(getLispMapRegisterBuffer()); |
| 139 | DatagramPacket notifyBuff = convToDatagram(getLispMapNotifyBuffer()); |
Jian Li | e4f1216 | 2016-09-13 00:09:09 +0900 | [diff] [blame] | 140 | |
| 141 | List<Object> list = Lists.newArrayList(); |
| 142 | decoder.decode(new ChannelHandlerContextAdapter(), requestBuff, list); |
| 143 | decoder.decode(new ChannelHandlerContextAdapter(), replyBuff, list); |
| 144 | decoder.decode(new ChannelHandlerContextAdapter(), registerBuff, list); |
| 145 | decoder.decode(new ChannelHandlerContextAdapter(), notifyBuff, list); |
| 146 | |
| 147 | assertThat(list.size(), is(4)); |
| 148 | assertThat(list.get(0), is(instanceOf(LispMapRequest.class))); |
| 149 | assertThat(list.get(1), is(instanceOf(LispMapReply.class))); |
| 150 | assertThat(list.get(2), is(instanceOf(LispMapRegister.class))); |
| 151 | assertThat(list.get(3), is(instanceOf(LispMapNotify.class))); |
| 152 | } |
| 153 | } |