Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 1 | /* |
Brian O'Connor | 5ab426f | 2016-04-09 01:19:45 -0700 | [diff] [blame] | 2 | * Copyright 2015-present Open Networking Laboratory |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 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.openflow.controller.impl; |
| 17 | |
| 18 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 19 | import org.junit.After; |
| 20 | import org.junit.Before; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 21 | import org.junit.Test; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 22 | import org.onosproject.openflow.ChannelAdapter; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 23 | import org.onosproject.openflow.ChannelHandlerContextAdapter; |
| 24 | import org.projectfloodlight.openflow.protocol.OFHello; |
| 25 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 26 | import io.netty.buffer.ByteBuf; |
| 27 | import io.netty.buffer.ByteBufAllocator; |
| 28 | import io.netty.channel.Channel; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 29 | import static org.hamcrest.MatcherAssert.assertThat; |
| 30 | import static org.hamcrest.Matchers.instanceOf; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 31 | import static org.hamcrest.Matchers.is; |
| 32 | import java.util.ArrayList; |
| 33 | import java.util.List; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 34 | |
| 35 | /** |
| 36 | * Tests for the OpenFlow message decoder. |
| 37 | */ |
| 38 | public class OFMessageDecoderTest { |
| 39 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 40 | private ByteBuf buf; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 41 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 42 | private ByteBuf getHelloMessageBuffer() { |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 43 | // OFHello, OF version 1, xid of 0, total of 8 bytes |
| 44 | byte[] messageData = {0x1, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x0}; |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 45 | buf.writeBytes(messageData); |
| 46 | return buf; |
Ray Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Yuta HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 49 | @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 Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 60 | /** |
| 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 HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 67 | 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 Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 74 | } |
| 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 HIGUCHI | 6ee6b8c | 2017-05-09 14:44:30 -0700 | [diff] [blame^] | 83 | 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 Milkey | 4bf8a58 | 2015-11-04 08:26:19 -0800 | [diff] [blame] | 106 | } |
| 107 | |
| 108 | } |