1 /* 2 * Copyright (C) 2017 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.assertNotEquals; 21 22 import androidx.test.filters.SmallTest; 23 24 import com.android.server.wifi.WifiBaseTest; 25 26 import org.junit.Test; 27 28 import java.nio.BufferUnderflowException; 29 import java.nio.ByteBuffer; 30 31 /** 32 * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.IconInfo}. 33 */ 34 @SmallTest 35 public class IconInfoTest extends WifiBaseTest { 36 private static final int TEST_WIDTH = 1111; 37 private static final int TEST_HEIGHT = 2222; 38 private static final String TEST_LANGUAGE = "language"; 39 private static final String TEST_ICON_TYPE = "iconType"; 40 private static final String TEST_FILE_NAME = "filename"; 41 42 private static final IconInfo TEST_ICON_INFO = 43 new IconInfo(TEST_WIDTH, TEST_HEIGHT, TEST_LANGUAGE, TEST_ICON_TYPE, TEST_FILE_NAME); 44 45 @Test testGetWidth()46 public void testGetWidth() { 47 assertEquals(TEST_WIDTH, TEST_ICON_INFO.getWidth()); 48 } 49 50 @Test testGetHeight()51 public void testGetHeight() { 52 assertEquals(TEST_HEIGHT, TEST_ICON_INFO.getHeight()); 53 } 54 55 @Test testGetLanguage()56 public void testGetLanguage() { 57 assertEquals(TEST_LANGUAGE, TEST_ICON_INFO.getLanguage()); 58 } 59 60 @Test testGetIconType()61 public void testGetIconType() { 62 assertEquals(TEST_ICON_TYPE, TEST_ICON_INFO.getIconType()); 63 } 64 65 @Test testGetFileName()66 public void testGetFileName() { 67 assertEquals(TEST_FILE_NAME, TEST_ICON_INFO.getFileName()); 68 } 69 70 /** 71 * Verify that BufferUnderflowException will be thrown when parsing an empty buffer. 72 * @throws Exception 73 */ 74 @Test(expected = BufferUnderflowException.class) parseEmptyBuffer()75 public void parseEmptyBuffer() throws Exception { 76 IconInfo.parse(ByteBuffer.allocate(0)); 77 } 78 79 /** 80 * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer 81 * (missing a byte at the end). 82 * 83 * @throws Exception 84 */ 85 @Test(expected = BufferUnderflowException.class) parseTruncatedBuffer()86 public void parseTruncatedBuffer() throws Exception { 87 ByteBuffer buffer = ByteBuffer.wrap(IconInfoTestUtil.TEST_ICON_INFO_RAW_BYTES); 88 buffer.limit(buffer.remaining() - 1); 89 IconInfo.parse(buffer); 90 } 91 92 /** 93 * Verify that an expected {@link IconInfo} will be returned when parsing a buffer containing 94 * pre-defined test data. 95 * 96 * @throws Exception 97 */ 98 @Test parseBufferWithTestData()99 public void parseBufferWithTestData() throws Exception { 100 ByteBuffer buffer = ByteBuffer.wrap(IconInfoTestUtil.TEST_ICON_INFO_RAW_BYTES); 101 assertEquals(IconInfoTestUtil.TEST_ICON_INFO, IconInfo.parse(buffer)); 102 } 103 104 /** 105 * Tests the hash function. 106 */ 107 @Test testHashCode()108 public void testHashCode() { 109 int width1 = 1; 110 int height1 = 1; 111 String language1 = "language1"; 112 String iconType1 = "iconType1"; 113 String fileName1 = "filename1"; 114 115 int width2 = 2; 116 int height2 = 2; 117 String language2 = "language2"; 118 String iconType2 = "iconType2"; 119 String fileName2 = "filename2"; 120 121 IconInfo info1 = new IconInfo(width1, height1, language1, iconType1, fileName1); 122 IconInfo info2 = new IconInfo(width2, height2, language2, iconType2, fileName2); 123 IconInfo info1Dup = new IconInfo(width1, height1, language1, iconType1, fileName1); 124 125 assertNotEquals(info1.hashCode(), info2.hashCode()); 126 assertEquals(info1.hashCode(), info1Dup.hashCode()); 127 } 128 } 129