Gustavo Silva | 7055cce | 2021-01-22 13:48:30 -0300 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2021-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 | package org.onlab.packet; |
| 17 | |
| 18 | import com.google.common.io.Resources; |
| 19 | |
| 20 | import java.nio.ByteBuffer; |
| 21 | |
| 22 | import org.junit.Test; |
| 23 | |
| 24 | import static org.junit.Assert.*; |
| 25 | |
| 26 | /** |
| 27 | * Unit tests for PPPoED class. |
| 28 | */ |
| 29 | public class PPPoEDTest { |
| 30 | |
| 31 | private static final byte VERSION = (byte) 1; |
| 32 | private static final byte TYPE = (byte) 1; |
| 33 | private static final short NO_SESSION = (short) 0; |
| 34 | private static final short SESSION = (short) 0x02e2; |
| 35 | |
| 36 | private static final String AC_NAME = "pfSense.localdomain"; |
| 37 | private static final long AC_COOKIE = 32098554083868671L; |
| 38 | private static final String SERVICE_NAME = "*"; |
| 39 | |
| 40 | private static final String PADI = "pppoed/padi.bin"; |
| 41 | private static final String PADO = "pppoed/pado.bin"; |
| 42 | private static final String PADR = "pppoed/padr.bin"; |
| 43 | private static final String PADS = "pppoed/pads.bin"; |
| 44 | private static final String PADT = "pppoed/padt.bin"; |
| 45 | |
| 46 | @Test |
| 47 | public void testDeserializeBadInput() throws Exception { |
| 48 | PacketTestUtils.testDeserializeBadInput(PPPoED.deserializer()); |
| 49 | } |
| 50 | |
| 51 | @Test |
| 52 | public void testTruncatedPacket() throws Exception { |
| 53 | byte[] byteHeader = ByteBuffer.allocate(6) |
| 54 | .put((byte) 0x11) // version/type |
| 55 | .put((byte) 0x09) // code |
| 56 | .putShort((short) 0x0000) // transaction id |
| 57 | .putShort((short) 0x0000) // payload length |
| 58 | .array(); |
| 59 | |
| 60 | PacketTestUtils.testDeserializeTruncated(PPPoED.deserializer(), byteHeader); |
| 61 | } |
| 62 | |
| 63 | @Test |
| 64 | public void testDeserializePadi() throws Exception { |
| 65 | byte[] data = Resources.toByteArray(PPPoEDTest.class.getResource(PADI)); |
| 66 | Ethernet eth = Ethernet.deserializer().deserialize(data, 0, data.length); |
| 67 | PPPoED pppoed = (PPPoED) eth.getPayload(); |
| 68 | assertPPPoEDHeader(pppoed, PPPoED.PPPOED_CODE_PADI); |
| 69 | assertEquals(NO_SESSION, pppoed.getSessionId()); |
| 70 | assertEquals(0, pppoed.getTags().size()); |
| 71 | } |
| 72 | |
| 73 | @Test |
| 74 | public void testDeserializePado() throws Exception { |
| 75 | byte[] data = Resources.toByteArray(PPPoEDTest.class.getResource(PADO)); |
| 76 | Ethernet eth = Ethernet.deserializer().deserialize(data, 0, data.length); |
| 77 | |
| 78 | PPPoED pppoed = (PPPoED) eth.getPayload(); |
| 79 | assertPPPoEDHeader(pppoed, PPPoED.PPPOED_CODE_PADO); |
| 80 | assertEquals(NO_SESSION, pppoed.getSessionId()); |
| 81 | |
| 82 | assertEquals(3, pppoed.getTags().size()); |
| 83 | |
| 84 | PPPoEDTag acName = pppoed.getTag(PPPoEDTag.PPPOED_TAG_AC_NAME); |
| 85 | assertArrayEquals(AC_NAME.getBytes(), acName.value); |
| 86 | |
| 87 | PPPoEDTag serviceName = pppoed.getTag(PPPoEDTag.PPPOED_TAG_SERVICE_NAME); |
| 88 | assertArrayEquals(SERVICE_NAME.getBytes(), serviceName.value); |
| 89 | |
| 90 | PPPoEDTag acCookie = pppoed.getTag(PPPoEDTag.PPPOED_TAG_AC_COOKIE); |
| 91 | assertEquals(AC_COOKIE, ByteBuffer.wrap(acCookie.value).getLong()); |
| 92 | } |
| 93 | |
| 94 | @Test |
| 95 | public void testDeserializePadr() throws Exception { |
| 96 | byte[] data = Resources.toByteArray(PPPoEDTest.class.getResource(PADR)); |
| 97 | Ethernet eth = Ethernet.deserializer().deserialize(data, 0, data.length); |
| 98 | |
| 99 | PPPoED pppoed = (PPPoED) eth.getPayload(); |
| 100 | assertPPPoEDHeader(pppoed, PPPoED.PPPOED_CODE_PADR); |
| 101 | assertEquals(NO_SESSION, pppoed.getSessionId()); |
| 102 | |
| 103 | assertEquals(1, pppoed.getTags().size()); |
| 104 | |
| 105 | PPPoEDTag acCookie = pppoed.getTag(PPPoEDTag.PPPOED_TAG_AC_COOKIE); |
| 106 | assertEquals(AC_COOKIE, ByteBuffer.wrap(acCookie.value).getLong()); |
| 107 | } |
| 108 | |
| 109 | @Test |
| 110 | public void testDeserializePads() throws Exception { |
| 111 | byte[] data = Resources.toByteArray(PPPoEDTest.class.getResource(PADS)); |
| 112 | Ethernet eth = Ethernet.deserializer().deserialize(data, 0, data.length); |
| 113 | |
| 114 | PPPoED pppoed = (PPPoED) eth.getPayload(); |
| 115 | assertPPPoEDHeader(pppoed, PPPoED.PPPOED_CODE_PADS); |
| 116 | assertEquals(SESSION, pppoed.getSessionId()); |
| 117 | |
| 118 | assertEquals(2, pppoed.getTags().size()); |
| 119 | PPPoEDTag acName = pppoed.getTag(PPPoEDTag.PPPOED_TAG_AC_NAME); |
| 120 | assertArrayEquals(AC_NAME.getBytes(), acName.value); |
| 121 | |
| 122 | PPPoEDTag acCookie = pppoed.getTag(PPPoEDTag.PPPOED_TAG_AC_COOKIE); |
| 123 | assertEquals(AC_COOKIE, ByteBuffer.wrap(acCookie.value).getLong()); |
| 124 | } |
| 125 | |
| 126 | @Test |
| 127 | public void testDeserializePadt() throws Exception { |
| 128 | byte[] data = Resources.toByteArray(PPPoEDTest.class.getResource(PADT)); |
| 129 | Ethernet eth = Ethernet.deserializer().deserialize(data, 0, data.length); |
| 130 | |
| 131 | PPPoED pppoed = (PPPoED) eth.getPayload(); |
| 132 | assertPPPoEDHeader(pppoed, PPPoED.PPPOED_CODE_PADT); |
| 133 | assertEquals(SESSION, pppoed.getSessionId()); |
| 134 | |
| 135 | assertEquals(1, pppoed.getTags().size()); |
| 136 | PPPoEDTag acCookie = pppoed.getTag(PPPoEDTag.PPPOED_TAG_AC_COOKIE); |
| 137 | assertEquals(AC_COOKIE, ByteBuffer.wrap(acCookie.value).getLong()); |
| 138 | } |
| 139 | |
| 140 | private void assertPPPoEDHeader(PPPoED pppoed, byte code) { |
| 141 | assertNotNull(pppoed); |
| 142 | assertEquals(VERSION, pppoed.getVersion()); |
| 143 | assertEquals(TYPE, pppoed.getType()); |
| 144 | assertEquals(code, pppoed.getCode()); |
| 145 | } |
| 146 | |
| 147 | } |