blob: 72c93c0e0b6b2edd8296b584943600b3780a05e6 [file] [log] [blame]
Chinmaya Agarwal8bfea5e2019-10-09 16:53:22 +05301/*
2 * Copyright 2016-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16package org.onosproject.isis.io.isispacket.tlv;
17
18import org.easymock.EasyMock;
19import org.jboss.netty.buffer.ChannelBuffer;
20import org.jboss.netty.buffer.ChannelBuffers;
21import org.junit.After;
22import org.junit.Before;
23import org.junit.Test;
24import static org.hamcrest.CoreMatchers.is;
25import static org.junit.Assert.assertThat;
26
27/**
28 * Unit test class for NeighborForExtendedIs.
29 *
30 * Here we have passed a byte array containing data for 2 neighbors along with
31 * their Sub TLVs. The test case checks whether the code is able to parse the Sub TLVs
32 * for each neighbor or not. Along with this it also checks for neighbor id and metric
33 * assigned to each neighbor.
34 */
35public class NeighborForExtendedIsTest {
36 private final byte[] tlv = {
37 (byte) 0x10, (byte) 0x00, (byte) 0x10, (byte) 0x00, (byte) 0x10, (byte) 0x02, (byte) 0x00, (byte) 0x00,
38 (byte) 0x00, (byte) 0x0a, (byte) 0x3f, (byte) 0x06, (byte) 0x04, (byte) 0x14, (byte) 0x14, (byte) 0x14,
39 (byte) 0xbe, (byte) 0x08, (byte) 0x04, (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0xd1, (byte) 0x09,
40 (byte) 0x04, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x0a, (byte) 0x04, (byte) 0x49,
41 (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x0b, (byte) 0x20, (byte) 0x49, (byte) 0x98, (byte) 0x96,
42 (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96,
43 (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96,
44 (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96,
45 (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x12, (byte) 0x03, (byte) 0x00,
46 (byte) 0x00, (byte) 0x00, (byte) 0x10, (byte) 0x00, (byte) 0x10, (byte) 0x00, (byte) 0x10, (byte) 0x01,
47 (byte) 0x00, (byte) 0x00, (byte) 0x00, (byte) 0x0a, (byte) 0x3f, (byte) 0x06, (byte) 0x04, (byte) 0x1e,
48 (byte) 0x1e, (byte) 0x1e, (byte) 0xce, (byte) 0x08, (byte) 0x04, (byte) 0x1e, (byte) 0x1e, (byte) 0x1e,
49 (byte) 0xa9, (byte) 0x09, (byte) 0x04, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x0a,
50 (byte) 0x04, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x0b, (byte) 0x20, (byte) 0x49,
51 (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49,
52 (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49,
53 (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49,
54 (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x49, (byte) 0x98, (byte) 0x96, (byte) 0x80, (byte) 0x12,
55 (byte) 0x03, (byte) 0x00, (byte) 0x00, (byte) 0x00
56 };
57
58 //tlv2 bytes are for testing the else part of readFrom() method
59 private final byte[] tlv2 = {
60 (byte) 0x10, (byte) 0x00, (byte) 0x10, (byte) 0x00, (byte) 0x10, (byte) 0x02, (byte) 0x00, (byte) 0x00,
61 (byte) 0x00, (byte) 0x0a, (byte) 0x06, (byte) 0x2D, (byte) 0x04, (byte) 0x14, (byte) 0x14, (byte) 0x14,
62 (byte) 0xbe, (byte) 0x2D, (byte) 0xd1, (byte) 0x14, (byte) 0x14, (byte) 0x14, (byte) 0xd1, (byte) 0x09
63 };
64 private final String neighborId1 = "1000.1000.1002.00";
65 private final String neighborId2 = "1000.1000.1001.00";
66 private final int metric = 10;
67 private final int subTlvLength = 6;
68 private NeighborForExtendedIs neighborForExtendedIs;
69 private ChannelBuffer channelBuffer;
70
71 @Before
72 public void setUp() throws Exception {
73 neighborForExtendedIs = new NeighborForExtendedIs();
74 channelBuffer = EasyMock.createMock(ChannelBuffer.class);
75 }
76
77 @After
78 public void tearDown() throws Exception {
79 neighborForExtendedIs = null;
80 channelBuffer = null;
81 }
82
83 /**
84 * Tests readFrom() method.
85 */
86 @Test
87 public void testReadFrom() throws Exception {
88 channelBuffer = ChannelBuffers.copiedBuffer(tlv);
89 neighborForExtendedIs.readFrom(channelBuffer);
90 assertThat(neighborForExtendedIs.teSubTlv().size(), is(subTlvLength));
91 assertThat(neighborForExtendedIs.neighborId(), is(neighborId1));
92 assertThat(neighborForExtendedIs.metric(), is(metric));
93
94 neighborForExtendedIs = new NeighborForExtendedIs();
95 neighborForExtendedIs.readFrom(channelBuffer);
96 assertThat(neighborForExtendedIs.teSubTlv().size(), is(subTlvLength));
97 assertThat(neighborForExtendedIs.neighborId(), is(neighborId2));
98 assertThat(neighborForExtendedIs.metric(), is(metric));
99 }
100
101 /**
102 * Tests else condition of readFrom() method.
103 */
104 @Test
105 public void testElsePartOfReadFrom() throws Exception {
106 channelBuffer = ChannelBuffers.copiedBuffer(tlv2);
107 neighborForExtendedIs = new NeighborForExtendedIs();
108 neighborForExtendedIs.readFrom(channelBuffer);
109 assertThat(neighborForExtendedIs.neighborId(), is(neighborId1));
110 assertThat(neighborForExtendedIs.metric(), is(metric));
111 }
112}