|
| 1 | +package net.corda.serialization.internal |
| 2 | + |
| 3 | +import net.corda.core.crypto.SecureHash |
| 4 | +import net.corda.core.crypto.SecureHash.Companion.SHA2_256 |
| 5 | +import net.corda.core.crypto.SecureHash.Companion.SHA2_512 |
| 6 | +import net.corda.core.crypto.algorithm |
| 7 | +import net.corda.core.serialization.SerializationDefaults |
| 8 | +import net.corda.core.serialization.deserialize |
| 9 | +import net.corda.core.serialization.serialize |
| 10 | +import net.corda.testing.core.SerializationEnvironmentRule |
| 11 | +import org.junit.Assert.assertArrayEquals |
| 12 | +import org.junit.Rule |
| 13 | +import org.junit.Test |
| 14 | +import kotlin.test.assertEquals |
| 15 | +import kotlin.test.assertTrue |
| 16 | + |
| 17 | +class SecureHashSerializationTest { |
| 18 | + @Rule |
| 19 | + @JvmField |
| 20 | + val testSerialization = SerializationEnvironmentRule() |
| 21 | + |
| 22 | + @Test(timeout = 300_000) |
| 23 | + fun `serialize and deserialize SHA-256`() { |
| 24 | + val before = SecureHash.randomSHA256() |
| 25 | + val bytes = before.serialize(context = SerializationDefaults.P2P_CONTEXT.withoutReferences()).bytes |
| 26 | + val after = bytes.deserialize<SecureHash>() |
| 27 | + assertEquals(before, after) |
| 28 | + assertArrayEquals(before.bytes, after.bytes) |
| 29 | + assertEquals(before.algorithm, after.algorithm) |
| 30 | + assertEquals(after.algorithm, SHA2_256) |
| 31 | + assertTrue(after is SecureHash.SHA256) |
| 32 | + } |
| 33 | + |
| 34 | + @Test(timeout = 300_000) |
| 35 | + fun `serialize and deserialize SHA-512`() { |
| 36 | + val before = SecureHash.random(SHA2_512) |
| 37 | + val bytes = before.serialize(context = SerializationDefaults.P2P_CONTEXT.withoutReferences()).bytes |
| 38 | + val after = bytes.deserialize<SecureHash>() |
| 39 | + assertEquals(before, after) |
| 40 | + assertArrayEquals(before.bytes, after.bytes) |
| 41 | + assertEquals(before.algorithm, after.algorithm) |
| 42 | + assertEquals(after.algorithm, SHA2_512) |
| 43 | + assertTrue(after is SecureHash.HASH) |
| 44 | + } |
| 45 | +} |
0 commit comments