1 /* 2 * Copyright 2016-2021 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. 3 */ 4 import org.junit.Test 5 import java.io.* 6 import kotlin.test.* 7 8 /* 9 * This is intentionally put here instead of coreAgentTest to avoid accidental classpath replacing 10 * and ruining core agent test. 11 */ 12 class PrecompiledDebugProbesTest { 13 14 private val overwrite = java.lang.Boolean.getBoolean("overwrite.probes") 15 16 @Test testClassFileContentnull17 fun testClassFileContent() { 18 val clz = Class.forName("kotlin.coroutines.jvm.internal.DebugProbesKt") 19 val classFileResourcePath = clz.name.replace(".", "/") + ".class" 20 val array = clz.classLoader.getResourceAsStream(classFileResourcePath).use { it.readBytes() } 21 assertJava8Compliance(array) 22 // we expect the integration testing project to be in a subdirectory of the main kotlinx.coroutines project 23 val base = File("").absoluteFile.parentFile 24 val probes = File(base, "kotlinx-coroutines-core/jvm/resources/DebugProbesKt.bin") 25 val binContent = probes.readBytes() 26 if (overwrite) { 27 FileOutputStream(probes).use { it.write(array) } 28 println("Content was successfully overwritten!") 29 } else { 30 assertTrue( 31 array.contentEquals(binContent), 32 "Compiled DebugProbesKt.class does not match the file shipped as a resource in kotlinx-coroutines-core. " + 33 "Typically it happens because of the Kotlin version update (-> binary metadata). " + 34 "In that case, run the same test with -Poverwrite.probes=true." 35 ) 36 } 37 } 38 assertJava8Compliancenull39 private fun assertJava8Compliance(classBytes: ByteArray) { 40 DataInputStream(classBytes.inputStream()).use { 41 val magic: Int = it.readInt() 42 if (magic != -0x35014542) throw IllegalArgumentException("Not a valid class!") 43 val minor: Int = it.readUnsignedShort() 44 val major: Int = it.readUnsignedShort() 45 assertEquals(52, major) 46 assertEquals(0, minor) 47 } 48 } 49 } 50