blob: a4e74b9ae6558242daf1cb85f4bcaa7e77ae294c [file] [log] [blame]
Daniele Moro94660a02019-12-02 12:02:07 -08001/*
Joey Armstronga68e7852024-01-28 13:27:02 -05002 * Copyright 2019-2024 Open Networking Foundation (ONF) and the ONF Contributors
Daniele Moro94660a02019-12-02 12:02:07 -08003 *
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 */
16
17package org.opencord.bng.packets;
18
19import org.junit.Before;
20import org.junit.Test;
21import org.onlab.packet.Deserializer;
22import org.onlab.packet.IpAddress;
23import org.onlab.packet.PacketTestUtils;
24
25import java.nio.ByteBuffer;
26
27import static org.junit.Assert.assertArrayEquals;
28import static org.junit.Assert.assertEquals;
29
30public class IpcpTest {
31
32 private Deserializer<Ipcp> deserializer;
33
34 private byte ipAddressTlvSize = 0x6;
35 private IpAddress ipAddress = IpAddress.valueOf("10.0.0.1");
36
37 private byte optionalTlvSize = 0x6;
38 private byte optionalTlvType = 0x01;
39 private byte[] optionalTlvValue = new byte[]{0x4, 0x3, 0x2, 0x1};
40
41 private byte code = Ipcp.CONF_REQ;
42 private byte identifier = 0x1;
43 private short length = (short) (Ipcp.MIN_HEADER_LENGTH + ipAddressTlvSize + optionalTlvSize);
44
45
46 private byte[] bytes;
47
48 @Before
49 public void setUp() throws Exception {
50 deserializer = Ipcp.deserializer();
51 ByteBuffer bb = ByteBuffer.allocate(Ipcp.MIN_HEADER_LENGTH + ipAddressTlvSize + optionalTlvSize);
52
53 bb.put(code);
54 bb.put(identifier);
55 bb.putShort(length);
56
57 bb.put(PppTlv.IPCPTLV_IP_ADDRESS);
58 bb.put(ipAddressTlvSize);
59 bb.put(ipAddress.toOctets());
60
61 bb.put(optionalTlvType);
62 bb.put(optionalTlvSize);
63 bb.put(optionalTlvValue);
64
65 bytes = bb.array();
66 }
67
68 @Test
69 public void testDeserializeBadInput() throws Exception {
70 PacketTestUtils.testDeserializeBadInput(deserializer);
71 }
72
73 @Test
74 public void testDeserializeTruncated() throws Exception {
75 PacketTestUtils.testDeserializeTruncated(deserializer, bytes);
76 }
77
78 /**
79 * Tests deserialize and getters.
80 */
81 @Test
82 public void testDeserialize() throws Exception {
83 Ipcp ipcp = deserializer.deserialize(bytes, 0, bytes.length);
84 PppTlv optionalTlv = ipcp.getIpcpTlvList().get(0);
85
86 assertEquals(code, ipcp.getCode());
87 assertEquals(identifier, ipcp.getIdentifier());
88 assertEquals(length, ipcp.getLength());
89
90 assertEquals(optionalTlvType, optionalTlv.getType());
91 assertEquals(optionalTlvSize, optionalTlv.getLength());
92 assertArrayEquals(optionalTlvValue, optionalTlv.getValue());
93
94 assertEquals(PppTlv.IPCPTLV_IP_ADDRESS, ipcp.getIpAddressTlv().getType());
95 assertEquals(ipAddressTlvSize, ipcp.getIpAddressTlv().getLength());
96 assertEquals(ipAddress, IpAddress.valueOf(IpAddress.Version.INET,
97 ipcp.getIpAddressTlv().getValue()));
98 assertEquals(ipAddress, ipcp.getIpAddress());
99 }
100}