blob: c016e6104755658b27ea0354f76b32ebc701dbc8 [file] [log] [blame]
Makoto Onuki590096a2016-03-25 17:15:30 -07001/*
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 */
16package com.android.server.pm.backup;
17
18import android.content.pm.ApplicationInfo;
19import android.content.pm.PackageInfo;
20import android.content.pm.PackageParser.Package;
21import android.content.pm.Signature;
Amith Yamasani14c716c2018-03-05 20:39:04 +000022import android.test.AndroidTestCase;
Makoto Onuki590096a2016-03-25 17:15:30 -070023import android.test.MoreAsserts;
Amith Yamasani14c716c2018-03-05 20:39:04 +000024import android.test.suitebuilder.annotation.SmallTest;
Makoto Onuki590096a2016-03-25 17:15:30 -070025
26import com.android.server.backup.BackupUtils;
27
28import java.util.ArrayList;
29import java.util.Arrays;
30
31@SmallTest
Amith Yamasani14c716c2018-03-05 20:39:04 +000032public class BackupUtilsTest extends AndroidTestCase {
Makoto Onuki590096a2016-03-25 17:15:30 -070033
Amith Yamasani14c716c2018-03-05 20:39:04 +000034 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 Onuki590096a2016-03-25 17:15:30 -070040 }
41
Amith Yamasani14c716c2018-03-05 20:39:04 +000042 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 Onuki590096a2016-03-25 17:15:30 -070047
Amith Yamasani14c716c2018-03-05 20:39:04 +000048 return pi;
Makoto Onuki590096a2016-03-25 17:15:30 -070049 }
50
Amith Yamasani14c716c2018-03-05 20:39:04 +000051 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 Onuki590096a2016-03-25 17:15:30 -070056
Amith Yamasani14c716c2018-03-05 20:39:04 +000057 PackageInfo pi;
Makoto Onuki590096a2016-03-25 17:15:30 -070058
Amith Yamasani14c716c2018-03-05 20:39:04 +000059 // 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 Onuki590096a2016-03-25 17:15:30 -070083 }
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 Onuki590096a2016-03-25 17:15:30 -0700118}