1 /* Copyright (C)2013 Pantheon Technologies, s.r.o. All rights reserved. */
\r
2 package org.openflow.lib;
\r
4 import io.netty.buffer.ByteBuf;
\r
5 import io.netty.channel.embedded.EmbeddedChannel;
\r
7 import org.junit.Assert;
\r
8 import org.junit.Before;
\r
9 import org.junit.Test;
\r
10 import org.openflow.lib.OfFrameDecoder;
\r
13 * Testing class of {@link OfFrameDecoder}
\r
14 * @author michal.polkorab
\r
16 public class OFFrameDecoderTest {
\r
18 private EmbeddedChannel embch;
\r
21 * Sets up test environment
\r
24 public void setUp() {
\r
25 embch = new EmbeddedChannel(new OfFrameDecoder());
\r
29 * Test of decoding {@link OfFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
\r
30 * @throws Exception
\r
33 public void testDecode8BMessage() throws Exception {
\r
34 byte[] msgs = new byte[]{0x04, 0x0, 0x0, 0x08, 0x0, 0x0, 0x0, 0x01};
\r
35 ByteBuf writeObj = embch.alloc().buffer(64);
\r
36 writeObj.writeBytes(msgs);
\r
37 embch.writeInbound(writeObj);
\r
39 ByteBuf inObj = (ByteBuf) embch.readInbound();
\r
40 Assert.assertEquals(8, inObj.readableBytes());
\r
44 * Test of decoding {@link OfFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
\r
45 * @throws Exception
\r
48 public void testDecode16BMessage() throws Exception {
\r
49 byte[] msgs = new byte[]{0x04, 0x00, 0x00, 0x10, 0x00, 0x00, 0x00, 0x01, 0x04, 0x00,
\r
50 0x00, 0x08, 0x00, 0x00, 0x00, 0x01};
\r
51 ByteBuf writeObj = embch.alloc().buffer(64);
\r
52 writeObj.writeBytes(msgs);
\r
53 embch.writeInbound(writeObj);
\r
55 ByteBuf inObj = (ByteBuf) embch.readInbound();
\r
56 Assert.assertEquals(16, inObj.readableBytes());
\r
60 * Test of decoding {@link OfFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
\r
61 * @throws Exception
\r
64 public void testDecodeIncompleteMessage() throws Exception {
\r
65 byte[] msgs = new byte[]{0x04, 0x0, 0x0, 0x08, 0x0};
\r
66 ByteBuf writeObj = embch.alloc().buffer(64);
\r
67 writeObj.writeBytes(msgs);
\r
68 embch.writeInbound(writeObj);
\r
70 ByteBuf inObj = (ByteBuf) embch.readInbound();
\r
71 Assert.assertNull(inObj);
\r
75 * Test of decoding {@link OfFrameDecoder#decode(io.netty.channel.ChannelHandlerContext, io.netty.buffer.ByteBuf, java.util.List)}
\r
76 * @throws Exception
\r
79 public void testDecodeCompleteAndPartialMessage() throws Exception {
\r
80 byte[] msgs = new byte[]{0x04, 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x01,
\r
81 0x04, 0x00, 0x00, 0x08, 0x00};
\r
82 ByteBuf writeObj = embch.alloc().buffer(64);
\r
83 writeObj.writeBytes(msgs);
\r
84 embch.writeInbound(writeObj);
\r
86 ByteBuf inObj = (ByteBuf) embch.readInbound();
\r
87 Assert.assertEquals(8, inObj.readableBytes());
\r
88 inObj = (ByteBuf) embch.readInbound();
\r
89 Assert.assertNull(inObj);
\r