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 17 package com.android.server.wifi.hotspot2.anqp; 18 19 import static org.junit.Assert.assertEquals; 20 import static org.junit.Assert.assertFalse; 21 import static org.junit.Assert.assertTrue; 22 23 import androidx.test.filters.SmallTest; 24 25 import com.android.server.wifi.WifiBaseTest; 26 27 import org.junit.Test; 28 29 import java.net.ProtocolException; 30 import java.nio.ByteBuffer; 31 import java.nio.ByteOrder; 32 33 /** 34 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.HSWanMetricsElement}. 35 */ 36 @SmallTest 37 public class HSWanMetricsElementTest extends WifiBaseTest { 38 private static final int TEST_LINK_STATUS = HSWanMetricsElement.LINK_STATUS_UP; 39 private static final boolean TEST_SYMMETRIC_LINK = true; 40 private static final boolean TEST_AT_CAPACITY = true; 41 private static final long TEST_DOWNLINK_SPEED = 0x1234556L; 42 private static final long TEST_UPLINK_SPEED = 0x342343L; 43 private static final int TEST_DOWNLINK_LOAD = 0x23; 44 private static final int TEST_UPLINK_LOAD = 0x45; 45 private static final int TEST_LMD = 0x2132; 46 47 private static final HSWanMetricsElement TEST_ELEMENT = new HSWanMetricsElement( 48 TEST_LINK_STATUS, TEST_SYMMETRIC_LINK, TEST_AT_CAPACITY, 49 TEST_DOWNLINK_SPEED, TEST_UPLINK_SPEED, TEST_DOWNLINK_LOAD, 50 TEST_UPLINK_LOAD, TEST_LMD); 51 52 /** 53 * Helper function for generating a ByteBuffer with the test data. 54 * 55 * @return {@link ByteBuffer} 56 */ getTestBuffer()57 private ByteBuffer getTestBuffer() { 58 ByteBuffer buffer = ByteBuffer.allocate(HSWanMetricsElement.EXPECTED_BUFFER_SIZE) 59 .order(ByteOrder.LITTLE_ENDIAN); 60 int wanInfo = TEST_LINK_STATUS & HSWanMetricsElement.LINK_STATUS_MASK; 61 if (TEST_SYMMETRIC_LINK) wanInfo |= HSWanMetricsElement.SYMMETRIC_LINK_MASK; 62 if (TEST_AT_CAPACITY) wanInfo |= HSWanMetricsElement.AT_CAPACITY_MASK; 63 buffer.put((byte) wanInfo); 64 buffer.putInt((int) (TEST_DOWNLINK_SPEED & 0xFFFFFFFFL)); 65 buffer.putInt((int) (TEST_UPLINK_SPEED & 0xFFFFFFFFL)); 66 buffer.put((byte) (TEST_DOWNLINK_LOAD & 0xFF)); 67 buffer.put((byte) (TEST_UPLINK_LOAD & 0xFF)); 68 buffer.putShort((short) (TEST_LMD & 0xFFFF)); 69 buffer.position(0); 70 return buffer; 71 } 72 getUninitializedBuffer()73 private ByteBuffer getUninitializedBuffer() { 74 // Setup an uninitialized WAN Metrics element (or initialized with 0's) 75 ByteBuffer buffer = ByteBuffer.allocate(HSWanMetricsElement.EXPECTED_BUFFER_SIZE) 76 .order(ByteOrder.LITTLE_ENDIAN); 77 buffer.put(new byte[HSWanMetricsElement.EXPECTED_BUFFER_SIZE]); 78 buffer.position(0); 79 return buffer; 80 } 81 82 @Test testGetStatus()83 public void testGetStatus() { 84 assertEquals(TEST_LINK_STATUS, TEST_ELEMENT.getStatus()); 85 } 86 87 @Test testIsSymmetric()88 public void testIsSymmetric() { 89 assertEquals(TEST_SYMMETRIC_LINK, TEST_ELEMENT.isSymmetric()); 90 } 91 92 @Test testIsAtCapacity()93 public void testIsAtCapacity() { 94 assertEquals(TEST_AT_CAPACITY, TEST_ELEMENT.isAtCapacity()); 95 } 96 97 @Test testGetDownlinkSpeed()98 public void testGetDownlinkSpeed() { 99 assertEquals(TEST_DOWNLINK_SPEED, TEST_ELEMENT.getDownlinkSpeed()); 100 } 101 102 @Test testGetUplinkSpeed()103 public void testGetUplinkSpeed() { 104 assertEquals(TEST_UPLINK_SPEED, TEST_ELEMENT.getUplinkSpeed()); 105 } 106 107 @Test testGetDownlinkLoad()108 public void testGetDownlinkLoad() { 109 assertEquals(TEST_DOWNLINK_LOAD, TEST_ELEMENT.getDownlinkLoad()); 110 } 111 112 @Test testGetUplinkLoad()113 public void testGetUplinkLoad() { 114 assertEquals(TEST_UPLINK_LOAD, TEST_ELEMENT.getUplinkLoad()); 115 } 116 117 @Test testGetLMD()118 public void testGetLMD() { 119 assertEquals(TEST_LMD, TEST_ELEMENT.getLMD()); 120 } 121 122 /** 123 * Verify that ProtocolException will be thrown when parsing an empty buffer. 124 * 125 * @throws Exception 126 */ 127 @Test(expected = ProtocolException.class) parseEmptyBuffer()128 public void parseEmptyBuffer() throws Exception { 129 HSWanMetricsElement.parse(ByteBuffer.allocate(0)); 130 } 131 132 /** 133 * Verify that ProtocolException will be thrown when a buffer with size less than the 134 * expected. 135 * 136 * @throws Exception 137 */ 138 @Test(expected = ProtocolException.class) parseBufferWithLessThanExpectedSize()139 public void parseBufferWithLessThanExpectedSize() throws Exception { 140 ByteBuffer buffer = ByteBuffer.allocate(HSWanMetricsElement.EXPECTED_BUFFER_SIZE - 1); 141 buffer.put(new byte[HSWanMetricsElement.EXPECTED_BUFFER_SIZE - 1]); 142 buffer.position(0); 143 HSWanMetricsElement.parse(buffer); 144 } 145 146 /** 147 * Verify that ProtocolException will be thrown when a buffer with size more than the 148 * expected. 149 * 150 * @throws Exception 151 */ 152 @Test(expected = ProtocolException.class) parseBufferWithMoreThanExpectedSize()153 public void parseBufferWithMoreThanExpectedSize() throws Exception { 154 ByteBuffer buffer = ByteBuffer.allocate(HSWanMetricsElement.EXPECTED_BUFFER_SIZE + 1); 155 buffer.put(new byte[HSWanMetricsElement.EXPECTED_BUFFER_SIZE + 1]); 156 buffer.position(0); 157 HSWanMetricsElement.parse(buffer); 158 } 159 160 /** 161 * Verify that the expected HSWanMetricsElement is returned when parsing 162 * a buffer containing the test data. 163 * 164 * @throws Exception 165 */ 166 @Test parseBufferWithTestData()167 public void parseBufferWithTestData() throws Exception { 168 ByteBuffer buffer = getTestBuffer(); 169 HSWanMetricsElement expectedElement = new HSWanMetricsElement( 170 TEST_LINK_STATUS, TEST_SYMMETRIC_LINK, TEST_AT_CAPACITY, 171 TEST_DOWNLINK_SPEED, TEST_UPLINK_SPEED, TEST_DOWNLINK_LOAD, 172 TEST_UPLINK_LOAD, TEST_LMD); 173 assertEquals(expectedElement, HSWanMetricsElement.parse(buffer)); 174 assertTrue(expectedElement.isElementInitialized()); 175 } 176 177 /** 178 * Verify that an element with all 0's (uninitialized) is detected as uninitialized. 179 * 180 * @throws Exception 181 */ 182 @Test testIsInitialized()183 public void testIsInitialized() throws Exception { 184 ByteBuffer buffer = getUninitializedBuffer(); 185 HSWanMetricsElement parsedElement = HSWanMetricsElement.parse(buffer); 186 assertFalse(parsedElement.isElementInitialized()); 187 } 188 } 189