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.net.ProtocolException; 26import java.nio.ByteBuffer; 27import java.nio.ByteOrder; 28 29/** 30 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.HSWanMetricsElement}. 31 */ 32@SmallTest 33public class HSWanMetricsElementTest { 34 private static final int TEST_LINK_STATUS = HSWanMetricsElement.LINK_STATUS_UP; 35 private static final boolean TEST_SYMMETRIC_LINK = true; 36 private static final boolean TEST_AT_CAPACITY = true; 37 private static final long TEST_DOWNLINK_SPEED = 0x1234556L; 38 private static final long TEST_UPLINK_SPEED = 0x342343L; 39 private static final int TEST_DOWNLINK_LOAD = 0x23; 40 private static final int TEST_UPLINK_LOAD = 0x45; 41 private static final int TEST_LMD = 0x2132; 42 43 /** 44 * Helper function for generating a ByteBuffer with the test data. 45 * 46 * @return {@link ByteBuffer} 47 */ 48 private ByteBuffer getTestBuffer() { 49 ByteBuffer buffer = ByteBuffer.allocate(HSWanMetricsElement.EXPECTED_BUFFER_SIZE) 50 .order(ByteOrder.LITTLE_ENDIAN); 51 int wanInfo = TEST_LINK_STATUS & HSWanMetricsElement.LINK_STATUS_MASK; 52 if (TEST_SYMMETRIC_LINK) wanInfo |= HSWanMetricsElement.SYMMETRIC_LINK_MASK; 53 if (TEST_AT_CAPACITY) wanInfo |= HSWanMetricsElement.AT_CAPACITY_MASK; 54 buffer.put((byte) wanInfo); 55 buffer.putInt((int) (TEST_DOWNLINK_SPEED & 0xFFFFFFFFL)); 56 buffer.putInt((int) (TEST_UPLINK_SPEED & 0xFFFFFFFFL)); 57 buffer.put((byte) (TEST_DOWNLINK_LOAD & 0xFF)); 58 buffer.put((byte) (TEST_UPLINK_LOAD & 0xFF)); 59 buffer.putShort((short) (TEST_LMD & 0xFFFF)); 60 buffer.position(0); 61 return buffer; 62 } 63 64 /** 65 * Verify that ProtocolException will be thrown when parsing an empty buffer. 66 * 67 * @throws Exception 68 */ 69 @Test(expected = ProtocolException.class) 70 public void parseEmptyBuffer() throws Exception { 71 HSWanMetricsElement.parse(ByteBuffer.allocate(0)); 72 } 73 74 /** 75 * Verify that ProtocolException will be thrown when a buffer with size less than the 76 * expected. 77 * 78 * @throws Exception 79 */ 80 @Test(expected = ProtocolException.class) 81 public void parseBufferWithLessThanExpectedSize() throws Exception { 82 ByteBuffer buffer = ByteBuffer.allocate(HSWanMetricsElement.EXPECTED_BUFFER_SIZE - 1); 83 buffer.put(new byte[HSWanMetricsElement.EXPECTED_BUFFER_SIZE - 1]); 84 buffer.position(0); 85 HSWanMetricsElement.parse(buffer); 86 } 87 88 /** 89 * Verify that ProtocolException will be thrown when a buffer with size more than the 90 * expected. 91 * 92 * @throws Exception 93 */ 94 @Test(expected = ProtocolException.class) 95 public void parseBufferWithMoreThanExpectedSize() throws Exception { 96 ByteBuffer buffer = ByteBuffer.allocate(HSWanMetricsElement.EXPECTED_BUFFER_SIZE + 1); 97 buffer.put(new byte[HSWanMetricsElement.EXPECTED_BUFFER_SIZE + 1]); 98 buffer.position(0); 99 HSWanMetricsElement.parse(buffer); 100 } 101 102 /** 103 * Verify that the expected HSWanMetricsElement is returned when parsing 104 * a buffer containing the test data. 105 * 106 * @throws Exception 107 */ 108 @Test 109 public void parseBufferWithTestData() throws Exception { 110 ByteBuffer buffer = getTestBuffer(); 111 HSWanMetricsElement expectedElement = new HSWanMetricsElement( 112 TEST_LINK_STATUS, TEST_SYMMETRIC_LINK, TEST_AT_CAPACITY, 113 TEST_DOWNLINK_SPEED, TEST_UPLINK_SPEED, TEST_DOWNLINK_LOAD, 114 TEST_UPLINK_LOAD, TEST_LMD); 115 assertEquals(expectedElement, HSWanMetricsElement.parse(buffer)); 116 } 117} 118