From 67e0ca7b632699f54aec27069efd4e0c547ab708 Mon Sep 17 00:00:00 2001
From: Maria Glukhova <siamezzze@gmail.com>
Date: Fri, 13 Jan 2017 23:22:10 +0200
Subject: [PATCH] Add some tests for APK comarator.

---
 tests/comparators/test_apk.py         |  66 ++++++++++++++++++++++++++++++++++
 tests/data/apk_manifest_expected_diff |  11 ++++++
 tests/data/apk_zipinfo_expected_diff  |   6 ++++
 tests/data/test1.apk                  | Bin 0 -> 866 bytes
 tests/data/test2.apk                  | Bin 0 -> 864 bytes
 5 files changed, 83 insertions(+)
 create mode 100644 tests/comparators/test_apk.py
 create mode 100644 tests/data/apk_manifest_expected_diff
 create mode 100644 tests/data/apk_zipinfo_expected_diff
 create mode 100644 tests/data/test1.apk
 create mode 100644 tests/data/test2.apk

diff --git a/tests/comparators/test_apk.py b/tests/comparators/test_apk.py
new file mode 100644
index 0000000..3093027
--- /dev/null
+++ b/tests/comparators/test_apk.py
@@ -0,0 +1,66 @@
+# -*- coding: utf-8 -*-
+#
+# diffoscope: in-depth comparison of files, archives, and directories
+#
+# Copyright �� 2017 Maria Glukhova <siammezzze@gmail.com>
+#
+# diffoscope is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License as published by
+# the Free Software Foundation, either version 3 of the License, or
+# (at your option) any later version.
+#
+# diffoscope is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with diffoscope.  If not, see <https://www.gnu.org/licenses/>.
+
+import pytest
+
+from diffoscope.comparators.apk import ApkFile
+
+from utils import skip_unless_tools_exist, data, load_fixture, \
+    assert_non_existing
+
+apk1 = load_fixture(data('test1.apk'))
+apk2 = load_fixture(data('test2.apk'))
+
+def test_identification(apk1):
+    assert isinstance(apk1, ApkFile)
+
+def test_no_differences(apk1):
+    difference = apk1.compare(apk1)
+    assert difference is None
+
+@pytest.fixture
+def differences(apk1, apk2):
+    return apk1.compare(apk2).details
+
+@skip_unless_tools_exist('apktool', 'zipinfo')
+def test_compare_non_existing(monkeypatch, apk1):
+    assert_non_existing(monkeypatch, apk1)
+
+@skip_unless_tools_exist('apktool', 'zipinfo')
+def test_zipinfo(differences):
+    assert differences[0].source1 == 'zipinfo {}'
+    assert differences[0].source2 == 'zipinfo {}'
+    expected_diff = open(data('apk_zipinfo_expected_diff')).read()
+    assert differences[0].unified_diff == expected_diff
+
+@skip_unless_tools_exist('apktool', 'zipinfo')
+def test_android_manifest(differences):
+    manifest_diff = next((d for d in differences
+                          if d.source1 == 'AndroidManifest.xml'), None)
+    assert manifest_diff is not None
+    assert manifest_diff.source2 == 'AndroidManifest.xml'
+    expected_diff = open(data('apk_manifest_expected_diff')).read()
+    assert manifest_diff.unified_diff == expected_diff
+
+@skip_unless_tools_exist('apktool', 'zipinfo')
+def test_apk_metadata_source(differences):
+    metadata_diff = next((d for d in differences
+                          if d.source1 == 'APK metadata'), None)
+    assert metadata_diff is not None
+    assert metadata_diff.source2 == 'APK metadata'
diff --git a/tests/data/apk_manifest_expected_diff b/tests/data/apk_manifest_expected_diff
new file mode 100644
index 0000000..9d37a92
--- /dev/null
+++ b/tests/data/apk_manifest_expected_diff
@@ -0,0 +1,11 @@
+@@ -1,9 +1,9 @@
+ <?xml version="1.0" encoding="utf-8"?>
+-<manifest android:versionCode="1" android:versionName="1.0" package="com.any.any" platformBuildVersionCode="24" platformBuildVersionName="7.0.0"
++<manifest android:versionCode="2" android:versionName="2.0" package="com.any.any" platformBuildVersionCode="24" platformBuildVersionName="7.0.0"
+   xmlns:android="http://schemas.android.com/apk/res/android">
+     <uses-permission android:name="android.permission.INTERNET" />
+     <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
+     <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
+     <application>
+         <activity android:name="com.any.any.SomeActivity">
+             <intent-filter>
diff --git a/tests/data/apk_zipinfo_expected_diff b/tests/data/apk_zipinfo_expected_diff
new file mode 100644
index 0000000..d1810dc
--- /dev/null
+++ b/tests/data/apk_zipinfo_expected_diff
@@ -0,0 +1,6 @@
+@@ -1,3 +1,3 @@
+-Zip file size: 866 bytes, number of entries: 1
++Zip file size: 864 bytes, number of entries: 1
+ -rw----     2.0 fat     2096 bl defN 80-Jan-01 02:00 AndroidManifest.xml
+-1 file, 2096 bytes uncompressed, 714 bytes compressed:  65.9%
++1 file, 2096 bytes uncompressed, 712 bytes compressed:  66.0%
diff --git a/tests/data/test1.apk b/tests/data/test1.apk
new file mode 100644
index 0000000000000000000000000000000000000000..45876d61fab82fc0c492af2cf60e56a3792f820f
GIT binary patch
diff --git a/tests/data/test2.apk b/tests/data/test2.apk
new file mode 100644
index 0000000000000000000000000000000000000000..925fb42d60056f56ae76d8ee1cd4f16c457e8d89
GIT binary patch
-- 
2.11.0

