Skip to content

Commit d2c7019

Browse files
committed
fingerprint extraction test
1 parent d17fc57 commit d2c7019

File tree

11 files changed

+180
-99
lines changed

11 files changed

+180
-99
lines changed

cmd_output.txt

Lines changed: 0 additions & 51 deletions
This file was deleted.

src/main/kotlin/models/Fingerprint.kt

Lines changed: 91 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,91 @@
1+
package models
2+
3+
import utils.PrintUtils
4+
import utils.runCommand
5+
import utils.signingReportTask
6+
7+
private class Fingerprint {
8+
9+
companion object {
10+
private var key: String? = null
11+
private var until: String? = null
12+
private var error: String? = null
13+
private var variantLocated = false
14+
private var moduleLocated = false
15+
}
16+
17+
fun extract(module: String, variant: String, debug: Boolean, trace: String): String {
18+
val lines = trace.split("\n")
19+
lines.forEach { line ->
20+
when {
21+
line.toLowerCase().contains("downloading") -> if (debug) {
22+
PrintUtils.print(module, line, debug)
23+
}
24+
line.toLowerCase().contains("unzipping") -> if (debug) {
25+
PrintUtils.print(module, line, debug)
26+
}
27+
line.toLowerCase().contains("permissions") -> if (debug) {
28+
PrintUtils.print(module, line, debug)
29+
}
30+
line.toLowerCase().contains("config:") && moduleLocated && variantLocated -> {
31+
val k = line.split(": ")[1].trim()
32+
val valid = !k.equals("none", ignoreCase = true)
33+
if (!valid) {
34+
key = k
35+
PrintUtils.print(module, "\uD83E\uDD2F no config defined for variant $variant", true)
36+
if (debug) {
37+
until = key
38+
}
39+
} else if (debug) {
40+
PrintUtils.print(module, "Module: $module", true)
41+
PrintUtils.print(module, "Variant: $variant", true)
42+
}
43+
44+
}
45+
line.toLowerCase().contains("sha1") && moduleLocated && variantLocated -> {
46+
key = line.split(" ")[1]
47+
if (debug) {
48+
PrintUtils.print(module, line, debug)
49+
}
50+
}
51+
line.toLowerCase().contains("error") -> {
52+
error = line.split(": ")[1]
53+
}
54+
line.toLowerCase().contains("valid until") && moduleLocated && variantLocated -> {
55+
until = line.split(": ")[1]
56+
if (debug) {
57+
PrintUtils.print(module, line, debug)
58+
}
59+
}
60+
line.toLowerCase().contains("store") && moduleLocated && variantLocated -> if (debug) {
61+
PrintUtils.print(module, line, debug)
62+
}
63+
line.toLowerCase().contains("variant") && moduleLocated -> {
64+
val locV = line.split(" ")[1]
65+
if (locV == variant) {
66+
variantLocated = true
67+
}
68+
}
69+
line.toLowerCase().contains(":$module") -> moduleLocated = true
70+
}
71+
if (key != null && (!debug || debug && until != null)) {
72+
return key!!
73+
}
74+
}
75+
return ""
76+
}
77+
}
78+
79+
/**
80+
* Gets the signing report trace and extracts the fingerprint
81+
*/
82+
fun fingerPrint(module: String, variant: String, debug: Boolean): String {
83+
return signingReportTask().runCommand().extractFingerprint(module, variant, debug)
84+
}
85+
86+
/**
87+
* Returns the SHA1 fingerprint for the given trace
88+
*/
89+
fun String.extractFingerprint(module: String = "app", variant: String = "debug", debug: Boolean = false): String {
90+
return Fingerprint().extract(module, variant, debug, this)
91+
}

src/main/kotlin/models/Os.kt

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
package models
2+
3+
enum class Os {
4+
WINDOWS,
5+
OSX
6+
}
7+
8+
fun getOs(): Os = when {
9+
System.getProperty("os.name").toLowerCase().contains("windows") -> Os.WINDOWS
10+
else -> Os.OSX
11+
}

src/main/kotlin/utils/Execution.kt

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,9 @@ import org.gradle.api.NamedDomainObjectContainer
77
import org.gradle.api.Project
88
import org.slf4j.Logger
99
import org.slf4j.LoggerFactory
10-
import java.io.BufferedReader
1110
import java.io.IOException
12-
import java.io.InputStreamReader
1311

