2 * Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
5 * Licensed under the Apache License, Version 2.0 (the "License"); you may
6 * not use this file except in compliance with the License. You may obtain
7 * a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
13 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
14 * License for the specific language governing permissions and limitations
18 package org.openflow.protocol;
20 import static org.junit.Assert.assertArrayEquals;
22 import java.util.List;
24 import junit.framework.TestCase;
26 import org.jboss.netty.buffer.ChannelBuffer;
27 import org.jboss.netty.buffer.ChannelBuffers;
28 import org.openflow.protocol.action.MockVendorAction;
29 import org.openflow.protocol.action.MockVendorActionFactory;
30 import org.openflow.protocol.action.OFAction;
31 import org.openflow.protocol.action.OFActionVendorGeneric;
32 import org.openflow.protocol.factory.BasicFactory;
33 import org.openflow.protocol.factory.MessageParseException;
34 import org.openflow.protocol.factory.OFVendorActionRegistry;
35 import org.openflow.util.U16;
37 public class BasicFactoryTest extends TestCase {
39 public void testCreateAndParse() throws MessageParseException {
40 BasicFactory factory = new BasicFactory();
41 OFMessage m = factory.getMessage(OFType.HELLO);
42 m.setVersion((byte) 1);
43 m.setType(OFType.ECHO_REQUEST);
44 m.setLength(U16.t(8));
46 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
47 ChannelBuffer bb2 = ChannelBuffers.dynamicBuffer();
49 bb2.writeBytes(bb, bb.readableBytes()-1);
50 TestCase.assertNull(factory.parseMessage(bb2));
51 bb2.writeByte(bb.readByte());
52 List<OFMessage> message = factory.parseMessage(bb2);
53 TestCase.assertNotNull(message);
54 TestCase.assertEquals(message.size(), 1);
55 TestCase.assertTrue(message.get(0).getType() == OFType.ECHO_REQUEST);
58 public void testInvalidMsgParse() throws MessageParseException {
59 BasicFactory factory = new BasicFactory();
60 OFMessage m = factory.getMessage(OFType.HELLO);
61 m.setVersion((byte) 1);
62 m.setType(OFType.ECHO_REQUEST);
63 m.setLength(U16.t(16));
65 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
67 List<OFMessage> message = factory.parseMessage(bb);
68 TestCase.assertNull(message);
71 public void testCurrouptedMsgParse() throws MessageParseException {
72 BasicFactory factory = new BasicFactory();
73 OFMessage m = factory.getMessage(OFType.HELLO);
74 m.setVersion((byte) 1);
75 m.setType(OFType.ERROR);
76 m.setLength(U16.t(8));
78 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
81 factory.parseMessage(bb);
84 TestCase.assertEquals(MessageParseException.class, e.getClass());
88 public void testCustomVendorAction() throws MessageParseException {
89 BasicFactory factory = new BasicFactory();
90 OFVendorActionRegistry.getInstance().register(
91 MockVendorAction.VENDOR_ID, new MockVendorActionFactory());
94 byte[] deadBeefMessage = {
95 (byte) 0xff, (byte) 0xff, // action vendor
97 (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte)0xef, // deadbeaf
98 0x01, 0x02, 0x03, 0x04,
99 0x05, 0x06, 0x07, 0x08 // pad
102 ChannelBuffer buf = ChannelBuffers.copiedBuffer(deadBeefMessage);
104 List<OFAction> actions = factory.parseActions(buf,deadBeefMessage.length);
105 assertEquals(1, actions.size());
106 OFAction ofAction = actions.get(0);
107 assertTrue("Action should be MockVendorAction, but is "+ofAction.getClass(), ofAction instanceof MockVendorAction);
108 assertArrayEquals( new byte[] { 1,2,3,4,5,6,7,8}, ((MockVendorAction)ofAction).getMockData());
113 public void testGenericVendorAction() throws MessageParseException {
114 byte[] nonDeadBeefMessage = {
115 (byte) 0xff, (byte) 0xff, // action vendor
116 0x00, 0x10, // length
117 (byte) 0x7e, (byte) 0xe7, (byte) 0xbe, (byte)0xef, // deadbeaf
118 0x01, 0x02, 0x03, 0x04,
119 0x05, 0x06, 0x07, 0x08 // pad
122 BasicFactory factory = new BasicFactory();
123 OFVendorActionRegistry.getInstance().register(
124 MockVendorAction.VENDOR_ID, new MockVendorActionFactory());
126 ChannelBuffer buf = ChannelBuffers.copiedBuffer(nonDeadBeefMessage);
128 List<OFAction> actions = factory.parseActions(buf,nonDeadBeefMessage.length);
129 assertEquals(1, actions.size());
130 OFAction ofAction = actions.get(0);
131 assertTrue("Action should be OFActionVendorGeneric, but is "+ofAction.getClass(), ofAction instanceof OFActionVendorGeneric);