ProtocolPortTupleTest.java revision 1dc52f076977039a6bf112885feca6638c05cd29
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.test.suitebuilder.annotation.SmallTest; 22 23import org.junit.Test; 24 25import java.nio.BufferUnderflowException; 26import java.nio.ByteBuffer; 27import java.nio.ByteOrder; 28 29/** 30 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.ProtocolPortTuple}. 31 */ 32@SmallTest 33public class ProtocolPortTupleTest { 34 private static final int TEST_PROTOCOL = 1; 35 private static final int TEST_PORT = 2; 36 private static final int TEST_STATUS = ProtocolPortTuple.PROTO_STATUS_CLOSED; 37 38 /** 39 * Helper function for generating a buffer with test data. 40 * 41 * @param protocol Protocol value 42 * @param port Port value 43 * @param status Status value 44 * @return {@link ByteBuffer} 45 */ 46 private ByteBuffer getTestBuffer(int protocol, int port, int status) { 47 ByteBuffer buffer = ByteBuffer.allocate(ProtocolPortTuple.RAW_BYTE_SIZE) 48 .order(ByteOrder.LITTLE_ENDIAN); 49 buffer.put((byte) protocol); 50 buffer.putShort((short) port); 51 buffer.put((byte) status); 52 buffer.position(0); 53 return buffer; 54 } 55 56 /** 57 * Verify that BufferUnderflowException will be thrown when parsing an empty buffer. 58 * 59 * @throws Exception 60 */ 61 @Test(expected = BufferUnderflowException.class) 62 public void parseEmptyBuffer() throws Exception { 63 ProtocolPortTuple.parse(ByteBuffer.allocate(0)); 64 } 65 66 /** 67 * Verify that BufferUnderflowException will be thrown when parsing a buffer without 68 * the complete tuple data (missing status field). 69 * 70 * @throws Exception 71 */ 72 @Test(expected = BufferUnderflowException.class) 73 public void parseBufferWithIncompleteData() throws Exception { 74 ByteBuffer buffer = ByteBuffer.allocate(ProtocolPortTuple.RAW_BYTE_SIZE - 1); 75 buffer.put(new byte[ProtocolPortTuple.RAW_BYTE_SIZE - 1]); 76 buffer.position(0); 77 ProtocolPortTuple.parse(buffer); 78 } 79 80 /** 81 * Verify that the expected ProtocolPortTuple is returned when parsing a buffer contained 82 * the test data. 83 * 84 * @throws Exception 85 */ 86 @Test 87 public void parseBufferWithTestData() throws Exception { 88 ByteBuffer buffer = getTestBuffer(TEST_PROTOCOL, TEST_PORT, TEST_STATUS); 89 ProtocolPortTuple expected = new ProtocolPortTuple(TEST_PROTOCOL, TEST_PORT, TEST_STATUS); 90 assertEquals(expected, ProtocolPortTuple.parse(buffer)); 91 } 92} 93