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