blob: aa1e24a0e9da0c6ade6d69d4837115475d8cddb9 [file] [log] [blame]
Daniele Moro94660a02019-12-02 12:02:07 -08001/*
2 * Copyright 2019-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 */
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.assertEquals;
27
28public class PppoeTest {
29 private Deserializer<Pppoe> deserializer;
30
31 private byte versionAndType = 0x11;
32 private byte code = 0x9;
33 private byte codeSession = 0x0;
34 private short sessionId = 0x0102;
35 private short payloadLength = 0;
36
37 private byte[] padding = new byte[]{0x0, 0x0, 0x0};
38
39 private byte[] bytesDisc;
40 private byte[] bytesPadded;
41 private byte[] bytesSession;
42
43 @Before
44 public void setUp() throws Exception {
45 deserializer = Pppoe.deserializer();
46 ByteBuffer bb = ByteBuffer.allocate(Pppoe.HEADER_LENGTH);
47
48 bb.put(versionAndType);
49 bb.put(code);
50 bb.putShort(sessionId);
51 bb.putShort(payloadLength);
52
53 bytesDisc = bb.array();
54 ByteBuffer bbPadded = ByteBuffer.allocate(Pppoe.HEADER_LENGTH + 3);
55
56 bbPadded.put(versionAndType);
57 bbPadded.put(code);
58 bbPadded.putShort(sessionId);
59 bbPadded.putShort(payloadLength);
60 bbPadded.put(padding);
61
62 bytesPadded = bbPadded.array();
63
64 ByteBuffer bbSession = ByteBuffer.allocate(Pppoe.HEADER_LENGTH + 2);
65
66 bbSession.put(versionAndType);
67 bbSession.put(codeSession);
68 bbSession.putShort(sessionId);
69 bbSession.putShort((short) (payloadLength + 2));
70 bbSession.putShort(PppProtocolType.LCP.code());
71
72 bytesSession = bbSession.array();
73 }
74
75 @Test
76 public void testDeserializeBadInput() throws Exception {
77 PacketTestUtils.testDeserializeBadInput(deserializer);
78 }
79
80 @Test
81 public void testDeserializeTruncated() throws Exception {
82 PacketTestUtils.testDeserializeTruncated(deserializer, bytesDisc);
83 }
84
85 /**
86 * Tests deserialize and getters.
87 */
88 @Test
89 public void testDeserializeDiscovery() throws Exception {
90 Pppoe pppoe = deserializer.deserialize(bytesDisc, 0, bytesDisc.length);
91
92 assertEquals(versionAndType, (pppoe.getVersion() << 4) | pppoe.getTypeId());
93 assertEquals(code, pppoe.getPacketType().code());
94 assertEquals(sessionId, pppoe.getSessionId());
95 assertEquals(payloadLength, pppoe.getPayloadLength());
96 }
97
98 /**
99 * Tests deserialize with padded packet.
100 */
101 @Test
102 public void testDeserializePadded() throws Exception {
103 Pppoe pppoe = deserializer.deserialize(bytesPadded, 0, bytesPadded.length);
104
105 assertEquals(versionAndType, (pppoe.getVersion() << 4) | pppoe.getTypeId());
106 assertEquals(code, pppoe.getPacketType().code());
107 assertEquals(sessionId, pppoe.getSessionId());
108 assertEquals(payloadLength, pppoe.getPayloadLength());
109 }
110
111 /**
112 * Tests deserialize of PPPoE session packets.
113 */
114 @Test
115 public void testDeserializeSession() throws Exception {
116 Pppoe pppoe = deserializer.deserialize(bytesSession, 0, bytesSession.length);
117
118 assertEquals(versionAndType, (pppoe.getVersion() << 4) | pppoe.getTypeId());
119 assertEquals(codeSession, pppoe.getPacketType().code());
120 assertEquals(sessionId, pppoe.getSessionId());
121 assertEquals(payloadLength + 2, pppoe.getPayloadLength());
122 assertEquals(PppProtocolType.LCP.code(), pppoe.getPppProtocol());
123 }
124}