1 /* 2 * Copyright (C) 2023 The Android Open Source Project 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 android.net.dhcp6 18 19 import androidx.test.filters.SmallTest 20 import androidx.test.runner.AndroidJUnit4 21 import com.android.net.module.util.HexDump 22 import com.android.testutils.assertThrows 23 import java.nio.ByteBuffer 24 import kotlin.test.assertTrue 25 import org.junit.Test 26 import org.junit.runner.RunWith 27 28 @RunWith(AndroidJUnit4::class) 29 @SmallTest 30 class Dhcp6PacketTest { 31 @Test testDecodeDhcp6SolicitPacketnull32 fun testDecodeDhcp6SolicitPacket() { 33 val solicitHex = 34 // Solicit, Transaction ID 35 "01000F51" + 36 // client identifier option(option_len=12) 37 "0001000C0003001B024CCBFFFE5F6EA9" + 38 // elapsed time option(option_len=2) 39 "000800020000" + 40 // IA_PD option(option_len=41, including IA prefix option) 41 "00190029DE3570F50000000000000000" + 42 // IA prefix option(option_len=25) 43 "001A001900000000000000004000000000000000000000000000000000" 44 val bytes = HexDump.hexStringToByteArray(solicitHex) 45 val packet = Dhcp6Packet.decodePacket(ByteBuffer.wrap(bytes)) 46 assertTrue(packet is Dhcp6SolicitPacket) 47 } 48 49 @Test testDecodeDhcp6SolicitPacket_incorrectOptionLengthnull50 fun testDecodeDhcp6SolicitPacket_incorrectOptionLength() { 51 val solicitHex = 52 // Solicit, Transaction ID 53 "01000F51" + 54 // client identifier option(option_len=12) 55 "0001000C0003001B024CCBFFFE5F6EA9" + 56 // elapsed time option(wrong option_len=4) 57 "000800040000" + 58 // IA_PD option(option_len=41, including IA prefix option) 59 "00190029DE3570F50000000000000000" + 60 // IA prefix option(option_len=25) 61 "001A001900000000000000004000000000000000000000000000000000" 62 val bytes = HexDump.hexStringToByteArray(solicitHex) 63 assertThrows(Dhcp6Packet.ParseException::class.java) { 64 Dhcp6Packet.decodePacket(ByteBuffer.wrap(bytes)) 65 } 66 } 67 68 @Test testDecodeDhcp6SolicitPacket_lastTruncatedOptionnull69 fun testDecodeDhcp6SolicitPacket_lastTruncatedOption() { 70 val solicitHex = 71 // Solicit, Transaction ID 72 "01000F51" + 73 // client identifier option(option_len=12) 74 "0001000C0003001B024CCBFFFE5F6EA9" + 75 // elapsed time option(option_len=2) 76 "000800020000" + 77 // IA_PD option(option_len=41, including IA prefix option) 78 "00190029DE3570F50000000000000000" + 79 // IA prefix option(option_len=25, missing one byte) 80 "001A0019000000000000000040000000000000000000000000000000" 81 val bytes = HexDump.hexStringToByteArray(solicitHex) 82 assertThrows(Dhcp6Packet.ParseException::class.java) { 83 Dhcp6Packet.decodePacket(ByteBuffer.wrap(bytes)) 84 } 85 } 86 87 @Test testDecodeDhcp6SolicitPacket_middleTruncatedOptionnull88 fun testDecodeDhcp6SolicitPacket_middleTruncatedOption() { 89 val solicitHex = 90 // Solicit, Transaction ID 91 "01000F51" + 92 // client identifier option(option_len=12, missing one byte) 93 "0001000C0003001B024CCBFFFE5F6E" + 94 // elapsed time option(option_len=2) 95 "000800020000" + 96 // IA_PD option(option_len=41, including IA prefix option) 97 "00190029DE3570F50000000000000000" + 98 // IA prefix option(option_len=25) 99 "001A001900000000000000004000000000000000000000000000000000" 100 val bytes = HexDump.hexStringToByteArray(solicitHex) 101 assertThrows(Dhcp6Packet.ParseException::class.java) { 102 Dhcp6Packet.decodePacket(ByteBuffer.wrap(bytes)) 103 } 104 } 105 106 @Test testDecodeDhcp6AdvertisePacketnull107 fun testDecodeDhcp6AdvertisePacket() { 108 val advertiseHex = 109 // Advertise, Transaction ID 110 "0200078A" + 111 // server identifier option(option_len=10) 112 "0002000A0003000186C9B26AED4D" + 113 // client identifier option(option_len=12) 114 "0001000C0003001B024CCBFFFE5F6EA9" + 115 // IA_PD option(option_len=70, including IA prefix option) 116 "001900460CDDCA0C000000CF0000014C" + 117 // IA prefix option(option_len=25, prefix="2401:fa00:49c:412::/64") 118 "001A00190000019F0000064F402401FA00049C04810000000000000000" + 119 // IA prefix option(option_len=25, prefix="fdfd:9ed6:7950:2::/64") 120 "001A00190000019F0000A8C040FDFD9ED6795000010000000000000000" 121 val bytes = HexDump.hexStringToByteArray(advertiseHex) 122 val packet = Dhcp6Packet.decodePacket(ByteBuffer.wrap(bytes)) 123 assertTrue(packet is Dhcp6AdvertisePacket) 124 } 125 126 @Test testDecodeDhcp6SolicitPacket_unsupportedOptionnull127 fun testDecodeDhcp6SolicitPacket_unsupportedOption() { 128 val advertiseHex = 129 // Advertise, Transaction ID 130 "0200078A" + 131 // server identifier option(option_len=10) 132 "0002000A0003000186C9B26AED4D" + 133 // client identifier option(option_len=12) 134 "0001000C0003001B024CCBFFFE5F6EA9" + 135 // SOL_MAX_RT (don't support this option yet) 136 "005200040000003C" + 137 // IA_PD option(option_len=70, including IA prefix option) 138 "001900460CDDCA0C000000CF0000014C" + 139 // IA prefix option(option_len=25, prefix="2401:fa00:49c:412::/64") 140 "001A00190000019F0000064F402401FA00049C04810000000000000000" + 141 // IA prefix option(option_len=25, prefix="fdfd:9ed6:7950:2::/64") 142 "001A00190000019F0000A8C040FDFD9ED6795000010000000000000000" 143 val bytes = HexDump.hexStringToByteArray(advertiseHex) 144 // The unsupported option will be skipped normally and won't throw ParseException. 145 val packet = Dhcp6Packet.decodePacket(ByteBuffer.wrap(bytes)) 146 assertTrue(packet is Dhcp6AdvertisePacket) 147 } 148 } 149