Daniele Moro | 94660a0 | 2019-12-02 12:02:07 -0800 | [diff] [blame] | 1 | /* |
Joey Armstrong | 0964a2c | 2023-01-05 14:26:05 -0500 | [diff] [blame] | 2 | * Copyright 2019-2023 Open Networking Foundation (ONF) and the ONF Contributors |
Daniele Moro | 94660a0 | 2019-12-02 12:02:07 -0800 | [diff] [blame] | 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 | */ |
| 16 | |
| 17 | package org.opencord.bng.packets; |
| 18 | |
| 19 | import org.junit.Before; |
| 20 | import org.junit.Test; |
| 21 | import org.onlab.packet.Deserializer; |
| 22 | import org.onlab.packet.IpAddress; |
| 23 | import org.onlab.packet.PacketTestUtils; |
| 24 | |
| 25 | import java.nio.ByteBuffer; |
| 26 | |
| 27 | import static org.junit.Assert.assertArrayEquals; |
| 28 | import static org.junit.Assert.assertEquals; |
| 29 | |
| 30 | public 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 | } |