1 package org.openflow.protocol;
3 import java.nio.ByteBuffer;
6 import junit.framework.TestCase;
8 import org.openflow.protocol.OFError.OFErrorType;
9 import org.openflow.protocol.OFError.OFHelloFailedCode;
10 import org.openflow.protocol.factory.BasicFactory;
11 import org.openflow.protocol.factory.OFMessageFactory;
12 import org.openflow.util.OFTestCase;
14 public class OFErrorTest extends OFTestCase {
15 public void testWriteRead() throws Exception {
16 OFError msg = (OFError) messageFactory.getMessage(OFType.ERROR);
17 msg.setMessageFactory(messageFactory);
18 msg.setErrorType((short) OFErrorType.OFPET_HELLO_FAILED.ordinal());
19 msg.setErrorCode((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
21 ByteBuffer bb = ByteBuffer.allocate(1024);
26 TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.ordinal(),
28 TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
29 .ordinal(), msg.getErrorType());
30 TestCase.assertNull(msg.getOffendingMsg());
32 msg.setOffendingMsg(new OFHello());
37 TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.ordinal(),
39 TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
40 .ordinal(), msg.getErrorType());
41 TestCase.assertNotNull(msg.getOffendingMsg());
42 TestCase.assertEquals(OFHello.MINIMUM_LENGTH,
43 msg.getOffendingMsg().length);
46 public void testGarbageAtEnd() {
47 // This is a OFError msg (12 bytes), that encaps a OFVendor msg (24
49 // AND some zeros at the end (40 bytes) for a total of 76 bytes
50 // THIS is what an NEC sends in reply to Nox's VENDOR request
51 byte[] oferrorRaw = { 0x01, 0x01, 0x00, 0x4c, 0x00, 0x00, 0x10,
52 (byte) 0xcc, 0x00, 0x01, 0x00, 0x01, 0x01, 0x04, 0x00, 0x18,
53 0x00, 0x00, 0x10, (byte) 0xcc, 0x00, 0x00, 0x23, 0x20, 0x00,
54 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
55 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
56 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
57 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
58 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
60 OFMessageFactory factory = new BasicFactory();
61 ByteBuffer oferrorBuf = ByteBuffer.wrap(oferrorRaw);
62 List<OFMessage> msgs = factory.parseMessages(oferrorBuf,
64 TestCase.assertEquals(1, msgs.size());
65 OFMessage msg = msgs.get(0);
66 TestCase.assertEquals(76, msg.getLengthU());
67 ByteBuffer out = ByteBuffer.allocate(1024);
69 TestCase.assertEquals(76, out.position());