blob: 6825008d98b0f9c0134b77a3f13dc39b54911fd7 [file] [log] [blame]
Umesh Krishnaswamy345ee992012-12-13 20:29:48 -08001/**
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
18package org.openflow.protocol;
19
20import java.util.List;
21
22import org.jboss.netty.buffer.ChannelBuffer;
23import org.jboss.netty.buffer.ChannelBuffers;
24import org.openflow.protocol.factory.BasicFactory;
25import org.openflow.protocol.factory.MessageParseException;
26import org.openflow.util.U16;
27
28import junit.framework.TestCase;
29
30public class BasicFactoryTest extends TestCase {
31
32 public void testCreateAndParse() throws MessageParseException {
33 BasicFactory factory = new BasicFactory();
34 OFMessage m = factory.getMessage(OFType.HELLO);
35 m.setVersion((byte) 1);
36 m.setType(OFType.ECHO_REQUEST);
37 m.setLength(U16.t(8));
38 m.setXid(0xdeadbeef);
39 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
40 ChannelBuffer bb2 = ChannelBuffers.dynamicBuffer();
41 m.writeTo(bb);
42 bb2.writeBytes(bb, bb.readableBytes()-1);
43 TestCase.assertNull(factory.parseMessage(bb2));
44 bb2.writeByte(bb.readByte());
45 List<OFMessage> message = factory.parseMessage(bb2);
46 TestCase.assertNotNull(message);
47 TestCase.assertEquals(message.size(), 1);
48 TestCase.assertTrue(message.get(0).getType() == OFType.ECHO_REQUEST);
49 }
50
51 public void testInvalidMsgParse() throws MessageParseException {
52 BasicFactory factory = new BasicFactory();
53 OFMessage m = factory.getMessage(OFType.HELLO);
54 m.setVersion((byte) 1);
55 m.setType(OFType.ECHO_REQUEST);
56 m.setLength(U16.t(16));
57 m.setXid(0xdeadbeef);
58 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
59 m.writeTo(bb);
60 List<OFMessage> message = factory.parseMessage(bb);
61 TestCase.assertNull(message);
62 }
63
64 public void testCurrouptedMsgParse() throws MessageParseException {
65 BasicFactory factory = new BasicFactory();
66 OFMessage m = factory.getMessage(OFType.HELLO);
67 m.setVersion((byte) 1);
68 m.setType(OFType.ERROR);
69 m.setLength(U16.t(8));
70 m.setXid(0xdeadbeef);
71 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
72 m.writeTo(bb);
73 try {
74 factory.parseMessage(bb);
75 }
76 catch(Exception e) {
77 TestCase.assertEquals(MessageParseException.class, e.getClass());
78 }
79 }
80
81}