BUG-730 : added null checks to tests to increase branch coverage
[bgpcep.git] / pcep / impl / src / test / java / org / opendaylight / protocol / pcep / impl / PCEPTlvParserTest.java
1 /*
2  * Copyright (c) 2013 Cisco Systems, Inc. and others.  All rights reserved.
3  *
4  * This program and the accompanying materials are made available under the
5  * terms of the Eclipse Public License v1.0 which accompanies this distribution,
6  * and is available at http://www.eclipse.org/legal/epl-v10.html
7  */
8 package org.opendaylight.protocol.pcep.impl;
9
10 import static org.junit.Assert.assertArrayEquals;
11 import static org.junit.Assert.assertEquals;
12 import static org.junit.Assert.assertNull;
13
14 import com.google.common.collect.Lists;
15 import io.netty.buffer.ByteBuf;
16 import io.netty.buffer.Unpooled;
17 import java.util.List;
18 import org.junit.Test;
19 import org.opendaylight.protocol.pcep.impl.TestVendorInformationTlvParser.TestEnterpriseSpecificInformation;
20 import org.opendaylight.protocol.pcep.impl.tlv.AbstractVendorSpecificTlvParser;
21 import org.opendaylight.protocol.pcep.impl.tlv.NoPathVectorTlvParser;
22 import org.opendaylight.protocol.pcep.impl.tlv.OFListTlvParser;
23 import org.opendaylight.protocol.pcep.impl.tlv.OrderTlvParser;
24 import org.opendaylight.protocol.pcep.impl.tlv.OverloadedDurationTlvParser;
25 import org.opendaylight.protocol.pcep.impl.tlv.ReqMissingTlvParser;
26 import org.opendaylight.protocol.pcep.spi.PCEPDeserializerException;
27 import org.opendaylight.protocol.util.ByteArray;
28 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.iana.rev130816.EnterpriseNumber;
29 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv;
30 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.OfId;
31 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.RequestId;
32 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfList;
33 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.of.list.tlv.OfListBuilder;
34 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.Order;
35 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.order.tlv.OrderBuilder;
36 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDuration;
37 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.overload.duration.tlv.OverloadDurationBuilder;
38 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.pcrep.message.pcrep.message.replies.result.failure._case.no.path.tlvs.NoPathVectorBuilder;
39 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissing;
40 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.req.missing.tlv.ReqMissingBuilder;
41 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlv;
42 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vendor.information.tlvs.VendorInformationTlvBuilder;
43 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlv;
44 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.VsTlvBuilder;
45 import org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.vs.tlv.vs.tlv.VendorPayload;
46 import org.opendaylight.yangtools.yang.binding.DataContainer;
47
48 public class PCEPTlvParserTest {
49
50     private static final byte[] noPathVectorBytes = { 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, (byte) 0xa7 };
51     private static final byte[] overloadedBytes = { 0x00, 0x02, 0x00, 0x04, 0x7f, (byte) 0xff, (byte) 0xff, (byte) 0xff };
52     private static final byte[] reqMissingBytes = { 0x00, 0x03, 0x00, 0x04, (byte) 0xF7, (byte) 0x82, 0x35, 0x17 };
53     private static final byte[] orderBytes = { 0x00, 0x05, 0x00, 0x08, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, (byte) 0xFF, 0x00, 0x00,
54         0x00, 0x01 };
55     private static final byte[] ofListBytes = { 0x00, 0x04, 0x00, 0x04, 0x12, 0x34, 0x56, 0x78 };
56     private static final byte[] vsTlvBytes = { 0x00, 0x1b, 0x00, 0x0c, 0x00, 0x00, 0x00, 0x09, 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00,
57         0x05 };
58     private static final byte[] VENDOR_INFO_BYTES = {
59         0x00, 0x07, 0x00, 0x08,
60         /* Enterprise number */
61         0x00, 0x00, 0x00, 0x00,
62         /* Enterprise specific information */
63         0x00, 0x00, 0x00, 0x05
64     };
65
66     private final AbstractVendorSpecificTlvParser vsParser = new AbstractVendorSpecificTlvParser() {
67
68         @Override
69         protected void serializeVendorPayload(final VendorPayload payload, final ByteBuf buffer) {
70             buffer.writeBytes(new byte[] { 0x00, 0x01, 0x00, 0x04, 0x00, 0x00, 0x00, 0x05 });
71         }
72
73         @Override
74         protected VendorPayload parseVendorPayload(final ByteBuf payloadBytes) throws PCEPDeserializerException {
75             return PCEPTlvParserTest.this.vp;
76         }
77
78         @Override
79         protected long getEnterpriseNumber() {
80             return 9;
81         }
82     };
83
84     private final VendorPayload vp = new VendorPayload() {
85
86         @Override
87         public Class<? extends DataContainer> getImplementedInterface() {
88             return null;
89         }
90     };
91
92     @Test
93     public void testNoPathVectorTlv() throws PCEPDeserializerException {
94         final NoPathVectorTlvParser parser = new NoPathVectorTlvParser();
95         final NoPathVectorTlv tlv = new NoPathVectorBuilder().setFlags(
96                 new org.opendaylight.yang.gen.v1.urn.opendaylight.params.xml.ns.yang.pcep.types.rev131005.NoPathVectorTlv.Flags(false, true, false, true, false, true, true, true)).build();
97         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(noPathVectorBytes, 4))));
98         final ByteBuf buff = Unpooled.buffer();
99         parser.serializeTlv(tlv, buff);
100         assertArrayEquals(noPathVectorBytes, ByteArray.getAllBytes(buff));
101         assertNull(parser.parseTlv(null));
102     }
103
104     @Test
105     public void testOverloadedDurationTlv() throws PCEPDeserializerException {
106         final OverloadedDurationTlvParser parser = new OverloadedDurationTlvParser();
107         final OverloadDuration tlv = new OverloadDurationBuilder().setDuration(0x7FFFFFFFL).build();
108         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(overloadedBytes, 4))));
109         final ByteBuf buff = Unpooled.buffer();
110         parser.serializeTlv(tlv, buff);
111         assertArrayEquals(overloadedBytes, ByteArray.getAllBytes(buff));
112         assertNull(parser.parseTlv(null));
113     }
114
115     @Test
116     public void testReqMissingTlv() throws PCEPDeserializerException {
117         final ReqMissingTlvParser parser = new ReqMissingTlvParser();
118         final ReqMissing tlv = new ReqMissingBuilder().setRequestId(new RequestId(0xF7823517L)).build();
119         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(reqMissingBytes, 4))));
120         final ByteBuf buff = Unpooled.buffer();
121         parser.serializeTlv(tlv, buff);
122         assertArrayEquals(reqMissingBytes, ByteArray.getAllBytes(buff));
123         assertNull(parser.parseTlv(null));
124     }
125
126     @Test
127     public void testOrderTlv() throws PCEPDeserializerException {
128         final OrderTlvParser parser = new OrderTlvParser();
129         final Order tlv = new OrderBuilder().setDelete(0xFFFFFFFFL).setSetup(0x00000001L).build();
130         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(orderBytes, 4))));
131         final ByteBuf buff = Unpooled.buffer();
132         parser.serializeTlv(tlv, buff);
133         assertArrayEquals(orderBytes, ByteArray.getAllBytes(buff));
134         assertNull(parser.parseTlv(null));
135     }
136
137     @Test
138     public void testOFListTlv() throws PCEPDeserializerException {
139         final OFListTlvParser parser = new OFListTlvParser();
140         final List<OfId> ids = Lists.newArrayList();
141         ids.add(new OfId(0x1234));
142         ids.add(new OfId(0x5678));
143         final OfList tlv = new OfListBuilder().setCodes(ids).build();
144         assertEquals(tlv, parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(ofListBytes, 4))));
145         final ByteBuf buff = Unpooled.buffer();
146         parser.serializeTlv(tlv, buff);
147         assertArrayEquals(ofListBytes, ByteArray.getAllBytes(buff));
148         assertNull(parser.parseTlv(null));
149     }
150
151     @Test
152     public void testVendorSpecificTlv() throws PCEPDeserializerException {
153         final VsTlv tlv = new VsTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(9L)).setVendorPayload(this.vp).build();
154         assertEquals(tlv, this.vsParser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(vsTlvBytes, 4))));
155         final ByteBuf buff = Unpooled.buffer();
156         this.vsParser.serializeTlv(tlv, buff);
157         assertArrayEquals(vsTlvBytes, ByteArray.getAllBytes(buff));
158         assertNull(this.vsParser.parseTlv(null));
159     }
160
161     @Test
162     public void testVendorInformationTlv() throws PCEPDeserializerException {
163         final TestVendorInformationTlvParser parser = new TestVendorInformationTlvParser();
164         final TestEnterpriseSpecificInformation esInfo = new TestEnterpriseSpecificInformation(5);
165         final VendorInformationTlv viTlv = new VendorInformationTlvBuilder().setEnterpriseNumber(new EnterpriseNumber(0L))
166                 .setEnterpriseSpecificInformation(esInfo).build();
167
168         final VendorInformationTlv parsedTlv = parser.parseTlv(Unpooled.wrappedBuffer(ByteArray.cutBytes(VENDOR_INFO_BYTES, 8)));
169         assertEquals(viTlv, parsedTlv);
170
171         final ByteBuf buff = Unpooled.buffer(VENDOR_INFO_BYTES.length);
172         parser.serializeTlv(viTlv, buff);
173         assertArrayEquals(VENDOR_INFO_BYTES, ByteArray.getAllBytes(buff));
174         assertNull(parser.parseTlv(null));
175     }
176 }