1 /*
2  * Copyright (C) 2020 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 com.android.server.biometrics;
18 
19 import static junit.framework.Assert.assertEquals;
20 
21 import android.hardware.keymaster.HardwareAuthToken;
22 import android.hardware.keymaster.Timestamp;
23 import android.platform.test.annotations.Presubmit;
24 
25 import androidx.test.filters.SmallTest;
26 
27 import org.junit.Test;
28 
29 @Presubmit
30 @SmallTest
31 public class HardwareAuthTokenUtilsTest {
32 
33     @Test
testByteArrayLoopBack()34     public void testByteArrayLoopBack() {
35         final byte[] hat = new byte[69];
36         for (int i = 0; i < 69; i++) {
37             hat[i] = (byte) i;
38         }
39 
40         final HardwareAuthToken hardwareAuthToken = HardwareAuthTokenUtils.toHardwareAuthToken(hat);
41         final byte[] hat2 = HardwareAuthTokenUtils.toByteArray(hardwareAuthToken);
42 
43         for (int i = 0; i < hat.length; i++) {
44             assertEquals(hat[i], hat2[i]);
45         }
46     }
47 
48     @Test
testHardwareAuthTokenLoopBack()49     public void testHardwareAuthTokenLoopBack() {
50         final long testChallenge = 1000L;
51         final long testUserId = 2000L;
52         final long testAuthenticatorId = 3000L;
53         final int testAuthenticatorType = 4000;
54         final long testTimestamp = 5000L;
55 
56         final HardwareAuthToken hardwareAuthToken = new HardwareAuthToken();
57         hardwareAuthToken.challenge = testChallenge;
58         hardwareAuthToken.userId = testUserId;
59         hardwareAuthToken.authenticatorId = testAuthenticatorId;
60         hardwareAuthToken.authenticatorType = testAuthenticatorType;
61         hardwareAuthToken.timestamp = new Timestamp();
62         hardwareAuthToken.timestamp.milliSeconds = testTimestamp;
63         hardwareAuthToken.mac = new byte[32];
64 
65         for (int i = 0; i < hardwareAuthToken.mac.length; i++) {
66             hardwareAuthToken.mac[i] = (byte) i;
67         }
68 
69         final byte[] hat = HardwareAuthTokenUtils.toByteArray(hardwareAuthToken);
70         final HardwareAuthToken hardwareAuthToken2 =
71                 HardwareAuthTokenUtils.toHardwareAuthToken(hat);
72 
73         assertEquals(testChallenge, hardwareAuthToken2.challenge);
74         assertEquals(testUserId, hardwareAuthToken2.userId);
75         assertEquals(testAuthenticatorId, hardwareAuthToken2.authenticatorId);
76         assertEquals(testAuthenticatorType, hardwareAuthToken2.authenticatorType);
77         assertEquals(testTimestamp, hardwareAuthToken2.timestamp.milliSeconds);
78 
79         for (int i = 0; i < hardwareAuthToken.mac.length; i++) {
80             assertEquals(hardwareAuthToken.mac[i], hardwareAuthToken2.mac[i]);
81         }
82     }
83 }
84