RoamingConsortiumElementTest.java revision 74339de52d7066f22771d914e698da503232c107
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; 20import static org.junit.Assert.assertTrue; 21 22import android.test.suitebuilder.annotation.SmallTest; 23import android.util.Pair; 24 25import org.junit.Test; 26 27import java.io.ByteArrayOutputStream; 28import java.io.IOException; 29import java.net.ProtocolException; 30import java.nio.BufferUnderflowException; 31import java.nio.ByteBuffer; 32import java.util.ArrayList; 33import java.util.List; 34 35/** 36 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.RoamingConsortiumElement}. 37 */ 38@SmallTest 39public class RoamingConsortiumElementTest { 40 // Default test data. Each test data contained a pair indicating the number of bytes for the 41 // OI and the value of the OI. 42 private static final Pair<Integer, Long> TEST_OI1 = new Pair<Integer, Long>(1, 0x12L); 43 private static final Pair<Integer, Long> TEST_OI2 = new Pair<Integer, Long>(2, 0x1234L); 44 private static final Pair<Integer, Long> TEST_OI3 = new Pair<Integer, Long>(4, 0x12345678L); 45 private static final Pair<Integer, Long> TEST_OI4 = new Pair<Integer, Long>(8, 0x1234567890L); 46 47 /** 48 * Helper function for appending an OI field to the given output stream. 49 * 50 * @param stream The output stream to write to 51 * @param OI The OI to write to the output stream 52 */ 53 private void appendOI(ByteArrayOutputStream stream, Pair<Integer, Long> oi) { 54 stream.write(oi.first.byteValue()); 55 // Write the OI data in big-endian. 56 for (int i = oi.first.intValue() - 1; i >= 0; i--) { 57 stream.write((byte) ((oi.second.longValue() >> i * Byte.SIZE) & 0xFF)); 58 } 59 } 60 /** 61 * Helper function for generating test data with the provided OIs. 62 * 63 * @param OIs The OIs to generate the data with 64 * @return byte[] of data 65 * @throws IOException 66 */ 67 private byte[] getTestData(List<Pair<Integer, Long>> ois) throws IOException { 68 ByteArrayOutputStream stream = new ByteArrayOutputStream(); 69 for (Pair<Integer, Long> oi : ois) { 70 appendOI(stream, oi); 71 } 72 return stream.toByteArray(); 73 } 74 75 /** 76 * Helper function for generating test data using the predefined OIs. 77 * 78 * @return byte[] of data 79 * @throws IOException 80 */ 81 private byte[] getDefaultTestData() throws IOException { 82 List<Pair<Integer, Long>> oiList = new ArrayList<>(); 83 oiList.add(TEST_OI1); 84 oiList.add(TEST_OI2); 85 oiList.add(TEST_OI3); 86 oiList.add(TEST_OI4); 87 return getTestData(oiList); 88 } 89 90 /** 91 * Helper function for creating a RoamingConsortiumElement using the predefined OIs. 92 * 93 * @return {@link RoamingConsortiumElement} 94 */ 95 private RoamingConsortiumElement getDefaultElement() { 96 List<Long> oiList = new ArrayList<>(); 97 oiList.add(TEST_OI1.second); 98 oiList.add(TEST_OI2.second); 99 oiList.add(TEST_OI3.second); 100 oiList.add(TEST_OI4.second); 101 return new RoamingConsortiumElement(oiList); 102 } 103 104 /** 105 * Verify that no exception will be thrown when parsing an empty buffer and the returned 106 * RoamingConsortiumElement will contained an empty list of OIs. 107 * 108 * @throws Exception 109 */ 110 @Test 111 public void parseEmptyBuffer() throws Exception { 112 RoamingConsortiumElement element = RoamingConsortiumElement.parse(ByteBuffer.allocate(0)); 113 assertTrue(element.getOIs().isEmpty()); 114 } 115 116 /** 117 * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer 118 * (missing a byte at the end). 119 * 120 * @throws Exception 121 */ 122 @Test(expected = BufferUnderflowException.class) 123 public void parseTruncatedBuffer() throws Exception { 124 ByteBuffer buffer = ByteBuffer.wrap(getDefaultTestData()); 125 buffer.limit(buffer.remaining() - 1); 126 RoamingConsortiumElement.parse(buffer); 127 } 128 129 /** 130 * Verify that an expected RoamingConsortiumElement will be returned when parsing a buffer 131 * containing valid data. 132 * 133 * @throws Exception 134 */ 135 @Test 136 public void parseBufferWithDefaultTestData() throws Exception { 137 // Setup expected element. 138 RoamingConsortiumElement expectedElement = getDefaultElement(); 139 140 ByteBuffer buffer = ByteBuffer.wrap(getDefaultTestData()); 141 assertEquals(expectedElement, RoamingConsortiumElement.parse(buffer)); 142 } 143 144 /** 145 * Verify that ProtocolException will be thrown when parsing a buffer contained an OI length 146 * that's less than minimum allowed. 147 * 148 * @throws Exception 149 */ 150 @Test(expected = ProtocolException.class) 151 public void parseBufferWithOILengthLessThanMinimum() throws Exception { 152 ByteBuffer buffer = ByteBuffer.allocate(1); 153 buffer.put((byte) (RoamingConsortiumElement.MINIMUM_OI_LENGTH - 1)); 154 buffer.position(0); 155 RoamingConsortiumElement.parse(buffer); 156 } 157 158 /** 159 * Verify that ProtocolException will be thrown when parsing a buffer contained an OI length 160 * that's more than maximum allowed. 161 * 162 * @throws Exception 163 */ 164 @Test(expected = ProtocolException.class) 165 public void parseBufferWithOILengthMoreThanMaximum() throws Exception { 166 ByteBuffer buffer = ByteBuffer.allocate(1); 167 buffer.put((byte) (RoamingConsortiumElement.MAXIMUM_OI_LENGTH + 1)); 168 buffer.position(0); 169 RoamingConsortiumElement.parse(buffer); 170 } 171} 172