1 // Copyright 2022 The Chromium Authors 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 package org.chromium.base; 6 7 import static org.hamcrest.CoreMatchers.anyOf; 8 import static org.hamcrest.CoreMatchers.is; 9 import static org.junit.Assert.assertThat; 10 11 import static org.chromium.base.PackageUtils.byteArrayToHexString; 12 import static org.chromium.base.PackageUtils.getCertificateSHA256FingerprintForPackage; 13 14 import android.content.pm.PackageManager; 15 16 import androidx.test.filters.SmallTest; 17 18 import org.junit.Assert; 19 import org.junit.Test; 20 import org.junit.runner.RunWith; 21 22 import org.chromium.base.test.BaseJUnit4ClassRunner; 23 import org.chromium.base.test.util.Batch; 24 25 import java.util.Collections; 26 import java.util.List; 27 28 /** Tests for {@link PackageUtils}. */ 29 @RunWith(BaseJUnit4ClassRunner.class) 30 @Batch(Batch.UNIT_TESTS) 31 public class PackageUtilsTest { 32 private static final byte[] BYTE_ARRAY = 33 new byte[] { 34 (byte) 0xaa, 35 (byte) 0xbb, 36 (byte) 0xcc, 37 (byte) 0x10, 38 (byte) 0x20, 39 (byte) 0x30, 40 (byte) 0x01, 41 (byte) 0x02 42 }; 43 private static final String STRING_ARRAY = "AA:BB:CC:10:20:30:01:02"; 44 45 private static final String SHA_256_FINGERPRINT_PUBLIC = 46 "32:A2:FC:74:D7:31:10:58:59:E5:A8:5D:F1:6D:95:F1:02:D8:5B" 47 + ":22:09:9B:80:64:C5:D8:91:5C:61:DA:D1:E0"; 48 private static final String SHA_256_FINGERPRINT_OFFICIAL = 49 "19:75:B2:F1:71:77:BC:89:A5:DF:F3:1F:9E:64:A6:CA:E2:81:A5" 50 + ":3D:C1:D1:D5:9B:1D:14:7F:E1:C8:2A:FA:00"; 51 private static final String PACKAGE_NAME = 52 ContextUtils.getApplicationContext().getPackageName(); 53 54 @Test 55 @SmallTest testByteArrayToHexString()56 public void testByteArrayToHexString() { 57 Assert.assertEquals(STRING_ARRAY, byteArrayToHexString(BYTE_ARRAY)); 58 } 59 60 @Test 61 @SmallTest testSHA256CertificateChecks()62 public void testSHA256CertificateChecks() { 63 PackageManager pm = ContextUtils.getApplicationContext().getPackageManager(); 64 List<String> fingerprints = getCertificateSHA256FingerprintForPackage(PACKAGE_NAME); 65 66 assertThat( 67 fingerprints, 68 anyOf( 69 is(Collections.singletonList(SHA_256_FINGERPRINT_PUBLIC)), 70 is(Collections.singletonList(SHA_256_FINGERPRINT_OFFICIAL)))); 71 } 72 } 73