blob: b937118bd94391110b04f9a717e204c383d9dd37 [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};
Jian Li3e1bac22016-12-06 02:08:12 +090058 byte[] replyRecord = {0x0, 0x0, 0x0, 0x1};
Jian Lie4f12162016-09-13 00:09:09 +090059 buffer.writeBytes(messageData);
60 buffer.writeBytes(eidData);
61 buffer.writeBytes(rlocData);
Jian Li3e1bac22016-12-06 02:08:12 +090062 buffer.writeBytes(replyRecord);
Jian Lie4f12162016-09-13 00:09:09 +090063 return buffer;
64 }
65
66 private ByteBuf getLispMapReplyBuffer() {
67 ByteBuf buffer = Unpooled.buffer();
68
69 // specify message type
70 buffer.writeByte(MAP_REPLY << TYPE_SHIFT_BIT);
71
72 // fill up message payload
73 byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
74 buffer.writeBytes(messageData);
75 return buffer;
76 }
77
78 private ByteBuf getLispMapRegisterBuffer() {
79 ByteBuf buffer = Unpooled.buffer();
80
81 // specify message type
82 buffer.writeByte(MAP_REGISTER << TYPE_SHIFT_BIT);
83
84 // fill up message payload
85 byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
86 byte[] keyId = {0x0, 0x1};
87
88 // assume that we have auth data which has 2 bytes size
89 byte[] authDataLength = {0x0, 0x2};
90 byte[] authData = {0x0, 0x0};
91
92 buffer.writeBytes(messageData);
93 buffer.writeBytes(keyId);
94 buffer.writeBytes(authDataLength);
95 buffer.writeBytes(authData);
96 return buffer;
97 }
98
99 private ByteBuf getLispMapNotifyBuffer() {
100 ByteBuf buffer = Unpooled.buffer();
101
102 // specify message type
103 buffer.writeByte(MAP_NOTIFY << TYPE_SHIFT_BIT);
104
105 // fill up message payload
106 byte[] messageData = {0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0};
107 byte[] keyId = {0x0, 0x1};
108
109 // assume that we have auth data which has 2 bytes size
110 byte[] authDataLength = {0x0, 0x2};
111 byte[] authData = {0x0, 0x0};
112
113 buffer.writeBytes(messageData);
114 buffer.writeBytes(keyId);
115 buffer.writeBytes(authDataLength);
116 buffer.writeBytes(authData);
117
118 return buffer;
119 }
120
Jian Li6322a362016-10-31 00:57:19 +0900121 private DatagramPacket convToDatagram(ByteBuf byteBuf) {
122 InetSocketAddress source = new InetSocketAddress(0);
123 return new DatagramPacket(byteBuf, source);
124 }
125
Jian Lie4f12162016-09-13 00:09:09 +0900126 @Test(expected = IllegalArgumentException.class)
127 public void testDecodeNoChannel() throws Exception {
128 LispMessageDecoder decoder = new LispMessageDecoder();
129
130 List<Object> list = Lists.newArrayList();
Jian Li6322a362016-10-31 00:57:19 +0900131 decoder.decode(new ChannelHandlerContextAdapter(),
132 convToDatagram(Unpooled.buffer()), list);
Jian Lie4f12162016-09-13 00:09:09 +0900133 }
134
135 @Test
136 public void testDecode() throws Exception {
137 LispMessageDecoder decoder = new LispMessageDecoder();
Jian Li6322a362016-10-31 00:57:19 +0900138 DatagramPacket requestBuff = convToDatagram(getLispMapRequestBuffer());
139 DatagramPacket replyBuff = convToDatagram(getLispMapReplyBuffer());
140 DatagramPacket registerBuff = convToDatagram(getLispMapRegisterBuffer());
141 DatagramPacket notifyBuff = convToDatagram(getLispMapNotifyBuffer());
Jian Lie4f12162016-09-13 00:09:09 +0900142
143 List<Object> list = Lists.newArrayList();
144 decoder.decode(new ChannelHandlerContextAdapter(), requestBuff, list);
145 decoder.decode(new ChannelHandlerContextAdapter(), replyBuff, list);
146 decoder.decode(new ChannelHandlerContextAdapter(), registerBuff, list);
147 decoder.decode(new ChannelHandlerContextAdapter(), notifyBuff, list);
148
149 assertThat(list.size(), is(4));
150 assertThat(list.get(0), is(instanceOf(LispMapRequest.class)));
151 assertThat(list.get(1), is(instanceOf(LispMapReply.class)));
152 assertThat(list.get(2), is(instanceOf(LispMapRegister.class)));
153 assertThat(list.get(3), is(instanceOf(LispMapNotify.class)));
154 }
155}