blob: f2617cdda9e577a905a402874e5f57760812b70e [file] [log] [blame]
Ray Milkey4bf8a582015-11-04 08:26:19 -08001/*
Brian O'Connor5ab426f2016-04-09 01:19:45 -07002 * Copyright 2015-present Open Networking Laboratory
Ray Milkey4bf8a582015-11-04 08:26:19 -08003 *
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.openflow.controller.impl;
17
18
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070019import org.junit.After;
20import org.junit.Before;
Ray Milkey4bf8a582015-11-04 08:26:19 -080021import org.junit.Test;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070022import org.onosproject.openflow.ChannelAdapter;
Ray Milkey4bf8a582015-11-04 08:26:19 -080023import org.onosproject.openflow.ChannelHandlerContextAdapter;
24import org.projectfloodlight.openflow.protocol.OFHello;
25
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070026import io.netty.buffer.ByteBuf;
27import io.netty.buffer.ByteBufAllocator;
28import io.netty.channel.Channel;
Ray Milkey4bf8a582015-11-04 08:26:19 -080029import static org.hamcrest.MatcherAssert.assertThat;
30import static org.hamcrest.Matchers.instanceOf;
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070031import static org.hamcrest.Matchers.is;
32import java.util.ArrayList;
33import java.util.List;
Ray Milkey4bf8a582015-11-04 08:26:19 -080034
35/**
36 * Tests for the OpenFlow message decoder.
37 */
38public class OFMessageDecoderTest {
39
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070040 private ByteBuf buf;
Ray Milkey4bf8a582015-11-04 08:26:19 -080041
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070042 private ByteBuf getHelloMessageBuffer() {
Ray Milkey4bf8a582015-11-04 08:26:19 -080043 // OFHello, OF version 1, xid of 0, total of 8 bytes
44 byte[] messageData = {0x1, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x0};
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070045 buf.writeBytes(messageData);
46 return buf;
Ray Milkey4bf8a582015-11-04 08:26:19 -080047 }
48
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070049 @Before
50 public void setUp() {
51 buf = ByteBufAllocator.DEFAULT.buffer();
52 }
53
54 @After
55 public void tearDown() {
56 buf.release();
57 }
58
59
Ray Milkey4bf8a582015-11-04 08:26:19 -080060 /**
61 * Tests decoding a message on a closed channel.
62 *
63 * @throws Exception when an exception is thrown from the decoder
64 */
65 @Test
66 public void testDecodeNoChannel() throws Exception {
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070067 OFMessageDecoder decoder = OFMessageDecoder.getInstance();
68 ByteBuf channelBuffer = getHelloMessageBuffer();
69 List<Object> out = new ArrayList<>();
70 decoder.decode(new ChannelHandlerContextAdapter(),
71 channelBuffer,
72 out);
73 assertThat(out.size(), is(0));
Ray Milkey4bf8a582015-11-04 08:26:19 -080074 }
75
76 /**
77 * Tests decoding a message.
78 *
79 * @throws Exception when an exception is thrown from the decoder
80 */
81 @Test
82 public void testDecode() throws Exception {
Yuta HIGUCHI6ee6b8c2017-05-09 14:44:30 -070083 OFMessageDecoder decoder = OFMessageDecoder.getInstance();
84 ByteBuf channelBuffer = getHelloMessageBuffer();
85 List<Object> out = new ArrayList<>();
86 decoder.decode(new ActiveChannelHandlerContextAdapter(),
87 channelBuffer,
88 out);
89 assertThat(out.size(), is(1));
90 assertThat(out.get(0), instanceOf(OFHello.class));
91 }
92
93 public class ActiveChannelHandlerContextAdapter
94 extends ChannelHandlerContextAdapter {
95
96 @Override
97 public Channel channel() {
98 return new ChannelAdapter() {
99 @Override
100 public boolean isActive() {
101 return true;
102 }
103 };
104 }
105
Ray Milkey4bf8a582015-11-04 08:26:19 -0800106 }
107
108}