Rewrite compatibility checking in unit test.

Reweite the checking because protobuf support modules for Python has been
removed from prebuilt tools.

Test: atest TeleServiceTests:IsoToEccProtobufRepositoryTest#testEccDataContent
Bug: 119277125

Change-Id: Ie79c3c69026671a8cb3c96382ae4aa971c74d094
diff --git a/ecc/README.md b/ecc/README.md
index ef795d5..b5dc538 100644
--- a/ecc/README.md
+++ b/ecc/README.md
@@ -6,43 +6,28 @@
 
 gen_eccdata.sh
   - A script to launch the newest conversion toolset to convert
-    input/eccdata.txt into output/eccdata, and invoke compatibility
-    verification tools of earlier versions of toolsets.
+    input/eccdata.txt into output/eccdata.
 
 input/eccdata.txt
   - A text file in ProtoBuf text format which contains all known ECC data.
-    This file shall be compatible with the last version of format toolset,
-    but may not be compatible with all earlier versions.
 
 output/eccdata
-  - The binary file generated from input files, with the last version of
-    format toolset. This file shall be compatible with all earlier versions.
+  - The binary file generated from input files.
 
 conversion_toolset_v*
-  - Contains format definitions, converting tools and verification tools of
-    one version of ECC data format.
+  - Contains format definitions and converting tools.
 
 proto
   - A symbolic link references to protobuf folder of the newest version of
     conversion toolsets. It's used in Android.mk.
 
-Conversion Toolset
-===================
-
-Every version of conversion toolset shall include at least:
-  - A script to generate eccdata from a known version of input/eccdata.txt.
-  - A script to verify compatibility of output/eccdata generated by any
-    newer version of format toolset.
-
-The output/eccdata generated by a version of conversion toolset shall be
-able to pass compatibility verifications of all previous versions of format
-toolset.
-
 Updating ECC database
 ===================
 Steps to update the ECC database:
 1. Edit input/eccdata.txt
 2. Source and launch
 3. Run gen_eccdata.sh
-The database file "output/eccdata" should be updated.
-
+4. Make TeleService
+5. Push TeleService.apk to system/priv-app/TeleService
+6. Reboot device
+7. run 'atest TeleServiceTests:IsoToEccProtobufRepositoryTest#testEccDataContent'
diff --git a/ecc/conversion_toolset_v1/env.sh b/ecc/conversion_toolset_v1/env.sh
index 23d9f10..534c807 100644
--- a/ecc/conversion_toolset_v1/env.sh
+++ b/ecc/conversion_toolset_v1/env.sh
@@ -20,19 +20,3 @@
 PROTOBUF_DIR="${LOCAL_TOOLSET_DIR}/proto"
 PROTOBUF_FILE="${PROTOBUF_DIR}/protobuf_ecc_data.proto"
 RAW_DATA="${INTERMEDIATE_DIR}/eccdata.raw"
-
-read -d "" PYTHON_COMMAND << END || :
-${ANDROID_BUILD_TOP}/prebuilts/python/${KERNEL}-x86/2.7.5/bin/python
-END
-PYTHONPATH="${PYTHONPATH}:${INTERMEDIATE_DIR}"
-PYTHONPATH="${PYTHONPATH}:${ANDROID_BUILD_TOP}/external/nanopb-c/generator/"
-
-if ! [ -x "${PYTHON_COMMAND}" ] ; then
-  echo "Missing ${PYTHON_COMMAND}." 1>&2
-  exit 1
-fi
-
-"${PROTOC_COMMAND}" \
-  --python_out="${INTERMEDIATE_DIR}" \
-  --proto_path="${PROTOBUF_DIR}" \
-  "${PROTOBUF_FILE}"
diff --git a/ecc/conversion_toolset_v1/gen_eccdata.sh b/ecc/conversion_toolset_v1/gen_eccdata.sh
index 8dd751f..1efac37 100644
--- a/ecc/conversion_toolset_v1/gen_eccdata.sh
+++ b/ecc/conversion_toolset_v1/gen_eccdata.sh
@@ -18,19 +18,12 @@
 LOCAL_TOOLSET_DIR="${ECC_ROOT}/conversion_toolset_v1"
 source "${LOCAL_TOOLSET_DIR}/env.sh"
 
+echo "Converting eccdata..."
 ${ANDROID_BUILD_TOP}/prebuilts/tools/linux-x86_64/protoc/bin/protoc \
   --encode=ecc.AllInfo proto/protobuf_ecc_data.proto \
   < "${INPUT_DATA}" > "${RAW_DATA}"
 
-echo
-echo "Starting strict verification"
-"${PYTHON_COMMAND}" -B \
-  "${LOCAL_TOOLSET_DIR}/verify_protobuf_compatibility.py" \
-  --input="${RAW_DATA}" --strict
-echo "Passed strict verification"
-
-echo
-echo "Compressing and encoding eccdata"
+echo "Compressing eccdata..."
 gzip -c < "${RAW_DATA}" > "${OUTPUT_DATA}"
