diff --git a/build.gradle b/build.gradle index 89e9d64..cbc1dc4 100644 --- a/build.gradle +++ b/build.gradle @@ -6,7 +6,7 @@ plugins { } group 'com.stringcare' -version '3.3' +version '3.4' def siteUrl = 'https://github.com/StringCare/KotlinGradlePlugin' def gitUrl = 'https://github.com/StringCare/KotlinGradlePlugin.git' diff --git a/src/main/kotlin/StringCare.kt b/src/main/kotlin/StringCare.kt index c9f1cf5..291118a 100644 --- a/src/main/kotlin/StringCare.kt +++ b/src/main/kotlin/StringCare.kt @@ -43,32 +43,25 @@ open class StringCare : Plugin { this.project.afterEvaluate { extension.modules.forEach { module -> - when { - module.stringFiles.isNotEmpty() && module.srcFolders.isNotEmpty() -> { - moduleMap[module.name] = Configuration(module.name).apply { - stringFiles.addAll(module.stringFiles) - srcFolders.addAll(module.srcFolders) - debug = extension.debug - } - } - module.srcFolders.isNotEmpty() -> { - moduleMap[module.name] = Configuration(module.name).apply { - stringFiles.addAll(defaultConfig().stringFiles) - srcFolders.addAll(module.srcFolders) - debug = extension.debug - } - } - module.stringFiles.isNotEmpty() -> { - moduleMap[module.name] = Configuration(module.name).apply { - stringFiles.addAll(module.stringFiles) - srcFolders.addAll(defaultConfig().srcFolders) - debug = extension.debug - } - } + moduleMap[module.name] = Configuration(module.name).apply { + debug = extension.debug + } + if (module.srcFolders.isNotEmpty()) { + moduleMap[module.name]!!.srcFolders.addAll(module.srcFolders) + } + if (module.stringFiles.isNotEmpty()) { + moduleMap[module.name]!!.stringFiles.addAll(module.stringFiles) + } + if (module.assetsFiles.isNotEmpty()) { + moduleMap[module.name]!!.assetsFiles.addAll(module.assetsFiles) + } + + if (moduleMap[module.name]!!.srcFolders.isEmpty()) { + moduleMap[module.name]!!.srcFolders.addAll(defaultConfig().srcFolders) + } + if (moduleMap[module.name]!!.stringFiles.isEmpty()) { + moduleMap[module.name]!!.stringFiles.addAll(defaultConfig().stringFiles) } - } - if (moduleMap.isEmpty()) { - moduleMap[defaultMainModule] = defaultConfig().normalize() } this.project.registerTask() } @@ -95,13 +88,14 @@ open class StringCare : Plugin { PrintUtils.print(module, "$variant:$key") PrintUtils.print(module, backupStringRes) moduleMap[module]?.let { configuration -> - backupFiles(absoluteProjectPath, configuration) + backupResourceFiles(absoluteProjectPath, configuration) } moduleMap[module]?.let { configuration -> - val files = locateFiles(absoluteProjectPath, configuration) + val files = locateResourceFiles(absoluteProjectPath, configuration) files.forEach { file -> - modifyXML(file.file, extension.main_module, key, extension.debug, + modifyXML( + file.file, extension.main_module, key, extension.debug, variantOrFlavor?.applicationId ?: "" ) } @@ -114,9 +108,9 @@ open class StringCare : Plugin { } PrintUtils.print(module, "$variant:$key") PrintUtils.print(module, backupStringRes) - backupFiles(absoluteProjectPath, defaultConfiguration) + backupResourceFiles(absoluteProjectPath, defaultConfiguration) PrintUtils.print(module, obfuscateStringRes) - val files = locateFiles(absoluteProjectPath, defaultConfiguration) + val files = locateResourceFiles(absoluteProjectPath, defaultConfiguration) files.forEach { file -> modifyXML(file.file, extension.main_module, key, extension.debug) } @@ -126,14 +120,70 @@ open class StringCare : Plugin { }, mergeResourcesFinish = { module, variant -> + PrintUtils.print(module, restoreStringRes) + val variantOrFlavor = extension.variants.find { + variant.toLowerCase().contains(it.name.toLowerCase()) + } + if (variantOrFlavor != null && variantOrFlavor.skip) { + return@ExecutionListener + } + restoreResourceFiles(absoluteProjectPath, module) + }, + mergeAssetsStart = { module, variant -> + fingerPrint(module, variant, extension.debug) { key -> + if ("none" == key) { + return@fingerPrint + } + when { + moduleMap.containsKey(module) -> { + val variantOrFlavor = extension.variants.find { + variant.toLowerCase().contains(it.name.toLowerCase()) + } + if (variantOrFlavor != null && variantOrFlavor.skip) { + PrintUtils.print(module, "Skipping $variant") + return@fingerPrint + } + + PrintUtils.print(module, "$variant:$key") + PrintUtils.print(module, backupAssets) + moduleMap[module]?.let { configuration -> + backupAssetsFiles(absoluteProjectPath, configuration) + } + + moduleMap[module]?.let { configuration -> + val files = locateAssetsFiles(absoluteProjectPath, configuration) + files.forEach { file -> + if (extension.debug) { + PrintUtils.print(null, file.file.getContent()) + } + obfuscateFile( + extension.main_module, + key, + file.file, + variantOrFlavor?.applicationId ?: "" + ) + if (extension.debug) { + PrintUtils.print(null, file.file.getContent()) + } + } + } + PrintUtils.print(module, obfuscateAssets) + } + } + } + + }, + mergeAssetsFinish = { module, variant -> + PrintUtils.print(module, restoreAssets) val variantOrFlavor = extension.variants.find { variant.toLowerCase().contains(it.name.toLowerCase()) } if (variantOrFlavor != null && variantOrFlavor.skip) { return@ExecutionListener } - restoreFiles(absoluteProjectPath, module) + restoreAssetsFiles(absoluteProjectPath, module) } + )) } @@ -155,6 +205,7 @@ open class StringCare : Plugin { } open class Configuration(var name: String) { + var assetsFiles = mutableListOf() var stringFiles = mutableListOf() var srcFolders = mutableListOf() var debug = false diff --git a/src/main/kotlin/components/AParser.kt b/src/main/kotlin/components/AParser.kt new file mode 100644 index 0000000..ba6fc2d --- /dev/null +++ b/src/main/kotlin/components/AParser.kt @@ -0,0 +1,51 @@ +package components + +import StringCare.* +import models.AssetsFile +import models.ResourceFile +import java.io.File + +fun locateAssetsFiles(projectPath: String, configuration: Configuration): List { + if (configuration.debug) { + println("== ASSETS FILES FOUND ======================================") + } + return File(projectPath).walkTopDown() + .filterIndexed { _, file -> + file.validForAssetsConfiguration(configuration.normalize()) + }.map { + it.assetsFile(configuration.normalize())!! + }.toList() +} + +fun backupAssetsFiles(projectPath: String, configuration: Configuration): List { + val files = locateAssetsFiles(projectPath, configuration.normalize()) + files.forEach { resource -> + resource.backup() + } + return files +} + +fun restoreAssetsFiles(projectPath: String, module: String): List { + val resourceFiles = File("${StringCare.tempFolder}${File.separator}$module") + .walkTopDown().toList().filter { file -> + !file.isDirectory + }.map { + it.restore(projectPath) + } + StringCare.resetFolder() + return resourceFiles +} + +fun obfuscateFile(mainModule: String, key: String, file: File, mockId: String = "") { + val obfuscation = Stark.obfuscate( + mainModule, + key, + file.readBytes(), + mockId + ) + file.writeBytes(obfuscation) +} + +fun revealFile(mainModule: String, key: String, file: File, mockId: String = "") { + file.writeBytes(Stark.reveal(mainModule, key, file.readBytes(), mockId)) +} diff --git a/src/main/kotlin/components/ExecutionListener.kt b/src/main/kotlin/components/ExecutionListener.kt index 81786eb..7620f60 100644 --- a/src/main/kotlin/components/ExecutionListener.kt +++ b/src/main/kotlin/components/ExecutionListener.kt @@ -12,7 +12,9 @@ class ExecutionListener( private val debug: Boolean = false, val dataFound: (module: String, variant: String) -> Unit, val mergeResourcesStart: (module: String, variant: String) -> Unit, - val mergeResourcesFinish: (module: String, variant: String) -> Unit + val mergeResourcesFinish: (module: String, variant: String) -> Unit, + val mergeAssetsStart: (module: String, variant: String) -> Unit, + val mergeAssetsFinish: (module: String, variant: String) -> Unit ) : BuildListener, TaskExecutionListener { @@ -27,6 +29,12 @@ class ExecutionListener( } mergeResourcesStart(it, PrintUtils.uncapitalize(task.onMergeResourcesStartsVariant())) } + task.onMergeAssetsStarts() -> task.getModuleName()?.let { + if (debug) { + PrintUtils.print(it, "Module: $it", true) + } + mergeAssetsStart(it, PrintUtils.uncapitalize(task.onMergeAssetsStartsVariant())) + } } } @@ -35,6 +43,9 @@ class ExecutionListener( task.onMergeResourcesFinish() -> task.getModuleName()?.let { mergeResourcesFinish(it, PrintUtils.uncapitalize(task.onMergeResourcesFinishVariant())) } + task.onMergeAssetsFinish() -> task.getModuleName()?.let { + mergeAssetsFinish(it, PrintUtils.uncapitalize(task.onMergeAssetsFinishVariant())) + } } } diff --git a/src/main/kotlin/components/Extensions.kt b/src/main/kotlin/components/Extensions.kt index 0ad60ad..33efaa6 100644 --- a/src/main/kotlin/components/Extensions.kt +++ b/src/main/kotlin/components/Extensions.kt @@ -1,10 +1,9 @@ package components import StringCare -import StringCare.VariantApplicationId -import StringCare.Configuration -import StringCare.Extension +import StringCare.* import groovy.json.StringEscapeUtils +import models.AssetsFile import models.ResourceFile import org.gradle.api.Project import org.gradle.api.Task @@ -15,7 +14,10 @@ import org.w3c.dom.Node import org.xml.sax.InputSource import task.SCPreview import task.SCTestObfuscation -import java.io.* +import java.io.File +import java.io.FileInputStream +import java.io.InputStreamReader +import java.io.StringWriter import javax.xml.parsers.DocumentBuilderFactory import javax.xml.transform.OutputKeys import javax.xml.transform.TransformerFactory @@ -74,9 +76,9 @@ fun String.androidTreatment(): String { return values.joinToString(separator = " ") } -fun File.validForConfiguration(configuration: Configuration): Boolean { +fun File.validForXMLConfiguration(configuration: Configuration): Boolean { var valid = this.absolutePath.contains("${File.separator}${configuration.name}${File.separator}") - && excluded().not() + && excludedForXML().not() if (valid) { valid = false configuration.srcFolders.forEach { folder -> @@ -105,19 +107,45 @@ fun File.validForConfiguration(configuration: Configuration): Boolean { } } } - if (configuration.debug && excluded().not()) { + if (configuration.debug && excludedForXML().not()) { println("${if (valid) "✔ " else "❌ not"} valid file ${this.absolutePath}") } return valid } -fun File.excluded(): Boolean { - val exclude = listOf( - "/build/", - "/.git/", - "/.gradle/", - "/gradle/" - ) +fun File.validForAssetsConfiguration(configuration: Configuration): Boolean { + var valid = this.absolutePath.contains("${File.separator}${configuration.name}${File.separator}") + && excludedForAssets().not() + if (valid) { + valid = false + configuration.assetsFiles.forEach { file -> + if (this.absolutePath.endsWith( + "${File.separator}$file".replace( + "${File.separator}${File.separator}", + File.separator + ) + ) + || (file.contains("*.") && this.absolutePath.endsWith(file.replace("*", ""))) + ) { + valid = true + } + } + } + if (configuration.debug && excludedForAssets().not()) { + println("${if (valid) "✔ " else "❌ not"} valid file ${this.absolutePath}") + } + return valid +} + +val exclude = listOf( + "/build/", + "/.git/", + "/.idea/", + "/.gradle/", + "/gradle/" +) + +fun File.excludedForXML(): Boolean { var valid = true exclude.forEach { value -> when { @@ -127,6 +155,16 @@ fun File.excluded(): Boolean { return (valid && this.isDirectory.not() && this.absolutePath.contains(".xml")).not() } +fun File.excludedForAssets(): Boolean { + var valid = true + exclude.forEach { value -> + when { + this.absolutePath.contains(value.normalizePath()) -> valid = false + } + } + return (valid && this.isDirectory.not() && this.absolutePath.contains("${File.separator}assets${File.separator}")).not() +} + fun File.resourceFile(configuration: Configuration): ResourceFile? { var sourceFolder = "" var validFile: File? = null @@ -161,6 +199,52 @@ fun File.resourceFile(configuration: Configuration): ResourceFile? { return if (valid) ResourceFile(validFile!!, sourceFolder, configuration.name) else null } +fun File.assetsFile(configuration: Configuration): AssetsFile? { + var sourceFolder = "" + var validFile: File? = null + var valid = false + configuration.srcFolders.forEach { folder -> + if (this.absolutePath.contains( + "${File.separator}$folder${File.separator}".replace( + "${File.separator}${File.separator}", + File.separator + ) + ) + ) { + sourceFolder = folder + valid = true + } + } + if (valid) { + valid = false + configuration.assetsFiles.forEach { file -> + if (this.absolutePath.endsWith( + "${File.separator}$file".replace( + "${File.separator}${File.separator}", + File.separator + ) + ) + || (file.contains("*.") && this.absolutePath.endsWith(file.replace("*", ""))) + ) { + valid = true + validFile = this + } + } + } + if (configuration.debug && excludedForAssets().not()) { + println( + "${when { + valid -> "valid file" + else -> "" + }}${when { + validFile != null -> validFile?.getContent() + else -> "the file is null" + }}" + ) + } + return if (valid) AssetsFile(validFile!!, sourceFolder, configuration.name) else null +} + fun Project.absolutePath(): String = this.file(wrapperWindows).absolutePath.replace( wrapperWindows, emptyChar @@ -222,6 +306,18 @@ fun ResourceFile.backup(): File { return backupFile } +fun AssetsFile.backup(): File { + val cleanPath = + "${StringCare.tempFolder}${File.separator}${this.module}${File.separator}${this.sourceFolder}${this.file.absolutePath.split( + this.sourceFolder + )[1]}" + .replace("${File.separator}${File.separator}", File.separator) + + val backupFile = File(cleanPath) + this.file.copyTo(backupFile, true) + return backupFile +} + fun File.restore(projectPath: String): File { val cleanPath = "$projectPath${File.separator}${this.absolutePath.split(StringCare.tempFolder)[1]}" .replace("${File.separator}${File.separator}", File.separator) @@ -308,6 +404,14 @@ fun Task.onMergeResourcesFinish(): Boolean = this.name.contains(merge) && this.name.contains(resources) && !this.name.contains(test) +fun Task.onMergeAssetsStarts(): Boolean = this.name.contains(generate) + && this.name.contains(assets) + && !this.name.contains(test) + +fun Task.onMergeAssetsFinish(): Boolean = this.name.contains(merge) + && this.name.contains(assets) + && !this.name.contains(test) + fun Task.dataFoundVariant(): String = this.name.substring(pre.length) .substring(0, this.name.substring(pre.length).length - build.length) @@ -317,6 +421,12 @@ fun Task.onMergeResourcesStartsVariant(): String = this.name.substring(merge.len fun Task.onMergeResourcesFinishVariant(): String = this.name.substring(merge.length) .substring(0, this.name.substring(merge.length).length - resources.length) +fun Task.onMergeAssetsStartsVariant(): String = this.name.substring(generate.length) + .substring(0, this.name.substring(generate.length).length - assets.length) + +fun Task.onMergeAssetsFinishVariant(): String = this.name.substring(merge.length) + .substring(0, this.name.substring(merge.length).length - assets.length) + fun R.logger(): Lazy { return lazy { LoggerFactory.getLogger(this.javaClass) } } diff --git a/src/main/kotlin/components/Stark.kt b/src/main/kotlin/components/Stark.kt index cdf89be..53b624c 100644 --- a/src/main/kotlin/components/Stark.kt +++ b/src/main/kotlin/components/Stark.kt @@ -69,7 +69,7 @@ open class Stark { external fun obfuscate(mainModule: String, key: String, value: ByteArray, mockId: String): ByteArray @JvmStatic - external fun reveal(mainModule: String, key: String, value: ByteArray): ByteArray + external fun reveal(mainModule: String, key: String, value: ByteArray, mockId: String): ByteArray } } \ No newline at end of file diff --git a/src/main/kotlin/components/Vars.kt b/src/main/kotlin/components/Vars.kt index bb60915..3788166 100644 --- a/src/main/kotlin/components/Vars.kt +++ b/src/main/kotlin/components/Vars.kt @@ -2,7 +2,7 @@ package components import java.io.File -internal const val version = "3.1" +internal const val version = "3.3" internal const val testProjectName = "KotlinSample" internal const val defaultDebug = false internal const val defaultMainModule = "app" @@ -20,8 +20,14 @@ internal const val copyCommandWindows = "copy" internal const val emptyChar = "" internal const val backupStringRes = "backupStringResources" internal const val obfuscateStringRes = "obfuscateStringResources" +internal const val restoreStringRes = "restoreStringResources" +internal const val backupAssets = "backupAssets" +internal const val obfuscateAssets = "obfuscateAssets" +internal const val restoreAssets = "restoreAssets" internal const val test = "Test" internal const val pre = "pre" internal const val build = "Build" +internal const val generate = "generate" internal const val merge = "merge" internal const val resources = "Resources" +internal const val assets = "Assets" diff --git a/src/main/kotlin/components/XParser.kt b/src/main/kotlin/components/XParser.kt index 92f6185..b928896 100644 --- a/src/main/kotlin/components/XParser.kt +++ b/src/main/kotlin/components/XParser.kt @@ -6,27 +6,27 @@ import models.SAttribute import models.StringEntity import java.io.File -fun locateFiles(projectPath: String, configuration: Configuration): List { +fun locateResourceFiles(projectPath: String, configuration: Configuration): List { if (configuration.debug) { - println("== FILES FOUND ======================================") + println("== RESOURCE FILES FOUND ======================================") } return File(projectPath).walkTopDown() .filterIndexed { _, file -> - file.validForConfiguration(configuration.normalize()) + file.validForXMLConfiguration(configuration.normalize()) }.map { it.resourceFile(configuration.normalize())!! }.toList() } -fun backupFiles(projectPath: String, configuration: Configuration): List { - val files = locateFiles(projectPath, configuration.normalize()) +fun backupResourceFiles(projectPath: String, configuration: Configuration): List { + val files = locateResourceFiles(projectPath, configuration.normalize()) files.forEach { resource -> resource.backup() } return files } -fun restoreFiles(projectPath: String, module: String): List { +fun restoreResourceFiles(projectPath: String, module: String): List { val resourceFiles = File("${StringCare.tempFolder}${File.separator}$module") .walkTopDown().toList().filter { file -> !file.isDirectory @@ -94,7 +94,7 @@ fun modifyXML(file: File, mainModule: String, key: String, debug: Boolean, mockI it.tag == "string" && it.index == i } entity?.let { - node.textContent = obfuscate(mainModule, key, it, mockId).value + node.textContent = obfuscateStringEntity(mainModule, key, it, mockId).value } } @@ -105,7 +105,7 @@ fun modifyXML(file: File, mainModule: String, key: String, debug: Boolean, mockI } } -fun obfuscate(mainModule: String, key: String, entity: StringEntity, mockId: String = ""): StringEntity { +fun obfuscateStringEntity(mainModule: String, key: String, entity: StringEntity, mockId: String = ""): StringEntity { val obfuscation = Stark.obfuscate( mainModule, key, when (entity.androidTreatment) { true -> entity.value.androidTreatment() @@ -116,9 +116,9 @@ fun obfuscate(mainModule: String, key: String, entity: StringEntity, mockId: Str return StringEntity(entity.name, entity.attributes, obfuscation, entity.tag, entity.index, entity.androidTreatment) } -fun reveal(mainModule: String, key: String, entity: StringEntity): StringEntity { +fun revealStringEntity(mainModule: String, key: String, entity: StringEntity, mockId: String = ""): StringEntity { val arr: ByteArray = entity.value.split(", ").map { it.toInt().toByte() }.toByteArray() - val original = String(Stark.reveal(mainModule, key, arr)) + val original = String(Stark.reveal(mainModule, key, arr, mockId)) return StringEntity(entity.name, entity.attributes, original, entity.tag, entity.index, entity.androidTreatment) } diff --git a/src/main/kotlin/components/jni/libsignKey.dll b/src/main/kotlin/components/jni/libsignKey.dll index c8c778b..be95e07 100644 Binary files a/src/main/kotlin/components/jni/libsignKey.dll and b/src/main/kotlin/components/jni/libsignKey.dll differ diff --git a/src/main/kotlin/components/jni/libsignKey.dylib b/src/main/kotlin/components/jni/libsignKey.dylib index 884a75c..2206eda 100755 Binary files a/src/main/kotlin/components/jni/libsignKey.dylib and b/src/main/kotlin/components/jni/libsignKey.dylib differ diff --git a/src/main/kotlin/models/AssetsFile.kt b/src/main/kotlin/models/AssetsFile.kt new file mode 100644 index 0000000..15ea274 --- /dev/null +++ b/src/main/kotlin/models/AssetsFile.kt @@ -0,0 +1,5 @@ +package models + +import java.io.File + +data class AssetsFile(val file: File, val sourceFolder: String, val module: String) \ No newline at end of file diff --git a/src/main/kotlin/task/SCPreview.kt b/src/main/kotlin/task/SCPreview.kt index e7aaa30..eaaff91 100644 --- a/src/main/kotlin/task/SCPreview.kt +++ b/src/main/kotlin/task/SCPreview.kt @@ -3,7 +3,7 @@ package task import StringCare.Companion.absoluteProjectPath import StringCare.Companion.moduleMap import components.getContent -import components.locateFiles +import components.locateResourceFiles import components.parseXML import org.gradle.api.DefaultTask import org.gradle.api.tasks.TaskAction @@ -16,7 +16,7 @@ open class SCPreview : DefaultTask() { println("Modules (${moduleMap.size})") moduleMap.forEach { entry -> - val files = locateFiles(absoluteProjectPath, entry.value) + val files = locateResourceFiles(absoluteProjectPath, entry.value) println("Located files(${files.size}) for obfuscating") files.forEach { file -> println("- ${file.file.name}") diff --git a/src/main/kotlin/task/SCTestObfuscation.kt b/src/main/kotlin/task/SCTestObfuscation.kt index 7cadcd5..88c20d7 100644 --- a/src/main/kotlin/task/SCTestObfuscation.kt +++ b/src/main/kotlin/task/SCTestObfuscation.kt @@ -30,7 +30,7 @@ open class SCTestObfuscation : DefaultTask() { } println("fingerprint: $key") println("variant: ${variant ?: "debug"}") - val filesToObfuscate = backupFiles(absoluteProjectPath, entry.value) + val filesToObfuscate = backupResourceFiles(absoluteProjectPath, entry.value) filesToObfuscate.forEach { file -> val originalEntities = parseXML(file.file) println("============================") @@ -51,7 +51,7 @@ open class SCTestObfuscation : DefaultTask() { println(file.file.getContent()) println("============================") } - restoreFiles(absoluteProjectPath, entry.value.name) + restoreResourceFiles(absoluteProjectPath, entry.value.name) } println("== END OBFUSCATION ==================================") diff --git a/src/test/kotlin/AssetsTest.kt b/src/test/kotlin/AssetsTest.kt new file mode 100644 index 0000000..4b65442 --- /dev/null +++ b/src/test/kotlin/AssetsTest.kt @@ -0,0 +1,130 @@ +import components.* +import org.junit.Before +import org.junit.Test +import java.io.File + +class AssetsTest { + + // private val logger by logger() + + private val configuration = defaultConfig().apply { + debug = true + stringFiles.add("strings_extra.xml") + srcFolders.add("src/other_source") + } + + @Before + fun setup() { + librarySetupTask.runCommand() + } + + @Test + fun `01 - (PLUGIN) locate assets files for default configuration`() { + val temp = tempPath() + prepareTask(temp).runCommand { _, _ -> + assert(locateAssetsFiles("$temp${File.separator}$testProjectName", configuration.apply { + assetsFiles = mutableListOf("*.json") + }).isNotEmpty()) + } + StringCare.resetFolder() + } + + @Test + fun `02 - (PLUGIN) backup assets files`() { + val temp = tempPath() + prepareTask(temp).runCommand { _, _ -> + assert(backupAssetsFiles("$temp${File.separator}$testProjectName", configuration.apply { + assetsFiles = mutableListOf("*.json") + }).isNotEmpty()) + } + StringCare.resetFolder() + } + + @Test + fun `03 - (PLUGIN) restore assets files`() { + val temp = tempPath() + prepareTask(temp).runCommand { _, report -> + println(report) + assert( + restoreAssetsFiles("$temp${File.separator}$testProjectName", defaultMainModule).isEmpty() + ) + assert( + backupAssetsFiles("$temp${File.separator}$testProjectName", configuration.apply { + assetsFiles = mutableListOf("*.json") + }).isNotEmpty() + ) + assert( + restoreAssetsFiles("$temp${File.separator}$testProjectName", defaultMainModule).isNotEmpty() + ) + } + } + + @Test + fun `04 - (PLUGIN) asset obfuscation`() { + val temp = tempPath() + signingReportTask(temp).runCommand { _, report -> + println(report) + val key = report.extractFingerprint() + println(key) + assert(key.isNotEmpty()) + val files = locateAssetsFiles( + "$temp${File.separator}$testProjectName", + configuration.apply { + assetsFiles = mutableListOf("*.json") + }) + assert(files.isNotEmpty()) + files.forEach { + println("-------------------------------------------------------") + val original = it.file.getContent() + println("original: \n $original") + obfuscateFile( + "$temp${File.separator}$mainModuleTest", + key, + it.file + ) + val obfuscated = it.file.getContent() + println("obfuscated: \n $obfuscated") + assert(original != obfuscated) + } + } + } + + @Test + fun `05 - (PLUGIN) asset reveal`() { + val temp = tempPath() + signingReportTask(temp).runCommand { _, report -> + println(report) + val key = report.extractFingerprint() + println(key) + assert(key.isNotEmpty()) + val files = locateAssetsFiles( + "$temp${File.separator}$testProjectName", + configuration.apply { + assetsFiles = mutableListOf("*.json") + }) + assert(files.isNotEmpty()) + files.forEach { + println("-------------------------------------------------------") + val original = it.file.getContent() + println("original: \n $original") + obfuscateFile( + "$temp${File.separator}$mainModuleTest", + key, + it.file + ) + val obfuscated = it.file.getContent() + println("obfuscated: \n $obfuscated") + assert(original != obfuscated) + revealFile( + "$temp${File.separator}$mainModuleTest", + key, + it.file + ) + val reveal = it.file.getContent() + println("reveal: \n $reveal") + assert(original == reveal) + } + } + } + +} \ No newline at end of file diff --git a/src/test/kotlin/SCTest.kt b/src/test/kotlin/SCTest.kt index e5601f8..6a31c97 100644 --- a/src/test/kotlin/SCTest.kt +++ b/src/test/kotlin/SCTest.kt @@ -49,7 +49,7 @@ class SCTest { fun `04 - (PLUGIN) locate string files for default configuration`() { val temp = tempPath() prepareTask(temp).runCommand { _, _ -> - assert(locateFiles("$temp${File.separator}$testProjectName", configuration).isNotEmpty()) + assert(locateResourceFiles("$temp${File.separator}$testProjectName", configuration).isNotEmpty()) } StringCare.resetFolder() } @@ -58,7 +58,7 @@ class SCTest { fun `05 - (PLUGIN) backup string files`() { val temp = tempPath() prepareTask(temp).runCommand { _, _ -> - assert(backupFiles("$temp${File.separator}$testProjectName", configuration).isNotEmpty()) + assert(backupResourceFiles("$temp${File.separator}$testProjectName", configuration).isNotEmpty()) } StringCare.resetFolder() } @@ -69,13 +69,13 @@ class SCTest { prepareTask(temp).runCommand { _, report -> println(report) assert( - restoreFiles("$temp${File.separator}$testProjectName", defaultMainModule).isEmpty() + restoreResourceFiles("$temp${File.separator}$testProjectName", defaultMainModule).isEmpty() ) assert( - backupFiles("$temp${File.separator}$testProjectName", configuration).isNotEmpty() + backupResourceFiles("$temp${File.separator}$testProjectName", configuration).isNotEmpty() ) assert( - restoreFiles("$temp${File.separator}$testProjectName", defaultMainModule).isNotEmpty() + restoreResourceFiles("$temp${File.separator}$testProjectName", defaultMainModule).isNotEmpty() ) } } @@ -85,7 +85,7 @@ class SCTest { val temp = tempPath() prepareTask(temp).runCommand { _, report -> println(report) - val files = locateFiles("$temp${File.separator}$testProjectName", configuration) + val files = locateResourceFiles("$temp${File.separator}$testProjectName", configuration) files.forEach { assert(parseXML(it.file).isNotEmpty()) } @@ -100,11 +100,11 @@ class SCTest { val key = report.extractFingerprint() println(key) assert(key.isNotEmpty()) - val files = locateFiles("$temp${File.separator}$testProjectName", configuration) + val files = locateResourceFiles("$temp${File.separator}$testProjectName", configuration) files.forEach { file -> val entities = parseXML(file.file) entities.forEach { entity -> - val obfuscated = obfuscate( + val obfuscated = obfuscateStringEntity( "$temp${File.separator}$mainModuleTest", key, entity, @@ -124,18 +124,18 @@ class SCTest { val key = report.extractFingerprint() println(key) assert(key.isNotEmpty()) - val files = locateFiles("$temp${File.separator}$testProjectName", configuration) + val files = locateResourceFiles("$temp${File.separator}$testProjectName", configuration) files.forEach { file -> val entities = parseXML(file.file) entities.forEach { entity -> - val obfuscated = obfuscate( + val obfuscated = obfuscateStringEntity( "$temp${File.separator}$mainModuleTest", key, entity ) assert(obfuscated.value != entity.value) - val original = reveal( + val original = revealStringEntity( "$temp${File.separator}$mainModuleTest", key, obfuscated @@ -158,13 +158,13 @@ class SCTest { val temp = tempPath() signingReportTask(temp).runCommand { _, report -> println(report) - val files = locateFiles("$temp${File.separator}$testProjectName", configuration) + val files = locateResourceFiles("$temp${File.separator}$testProjectName", configuration) files.forEach { file -> val entities = parseXML(file.file) assert(entities.isNotEmpty()) modifyXML(file.file, "$temp${File.separator}$mainModuleTest", report.extractFingerprint(), true) } - val filesObfuscated = locateFiles("$temp${File.separator}$testProjectName", configuration) + val filesObfuscated = locateResourceFiles("$temp${File.separator}$testProjectName", configuration) filesObfuscated.forEach { file -> val entities = parseXML(file.file) assert(entities.isEmpty()) @@ -177,20 +177,20 @@ class SCTest { val temp = tempPath() signingReportTask(temp).runCommand { _, report -> println(report) - val files = backupFiles("$temp${File.separator}$testProjectName", configuration) + val files = backupResourceFiles("$temp${File.separator}$testProjectName", configuration) assert(files.isNotEmpty()) files.forEach { file -> val entities = parseXML(file.file) assert(entities.isNotEmpty()) modifyXML(file.file, "$temp${File.separator}$mainModuleTest", report.extractFingerprint(), true) } - val filesObfuscated = locateFiles("$temp${File.separator}$testProjectName", configuration) + val filesObfuscated = locateResourceFiles("$temp${File.separator}$testProjectName", configuration) filesObfuscated.forEach { file -> val entities = parseXML(file.file) assert(entities.isEmpty()) } - val restoredFiles = restoreFiles("$temp${File.separator}$testProjectName", defaultMainModule) + val restoredFiles = restoreResourceFiles("$temp${File.separator}$testProjectName", defaultMainModule) assert(restoredFiles.isNotEmpty()) val originalEntities = mutableListOf() @@ -222,7 +222,7 @@ class SCTest { val temp = tempPath() signingReportTask(temp).runCommand { _, report -> println(report) - val files = locateFiles("$temp${File.separator}$testProjectName", configuration) + val files = locateResourceFiles("$temp${File.separator}$testProjectName", configuration) files.forEach { file -> val entities = parseXML(file.file) assert(entities.isNotEmpty()) @@ -233,7 +233,7 @@ class SCTest { true ) } - val filesObfuscated = locateFiles( + val filesObfuscated = locateResourceFiles( "$temp${File.separator}$testProjectName", configuration )