blob: bc085c54485fb0b5972f85296fe994d4d58ad8af [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.PacketTestUtils;
23
24import java.nio.ByteBuffer;
25
26import static org.junit.Assert.assertArrayEquals;
27import static org.junit.Assert.assertEquals;
28
29public class GenericPppTest {
30 private Deserializer<GenericPpp> deserializer;
31
32 private byte code = 0x1;
33 private byte identifier = 0x1;
34 private short length = 0x08;
35 private byte[] payload = new byte[]{0x1, 0x4, 0x3, 0x4};
36 private byte[] padding = new byte[]{0x0, 0x0, 0x0};
37
38 private String packetToString = "";
39
40 private byte[] bytes;
41 private byte[] bytesPadded;
42
43 @Before
44 public void setUp() throws Exception {
45 deserializer = GenericPpp.deserializer();
46 ByteBuffer bb = ByteBuffer.allocate(Ppp.MIN_HEADER_LENGTH + 4);
47
48 bb.put(code);
49 bb.put(identifier);
50 bb.putShort(length);
51 bb.put(payload);
52
53 bytes = bb.array();
54
55 ByteBuffer bbPadded = ByteBuffer.allocate(Ppp.MIN_HEADER_LENGTH + 4 + 3);
56
57 bbPadded.put(code);
58 bbPadded.put(identifier);
59 bbPadded.putShort(length);
60 bbPadded.put(payload);
61 bbPadded.put(padding);
62
63 bytesPadded = bbPadded.array();
64 }
65
66 private short getTypeLength(byte type, short length) {
67 return (short) ((0x7f & type) << 9 | 0x1ff & length);
68 }
69
70 @Test
71 public void testDeserializeBadInput() throws Exception {
72 PacketTestUtils.testDeserializeBadInput(deserializer);
73 }
74
75 @Test
76 public void testDeserializeTruncated() throws Exception {
77// PacketTestUtils.testDeserializeTruncated(deserializer, bytes);
78 }
79
80 /**
81 * Tests deserialize and getters.
82 */
83 @Test
84 public void testDeserialize() throws Exception {
85 GenericPpp ppp = deserializer.deserialize(bytes, 0, bytes.length);
86
87 assertEquals(code, ppp.getCode());
88 assertEquals(identifier, ppp.getIdentifier());
89 assertEquals(length, ppp.getLength());
90 assertArrayEquals(payload, ppp.getPayload().serialize());
91 }
92
93 /**
94 * Tests deserialize with padded packet.
95 */
96 @Test
97 public void testDeserializePadded() throws Exception {
98 GenericPpp ppp = deserializer.deserialize(bytesPadded, 0, bytesPadded.length);
99
100 assertEquals(code, ppp.getCode());
101 assertEquals(identifier, ppp.getIdentifier());
102 assertEquals(length, ppp.getLength());
103 assertArrayEquals(payload, ppp.getPayload().serialize());
104 }
105
106}