-echo "Done"
 
+echo "Done"
diff --git a/ecc/conversion_toolset_v1/verify_eccdata_compatibility.sh b/ecc/conversion_toolset_v1/verify_eccdata_compatibility.sh
deleted file mode 100644
index 8686722..0000000
--- a/ecc/conversion_toolset_v1/verify_eccdata_compatibility.sh
+++ /dev/null
@@ -1,28 +0,0 @@
-#!/bin/bash
-set -o errexit
-
-# Copyright 2018 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.
-
-LOCAL_TOOLSET_DIR="${ECC_ROOT}/conversion_toolset_v1"
-source "${LOCAL_TOOLSET_DIR}/env.sh"
-
-echo "Starting compatibility verification v1"
-echo "Decoding and decompressing eccdata"
-gunzip -c < "${OUTPUT_DATA}" > "${RAW_DATA}"
-${PYTHON_COMMAND} -B \
-  "${LOCAL_TOOLSET_DIR}/verify_protobuf_compatibility.py" \
-  --input="${RAW_DATA}"
-echo "Passed compatibility verification v1"
-
diff --git a/ecc/conversion_toolset_v1/verify_protobuf_compatibility.py b/ecc/conversion_toolset_v1/verify_protobuf_compatibility.py
deleted file mode 100644
index bc707eb..0000000
--- a/ecc/conversion_toolset_v1/verify_protobuf_compatibility.py
+++ /dev/null
@@ -1,85 +0,0 @@
-#!/usr/bin/python -B
-
-# Copyright 2018 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.
-
-# Notice:
-# - verify_eccdata_strict.py: Verify data which is generated by this
-# version of this toolset.
-# - verify_eccdata_compatibility.py: Verify data which is generated by any
-# newer version of this tool set for ensuring backward compatibility.
-
-import sys
-import argparse
-import protobuf_ecc_data_pb2
-
-parser = argparse.ArgumentParser()
-parser.add_argument("--input", required=True)
-parser.add_argument("--strict", action="store_true")
-args = parser.parse_args()
-
-all_ecc_info = protobuf_ecc_data_pb2.AllInfo()
-
-with open(args.input, "rb") as ecc_data_source_file:
-  all_ecc_info.ParseFromString(ecc_data_source_file.read())
-
-if (args.strict):
-  print("Verify in strict mode")
-
-assert all_ecc_info.HasField("revision")
-assert all_ecc_info.revision > 0
-assert len(all_ecc_info.countries) > 0
-
-loaded_iso = []
-for country_info in all_ecc_info.countries:
-  assert country_info.HasField("iso_code")
-  assert len(country_info.iso_code) > 0
-  assert country_info.iso_code == country_info.iso_code.strip().upper()
-  assert country_info.iso_code not in loaded_iso
-  loaded_iso.append(country_info.iso_code)
-  assert country_info.HasField("ecc_fallback")
-  assert len(country_info.ecc_fallback) > 0
-
-  if len(country_info.eccs) > 0:
-    loaded_phone_number = []
-    for ecc_info in country_info.eccs:
-      assert ecc_info.HasField("phone_number")
-      phone_number = ecc_info.phone_number.strip()
-      assert len(phone_number) > 0
-      assert phone_number not in loaded_phone_number
-      loaded_phone_number.append(phone_number)
-
-      if (args.strict):
-        assert len(ecc_info.types) > 0
-        loaded_types = []
-        for ecc_type in ecc_info.types:
-          assert ecc_type == protobuf_ecc_data_pb2.EccInfo.POLICE or \
-              ecc_type == protobuf_ecc_data_pb2.EccInfo.AMBULANCE or \
-              ecc_type == protobuf_ecc_data_pb2.EccInfo.FIRE
-          assert ecc_type not in loaded_types
-          loaded_types.append(ecc_type)
-      else:
-        # For forward compatibility, ecc_info.types could be null if a phone
-        # number contains only new types which is not defined now. Just leave
-        # a warning message for this case.
-        if len(ecc_info.types) == 0:
-          print("WARNING: No recognizable type for " + \
-              country_info.iso_code + " - " + ecc_info.phone_number)
-        else:
-          loaded_types = []
-          for ecc_type in ecc_info.types:
-            assert ecc_type not in loaded_types
-            loaded_types.append(ecc_type)
-  elif (args.strict):
-    print("Warning: Empty ecc list for country " + country_info.iso_code)
diff --git a/ecc/gen_eccdata.sh b/ecc/gen_eccdata.sh
index 5cf52de..4f3a097 100755
--- a/ecc/gen_eccdata.sh
+++ b/ecc/gen_eccdata.sh
@@ -53,15 +53,8 @@
 source "${TOOLSET_DIR}/gen_eccdata.sh"
 echo
 
