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.support.test.filters.SmallTest; 23 24import org.junit.Test; 25 26import java.nio.ByteBuffer; 27import java.nio.ByteOrder; 28import java.util.ArrayList; 29import java.util.List; 30 31 32/** 33 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.NAIRealmElement}. 34 */ 35@SmallTest 36public class NAIRealmElementTest { 37 /** 38 * Helper function for returning a ByteBuffer containing raw bytes for NAI Realm Element 39 * with specified number of NAI Realm Data. 40 * 41 * @param dataCount The number of NAI Realm Data to be added to the buffer 42 * @return {@link ByteBuffer} 43 */ 44 private static ByteBuffer getTestBufferWithNAIRealmData(int dataCount) { 45 int dataLength = NAIRealmDataTestUtil.TEST_REAML_WITH_UTF8_DATA_BYTES.length * dataCount; 46 // 2-bytes for the NAI Realm Data count header. 47 ByteBuffer buffer = ByteBuffer.allocate(dataLength + 2).order(ByteOrder.LITTLE_ENDIAN); 48 buffer.putShort((short) dataCount); 49 for (int i = 0; i < dataCount; i++) { 50 buffer.put(NAIRealmDataTestUtil.TEST_REAML_WITH_UTF8_DATA_BYTES); 51 } 52 buffer.position(0); 53 return buffer; 54 } 55 56 /** 57 * Verify that a NAIRealmElement with an empty NAIRealmData list will be returned when parsing 58 * from an empty buffer. 59 * 60 * @throws Exception 61 */ 62 @Test 63 public void parseEmptyBuffer() throws Exception { 64 assertTrue(NAIRealmElement.parse( 65 ByteBuffer.wrap(new byte[0])).getRealmDataList().isEmpty()); 66 } 67 68 /** 69 * Verify that an expected NAIRealmElement will be returned when parsing a buffer containing 70 * a NAI Realm Element with single NAI Realm Data. 71 * 72 * @throws Exception 73 */ 74 @Test 75 public void parseBufferWithSingleNAIRealmData() throws Exception { 76 // Setup expected NAIRealmElement. 77 List<NAIRealmData> realmDataList = new ArrayList<>(); 78 realmDataList.add(NAIRealmDataTestUtil.TEST_REALM_DATA); 79 NAIRealmElement expected = new NAIRealmElement(realmDataList); 80 81 assertEquals(expected, NAIRealmElement.parse(getTestBufferWithNAIRealmData(1))); 82 } 83 84 /** 85 * Verify that an expected NAIRealmElement will be returned when parsing a buffer containing 86 * a NAI Realm Element with multiple NAI Realm Data. 87 * 88 * @throws Exception 89 */ 90 @Test 91 public void parseBufferWithMultipleNAIRealmData() throws Exception { 92 // Setup expected NAIRealmElement. 93 List<NAIRealmData> realmDataList = new ArrayList<>(); 94 realmDataList.add(NAIRealmDataTestUtil.TEST_REALM_DATA); 95 realmDataList.add(NAIRealmDataTestUtil.TEST_REALM_DATA); 96 NAIRealmElement expected = new NAIRealmElement(realmDataList); 97 98 assertEquals(expected, NAIRealmElement.parse(getTestBufferWithNAIRealmData(2))); 99 } 100} 101