1 /* 2 * Copyright (C) 2018 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.phone.ecc; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import androidx.test.InstrumentationRegistry; 22 import androidx.test.runner.AndroidJUnit4; 23 24 import com.android.TelephonyTestBase; 25 import com.android.phone.ecc.nano.ProtobufEccData; 26 27 import org.junit.Test; 28 import org.junit.runner.RunWith; 29 30 import java.io.BufferedInputStream; 31 import java.io.ByteArrayOutputStream; 32 import java.io.IOException; 33 import java.io.InputStream; 34 import java.util.HashSet; 35 import java.util.Locale; 36 import java.util.zip.GZIPInputStream; 37 38 /** 39 * Unit tests for eccdata. 40 */ 41 @RunWith(AndroidJUnit4.class) 42 public class EccDataTest extends TelephonyTestBase { 43 @Test testEccDataContent()44 public void testEccDataContent() throws IOException { 45 InputStream eccData = new GZIPInputStream(new BufferedInputStream( 46 InstrumentationRegistry.getTargetContext().getAssets().open("eccdata"))); 47 ProtobufEccData.AllInfo allEccMessages = ProtobufEccData.AllInfo.parseFrom( 48 readInputStreamToByteArray(eccData)); 49 eccData.close(); 50 51 HashSet loadedIsos = new HashSet(300); 52 HashSet loadedNumbers = new HashSet(5); 53 HashSet loadedMncs = new HashSet(5); 54 55 for (ProtobufEccData.CountryInfo countryInfo : allEccMessages.countries) { 56 assertThat(countryInfo.isoCode).isNotEmpty(); 57 assertThat(countryInfo.isoCode).isEqualTo(countryInfo.isoCode.toUpperCase( 58 Locale.ROOT).trim()); 59 assertThat(loadedIsos.contains(countryInfo.isoCode)).isFalse(); 60 loadedIsos.add(countryInfo.isoCode); 61 62 loadedNumbers.clear(); 63 for (ProtobufEccData.EccInfo eccInfo : countryInfo.eccs) { 64 assertThat(eccInfo.phoneNumber).isNotEmpty(); 65 assertThat(eccInfo.phoneNumber).isEqualTo(eccInfo.phoneNumber.trim()); 66 assertThat(loadedNumbers.contains(eccInfo.phoneNumber)).isFalse(); 67 assertThat(eccInfo.types).isNotEmpty(); 68 loadedNumbers.add(eccInfo.phoneNumber); 69 if (eccInfo.routing == ProtobufEccData.EccInfo.Routing.NORMAL) { 70 loadedMncs.clear(); 71 for (String mnc : eccInfo.normalRoutingMncs) { 72 assertThat(mnc).isNotEmpty(); 73 assertThat(mnc).isEqualTo(mnc.trim()); 74 assertThat(loadedMncs.contains(mnc)).isFalse(); 75 assertThat(mnc.length()).isGreaterThan(1); 76 assertThat(mnc.length()).isLessThan(4); 77 loadedMncs.add(mnc); 78 } 79 } 80 } 81 } 82 } 83 84 /** 85 * Util function to convert inputStream to byte array before parsing proto data. 86 */ readInputStreamToByteArray(InputStream inputStream)87 private static byte[] readInputStreamToByteArray(InputStream inputStream) throws IOException { 88 ByteArrayOutputStream buffer = new ByteArrayOutputStream(); 89 int nRead; 90 byte[] data = new byte[16 * 1024]; // Read 16k chunks 91 while ((nRead = inputStream.read(data, 0, data.length)) != -1) { 92 buffer.write(data, 0, nRead); 93 } 94 buffer.flush(); 95 return buffer.toByteArray(); 96 } 97 } 98