3 * Copyright (c) 2013 Cisco Systems, Inc. and others. All rights reserved.
5 * This program and the accompanying materials are made available under the
6 * terms of the Eclipse Public License v1.0 which accompanies this distribution,
7 * and is available at http://www.eclipse.org/legal/epl-v10.html
10 package org.openflow.protocol;
12 import java.nio.ByteBuffer;
13 import java.util.List;
15 import junit.framework.TestCase;
17 import org.openflow.protocol.OFError.OFErrorType;
18 import org.openflow.protocol.OFError.OFHelloFailedCode;
19 import org.openflow.protocol.factory.BasicFactory;
20 import org.openflow.protocol.factory.OFMessageFactory;
21 import org.openflow.util.OFTestCase;
23 public class OFErrorTest extends OFTestCase {
24 public void testWriteRead() throws Exception {
25 OFError msg = (OFError) messageFactory.getMessage(OFType.ERROR);
26 msg.setMessageFactory(messageFactory);
27 msg.setErrorType((short) OFErrorType.OFPET_HELLO_FAILED.ordinal());
28 msg.setErrorCode((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
30 ByteBuffer bb = ByteBuffer.allocate(1024);
35 TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.ordinal(),
37 TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
38 .ordinal(), msg.getErrorType());
39 TestCase.assertNull(msg.getOffendingMsg());
41 msg.setOffendingMsg(new OFHello());
46 TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.ordinal(),
48 TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
49 .ordinal(), msg.getErrorType());
50 TestCase.assertNotNull(msg.getOffendingMsg());
51 TestCase.assertEquals(OFHello.MINIMUM_LENGTH,
52 msg.getOffendingMsg().length);
55 public void testGarbageAtEnd() {
56 // This is a OFError msg (12 bytes), that encaps a OFVendor msg (24
58 // AND some zeros at the end (40 bytes) for a total of 76 bytes
59 // THIS is what an NEC sends in reply to Nox's VENDOR request
60 byte[] oferrorRaw = { 0x01, 0x01, 0x00, 0x4c, 0x00, 0x00, 0x10,
61 (byte) 0xcc, 0x00, 0x01, 0x00, 0x01, 0x01, 0x04, 0x00, 0x18,
62 0x00, 0x00, 0x10, (byte) 0xcc, 0x00, 0x00, 0x23, 0x20, 0x00,
63 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
64 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
65 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
66 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
67 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
69 OFMessageFactory factory = new BasicFactory();
70 ByteBuffer oferrorBuf = ByteBuffer.wrap(oferrorRaw);
71 List<OFMessage> msgs = factory.parseMessages(oferrorBuf,
73 TestCase.assertEquals(1, msgs.size());
74 OFMessage msg = msgs.get(0);
75 TestCase.assertEquals(76, msg.getLengthU());
76 ByteBuffer out = ByteBuffer.allocate(1024);
78 TestCase.assertEquals(76, out.position());