312fcd3f57d616c6716e7f702e56a086a44609d0
[openflowjava.git] / third-party / openflowj_netty / src / test / java / org / openflow / protocol / BasicFactoryTest.java
1 /**
2 *    Copyright (c) 2008 The Board of Trustees of The Leland Stanford Junior
3 *    University
4 *
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
8 *
9 *         http://www.apache.org/licenses/LICENSE-2.0
10 *
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
15 *    under the License.
16 **/
17
18 package org.openflow.protocol;
19
20 import static org.junit.Assert.assertArrayEquals;
21
22 import java.util.List;
23
24 import junit.framework.TestCase;
25
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;
36
37 public class BasicFactoryTest extends TestCase {
38
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));
45         m.setXid(0xdeadbeef);
46         ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
47         ChannelBuffer bb2 = ChannelBuffers.dynamicBuffer();
48         m.writeTo(bb);
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);
56     }
57
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));
64         m.setXid(0xdeadbeef);
65         ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
66         m.writeTo(bb);
67         List<OFMessage> message = factory.parseMessage(bb);
68         TestCase.assertNull(message);
69     }
70
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));
77         m.setXid(0xdeadbeef);
78         ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
79         m.writeTo(bb);
80         try {
81                 factory.parseMessage(bb);
82         }
83         catch(Exception e) {
84             TestCase.assertEquals(MessageParseException.class, e.getClass());
85         }
86     }
87
88     public void testCustomVendorAction() throws MessageParseException {
89         BasicFactory factory = new BasicFactory();
90         OFVendorActionRegistry.getInstance().register(
91                 MockVendorAction.VENDOR_ID, new MockVendorActionFactory());
92
93
94         byte[] deadBeefMessage = {
95             (byte) 0xff, (byte) 0xff,          // action vendor
96             0x00, 0x10,                        // length
97             (byte) 0xde, (byte) 0xad, (byte) 0xbe, (byte)0xef,            // deadbeaf
98             0x01, 0x02, 0x03, 0x04,
99             0x05, 0x06, 0x07, 0x08               // pad
100         };
101
102         ChannelBuffer buf = ChannelBuffers.copiedBuffer(deadBeefMessage);
103
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());
109
110
111     }
112
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
120             };
121
122         BasicFactory factory = new BasicFactory();
123         OFVendorActionRegistry.getInstance().register(
124                 MockVendorAction.VENDOR_ID, new MockVendorActionFactory());
125
126         ChannelBuffer buf = ChannelBuffers.copiedBuffer(nonDeadBeefMessage);
127
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);
132     }
133
134 }