I18NameTest.java revision 450a34955b855f0d813400013a9dbeead9d84c7b
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.anqp; 18 19import static org.junit.Assert.*; 20 21import android.test.suitebuilder.annotation.SmallTest; 22 23import org.junit.Test; 24 25import java.net.ProtocolException; 26import java.nio.ByteBuffer; 27import java.nio.ByteOrder; 28import java.util.Locale; 29 30/** 31 * Unit tests for {@link com.android.server.wifi.anqp.I18Name}. 32 */ 33@SmallTest 34public class I18NameTest { 35 private static class I18NameTestMapping { 36 byte[] mBytes; 37 String mExpectedLanguage; 38 Locale mExpectedLocale; 39 String mExpectedText; 40 I18NameTestMapping(byte[] bytes, String language, Locale locale, String text) { 41 this.mBytes = bytes; 42 this.mExpectedLanguage = language; 43 this.mExpectedLocale = locale; 44 this.mExpectedText = text; 45 } 46 } 47 48 private static final byte[][] MALFORMED_I18_NAME_BYTES = 49 new byte[][] { 50 // Too short. 51 new byte[0], 52 new byte[] {(byte) 0x01}, 53 new byte[] {(byte) 0x01, (byte) 0x02}, 54 // Length value of 0x02 shorter than the length of the language code field 55 // (i.e. 3). 56 new byte[] { 57 (byte) 0x02, (byte) 0x00, (byte) 0x00, (byte) 0x00, 58 (byte) 0xb0, (byte) 0xb1}, 59 // Length value of 0xff longer than payload size. 60 new byte[] { 61 (byte) 0xff, (byte) 0x00, (byte) 0x00, (byte) 0x00, 62 (byte) 0xb0, (byte) 0xb1} 63 }; 64 65 private static final I18NameTestMapping[] I18_NAME_MAPPINGS = 66 new I18NameTestMapping[] { 67 new I18NameTestMapping( 68 new byte[] { 69 (byte) 0x09, (byte) 0x65, (byte) 0x6e, (byte) 0x00, 70 (byte) 0x74, (byte) 0x65, (byte) 0x73, (byte) 0x74, 71 (byte) 0x41, (byte) 0x70}, 72 "en", Locale.ENGLISH, "testAp"), 73 // TODO: Make sure the ISO-639 alpha-3 code is properly parsed (b/30311144). 74 /* 75 new I18NameTestMapping( 76 new byte[] { 77 (byte) 0x09, (byte) 0x65, (byte) 0x6e, (byte) 0x67, 78 (byte) 0x74, (byte) 0x65, (byte) 0x73, (byte) 0x74, 79 (byte) 0x41, (byte) 0x70}, 80 "eng", Locale.ENGLISH, "testAp"), 81 */ 82 new I18NameTestMapping( 83 new byte[] { 84 (byte) 0x0b, (byte) 0x66, (byte) 0x72, (byte) 0x00, 85 (byte) 0x62, (byte) 0x6c, (byte) 0x61, (byte) 0x68, 86 (byte) 0x62, (byte) 0x6c, (byte) 0x61, (byte) 0x68}, 87 "fr", Locale.FRENCH, "blahblah") 88 }; 89 90 /** 91 * Verifies that parsing malformed I18Name bytes results in a ProtocolException. 92 */ 93 @Test 94 public void testMalformedI18NameBytes() { 95 for (byte[] malformedBytes : MALFORMED_I18_NAME_BYTES) { 96 try { 97 I18Name i18Name = new I18Name(ByteBuffer.wrap( 98 malformedBytes).order(ByteOrder.LITTLE_ENDIAN)); 99 } catch (ProtocolException e) { 100 continue; 101 } 102 fail("Expected exception while parsing malformed I18 Name bytes: " + malformedBytes); 103 } 104 } 105 106 /** 107 * Verifies that a sampling of valid I18Name bytes are properly parsed. 108 */ 109 @Test 110 public void testI18NameParsing() { 111 112 for (I18NameTestMapping testMapping : I18_NAME_MAPPINGS) { 113 try { 114 115 I18Name i18Name = new I18Name(ByteBuffer.wrap( 116 testMapping.mBytes).order(ByteOrder.LITTLE_ENDIAN)); 117 assertEquals(testMapping.mExpectedLanguage, i18Name.getLanguage()); 118 assertEquals(testMapping.mExpectedLocale, i18Name.getLocale()); 119 assertEquals(testMapping.mExpectedText, i18Name.getText()); 120 } catch (ProtocolException e) { 121 fail("Exception encountered during parsing: " + e); 122 } 123 } 124 } 125} 126