4ed08bf0ea5ff8a02d6e10de87b3901553c839e5
[controller.git] / third-party / openflowj / src / test / java / org / openflow / protocol / OFErrorTest.java
1
2 /*
3  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
4  *
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
8  */
9
10 package org.openflow.protocol;
11
12 import java.nio.ByteBuffer;
13 import java.util.List;
14
15 import junit.framework.TestCase;
16
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;
22
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
29                 .ordinal());
30         ByteBuffer bb = ByteBuffer.allocate(1024);
31         bb.clear();
32         msg.writeTo(bb);
33         bb.flip();
34         msg.readFrom(bb);
35         TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.ordinal(),
36                 msg.getErrorType());
37         TestCase.assertEquals((short) OFHelloFailedCode.OFPHFC_INCOMPATIBLE
38                 .ordinal(), msg.getErrorType());
39         TestCase.assertNull(msg.getOffendingMsg());
40
41         msg.setOffendingMsg(new OFHello());
42         bb.clear();
43         msg.writeTo(bb);
44         bb.flip();
45         msg.readFrom(bb);
46         TestCase.assertEquals((short) OFErrorType.OFPET_HELLO_FAILED.ordinal(),
47                 msg.getErrorType());
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);
53     }
54
55     public void testGarbageAtEnd() {
56         // This is a OFError msg (12 bytes), that encaps a OFVendor msg (24
57         // bytes)
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,
68                 0x00 };
69         OFMessageFactory factory = new BasicFactory();
70         ByteBuffer oferrorBuf = ByteBuffer.wrap(oferrorRaw);
71         List<OFMessage> msgs = factory.parseMessages(oferrorBuf,
72                 oferrorRaw.length);
73         TestCase.assertEquals(1, msgs.size());
74         OFMessage msg = msgs.get(0);
75         TestCase.assertEquals(76, msg.getLengthU());
76         ByteBuffer out = ByteBuffer.allocate(1024);
77         msg.writeTo(out);
78         TestCase.assertEquals(76, out.position());
79     }
80 }