Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 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 | package com.android.server.pm.backup; |
| 17 | |
| 18 | import android.content.pm.ApplicationInfo; |
| 19 | import android.content.pm.PackageInfo; |
| 20 | import android.content.pm.PackageParser.Package; |
| 21 | import android.content.pm.Signature; |
Amith Yamasani | 14c716c | 2018-03-05 20:39:04 +0000 | [diff] [blame^] | 22 | import android.test.AndroidTestCase; |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 23 | import android.test.MoreAsserts; |
Amith Yamasani | 14c716c | 2018-03-05 20:39:04 +0000 | [diff] [blame^] | 24 | import android.test.suitebuilder.annotation.SmallTest; |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 25 | |
| 26 | import com.android.server.backup.BackupUtils; |
| 27 | |
| 28 | import java.util.ArrayList; |
| 29 | import java.util.Arrays; |
| 30 | |
| 31 | @SmallTest |
Amith Yamasani | 14c716c | 2018-03-05 20:39:04 +0000 | [diff] [blame^] | 32 | public class BackupUtilsTest extends AndroidTestCase { |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 33 | |
Amith Yamasani | 14c716c | 2018-03-05 20:39:04 +0000 | [diff] [blame^] | 34 | private Signature[] genSignatures(String... signatures) { |
| 35 | final Signature[] sigs = new Signature[signatures.length]; |
| 36 | for (int i = 0; i < signatures.length; i++){ |
| 37 | sigs[i] = new Signature(signatures[i].getBytes()); |
| 38 | } |
| 39 | return sigs; |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 40 | } |
| 41 | |
Amith Yamasani | 14c716c | 2018-03-05 20:39:04 +0000 | [diff] [blame^] | 42 | private PackageInfo genPackage(String... signatures) { |
| 43 | final PackageInfo pi = new PackageInfo(); |
| 44 | pi.packageName = "package"; |
| 45 | pi.applicationInfo = new ApplicationInfo(); |
| 46 | pi.signatures = genSignatures(signatures); |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 47 | |
Amith Yamasani | 14c716c | 2018-03-05 20:39:04 +0000 | [diff] [blame^] | 48 | return pi; |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Amith Yamasani | 14c716c | 2018-03-05 20:39:04 +0000 | [diff] [blame^] | 51 | public void testSignaturesMatch() { |
| 52 | final ArrayList<byte[]> stored1 = BackupUtils.hashSignatureArray(Arrays.asList( |
| 53 | "abc".getBytes())); |
| 54 | final ArrayList<byte[]> stored2 = BackupUtils.hashSignatureArray(Arrays.asList( |
| 55 | "abc".getBytes(), "def".getBytes())); |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 56 | |
Amith Yamasani | 14c716c | 2018-03-05 20:39:04 +0000 | [diff] [blame^] | 57 | PackageInfo pi; |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 58 | |
Amith Yamasani | 14c716c | 2018-03-05 20:39:04 +0000 | [diff] [blame^] | 59 | // False for null package. |
| 60 | assertFalse(BackupUtils.signaturesMatch(stored1, null)); |
| 61 | |
| 62 | // If it's a system app, signatures don't matter. |
| 63 | pi = genPackage("xyz"); |
| 64 | pi.applicationInfo.flags |= ApplicationInfo.FLAG_SYSTEM; |
| 65 | assertTrue(BackupUtils.signaturesMatch(stored1, pi)); |
| 66 | |
| 67 | // Non system apps. |
| 68 | assertTrue(BackupUtils.signaturesMatch(stored1, genPackage("abc"))); |
| 69 | |
| 70 | // Superset is okay. |
| 71 | assertTrue(BackupUtils.signaturesMatch(stored1, genPackage("abc", "xyz"))); |
| 72 | assertTrue(BackupUtils.signaturesMatch(stored1, genPackage("xyz", "abc"))); |
| 73 | |
| 74 | assertFalse(BackupUtils.signaturesMatch(stored1, genPackage("xyz"))); |
| 75 | assertFalse(BackupUtils.signaturesMatch(stored1, genPackage("xyz", "def"))); |
| 76 | |
| 77 | assertTrue(BackupUtils.signaturesMatch(stored2, genPackage("def", "abc"))); |
| 78 | assertTrue(BackupUtils.signaturesMatch(stored2, genPackage("x", "def", "abc", "y"))); |
| 79 | |
| 80 | // Subset is not okay. |
| 81 | assertFalse(BackupUtils.signaturesMatch(stored2, genPackage("abc"))); |
| 82 | assertFalse(BackupUtils.signaturesMatch(stored2, genPackage("def"))); |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 83 | } |
| 84 | |
| 85 | public void testHashSignature() { |
| 86 | final byte[] sig1 = "abc".getBytes(); |
| 87 | final byte[] sig2 = "def".getBytes(); |
| 88 | |
| 89 | final byte[] hash1a = BackupUtils.hashSignature(sig1); |
| 90 | final byte[] hash1b = BackupUtils.hashSignature(new Signature(sig1)); |
| 91 | |
| 92 | final byte[] hash2a = BackupUtils.hashSignature(sig2); |
| 93 | final byte[] hash2b = BackupUtils.hashSignature(new Signature(sig2)); |
| 94 | |
| 95 | assertEquals(32, hash1a.length); |
| 96 | MoreAsserts.assertEquals(hash1a, hash1b); |
| 97 | |
| 98 | assertEquals(32, hash2a.length); |
| 99 | MoreAsserts.assertEquals(hash2a, hash2b); |
| 100 | |
| 101 | assertFalse(Arrays.equals(hash1a, hash2a)); |
| 102 | |
| 103 | final ArrayList<byte[]> listA = BackupUtils.hashSignatureArray(Arrays.asList( |
| 104 | "abc".getBytes(), "def".getBytes())); |
| 105 | |
| 106 | final ArrayList<byte[]> listB = BackupUtils.hashSignatureArray(new Signature[]{ |
| 107 | new Signature("abc".getBytes()), new Signature("def".getBytes())}); |
| 108 | |
| 109 | assertEquals(2, listA.size()); |
| 110 | assertEquals(2, listB.size()); |
| 111 | |
| 112 | MoreAsserts.assertEquals(hash1a, listA.get(0)); |
| 113 | MoreAsserts.assertEquals(hash1a, listB.get(0)); |
| 114 | |
| 115 | MoreAsserts.assertEquals(hash2a, listA.get(1)); |
| 116 | MoreAsserts.assertEquals(hash2a, listB.get(1)); |
| 117 | } |
Makoto Onuki | 590096a | 2016-03-25 17:15:30 -0700 | [diff] [blame] | 118 | } |