blob: d3e74b3f1712059f7133482c7fa1149c67c71d36 [file] [log] [blame]
Jian Lie4f12162016-09-13 00:09:09 +09001/*
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 */
Jian Li5e505c62016-12-05 02:44:24 +090016package org.onosproject.lisp.ctl.impl;
Jian Lie4f12162016-09-13 00:09:09 +090017
18import com.google.common.collect.Lists;
19import io.netty.buffer.ByteBuf;
20import io.netty.buffer.Unpooled;
Jian Li6322a362016-10-31 00:57:19 +090021import io.netty.channel.socket.DatagramPacket;
Jian Lie4f12162016-09-13 00:09:09 +090022import org.junit.Test;
23import org.onosproject.lisp.msg.protocols.LispMapNotify;
24import org.onosproject.lisp.msg.protocols.LispMapRegister;
25import org.onosproject.lisp.msg.protocols.LispMapReply;
26import org.onosproject.lisp.msg.protocols.LispMapRequest;
27
Jian Li6322a362016-10-31 00:57:19 +090028import java.net.InetSocketAddress;
Jian Lie4f12162016-09-13 00:09:09 +090029import java.util.List;
30
31import static org.hamcrest.MatcherAssert.assertThat;
32import static org.hamcrest.Matchers.instanceOf;
33import static org.hamcrest.Matchers.is;
34
35/**
36 * Tests for LISP message decoder.
37 */
38public 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 Lif11594a2016-10-31 18:16:02 +090055 byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
Jian Lie4f12162016-09-13 00:09:09 +090056 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 Li6322a362016-10-31 00:57:19 +0900119 private DatagramPacket convToDatagram(ByteBuf byteBuf) {
120 InetSocketAddress source = new InetSocketAddress(0);
121 return new DatagramPacket(byteBuf, source);
122 }
123
Jian Lie4f12162016-09-13 00:09:09 +0900124 @Test(expected = IllegalArgumentException.class)
125 public void testDecodeNoChannel() throws Exception {
126 LispMessageDecoder decoder = new LispMessageDecoder();
127
128 List<Object> list = Lists.newArrayList();
Jian Li6322a362016-10-31 00:57:19 +0900129 decoder.decode(new ChannelHandlerContextAdapter(),
130 convToDatagram(Unpooled.buffer()), list);
Jian Lie4f12162016-09-13 00:09:09 +0900131 }
132
133 @Test
134 public void testDecode() throws Exception {
135 LispMessageDecoder decoder = new LispMessageDecoder();
Jian Li6322a362016-10-31 00:57:19 +0900136 DatagramPacket requestBuff = convToDatagram(getLispMapRequestBuffer());
137 DatagramPacket replyBuff = convToDatagram(getLispMapReplyBuffer());
138 DatagramPacket registerBuff = convToDatagram(getLispMapRegisterBuffer());
139 DatagramPacket notifyBuff = convToDatagram(getLispMapNotifyBuffer());
Jian Lie4f12162016-09-13 00:09:09 +0900140
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}