diff options
author | TreeHugger Robot <treehugger-gerrit@google.com> | 2017-05-31 22:15:06 +0000 |
---|---|---|
committer | Android (Google) Code Review <android-gerrit@google.com> | 2017-05-31 22:15:08 +0000 |
commit | 15252723876bb9d82edb6b24edbc40db11103ae2 (patch) | |
tree | 6ca0fc0e005d1a33ea302654bb7dfd35e58c91c6 /tests | |
parent | da01689b7c7062e5bf9613d2b6c93e035b50dc00 (diff) | |
parent | fa72bad238e1ac43501e0a2e27b50624e1431db7 (diff) |
Merge "hotspot2: anqp: add parsing support for Icon File ANQP element"
Diffstat (limited to 'tests')
-rw-r--r-- | tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSIconFileElementTest.java | 112 |
1 files changed, 112 insertions, 0 deletions
diff --git a/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSIconFileElementTest.java b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSIconFileElementTest.java new file mode 100644 index 000000000..c72d1d0e0 --- /dev/null +++ b/tests/wifitests/src/com/android/server/wifi/hotspot2/anqp/HSIconFileElementTest.java @@ -0,0 +1,112 @@ +/* + * Copyright (C) 2017 The Android Open Source Project + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package com.android.server.wifi.hotspot2.anqp; + +import static org.junit.Assert.assertEquals; + +import android.test.suitebuilder.annotation.SmallTest; + +import org.junit.Test; + +import java.io.ByteArrayOutputStream; +import java.io.IOException; +import java.nio.BufferUnderflowException; +import java.nio.ByteBuffer; +import java.nio.charset.StandardCharsets; + +/** + * Unit tests for {@link com.android.server.wifi.hotspot2.anqp.HSIconFileElement}. + */ +@SmallTest +public class HSIconFileElementTest { + private static final String TEST_ICON_TYPE = "png"; + private static final byte[] TEST_ICON_DATA = new byte[8]; + + /** + * Utility function for generating test data. + * + * @param statusCode Status code of the icon file download + * @return byte[] + */ + private static byte[] getTestData(int statusCode) throws IOException { + ByteArrayOutputStream out = new ByteArrayOutputStream(); + out.write((byte) statusCode); + if (statusCode != HSIconFileElement.STATUS_CODE_SUCCESS) { + // No need to write other data if status code is not success. + return out.toByteArray(); + } + + byte[] iconTypeBytes = TEST_ICON_TYPE.getBytes(StandardCharsets.US_ASCII); + out.write(iconTypeBytes.length); + out.write(iconTypeBytes); + out.write(TEST_ICON_DATA.length & 0xFF); + out.write((TEST_ICON_DATA.length >> 8) & 0xFF); + out.write(TEST_ICON_DATA); + return out.toByteArray(); + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing an empty buffer. + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseEmptyBuffer() throws Exception { + HSIconFileElement.parse(ByteBuffer.allocate(0)); + } + + /** + * Verify that BufferUnderflowException will be thrown when parsing a truncated buffer + * (missing a byte at the end). + * + * @throws Exception + */ + @Test(expected = BufferUnderflowException.class) + public void parseTruncatedBuffer() throws Exception { + ByteBuffer buffer = ByteBuffer.wrap(getTestData(HSIconFileElement.STATUS_CODE_SUCCESS)); + buffer.limit(buffer.remaining() - 1); + HSIconFileElement.parse(buffer); + } + + /** + * Verify that an expected {@link HSIconFileElement} is returned when parsing a buffer + * containing icon data. + * + * @throws Exception + */ + @Test + public void parseBufferWithIconData() throws Exception { + ByteBuffer buffer = ByteBuffer.wrap(getTestData(HSIconFileElement.STATUS_CODE_SUCCESS)); + HSIconFileElement expected = new HSIconFileElement( + HSIconFileElement.STATUS_CODE_SUCCESS, TEST_ICON_TYPE, TEST_ICON_DATA); + assertEquals(expected, HSIconFileElement.parse(buffer)); + } + + /** + * Verify that an expected {@link HSIconFileElement} is returned when parsing a buffer + * without icon data (icon file not found). + * + * @throws Exception + */ + @Test + public void parseBufferWithoutIconData() throws Exception { + ByteBuffer buffer = + ByteBuffer.wrap(getTestData(HSIconFileElement.STATUS_CODE_FILE_NOT_FOUND)); + HSIconFileElement expected = + new HSIconFileElement(HSIconFileElement.STATUS_CODE_FILE_NOT_FOUND, null, null); + assertEquals(expected, HSIconFileElement.parse(buffer)); + } +} |