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 java.util.List;
22 import junit.framework.TestCase;
24 import org.jboss.netty.buffer.ChannelBuffer;
25 import org.jboss.netty.buffer.ChannelBuffers;
26 import org.openflow.protocol.OFError.OFErrorType;
27 import org.openflow.protocol.OFError.OFHelloFailedCode;
28 import org.openflow.protocol.factory.BasicFactory;
29 import org.openflow.protocol.factory.MessageParseException;
30 import org.openflow.protocol.factory.OFMessageFactory;
31 import org.openflow.util.OFTestCase;
33 public class OFErrorTest extends OFTestCase {
34 public void testWriteRead() throws Exception {
35 OFError msg = (OFError) messageFactory.getMessage(OFType.ERROR);
36 msg.setMessageFactory(messageFactory);
37 msg.setErrorType((short) OFErrorType.OFPET_HELLO_FAILED.getValue());
38 msg.setErrorCode((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
40 ChannelBuffer bb = ChannelBuffers.dynamicBuffer();
44 TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.getValue(),
46 TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
47 .ordinal(), msg.getErrorType());
48 TestCase.assertNull(msg.getOffendingMsg());
50 msg.setOffendingMsg(new OFHello());
54 TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.getValue(),
56 TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
57 .ordinal(), msg.getErrorType());
58 TestCase.assertNotNull(msg.getOffendingMsg());
59 TestCase.assertEquals(OFHello.MINIMUM_LENGTH,
60 msg.getOffendingMsg().length);
63 public void testGarbageAtEnd() throws MessageParseException {
64 // This is a OFError msg (12 bytes), that encaps a OFVendor msg (24
66 // AND some zeros at the end (40 bytes) for a total of 76 bytes
67 // THIS is what an NEC sends in reply to Nox's VENDOR request
68 byte[] oferrorRaw = { 0x01, 0x01, 0x00, 0x4c, 0x00, 0x00, 0x10,
69 (byte) 0xcc, 0x00, 0x01, 0x00, 0x01, 0x01, 0x04, 0x00, 0x18,
70 0x00, 0x00, 0x10, (byte) 0xcc, 0x00, 0x00, 0x23, 0x20, 0x00,
71 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
72 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
73 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
74 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
75 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
77 OFMessageFactory factory = new BasicFactory();
78 ChannelBuffer oferrorBuf =
79 ChannelBuffers.wrappedBuffer(oferrorRaw);
80 List<OFMessage> msg = factory.parseMessage(oferrorBuf);
81 TestCase.assertNotNull(msg);
82 TestCase.assertEquals(msg.size(), 1);
83 TestCase.assertEquals(76, msg.get(0).getLengthU());
84 ChannelBuffer out = ChannelBuffers.dynamicBuffer();
85 msg.get(0).writeTo(out);
86 TestCase.assertEquals(76, out.readableBytes());