1/* 2 * Copyright (C) 2016 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 17package com.android.server.wifi.hotspot2.anqp; 18 19import static org.junit.Assert.assertEquals; 20 21import android.support.test.filters.SmallTest; 22 23import org.junit.Test; 24 25import java.net.ProtocolException; 26import java.nio.ByteBuffer; 27 28/** 29 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.IPAddressTypeAvailabilityElement}. 30 */ 31@SmallTest 32public class IPAddressTypeAvailabilityElementTest { 33 private static final int TEST_IPV4_AVAILABILITY = 34 IPAddressTypeAvailabilityElement.IPV4_PUBLIC; 35 private static final int TEST_IPV6_AVAILABILITY = 36 IPAddressTypeAvailabilityElement.IPV6_AVAILABLE; 37 38 private static int getIPAvailability() { 39 return (TEST_IPV4_AVAILABILITY << 2) | TEST_IPV6_AVAILABILITY; 40 } 41 42 /** 43 * Verify that ProtocolException will be thrown when parsing an empty buffer. 44 * 45 * @throws Exception 46 */ 47 @Test(expected = ProtocolException.class) 48 public void parseBufferEmptyBuffer() throws Exception { 49 IPAddressTypeAvailabilityElement.parse(ByteBuffer.allocate(0)); 50 } 51 52 /** 53 * Verify that ProtocolException will be thrown when parsing an buffer containing excess 54 * data. 55 * 56 * @throws Exception 57 */ 58 @Test(expected = ProtocolException.class) 59 public void parseBufferWithExcessData() throws Exception { 60 ByteBuffer buffer = ByteBuffer.allocate( 61 IPAddressTypeAvailabilityElement.EXPECTED_BUFFER_LENGTH + 1); 62 buffer.put((byte) getIPAvailability()); 63 buffer.put((byte) 0); // Excess data. 64 buffer.position(0); 65 IPAddressTypeAvailabilityElement.parse(ByteBuffer.allocate(0)); 66 } 67 68 /** 69 * Verify that the expected IPAddressTypeAvailabilityElement is returned when parsing 70 * a buffer containing the test data. 71 * 72 * @throws Exception 73 */ 74 @Test 75 public void parseBufferWithTestData() throws Exception { 76 ByteBuffer buffer = ByteBuffer.allocate( 77 IPAddressTypeAvailabilityElement.EXPECTED_BUFFER_LENGTH); 78 buffer.put((byte) getIPAvailability()); 79 buffer.position(0); 80 81 IPAddressTypeAvailabilityElement expectedElement = new IPAddressTypeAvailabilityElement( 82 TEST_IPV4_AVAILABILITY, TEST_IPV6_AVAILABILITY); 83 assertEquals(expectedElement, IPAddressTypeAvailabilityElement.parse(buffer)); 84 } 85} 86