blob: 01dde144e9c15927be001afa02b3eb524d9b90f7 [file] [log] [blame]
Ray Milkey4bf8a582015-11-04 08:26:19 -08001/*
2 * Copyright 2015 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.openflow.controller.impl;
17
18
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.jboss.netty.buffer.ChannelBuffers;
21import org.junit.Test;
Ray Milkeyd931a9b2016-03-02 17:01:30 -080022import org.onosproject.core.netty.ChannelAdapter;
Ray Milkey4bf8a582015-11-04 08:26:19 -080023import org.onosproject.openflow.ChannelHandlerContextAdapter;
24import org.projectfloodlight.openflow.protocol.OFHello;
25
26import static org.hamcrest.MatcherAssert.assertThat;
27import static org.hamcrest.Matchers.instanceOf;
28import static org.hamcrest.Matchers.notNullValue;
29import static org.hamcrest.Matchers.nullValue;
30
31/**
32 * Tests for the OpenFlow message decoder.
33 */
34public class OFMessageDecoderTest {
35
36 static class ConnectedChannel extends ChannelAdapter {
37 @Override
38 public boolean isConnected() {
39 return true;
40 }
41 }
42
43 private ChannelBuffer getHelloMessageBuffer() {
44 // OFHello, OF version 1, xid of 0, total of 8 bytes
45 byte[] messageData = {0x1, 0x0, 0x0, 0x8, 0x0, 0x0, 0x0, 0x0};
46 ChannelBuffer channelBuffer = ChannelBuffers.dynamicBuffer();
47 channelBuffer.writeBytes(messageData);
48 return channelBuffer;
49 }
50
51 /**
52 * Tests decoding a message on a closed channel.
53 *
54 * @throws Exception when an exception is thrown from the decoder
55 */
56 @Test
57 public void testDecodeNoChannel() throws Exception {
58 OFMessageDecoder decoder = new OFMessageDecoder();
59 ChannelBuffer channelBuffer = getHelloMessageBuffer();
60 Object message =
61 decoder.decode(new ChannelHandlerContextAdapter(),
62 new ChannelAdapter(),
63 channelBuffer);
64 assertThat(message, nullValue());
65 }
66
67 /**
68 * Tests decoding a message.
69 *
70 * @throws Exception when an exception is thrown from the decoder
71 */
72 @Test
73 public void testDecode() throws Exception {
74 OFMessageDecoder decoder = new OFMessageDecoder();
75 ChannelBuffer channelBuffer = getHelloMessageBuffer();
76 Object message =
77 decoder.decode(new ChannelHandlerContextAdapter(),
78 new ConnectedChannel(),
79 channelBuffer);
80 assertThat(message, notNullValue());
81 assertThat(message, instanceOf(OFHello.class));
82 }
83
84}