1 /*
<lambda>null2  * Copyright 2022 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 package androidx.tracing.perfetto
17 
18 import androidx.tracing.perfetto.jni.PerfettoNative
19 import com.google.common.truth.Truth.assertThat
20 import java.io.File
21 import java.io.InputStream
22 import java.security.MessageDigest
23 import java.util.zip.ZipFile
24 import org.junit.Test
25 import org.junit.runner.RunWith
26 import org.junit.runners.JUnit4
27 
28 @RunWith(JUnit4::class)
29 class ChecksumTest {
30     @Test
31     fun test_prebuilts_checksums() {
32 
33         // get the AAR from resources
34 
35         val allFiles =
36             javaClass.classLoader!!
37                 .getResources("")
38                 .asSequence()
39                 .filter { it.protocol == "file" }
40                 .map { File(it.path) }
41                 .flatMap { curr ->
42                     if (curr.isFile) listOf(curr)
43                     else curr.walkTopDown().filter { it.isFile }.toList()
44                 }
45 
46         val fileNameRx = Regex("tracing-perfetto-binary-(\\d+.*)\\.aar")
47         val aarFile = allFiles.single { it.name.matches(fileNameRx) }
48 
49         // verify version
50 
51         val version: String? = fileNameRx.matchEntire(aarFile.name)?.groupValues?.get(1)
52         assertThat(version).isEqualTo(PerfettoNative.Metadata.version)
53 
54         // verify checksums
55 
56         val libName = "libtracing_perfetto.so"
57         val zipFile = ZipFile(aarFile)
58         val soFiles =
59             zipFile
60                 .entries()
61                 .asSequence()
62                 .filter { it.name.matches(Regex("jni/[^/]+/$libName")) }
63                 .toList()
64 
65         assertThat(soFiles.size).isEqualTo(PerfettoNative.Metadata.checksums.size)
66 
67         soFiles.forEach { entry ->
68             val (_, arch, _) = entry.name.split("/")
69             val actual = calcSha(zipFile.getInputStream(entry))
70             val expected = PerfettoNative.Metadata.checksums[arch]
71             assertThat(actual).isEqualTo(expected)
72         }
73     }
74 
75     private fun calcSha(input: InputStream): String =
76         MessageDigest.getInstance("SHA-256")
77             .also { it.update(input.readBytes()) }
78             .digest()
79             .joinToString(separator = "") { "%02x".format(it) }
80             .also { assertThat(it).matches("[0-9a-f]{64}") }
81 }
82