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.zip.GZIPInputStream;
36 
37 /**
38  * Unit tests for eccdata.
39  */
40 @RunWith(AndroidJUnit4.class)
41 public class EccDataTest extends TelephonyTestBase {
42     @Test
testEccDataContent()43     public void testEccDataContent() throws IOException {
44         InputStream eccData = new GZIPInputStream(new BufferedInputStream(
45                 InstrumentationRegistry.getTargetContext().getAssets().open("eccdata")));
46         ProtobufEccData.AllInfo allEccMessages = ProtobufEccData.AllInfo.parseFrom(
47                 readInputStreamToByteArray(eccData));
48         eccData.close();
49 
50         HashSet loadedIsos = new HashSet(300);
51         HashSet loadedNumbers = new HashSet(5);
52 
53         for (ProtobufEccData.CountryInfo countryInfo : allEccMessages.countries) {
54             assertThat(countryInfo.isoCode).isNotEmpty();
55             assertThat(countryInfo.isoCode).isEqualTo(countryInfo.isoCode.toUpperCase().trim());
56             assertThat(loadedIsos.contains(countryInfo.isoCode)).isFalse();
57             loadedIsos.add(countryInfo.isoCode);
58 
59             loadedNumbers.clear();
60             for (ProtobufEccData.EccInfo eccInfo : countryInfo.eccs) {
61                 assertThat(eccInfo.phoneNumber).isNotEmpty();
62                 assertThat(eccInfo.phoneNumber).isEqualTo(eccInfo.phoneNumber.trim());
63                 assertThat(loadedNumbers.contains(eccInfo.phoneNumber)).isFalse();
64                 assertThat(eccInfo.types).isNotEmpty();
65                 loadedNumbers.add(eccInfo.phoneNumber);
66             }
67         }
68     }
69 
70     /**
71      * Util function to convert inputStream to byte array before parsing proto data.
72      */
readInputStreamToByteArray(InputStream inputStream)73     private static byte[] readInputStreamToByteArray(InputStream inputStream) throws IOException {
74         ByteArrayOutputStream buffer = new ByteArrayOutputStream();
75         int nRead;
76         byte[] data = new byte[16 * 1024]; // Read 16k chunks
77         while ((nRead = inputStream.read(data, 0, data.length)) != -1) {
78             buffer.write(data, 0, nRead);
79         }
80         buffer.flush();
81         return buffer.toByteArray();
82     }
83 }
84