14-
fun Project.absolutePath(): String = this.file(wrapper).absolutePath.replace(wrapper, emptyChar)
12+
fun Project.absolutePath(): String = this.file(wrapperOsX).absolutePath.replace(wrapperOsX, emptyChar)
1513
fun Project.createExtension(): Extension = this.extensions.create(extensionName, Extension::class.java)
1614
fun Project.createConfiguration(): NamedDomainObjectContainer<Configuration> =
1715
this.container<Configuration>(Configuration::class.java)
@@ -40,8 +38,10 @@ fun <R : Any> R.logger(): Lazy<Logger> {
4038
return lazy { LoggerFactory.getLogger(this.javaClass) }
4139
}
4240

43-
fun String.run(runner: (command: String, result: String) -> Unit) {
44-
runner(this, execute(this))
41+
fun String.runCommand(runner: (command: String, result: String) -> Unit = { _, _ -> }): String {
42+
val result = execute(this)
43+
runner(this, result)
44+
return result
4545
}
4646

4747
fun String.escape(): String = Regex.escape(this)

src/main/kotlin/utils/PrintUtils.kt

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
package utils
2+
3+
import StringCare
4+
import org.slf4j.LoggerFactory
5+
6+
class PrintUtils {
7+
8+
companion object {
9+
private var variant: String? = null
10+
private var module: String? = null
11+
private val logger = LoggerFactory.getLogger(StringCare::class.java)
12+
13+
fun init(module: String, variant: String) {
14+
PrintUtils.module = module
15+
PrintUtils.variant = variant
16+
}
17+
18+
private fun _print(value: String) {
19+
logger.info(value)
20+
}
21+
22+
fun print(message: String, tab: Boolean = false) {
23+
if (variant != null && module != null) {
24+
if (!tab) {
25+
_print(":$module:$message")
26+
} else {
27+
_print("\t" + message)
28+
}
29+
} else {
30+
_print(message)
31+
}
32+
}
33+
34+
fun print(module: String?, message: String, tab: Boolean = false) {
35+
if (module != null) {
36+
if (!tab) {
37+
_print(":$module:$message")
38+
} else {
39+
_print("\t" + message)
40+
}
41+
} else {
42+
_print(message)
43+
}
44+
}
45+
46+
fun uncapitalize(value: String): String {
47+
return value.substring(0, 1).toLowerCase() + value.substring(1, value.length)
48+
}
49+
}
50+
51+
}

src/main/kotlin/utils/Runner.kt

Lines changed: 0 additions & 4 deletions
This file was deleted.

src/main/kotlin/utils/Tasks.kt

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
package utils
2+
3+
import models.Os
4+
import models.getOs
5+
6+
internal fun signingReportTask(): String = "${when (getOs()) {
7+
Os.WINDOWS -> wrapperOsX
8+
Os.OSX -> wrapperWindows
9+
}} signingReport"

src/main/kotlin/utils/Vars.kt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
package utils
22

33
internal const val extensionName = "stringcare"
4-
internal const val wrapper = "gradlew"
4+
internal const val wrapperOsX = "./gradlew"
5+
internal const val wrapperWindows = "gradlew.bat"
56
internal const val emptyChar = ""

src/test/kotlin/SCTest.kt

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import models.extractFingerprint
12
import org.junit.Test
23
import utils.*
34

@@ -11,23 +12,29 @@ class SCTest {
1112
git clone https://github.com/StringCare/KotlinSample.git &&
1213
cd KotlinSample &&
1314
echo "sdk.dir=/Users/efrainespada/Library/Android/sdk" > local.properties &&
14-
./gradlew build &&
1515
./gradlew signingReport
1616
""".trimIndent()
1717

1818
@Test
1919
fun `terminal verification`() {
20-
"echo $extensionName".run { command, result ->
20+
"echo $extensionName".runCommand { command, result ->
2121
assert(command.contains(result.removeNewLines()))
2222
}
2323
}
2424

25-
2625
@Test
2726
fun `gradlew signingReport`() {
28-
signingReportTask.run { _, report ->
27+
signingReportTask.runCommand { _, report ->
2928
assert(report.contains("SHA1") && report.contains("BUILD SUCCESSFUL"))
3029
}
3130
}
3231

32+
@Test
33+
fun `fingerprint extraction`() {
34+
signingReportTask.runCommand { _, report ->
35+
val key = report.extractFingerprint()
36+
assert(key.split(":").size == 20)
37+
}
38+
}
39+
3340
}

src/test/kotlin/StreamGobbler.kt

Lines changed: 0 additions & 11 deletions
This file was deleted.

0 commit comments

Comments
 (0)