1 package org.openflow.codec.protocol;
3 import java.util.Arrays;
6 import junit.framework.TestCase;
8 import org.openflow.codec.io.DataBuffers;
9 import org.openflow.codec.io.IDataBuffer;
10 import org.openflow.codec.protocol.OFPErrorMsg.OFPHelloFailedCode;
11 import org.openflow.codec.protocol.factory.OFPBasicFactoryImpl;
12 import org.openflow.codec.protocol.factory.OFPMessageFactory;
13 import org.openflow.codec.util.OFTestCase;
15 public class OFPErrorMsgTest extends OFTestCase
17 public void testWriteRead() throws Exception
19 OFPErrorMsg msg = (OFPErrorMsg) messageFactory
20 .getMessage(OFPType.ERROR);
21 msg.setMessageFactory(messageFactory);
22 msg.setErrorType(OFPErrorType.OFPET_HELLO_FAILED);
23 msg.setErrorCode((short) OFPHelloFailedCode.OFPHFC_INCOMPATIBLE
25 IDataBuffer bb = DataBuffers.allocate(1024);
30 TestCase.assertEquals(OFPErrorType.OFPET_HELLO_FAILED,
32 TestCase.assertEquals(
33 (short) OFPHelloFailedCode.OFPHFC_INCOMPATIBLE.ordinal(),
35 TestCase.assertNull(msg.getOffendingMsg(bb));
37 msg.setOffendingMsg(new OFPHello(), bb);
42 TestCase.assertEquals(OFPErrorType.OFPET_HELLO_FAILED,
44 TestCase.assertEquals(
45 (short) OFPHelloFailedCode.OFPHFC_INCOMPATIBLE.ordinal(),
47 TestCase.assertNotNull(msg.getOffendingMsg(bb));
48 TestCase.assertEquals(OFPHello.MINIMUM_LENGTH,
49 msg.getOffendingMsg(bb).length);
52 public void testWriteReadExperimenter() throws Exception
54 OFPErrorMsg msg = (OFPErrorMsg) messageFactory
55 .getMessage(OFPType.ERROR);
56 msg.setMessageFactory(messageFactory);
57 msg.setErrorType(OFPErrorType.OFPET_EXPERIMENTER);
58 msg.setExpType((short) 2);
59 msg.setExperimenter(10);
60 msg.setErrorData(new byte[] { 1, 2, 3, 4 });
61 IDataBuffer bb = DataBuffers.allocate(1024);
66 TestCase.assertEquals(OFPErrorType.OFPET_EXPERIMENTER,
68 TestCase.assertEquals((short) 2, msg.getExpType());
69 TestCase.assertEquals(10, msg.getExperimenter());
70 TestCase.assertNull(msg.getOffendingMsg(bb));
71 TestCase.assertTrue(Arrays.equals(new byte[] { 1, 2, 3, 4 },
76 public void testGarbageAtEnd()
78 // This is a OFPErrorMsg msg (12 bytes), that encaps a OFVendor msg (24
80 // AND some zeros at the end (40 bytes) for a total of 76 bytes
81 // THIS is what an NEC sends in reply to Nox's VENDOR request
82 byte[] oferrorRaw = { 0x01, 0x01, 0x00, 0x4c, 0x00, 0x00, 0x10,
83 (byte) 0xcc, 0x00, 0x01, 0x00, 0x01, 0x01, 0x04, 0x00, 0x18,
84 0x00, 0x00, 0x10, (byte) 0xcc, 0x00, 0x00, 0x23, 0x20, 0x00,
85 0x00, 0x00, 0x08, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
86 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
87 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
88 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
89 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
91 OFPMessageFactory factory = new OFPBasicFactoryImpl();
92 IDataBuffer buffer = DataBuffers.allocate(1024).wrap(oferrorRaw);
94 List<OFPMessage> msgs = factory
95 .parseMessages(buffer, oferrorRaw.length);
96 TestCase.assertEquals(1, msgs.size());
97 OFPMessage msg = msgs.get(0);
98 TestCase.assertEquals(76, msg.getLengthU());
99 IDataBuffer buffer1 = DataBuffers.allocate(1024);
100 msg.writeTo(buffer1);
101 TestCase.assertEquals(76, buffer1.position());