1 /* 2 * Copyright (C) 2024 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.appsearch.stats; 18 19 import static com.google.common.truth.Truth.assertThat; 20 21 import android.annotation.NonNull; 22 23 import com.android.server.appsearch.util.StatsUtil; 24 25 import org.junit.Test; 26 27 import java.io.UnsupportedEncodingException; 28 import java.math.BigInteger; 29 import java.nio.charset.StandardCharsets; 30 import java.security.MessageDigest; 31 import java.security.NoSuchAlgorithmException; 32 33 /** 34 * Tests covering the functionalities in {@link StatsUtil} NOT requiring overriding any flags in 35 * {@link android.provider.DeviceConfig}. 36 * 37 * <p>To add tests rely on overriding the flags, please add them in the tests for {@link 38 * PlatformLogger} in mockingservicestests. 39 */ 40 public class StatsUtilTest { 41 42 @Test testCalculateHashCode_MD5_int32_shortString()43 public void testCalculateHashCode_MD5_int32_shortString() 44 throws NoSuchAlgorithmException, UnsupportedEncodingException { 45 final String str1 = "d1"; 46 final String str2 = "d2"; 47 48 int hashCodeForStr1 = StatsUtil.calculateHashCodeMd5(str1); 49 50 // hashing should be stable 51 assertThat(hashCodeForStr1).isEqualTo(StatsUtil.calculateHashCodeMd5(str1)); 52 assertThat(hashCodeForStr1).isNotEqualTo(StatsUtil.calculateHashCodeMd5(str2)); 53 } 54 55 @Test testGetCalculateCode_MD5_int32_mediumString()56 public void testGetCalculateCode_MD5_int32_mediumString() 57 throws NoSuchAlgorithmException, UnsupportedEncodingException { 58 final String str1 = "Siblings"; 59 final String str2 = "Teheran"; 60 61 int hashCodeForStr1 = StatsUtil.calculateHashCodeMd5(str1); 62 63 // hashing should be stable 64 assertThat(hashCodeForStr1).isEqualTo(StatsUtil.calculateHashCodeMd5(str1)); 65 assertThat(hashCodeForStr1).isNotEqualTo(StatsUtil.calculateHashCodeMd5(str2)); 66 } 67 68 @Test testCalculateHashCode_MD5_int32_longString()69 public void testCalculateHashCode_MD5_int32_longString() 70 throws NoSuchAlgorithmException, UnsupportedEncodingException { 71 final String str1 = "abcdefghijkl-mnopqrstuvwxyz"; 72 final String str2 = "abcdefghijkl-mnopqrstuvwxy123"; 73 74 int hashCodeForStr1 = StatsUtil.calculateHashCodeMd5(str1); 75 76 // hashing should be stable 77 assertThat(hashCodeForStr1).isEqualTo(StatsUtil.calculateHashCodeMd5(str1)); 78 assertThat(hashCodeForStr1).isNotEqualTo(StatsUtil.calculateHashCodeMd5(str2)); 79 } 80 81 @Test testCalculateHashCode_MD5_int32_sameAsBigInteger_intValue()82 public void testCalculateHashCode_MD5_int32_sameAsBigInteger_intValue() 83 throws NoSuchAlgorithmException, UnsupportedEncodingException { 84 final String emptyStr = ""; 85 final String shortStr = "a"; 86 final String mediumStr = "Teheran"; 87 final String longStr = "abcd-efgh-ijkl-mnop-qrst-uvwx-yz"; 88 89 int emptyHashCode = StatsUtil.calculateHashCodeMd5(emptyStr); 90 int shortHashCode = StatsUtil.calculateHashCodeMd5(shortStr); 91 int mediumHashCode = StatsUtil.calculateHashCodeMd5(mediumStr); 92 int longHashCode = StatsUtil.calculateHashCodeMd5(longStr); 93 94 assertThat(emptyHashCode).isEqualTo(calculateHashCodeMd5withBigInteger(emptyStr)); 95 assertThat(shortHashCode).isEqualTo(calculateHashCodeMd5withBigInteger(shortStr)); 96 assertThat(mediumHashCode).isEqualTo(calculateHashCodeMd5withBigInteger(mediumStr)); 97 assertThat(longHashCode).isEqualTo(calculateHashCodeMd5withBigInteger(longStr)); 98 } 99 100 @Test testCalculateHashCode_MD5_strIsNull()101 public void testCalculateHashCode_MD5_strIsNull() 102 throws NoSuchAlgorithmException, UnsupportedEncodingException { 103 assertThat(StatsUtil.calculateHashCodeMd5(/* str= */ null)).isEqualTo(-1); 104 } 105 calculateHashCodeMd5withBigInteger(@onNull String str)106 private static int calculateHashCodeMd5withBigInteger(@NonNull String str) 107 throws NoSuchAlgorithmException { 108 MessageDigest md = MessageDigest.getInstance("MD5"); 109 md.update(str.getBytes(StandardCharsets.UTF_8)); 110 byte[] digest = md.digest(); 111 return new BigInteger(digest).intValue(); 112 } 113 } 114