-# Check compatibility with every previous version
-rm -rf "${INTERMEDIATE_DIR}/*"
-source ${ECC_ROOT}/conversion_toolset_v1/verify_eccdata_compatibility.sh
-#rm -rf "${INTERMEDIATE_DIR}/*"
-#source ${ECC_ROOT}/conversion_toolset_v2/verify_eccdata_compatibility.sh
-#rm -rf "${INTERMEDIATE_DIR}/*"
-#source ${ECC_ROOT}/conversion_toolset_v3/verify_eccdata_compatibility.sh
-#...
-
-echo
-echo "Passed all compatibility verification!"
-
+echo "To verify data compatibility:"
+echo "  1. make TeleService"
+echo "  2. push TeleService.apk to system/priv-app/TeleService"
+echo "  3. reboot device"
+echo "  4. run 'atest TeleServiceTests:IsoToEccProtobufRepositoryTest#testEccDataContent'"
diff --git a/src/com/android/phone/ecc/IsoToEccProtobufRepository.java b/src/com/android/phone/ecc/IsoToEccProtobufRepository.java
index 0cd3108..7d9b4f0 100644
--- a/src/com/android/phone/ecc/IsoToEccProtobufRepository.java
+++ b/src/com/android/phone/ecc/IsoToEccProtobufRepository.java
@@ -25,6 +25,7 @@
 
 import androidx.annotation.NonNull;
 import androidx.annotation.Nullable;
+import androidx.annotation.VisibleForTesting;
 
 import java.io.BufferedInputStream;
 import java.io.IOException;
@@ -109,6 +110,11 @@
         }
     }
 
+    @VisibleForTesting(otherwise = VisibleForTesting.PRIVATE)
+    Map<String, CountryEccInfo> getEccTable() {
+        return mEccTable;
+    }
+
     private ProtobufEccData.AllInfo parseEccData(InputStream input) throws IOException {
         return ProtobufEccData.AllInfo.parseFrom(new GZIPInputStream(input));
     }
diff --git a/tests/src/com/android/phone/ecc/IsoToEccProtobufRepositoryTest.java b/tests/src/com/android/phone/ecc/IsoToEccProtobufRepositoryTest.java
new file mode 100644
index 0000000..f6e5ba2
--- /dev/null
+++ b/tests/src/com/android/phone/ecc/IsoToEccProtobufRepositoryTest.java
@@ -0,0 +1,81 @@
+/*
+ * Copyright (C) 2018 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.phone.ecc;
+
+import static com.google.common.truth.Truth.assertThat;
+
+import android.support.test.InstrumentationRegistry;
+import android.support.test.runner.AndroidJUnit4;
+import android.util.Log;
+
+import com.android.TelephonyTestBase;
+
+import org.junit.Test;
+import org.junit.runner.RunWith;
+
+import java.util.HashSet;
+import java.util.Map;
+
+/**
+ * Unit tests for IsoToEccProtobufRepository.
+ */
+
+@RunWith(AndroidJUnit4.class)
+public class IsoToEccProtobufRepositoryTest extends TelephonyTestBase {
+    private static final String LOG_TAG = "IsoToEccProtobufRepositoryTest";
+
+    @Test
+    public void testEccDataContent() {
+        IsoToEccProtobufRepository repository = IsoToEccProtobufRepository.getInstance();
+        repository.loadMappingTable(InstrumentationRegistry.getTargetContext());
+        Map<String, CountryEccInfo> eccTable = repository.getEccTable();
+        HashSet loadedIsos = new HashSet(300);
+        HashSet loadedNumbers = new HashSet(5);
+
+        assertThat(eccTable).isNotEmpty();
+        for (Map.Entry<String, CountryEccInfo> entry : eccTable.entrySet()) {
+            String countryIso = entry.getKey();
+            CountryEccInfo countryEccInfo = entry.getValue();
+            EccInfo[] eccInfoList = countryEccInfo.getEccInfoList();
+            if (eccInfoList.length > 0) {
+                Log.i(LOG_TAG, "Verifying country " + countryIso + " with "
+                        + eccInfoList.length + " ecc(s)");
+            } else {
+                Log.w(LOG_TAG, "Verifying country " + countryIso + " with no ecc");
+            }
+
+            assertThat(countryIso).isNotEmpty();
+            assertThat(countryIso).isEqualTo(countryIso.toUpperCase().trim());
+            assertThat(loadedIsos.contains(countryIso)).isFalse();
+            loadedIsos.add(countryIso);
+
+            assertThat(countryEccInfo.getFallbackEcc()).isNotEmpty();
+
+            if (eccInfoList.length != 0) {
+                loadedNumbers.clear();
+                for (EccInfo eccInfo : eccInfoList) {
+                    String eccNumber = eccInfo.getNumber();
+                    assertThat(eccNumber).isNotEmpty();
+                    assertThat(eccNumber).isEqualTo(eccNumber.trim());
+                    assertThat(eccInfo.getTypes()).isNotEmpty();
+                    assertThat(loadedNumbers.contains(eccNumber)).isFalse();
+                    loadedNumbers.add(eccNumber);
+                }
+            }
+        }
+    }
+}