mirror of
https://github.com/ReVanced/revanced-patcher.git
synced 2026-01-28 13:41:02 +00:00
fix(Io): JAR loading and saving (#8)
* refactor: Complete rewrite of `Io` * style: format code * style: rewrite todos * fix: use lateinit instead of nonnull assert for zipEntry * fix: use lateinit instead of nonnull assert for jarEntry & reuse zipEntry * docs: add docs to `Patcher` * test: match output of patcher * chore: add todo to `Io` for removing non-class files Co-authored-by: Sculas <contact@sculas.xyz>
This commit is contained in:
@@ -12,13 +12,16 @@ import net.revanced.patcher.writer.ASMWriter.setAt
|
||||
import org.junit.jupiter.api.assertDoesNotThrow
|
||||
import org.objectweb.asm.Opcodes.*
|
||||
import org.objectweb.asm.Type
|
||||
import org.objectweb.asm.tree.*
|
||||
import org.objectweb.asm.tree.FieldInsnNode
|
||||
import org.objectweb.asm.tree.LdcInsnNode
|
||||
import org.objectweb.asm.tree.MethodInsnNode
|
||||
import java.io.ByteArrayOutputStream
|
||||
import java.io.PrintStream
|
||||
import kotlin.test.Test
|
||||
|
||||
internal class PatcherTest {
|
||||
companion object {
|
||||
val testSigs: Array<Signature> = arrayOf(
|
||||
val testSignatures: Array<Signature> = arrayOf(
|
||||
// Java:
|
||||
// public static void main(String[] args) {
|
||||
// System.out.println("Hello, world!");
|
||||
@@ -45,8 +48,11 @@ internal class PatcherTest {
|
||||
|
||||
@Test
|
||||
fun testPatcher() {
|
||||
val testData = PatcherTest::class.java.getResourceAsStream("/test1.jar")!!
|
||||
val patcher = Patcher(testData, testSigs)
|
||||
val patcher = Patcher(
|
||||
PatcherTest::class.java.getResourceAsStream("/test1.jar")!!,
|
||||
ByteArrayOutputStream(),
|
||||
testSignatures
|
||||
)
|
||||
|
||||
patcher.addPatches(
|
||||
object : Patch("TestPatch") {
|
||||
@@ -74,9 +80,9 @@ internal class PatcherTest {
|
||||
startIndex + 1,
|
||||
FieldInsnNode(
|
||||
GETSTATIC,
|
||||
Type.getInternalName(System::class.java), // "java/io/System"
|
||||
Type.getInternalName(System::class.java), // "java/lang/System"
|
||||
"out",
|
||||
Type.getInternalName(PrintStream::class.java) // "java.io.PrintStream"
|
||||
"L" + Type.getInternalName(PrintStream::class.java) // "Ljava/io/PrintStream"
|
||||
),
|
||||
LdcInsnNode("Hello, ReVanced! Adding bytecode."),
|
||||
MethodInsnNode(
|
||||
@@ -111,41 +117,27 @@ internal class PatcherTest {
|
||||
)
|
||||
|
||||
// Apply all patches loaded in the patcher
|
||||
val result = patcher.applyPatches()
|
||||
val patchResult = patcher.applyPatches()
|
||||
// You can check if an error occurred
|
||||
for ((s, r) in result) {
|
||||
if (r.isFailure) {
|
||||
throw Exception("Patch $s failed", r.exceptionOrNull()!!)
|
||||
for ((patchName, result) in patchResult) {
|
||||
if (result.isFailure) {
|
||||
throw Exception("Patch $patchName failed", result.exceptionOrNull()!!)
|
||||
}
|
||||
}
|
||||
|
||||
// TODO Doesn't work, needs to be fixed.
|
||||
//val out = ByteArrayOutputStream()
|
||||
//patcher.saveTo(out)
|
||||
//assertTrue(
|
||||
// // 8 is a random value, it's just weird if it's any lower than that
|
||||
// out.size() > 8,
|
||||
// "Output must be at least 8 bytes"
|
||||
//)
|
||||
//
|
||||
//out.close()
|
||||
testData.close()
|
||||
patcher.save()
|
||||
}
|
||||
|
||||
// TODO Doesn't work, needs to be fixed.
|
||||
//@Test
|
||||
//fun `test patcher with no changes`() {
|
||||
// val testData = PatcherTest::class.java.getResourceAsStream("/test1.jar")!!
|
||||
// val available = testData.available()
|
||||
// val patcher = Patcher(testData, testSigs)
|
||||
//
|
||||
// val out = ByteArrayOutputStream()
|
||||
// patcher.saveTo(out)
|
||||
// assertEquals(available, out.size())
|
||||
//
|
||||
// out.close()
|
||||
// testData.close()
|
||||
//}
|
||||
@Test
|
||||
fun `test patcher with no changes`() {
|
||||
val testData = PatcherTest::class.java.getResourceAsStream("/test1.jar")!!
|
||||
// val available = testData.available()
|
||||
val out = ByteArrayOutputStream()
|
||||
Patcher(testData, out, testSignatures).save()
|
||||
// FIXME(Sculas): There seems to be a 1-byte difference, not sure what it is.
|
||||
// assertEquals(available, out.size())
|
||||
out.close()
|
||||
}
|
||||
|
||||
@Test()
|
||||
fun `should not raise an exception if any signature member except the name is missing`() {
|
||||
@@ -154,6 +146,7 @@ internal class PatcherTest {
|
||||
assertDoesNotThrow("Should raise an exception because opcodes is empty") {
|
||||
Patcher(
|
||||
PatcherTest::class.java.getResourceAsStream("/test1.jar")!!,
|
||||
ByteArrayOutputStream(),
|
||||
arrayOf(
|
||||
Signature(
|
||||
sigName,
|
||||
|
||||
@@ -1,12 +1,12 @@
|
||||
package net.revanced.patcher
|
||||
|
||||
import java.io.ByteArrayOutputStream
|
||||
import kotlin.test.Test
|
||||
|
||||
internal class ReaderTest {
|
||||
@Test
|
||||
fun `read jar containing multiple classes`() {
|
||||
val testData = PatcherTest::class.java.getResourceAsStream("/test2.jar")!!
|
||||
Patcher(testData, PatcherTest.testSigs) // reusing test sigs from PatcherTest
|
||||
testData.close()
|
||||
Patcher(testData, ByteArrayOutputStream(), PatcherTest.testSignatures) // reusing test sigs from PatcherTest
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,7 @@ object TestUtil {
|
||||
private fun AbstractInsnNode.nodeString(): String {
|
||||
val sb = NodeStringBuilder()
|
||||
when (this) {
|
||||
// TODO: Add more types
|
||||
// TODO(Sculas): Add more types
|
||||
is LdcInsnNode -> sb
|
||||
.addType("cst", cst)
|
||||
is FieldInsnNode -> sb
|
||||
|
||||
Reference in New Issue
Block a user