1 /* 2 * Copyright (C) 2021 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 17 package android.ext.services.displayhash; 18 19 import static org.junit.Assert.assertFalse; 20 import static org.junit.Assert.assertNotNull; 21 import static org.junit.Assert.assertTrue; 22 import static org.junit.Assume.assumeTrue; 23 24 import android.graphics.Rect; 25 26 import androidx.core.os.BuildCompat; 27 28 import org.junit.Before; 29 import org.junit.Test; 30 31 public class HmacKeyManagerTest { 32 private final byte[] mSalt = "SALT".getBytes(); 33 private final long mTimestamp = 1000; 34 private final Rect mBounds = new Rect(0, 0, 100, 200); 35 private final int mHashAlgorithm = 1; 36 private final byte[] mImageHash = {0, 3, 4, 3, 5, 2, 5, 1}; 37 38 private final HmacKeyManager mHmacKeyManager = new HmacKeyManager(); 39 40 @Before setup()41 public void setup() { 42 // HmacKeyManager was only introduced in S. 43 assumeTrue(BuildCompat.isAtLeastS()); 44 } 45 46 @Test testGenerateAndVerifyHmac()47 public void testGenerateAndVerifyHmac() { 48 byte[] hmac = generateHmac(); 49 50 assertTrue(mHmacKeyManager.verifyHmac(mSalt, mTimestamp, mBounds, 51 mHashAlgorithm, mImageHash, hmac)); 52 } 53 54 @Test testVerifyHmac_differentSalt()55 public void testVerifyHmac_differentSalt() { 56 byte[] hmac = generateHmac(); 57 58 assertFalse( 59 mHmacKeyManager.verifyHmac("fake salt".getBytes(), mTimestamp, mBounds, 60 mHashAlgorithm, mImageHash, hmac)); 61 } 62 63 @Test testVerifyHmac_differentTimestamp()64 public void testVerifyHmac_differentTimestamp() { 65 byte[] hmac = generateHmac(); 66 67 assertFalse(mHmacKeyManager.verifyHmac(mSalt, 2000 /* timestamp */, mBounds, 68 mHashAlgorithm, mImageHash, hmac)); 69 } 70 71 @Test testVerifyHmac_differentBounds()72 public void testVerifyHmac_differentBounds() { 73 byte[] hmac = generateHmac(); 74 75 assertFalse( 76 mHmacKeyManager.verifyHmac(mSalt, mTimestamp, new Rect(10, 10, 110, 210), 77 mHashAlgorithm, mImageHash, hmac)); 78 } 79 80 @Test testVerifyHmac_differentHashAlgorithm()81 public void testVerifyHmac_differentHashAlgorithm() { 82 byte[] hmac = generateHmac(); 83 84 assertFalse(mHmacKeyManager.verifyHmac(mSalt, mTimestamp, mBounds, 85 2 /* hashAlgorithm */, mImageHash, hmac)); 86 } 87 88 @Test testVerifyHmac_differentImageHash()89 public void testVerifyHmac_differentImageHash() { 90 byte[] hmac = generateHmac(); 91 92 assertFalse(mHmacKeyManager.verifyHmac(mSalt, mTimestamp, mBounds, 93 mHashAlgorithm, new byte[]{0, 3, 4, 3, 5, 2, 5, 2}, hmac)); 94 } 95 96 @Test testVerifyHmac_differentHmac()97 public void testVerifyHmac_differentHmac() { 98 assertFalse(mHmacKeyManager.verifyHmac(mSalt, mTimestamp, mBounds, 99 mHashAlgorithm, mImageHash, new byte[32])); 100 } 101 102 @Test testVerifyHmac_invalidHmac()103 public void testVerifyHmac_invalidHmac() { 104 assertFalse(mHmacKeyManager.verifyHmac(mSalt, mTimestamp, mBounds, 105 mHashAlgorithm, mImageHash, null)); 106 107 assertFalse(mHmacKeyManager.verifyHmac(mSalt, mTimestamp, mBounds, 108 mHashAlgorithm, mImageHash, new byte[8])); 109 } 110 generateHmac()111 private byte[] generateHmac() { 112 byte[] hmac = mHmacKeyManager.generateHmac(mSalt, mTimestamp, mBounds, mHashAlgorithm, 113 mImageHash); 114 assertNotNull(hmac); 115 return hmac; 116 } 117 } 118