diff --git a/build.gradle b/build.gradle index 97b3455..c1f0e77 100644 --- a/build.gradle +++ b/build.gradle @@ -8,8 +8,8 @@ buildscript { } } plugins { - id 'com.github.ben-manes.versions' version '0.15.0' - id "io.spring.dependency-management" version "1.0.3.RELEASE" + id 'com.github.ben-manes.versions' version '0.17.0' + id "io.spring.dependency-management" version "1.0.4.RELEASE" } subprojects { @@ -139,19 +139,21 @@ subprojects { } dependency 'ch.dissem.msgpack:msgpack:2.0.1' - dependency 'org.bouncycastle:bcprov-jdk15on:1.57' - dependency 'com.madgag.spongycastle:prov:1.56.0.0' - dependency 'org.apache.commons:commons-text:1.1' - dependency 'org.flywaydb:flyway-core:4.2.0' - dependency 'com.beust:klaxon:0.31' + dependency 'org.bouncycastle:bcprov-jdk15on:1.59' + dependency 'com.madgag.spongycastle:prov:1.58.0.0' + dependency 'org.apache.commons:commons-text:1.2' + dependency 'org.flywaydb:flyway-core:5.0.7' + dependency 'com.beust:klaxon:2.1.7' dependency 'args4j:args4j:2.33' dependency 'org.ini4j:ini4j:0.5.4' dependency 'com.h2database:h2:1.4.196' - dependency 'junit:junit:4.12' - dependency 'org.hamcrest:hamcrest-library:1.3' + dependency 'org.hamcrest:java-hamcrest:2.0.0.0' dependency 'com.nhaarman:mockito-kotlin:1.5.0' + + dependency 'org.junit.jupiter:junit-jupiter-api:5.1.0' + dependency 'org.junit.jupiter:junit-jupiter-engine:5.1.0' } } } diff --git a/core/build.gradle b/core/build.gradle index 05e5210..ce09283 100644 --- a/core/build.gradle +++ b/core/build.gradle @@ -26,9 +26,9 @@ artifacts { dependencies { compile 'org.slf4j:slf4j-api' compile 'ch.dissem.msgpack:msgpack' - testCompile 'junit:junit' - testCompile 'org.hamcrest:hamcrest-library' testCompile 'com.nhaarman:mockito-kotlin' + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' testCompile project(':cryptography-bc') } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/BitmessageContextTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/BitmessageContextTest.kt index 043f811..aa5eee1 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/BitmessageContextTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/BitmessageContextTest.kt @@ -35,11 +35,9 @@ import ch.dissem.bitmessage.utils.TTL import ch.dissem.bitmessage.utils.TestUtils import ch.dissem.bitmessage.utils.UnixTime.MINUTE import com.nhaarman.mockito_kotlin.* -import org.hamcrest.CoreMatchers.`is` -import org.hamcrest.CoreMatchers.notNullValue -import org.junit.Assert.* -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import java.util.* import kotlin.concurrent.thread @@ -55,7 +53,8 @@ class BitmessageContextTest { internal var removed = 0 override fun getItem(initialHash: ByteArray): ProofOfWorkRepository.Item { - return items[InventoryVector(initialHash)] ?: throw IllegalArgumentException("${hex(initialHash)} not found in $items") + return items[InventoryVector(initialHash)] + ?: throw IllegalArgumentException("${hex(initialHash)} not found in $items") } override fun getItems(): List { @@ -72,7 +71,10 @@ class BitmessageContextTest { } override fun putObject(objectMessage: ObjectMessage, nonceTrialsPerByte: Long, extraBytes: Long) { - items.put(InventoryVector(cryptography().getInitialHash(objectMessage)), ProofOfWorkRepository.Item(objectMessage, nonceTrialsPerByte, extraBytes)) + items.put( + InventoryVector(cryptography().getInitialHash(objectMessage)), + ProofOfWorkRepository.Item(objectMessage, nonceTrialsPerByte, extraBytes) + ) added++ } @@ -113,7 +115,7 @@ class BitmessageContextTest { TTL.msg = 2 * MINUTE } - @Before + @BeforeEach fun setUp() { testPowRepo.reset() } @@ -202,60 +204,73 @@ class BitmessageContextTest { ctx.addSubscribtion(address) verify(ctx.addresses, atLeastOnce()).save(address) - assertThat(address.isSubscribed, `is`(true)) + assertTrue(address.isSubscribed) verify(ctx.internals.inventory).getObjects(eq(address.stream), any(), any()) verify(testListener).receive(any()) } @Test fun `ensure identity is created`() { - assertThat(ctx.createIdentity(false), notNullValue()) + assertNotNull(ctx.createIdentity(false)) } @Test fun `ensure message is sent`() { - ctx.send(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"), TestUtils.loadContact(), - "Subject", "Message") + ctx.send( + TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"), TestUtils.loadContact(), + "Subject", "Message" + ) verify(ctx.internals.proofOfWorkRepository, timeout(10000)).putObject( - argThat { payload.type == ObjectType.MSG }, eq(1000L), eq(1000L)) + argThat { payload.type == ObjectType.MSG }, eq(1000L), eq(1000L) + ) assertEquals(2, testPowRepo.added) verify(ctx.messages, timeout(10000).atLeastOnce()).save(argThat { type == Type.MSG }) } @Test fun `ensure pubkey is requested if it is missing`() { - ctx.send(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"), + ctx.send( + TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"), BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT"), - "Subject", "Message") + "Subject", "Message" + ) verify(testPowRepo, timeout(10000).atLeastOnce()) .putObject(argThat { payload.type == ObjectType.GET_PUBKEY }, eq(1000L), eq(1000L)) verify(ctx.messages, timeout(10000).atLeastOnce()).save(argThat<Plaintext> { type == Type.MSG }) } - @Test(expected = IllegalArgumentException::class) + @Test fun `ensure sender must be identity`() { - ctx.send(BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT"), - BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT"), - "Subject", "Message") + assertThrows(IllegalArgumentException::class.java) { + ctx.send( + BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT"), + BitmessageAddress("BM-opWQhvk9xtMFvQA2Kvetedpk8LkbraWHT"), + "Subject", "Message" + ) + } } @Test fun `ensure broadcast is sent`() { - ctx.broadcast(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"), - "Subject", "Message") + ctx.broadcast( + TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8"), + "Subject", "Message" + ) verify(ctx.internals.proofOfWorkRepository, timeout(1000).atLeastOnce()) .putObject(argThat { payload.type == ObjectType.BROADCAST }, eq(1000L), eq(1000L)) verify(testPowEngine).calculateNonce(any(), any(), any<ProofOfWorkEngine.Callback>()) verify(ctx.messages, timeout(10000).atLeastOnce()).save(argThat<Plaintext> { type == Type.BROADCAST }) } - @Test(expected = IllegalArgumentException::class) + @Test fun `ensure sender without private key throws exception`() { - val msg = Plaintext.Builder(Type.BROADCAST) - .from(BitmessageAddress("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8")) - .message("Subject", "Message") - .build() - ctx.send(msg) + assertThrows(IllegalArgumentException::class.java) { + val msg = Plaintext.Builder(Type.BROADCAST) + .from(BitmessageAddress("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8")) + .message("Subject", "Message") + .build() + ctx.send(msg) + } } @Test @@ -326,6 +341,6 @@ class BitmessageContextTest { @Test fun `ensure status contains user agent`() { val userAgent = ctx.status().getProperty("user agent")?.value.toString() - assertThat(userAgent, `is`("/Jabit:${BitmessageContext.version}/")) + assertEquals("/Jabit:${BitmessageContext.version}/", userAgent) } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/DecryptionTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/DecryptionTest.kt index dd9bc95..fec7449 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/DecryptionTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/DecryptionTest.kt @@ -21,9 +21,9 @@ import ch.dissem.bitmessage.entity.payload.V4Broadcast import ch.dissem.bitmessage.entity.payload.V5Broadcast import ch.dissem.bitmessage.utils.TestBase import ch.dissem.bitmessage.utils.TestUtils -import org.junit.Assert.assertEquals -import org.junit.Assert.assertTrue -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test class DecryptionTest : TestBase() { @Test diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/DefaultMessageListenerTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/DefaultMessageListenerTest.kt index 57db6fe..dba554f 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/DefaultMessageListenerTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/DefaultMessageListenerTest.kt @@ -34,8 +34,8 @@ import ch.dissem.bitmessage.utils.TestUtils import ch.dissem.bitmessage.utils.UnixTime.MINUTE import ch.dissem.bitmessage.utils.UnixTime.now import com.nhaarman.mockito_kotlin.* -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.BeforeAll +import org.junit.jupiter.api.Test /** * @author Christian Basler @@ -47,7 +47,7 @@ class DefaultMessageListenerTest : TestBase() { cryptography = BouncyCryptography() ) - @Before + @BeforeAll fun setUp() { listener = ctx.networkListener as DefaultMessageListener } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/EncryptionTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/EncryptionTest.kt index 657b468..ee64b6f 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/EncryptionTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/EncryptionTest.kt @@ -24,9 +24,9 @@ import ch.dissem.bitmessage.entity.valueobject.PrivateKey import ch.dissem.bitmessage.utils.Singleton.cryptography import ch.dissem.bitmessage.utils.TestBase import ch.dissem.bitmessage.utils.TestUtils -import org.junit.Assert.assertEquals -import org.junit.Assert.assertNotNull -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.Test class EncryptionTest : TestBase() { @Test diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/ProofOfWorkServiceTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/ProofOfWorkServiceTest.kt index 276f4ab..29669f6 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/ProofOfWorkServiceTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/ProofOfWorkServiceTest.kt @@ -28,10 +28,9 @@ import ch.dissem.bitmessage.ports.ProofOfWorkRepository import ch.dissem.bitmessage.utils.Singleton import ch.dissem.bitmessage.utils.TestUtils import com.nhaarman.mockito_kotlin.* -import org.hamcrest.CoreMatchers.equalTo -import org.junit.Assert.assertThat -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import java.util.* import kotlin.properties.Delegates @@ -44,7 +43,7 @@ class ProofOfWorkServiceTest { private var obj by Delegates.notNull<ObjectMessage>() - @Before + @BeforeEach fun setUp() { cryptography = spy(BouncyCryptography()) Singleton.initialize(cryptography) @@ -96,6 +95,6 @@ class ProofOfWorkServiceTest { verify(ctx.proofOfWorkRepository).removeObject(eq(initialHash)) verify(ctx.inventory).storeObject(eq(objectMessage)) verify(ctx.networkHandler).offer(eq(objectMessage.inventoryVector)) - assertThat(plaintext.inventoryVector, equalTo(objectMessage.inventoryVector)) + assertEquals(objectMessage.inventoryVector, plaintext.inventoryVector) } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/SignatureTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/SignatureTest.kt index a3b965c..dacd01f 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/SignatureTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/SignatureTest.kt @@ -23,8 +23,8 @@ import ch.dissem.bitmessage.entity.payload.Pubkey import ch.dissem.bitmessage.entity.valueobject.PrivateKey import ch.dissem.bitmessage.utils.TestBase import ch.dissem.bitmessage.utils.TestUtils -import org.junit.Assert.* -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test class SignatureTest : TestBase() { @Test diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/entity/BitmessageAddressTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/entity/BitmessageAddressTest.kt index 95dcee1..2ab92eb 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/entity/BitmessageAddressTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/entity/BitmessageAddressTest.kt @@ -22,16 +22,15 @@ import ch.dissem.bitmessage.entity.payload.Pubkey.Feature.INCLUDE_DESTINATION import ch.dissem.bitmessage.entity.payload.V4Pubkey import ch.dissem.bitmessage.entity.valueobject.PrivateKey import ch.dissem.bitmessage.utils.* -import org.junit.Assert -import org.junit.Assert.* -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test import java.io.IOException import java.util.* class BitmessageAddressTest : TestBase() { @Test fun `ensure feature flag is calculated correctly`() { - Assert.assertEquals(1, Pubkey.Feature.bitfield(DOES_ACK)) + assertEquals(1, Pubkey.Feature.bitfield(DOES_ACK)) assertEquals(2, Pubkey.Feature.bitfield(INCLUDE_DESTINATION)) assertEquals(3, Pubkey.Feature.bitfield(DOES_ACK, INCLUDE_DESTINATION)) } @@ -74,7 +73,7 @@ class BitmessageAddressTest : TestBase() { try { address.pubkey = pubkey } catch (e: Exception) { - fail(e.message) + fail<Unit>(e.message) } } @@ -82,7 +81,7 @@ class BitmessageAddressTest : TestBase() { @Test fun `ensure V3Pubkey can be imported`() { val address = BitmessageAddress("BM-2D9Vc5rFxxR5vTi53T9gkLfemViHRMVLQZ") - Assert.assertArrayEquals(Bytes.fromHex("007402be6e76c3cb87caa946d0c003a3d4d8e1d5"), address.ripe) + assertArrayEquals(Bytes.fromHex("007402be6e76c3cb87caa946d0c003a3d4d8e1d5"), address.ripe) val objectMessage = TestUtils.loadObjectMessage(3, "V3Pubkey.payload") val pubkey = objectMessage.payload as Pubkey @@ -90,7 +89,7 @@ class BitmessageAddressTest : TestBase() { try { address.pubkey = pubkey } catch (e: Exception) { - fail(e.message) + fail<Unit>(e.message) } assertArrayEquals(Bytes.fromHex("007402be6e76c3cb87caa946d0c003a3d4d8e1d5"), pubkey.ripe) @@ -107,7 +106,7 @@ class BitmessageAddressTest : TestBase() { try { address.pubkey = pubkey } catch (e: Exception) { - fail(e.message) + fail<Unit>(e.message) } assertTrue(address.has(DOES_ACK)) diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/entity/ExtendedEncodingTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/entity/ExtendedEncodingTest.kt index bde06b0..53207a3 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/entity/ExtendedEncodingTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/entity/ExtendedEncodingTest.kt @@ -23,9 +23,9 @@ import ch.dissem.bitmessage.entity.valueobject.extended.Vote import ch.dissem.bitmessage.factory.ExtendedEncodingFactory import ch.dissem.bitmessage.utils.Bytes import ch.dissem.bitmessage.utils.TestUtils -import org.hamcrest.Matchers.* -import org.junit.Assert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Assumptions +import org.junit.jupiter.api.Test /** * @author Christian Basler @@ -33,12 +33,15 @@ import org.junit.Test class ExtendedEncodingTest { @Test fun `ensure simple message is decoded`() { - val extended = ExtendedEncodingFactory.unzip(Bytes.fromHex("78da9d59dd8e1bb715ee359fa097c4b65b498876babb76d0760bd970368eedb68e8daed3c0700c81d25012b333c3e99063ad1c0430d0a728d002bd289077caa59fa4df39244723693705b2f0da339c730ebff3c3f343ffe33f3f94da39b5d4ff9dd97cf3e32f9d10524afe4b3a2b57762dfd4acb453bbf967355c967b2755a1a2f1f0afe791eb85f2a7c374e2ab950ce8fe5dc96b59a7b3933956a36d2e9c6a8c2bc57ded84a2e6c532a10b9d678352b342d48674a53a846e6ca2be9adfcd3d58b2f33f96a05a190748d3d64ddd87726d74e5ebedcf81504417a6eaaa563018d56f4225595cb75633c3df7d191e04c3cab9c574521c4af656d6a69c2ab2cdd927639a959b0102f372f3742ecae6ef7afdb0668c2a229eb4297baf2ac5b2689932d4576f2809f89af01d3ae61d9af57ba921bdbd2f78f1ffee999269a2837ce3766d692943113555ae7648984f16fc6b5aa9057becd8d95d038ca95579fff5902497ccdf00064ad97fac6ebcab1bcd69139eec64dfcc9aa4d5b39e90abb2e3699105f60a3f8e13cfbdd583e37f3c63abbf009cfe5279fc84bb8db14ba61476ca929221a8d58c03e399471b660fd76c5decb3ebd456c54f3fcf4ec53125fb695f11bf93837016e231fdfd40ddcdb2d91cd679a4cca569bb5a6c8b72690a5cddb4267e22945b42532f1a2d2276e653d0798fc8d6c2b7a10e22b78859e66ac0e3da5b80a14613d3ce34b96a2671b20795bd68e390aab723ce14f615408543e1bdecc4c411aade12cf9ad034222afcdfc9a500a96472b248a313882bd80954f0a73ada59d7dabe73e8b30babde23b766a6c79482ec483070fc8f7b6f109362fc5e78cf51bbe391bcbf3b1bcf7762406dfdcfce1de3737a767f83dc7efbdc10e7db4c8703a121d93101154c2128911f2b043d3a80da912bccf6b85a19c81d80f07273083c4b788d10b71eb86b3436014e67a4ac2265fa8c2e99118464823215ee344c1d92d8242156bb5a1b482e8a1e496b8e4b5deac6d038266d9d2d1c8e4954628e88633563547e273aed514d485e20413222970db9ae370a6717460e8bf221fb1f8dccee968839abc6fb1d4445ae4862b8fbc55d2c72e9e1080fc0832cea81f3ffccbed51e9e6e3877f67f299df9ab82dbc81b5a2a3a3ff6da565e09543eccd6bb38da7141642951036b65dae90d2c14319a7d4f04b3e82067b61c2dc389271f93392f3ec8510b3762127e9753812a4a541d6928daa967a78767a3abaa07a02ba8c12b31eee065b2033a3d18865654eebebe1e9284511cc30e9bc9f2c330461d82912e5b46162e0eda0a2a97cf71d393d98f7b79da1a55dc879eb3cd40a5567536b216054327b81ea575be70c1528b8997822eb011372ee53dd68e6ab907314e556f63648b437a5ceba87ceac69e5c0ce0211b5688b696e504027f23b56e6c8e44717f26c1c5ee670a9d7b472b84165d7c3d1587c2f44ae173247facdf5347d1d223c8237a459c8d960da7d994e0764427c0f9fe9072f0070b80542aa4ed2de1c2937c5c2d1dbb13c3a7e7d5c1ee7af8e9f5e1c3fbf38beca8e17472396d668df362c3dc0d2d54fc0328e4b1ece1bad8f0f018cb610a3e0ef7635b990af9a568fe52060c33b0411ec05ef762bceef0f808a1039c911bb51db731210ea85c2019ceca93512d40030cd542d15cc3b39c863bd3dc6f1f04e57d65e4ff63c37daa605ce993d5264cda2985160a2d86af34e53d620817fe4fc13296b651ab7475f221593ad2955f56a6714032921b12d28339ebc53454b85116232f1980a2be70f8aff9f776a6257421851a779edee021597b87cf042886eb67b0c9f2ccbe41d21c46c19ff3de25a49f1401b928de5642207f9602ba01708c95dd0f715c887f7cfc781d7869c3e44d64a6c8d32e81c88ec71d3d86678f455758dd358b16617f2b83992c792f104a64e0d68cf8e19121a8e77b5ab4e4279ff7c17a44240f5541b428bd12e4146593b4265b9b729a9b62a8485a46d0fcf1630f9ef60dbb17c739651a9cdeea36920ba9896f7cf0db16f0f4cfc377074a9fcae5332ee2c35490fa31ea6c936d9d3f117e251fe8e4220dfd65658b2f28d45ffff8873b52abc6e2ad4f2771cae284e8d0a1d78578353418d1533cac2f4726dea314f1d53b6c2748547d05374f16aa9eab4166aaaa37986eb428935bfb64cc725a3f206f5238e62a14ed3b108b59b7671085542596052d0719c72a92d0d5e4b6709c430e9987a64b3ac2c771ec687bdd1b6f8b877c4945c4e9c555bce48634464980d1c95046e916a74adc97e4864ba19d378e8570a63869aaf1203d19359c9208c603771f067641d182eb5ebc9e73402c2a23557f2dcc0d498027802794cf28109389cee6053ea0a5d1491f1c4a7196a94c7218139954cccadb3daa6bd455bcd8913ade55d4d0d67973492e961ea2cc672a9fd144d229e6315aa487527bbe06bb23def0f4309dc6b8b025baf92fd0acd660127036890cf1ca4d1127eafc8921d2d9efb1b8687b80ffd4401931eda2178466267b72af4a7c13d34a224dba53884d50273c7c6cd636828fb2d5ffadc2122570eb7c4a1efdbc7874eafcafb5440cb6028a98a2f2dd645ec9c29cae3a01cbb34d4e882bcbc71e36efecaad7661b026d781af356e256f17415307e6a238839e85e3411fe265020d9714d07771a3da55aa84b11a45dd7e824061fdff37e73d501a32f919861ec3b5b3c220da87749e754f52e5396731c38520c80cbc69df467768c86d20e13d03e3cc841ac41303b69ae9b9a2ab876badebdbc6513e3bb1c784cbd9d28599217f91f8b572a153e0f8a4ca5dda86ce9eaaa28eb0c765db00922f9005d62b4ca17b372cd2b5351d38a8add71dbeae2e48a73dcf575b8fc66b271e89b96a3b06032c61a0496d0c1245b2d390ac00d1e99e81669ad7e1f645ce57740af98286340c9d088d72c032252c132a2169a62321a11ad0fedce5817e8221acf58b93dfe3df1e65aa1c77f7327794c637b381ab554973ec402f9783b7e34344a35b87e1ae3c266403c68566e0cd9e482152ab25c42b1e5ebbde6b1c26e2bd8e27d6bfd4ccfd0cb56e6ba166839bcdfbf79bc1e8a7f419897e1f32213e8acc49e2dd7a33064708de1842eca9d82584413e4ee694583876f742f2343bcf6e4854837e96b2c1c1f77b72a5d028385b62bb2a1d1c04778826b4c5cc971084c44a0179d705031d468e332e43d82093bd7b8aee82a223d7377561e606c78a15a062b65690b77386b3eda4d05506b606e38ecd782aa34842a16f75f245eb5f2c3ee7b2b93d4b57ded6cf525794edbe12fa2e1184d689eecb1aebeddc16d25654bed9da6a46f791bd8b14f1e41239a27705f4e412394b514258d325e9b2b16bce9b4561e79c86d27d17df08975a55aed7d5513310521a7e0bba157c72194c99a26339cf9096e9b619c6e02db6dc05ecdb355f99d8bbd011e22ff462822bb609eaefada61b214ee73054d0252465beb7228ec22c57dc6f516e2499bba076afab62a2dce26298bbb74ff1d8a98aaef27ae6e311b57fabcc0a203952a740beefdf849351d7e9266e119a3452012d4bcd85650f57a4df02732d3ab2642ff9a80a775aeb70afb74716d38749d3dec1349a891f5ccb8b3ffee2ac1b2b5326eb5d6c73d7e0a43c917c5548ff35f1f0e1ff00b5629026")) - assertThat<ExtendedEncoding>(extended, instanceOf(ExtendedEncoding::class.java)) - assertThat(extended!!.content, instanceOf<Any>(Message::class.java)) - assertThat((extended.content as Message).subject, `is`("Extended encoding on Windows works - but how ??")) - assertThat((extended.content as Message).body, notNullValue()) - assertThat((extended.content as Message).body.length, `is`(6233)) + val extended = + ExtendedEncodingFactory.unzip(Bytes.fromHex("78da9d59dd8e1bb715ee359fa097c4b65b498876babb76d0760bd970368eedb68e8daed3c0700c81d25012b333c3e99063ad1c0430d0a728d002bd289077caa59fa4df39244723693705b2f0da339c730ebff3c3f343ffe33f3f94da39b5d4ff9dd97cf3e32f9d10524afe4b3a2b57762dfd4acb453bbf967355c967b2755a1a2f1f0afe791eb85f2a7c374e2ab950ce8fe5dc96b59a7b3933956a36d2e9c6a8c2bc57ded84a2e6c532a10b9d678352b342d48674a53a846e6ca2be9adfcd3d58b2f33f96a05a190748d3d64ddd87726d74e5ebedcf81504417a6eaaa563018d56f4225595cb75633c3df7d191e04c3cab9c574521c4af656d6a69c2ab2cdd927639a959b0102f372f3742ecae6ef7afdb0668c2a229eb4297baf2ac5b2689932d4576f2809f89af01d3ae61d9af57ba921bdbd2f78f1ffee999269a2837ce3766d692943113555ae7648984f16fc6b5aa9057becd8d95d038ca95579fff5902497ccdf00064ad97fac6ebcab1bcd69139eec64dfcc9aa4d5b39e90abb2e3699105f60a3f8e13cfbdd583e37f3c63abbf009cfe5279fc84bb8db14ba61476ca929221a8d58c03e399471b660fd76c5decb3ebd456c54f3fcf4ec53125fb695f11bf93837016e231fdfd40ddcdb2d91cd679a4cca569bb5a6c8b72690a5cddb4267e22945b42532f1a2d2276e653d0798fc8d6c2b7a10e22b78859e66ac0e3da5b80a14613d3ce34b96a2671b20795bd68e390aab723ce14f615408543e1bdecc4c411aade12cf9ad034222afcdfc9a500a96472b248a313882bd80954f0a73ada59d7dabe73e8b30babde23b766a6c79482ec483070fc8f7b6f109362fc5e78cf51bbe391bcbf3b1bcf7762406dfdcfce1de3737a767f83dc7efbdc10e7db4c8703a121d93101154c2128911f2b043d3a80da912bccf6b85a19c81d80f07273083c4b788d10b71eb86b3436014e67a4ac2265fa8c2e99118464823215ee344c1d92d8242156bb5a1b482e8a1e496b8e4b5deac6d038266d9d2d1c8e4954628e88633563547e273aed514d485e20413222970db9ae370a6717460e8bf221fb1f8dccee968839abc6fb1d4445ae4862b8fbc55d2c72e9e1080fc0832cea81f3ffccbed51e9e6e3877f67f299df9ab82dbc81b5a2a3a3ff6da565e09543eccd6bb38da7141642951036b65dae90d2c14319a7d4f04b3e82067b61c2dc389271f93392f3ec8510b3762127e9753812a4a541d6928daa967a78767a3abaa07a02ba8c12b31eee065b2033a3d18865654eebebe1e9284511cc30e9bc9f2c330461d82912e5b46162e0eda0a2a97cf71d393d98f7b79da1a55dc879eb3cd40a5567536b216054327b81ea575be70c1528b8997822eb011372ee53dd68e6ab907314e556f63648b437a5ceba87ceac69e5c0ce0211b5688b696e504027f23b56e6c8e44717f26c1c5ee670a9d7b472b84165d7c3d1587c2f44ae173247facdf5347d1d223c8237a459c8d960da7d994e0764427c0f9fe9072f0070b80542aa4ed2de1c2937c5c2d1dbb13c3a7e7d5c1ee7af8e9f5e1c3fbf38beca8e17472396d668df362c3dc0d2d54fc0328e4b1ece1bad8f0f018cb610a3e0ef7635b990af9a568fe52060c33b0411ec05ef762bceef0f808a1039c911bb51db731210ea85c2019ceca93512d40030cd542d15cc3b39c863bd3dc6f1f04e57d65e4ff63c37daa605ce993d5264cda2985160a2d86af34e53d620817fe4fc13296b651ab7475f221593ad2955f56a6714032921b12d28339ebc53454b85116232f1980a2be70f8aff9f776a6257421851a779edee021597b87cf042886eb67b0c9f2ccbe41d21c46c19ff3de25a49f1401b928de5642207f9602ba01708c95dd0f715c887f7cfc781d7869c3e44d64a6c8d32e81c88ec71d3d86678f455758dd358b16617f2b83992c792f104a64e0d68cf8e19121a8e77b5ab4e4279ff7c17a44240f5541b428bd12e4146593b4265b9b729a9b62a8485a46d0fcf1630f9ef60dbb17c739651a9cdeea36920ba9896f7cf0db16f0f4cfc377074a9fcae5332ee2c35490fa31ea6c936d9d3f117e251fe8e4220dfd65658b2f28d45ffff8873b52abc6e2ad4f2771cae284e8d0a1d78578353418d1533cac2f4726dea314f1d53b6c2748547d05374f16aa9eab4166aaaa37986eb428935bfb64cc725a3f206f5238e62a14ed3b108b59b7671085542596052d0719c72a92d0d5e4b6709c430e9987a64b3ac2c771ec687bdd1b6f8b877c4945c4e9c555bce48634464980d1c95046e916a74adc97e4864ba19d378e8570a63869aaf1203d19359c9208c603771f067641d182eb5ebc9e73402c2a23557f2dcc0d498027802794cf28109389cee6053ea0a5d1491f1c4a7196a94c7218139954cccadb3daa6bd455bcd8913ade55d4d0d67973492e961ea2cc672a9fd144d229e6315aa487527bbe06bb23def0f4309dc6b8b025baf92fd0acd660127036890cf1ca4d1127eafc8921d2d9efb1b8687b80ffd4401931eda2178466267b72af4a7c13d34a224dba53884d50273c7c6cd636828fb2d5ffadc2122570eb7c4a1efdbc7874eafcafb5440cb6028a98a2f2dd645ec9c29cae3a01cbb34d4e882bcbc71e36efecaad7661b026d781af356e256f17415307e6a238839e85e3411fe265020d9714d07771a3da55aa84b11a45dd7e824061fdff37e73d501a32f919861ec3b5b3c220da87749e754f52e5396731c38520c80cbc69df467768c86d20e13d03e3cc841ac41303b69ae9b9a2ab876badebdbc6513e3bb1c784cbd9d28599217f91f8b572a153e0f8a4ca5dda86ce9eaaa28eb0c765db00922f9005d62b4ca17b372cd2b5351d38a8add71dbeae2e48a73dcf575b8fc66b271e89b96a3b06032c61a0496d0c1245b2d390ac00d1e99e81669ad7e1f645ce57740af98286340c9d088d72c032252c132a2169a62321a11ad0fedce5817e8221acf58b93dfe3df1e65aa1c77f7327794c637b381ab554973ec402f9783b7e34344a35b87e1ae3c266403c68566e0cd9e482152ab25c42b1e5ebbde6b1c26e2bd8e27d6bfd4ccfd0cb56e6ba166839bcdfbf79bc1e8a7f419897e1f32213e8acc49e2dd7a33064708de1842eca9d82584413e4ee694583876f742f2343bcf6e4854837e96b2c1c1f77b72a5d028385b62bb2a1d1c04778826b4c5cc971084c44a0179d705031d468e332e43d82093bd7b8aee82a223d7377561e606c78a15a062b65690b77386b3eda4d05506b606e38ecd782aa34842a16f75f245eb5f2c3ee7b2b93d4b57ded6cf525794edbe12fa2e1184d689eecb1aebeddc16d25654bed9da6a46f791bd8b14f1e41239a27705f4e412394b514258d325e9b2b16bce9b4561e79c86d27d17df08975a55aed7d5513310521a7e0bba157c72194c99a26339cf9096e9b619c6e02db6dc05ecdb355f99d8bbd011e22ff462822bb609eaefada61b214ee73054d0252465beb7228ec22c57dc6f516e2499bba076afab62a2dce26298bbb74ff1d8a98aaef27ae6e311b57fabcc0a203952a740beefdf849351d7e9266e119a3452012d4bcd85650f57a4df02732d3ab2642ff9a80a775aeb70afb74716d38749d3dec1349a891f5ccb8b3ffee2ac1b2b5326eb5d6c73d7e0a43c917c5548ff35f1f0e1ff00b5629026")) + assertTrue(extended is ExtendedEncoding) + assertTrue(extended?.content is Message) + (extended?.content as Message).apply { + assertEquals("Extended encoding on Windows works - but how ??", subject) + assertNotNull(body) + assertEquals(6233, body.length) + } } @@ -49,10 +52,7 @@ class ExtendedEncodingTest { .body("test bödy") .build() - assertThat(input.zip(), notNullValue()) - - val out = ExtendedEncodingFactory.unzip(input.zip()) - assertThat<ExtendedEncoding>(out, `is`(input)) + assertEquals(input, ExtendedEncodingFactory.unzip(input.zip())) } @Test @@ -72,10 +72,7 @@ class ExtendedEncodingTest { ) .build() - assertThat(input.zip(), notNullValue()) - - val out = ExtendedEncodingFactory.unzip(input.zip()) - assertThat<ExtendedEncoding>(out, `is`(input)) + assertEquals(input, ExtendedEncodingFactory.unzip(input.zip())) } @Test @@ -85,9 +82,6 @@ class ExtendedEncodingTest { .vote("+1") .build() - assertThat(input.zip(), notNullValue()) - - val out = ExtendedEncodingFactory.unzip(input.zip()) - assertThat<ExtendedEncoding>(out, `is`(input)) + assertEquals(input, ExtendedEncodingFactory.unzip(input.zip())) } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/entity/SerializationTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/entity/SerializationTest.kt index fe42b84..857c1dc 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/entity/SerializationTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/entity/SerializationTest.kt @@ -24,9 +24,8 @@ import ch.dissem.bitmessage.entity.valueobject.extended.Message import ch.dissem.bitmessage.factory.Factory import ch.dissem.bitmessage.utils.TestBase import ch.dissem.bitmessage.utils.TestUtils -import org.hamcrest.Matchers.`is` -import org.junit.Assert.* -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.ObjectInputStream @@ -95,7 +94,7 @@ class SerializationTest : TestBase() { received.isAccessible = true received.set(actual, null) - assertThat(expected, `is`(actual)) + assertEquals(actual, expected) } @Test @@ -103,10 +102,12 @@ class SerializationTest : TestBase() { val expected = Plaintext.Builder(MSG) .from(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8")) .to(TestUtils.loadContact()) - .message(Message.Builder() - .subject("Subject") - .body("Message") - .build()) + .message( + Message.Builder() + .subject("Subject") + .body("Message") + .build() + ) .ackData("ackMessage".toByteArray()) .signature(ByteArray(0)) .build() @@ -127,10 +128,12 @@ class SerializationTest : TestBase() { fun `ensure plaintext without recipient can be serialized (needed for saving drafts)`() { val expected = Plaintext.Builder(MSG) .from(TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8")) - .message(Message.Builder() - .subject("Subject") - .body("Message") - .build()) + .message( + Message.Builder() + .subject("Subject") + .body("Message") + .build() + ) .signature(ByteArray(0)) .status(Plaintext.Status.DRAFT) .build() diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/ports/ProofOfWorkEngineTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/ports/ProofOfWorkEngineTest.kt index 2bd3a0a..bfc0c8c 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/ports/ProofOfWorkEngineTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/ports/ProofOfWorkEngineTest.kt @@ -20,18 +20,24 @@ import ch.dissem.bitmessage.utils.Bytes import ch.dissem.bitmessage.utils.CallbackWaiter import ch.dissem.bitmessage.utils.Singleton.cryptography import ch.dissem.bitmessage.utils.TestBase -import org.junit.Assert.assertTrue -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertTimeoutPreemptively +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.Test +import java.time.Duration.ofSeconds class ProofOfWorkEngineTest : TestBase() { - @Test(timeout = 90000) + @Test fun `test SimplePOWEngine`() { - testPOW(SimplePOWEngine()) + assertTimeoutPreemptively(ofSeconds(90)) { + testPOW(SimplePOWEngine()) + } } - @Test(timeout = 90000) + @Test fun `test MultiThreadedPOWEngine`() { - testPOW(MultiThreadedPOWEngine()) + assertTimeoutPreemptively(ofSeconds(90)) { + testPOW(MultiThreadedPOWEngine()) + } } private fun testPOW(engine: ProofOfWorkEngine) { @@ -65,6 +71,6 @@ class ProofOfWorkEngineTest : TestBase() { val nonce2 = waiter2.waitForValue()!! println("Calculating nonce1 took ${waiter2.time}ms") assertTrue(Bytes.lt(cryptography().doubleSha512(nonce2, initialHash2), target2, 8)) - assertTrue("Second nonce1 must be quicker to find", waiter1.time > waiter2.time) + assertTrue(waiter1.time > waiter2.time, "Second nonce1 must be quicker to find") } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/Base64Test.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/Base64Test.kt index 35f9e3a..2869df0 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/Base64Test.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/Base64Test.kt @@ -17,9 +17,8 @@ package ch.dissem.bitmessage.utils import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography -import org.hamcrest.Matchers.`is` -import org.junit.Assert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class Base64Test { @Test @@ -29,7 +28,7 @@ class Base64Test { val data = cryptography.randomBytes(i) val string = Base64.encodeToString(data) val decoded = Base64.decode(string) - assertThat(decoded, `is`(data)) + assertEquals(data, decoded) } } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/BytesTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/BytesTest.kt index cabd0b9..545ec4e 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/BytesTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/BytesTest.kt @@ -16,10 +16,10 @@ package ch.dissem.bitmessage.utils -import org.junit.Assert.assertArrayEquals -import org.junit.Assert.assertEquals -import org.junit.Ignore -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertArrayEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Disabled +import org.junit.jupiter.api.Test import java.math.BigInteger import java.util.* @@ -46,7 +46,11 @@ class BytesTest { for (i in 1..255) { val bytes = byteArrayOf(0, v.toByte()) Bytes.inc(bytes, i.toByte()) - assertArrayEquals("value = " + v + "; inc = " + i + "; expected = " + (v + i), TestUtils.int16(v + i), bytes) + assertArrayEquals( + TestUtils.int16(v + i), + bytes, + "value = " + v + "; inc = " + i + "; expected = " + (v + i) + ) } } } @@ -55,7 +59,7 @@ class BytesTest { * This test is used to compare different implementations of the single byte lt comparison. It an safely be ignored. */ @Test - @Ignore + @Disabled fun `test lower than single byte`() { val a = ByteArray(1) val b = ByteArray(1) @@ -85,10 +89,13 @@ class BytesTest { val a = BigInteger.valueOf(rnd.nextLong()).pow(rnd.nextInt(5) + 1).abs() val b = BigInteger.valueOf(rnd.nextLong()).pow(rnd.nextInt(5) + 1).abs() println("a = " + a.toString(16) + "\tb = " + b.toString(16)) - assertEquals(a.compareTo(b) == -1, Bytes.lt( - Bytes.expand(a.toByteArray(), 100), - Bytes.expand(b.toByteArray(), 100), - 100)) + assertEquals( + a.compareTo(b) == -1, Bytes.lt( + Bytes.expand(a.toByteArray(), 100), + Bytes.expand(b.toByteArray(), 100), + 100 + ) + ) } } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/CollectionsTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/CollectionsTest.kt index bfb3fe5..bd7a775 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/CollectionsTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/CollectionsTest.kt @@ -16,17 +16,12 @@ package ch.dissem.bitmessage.utils -import org.junit.Test - -import java.util.LinkedList - -import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class CollectionsTest { @Test fun `ensure select random returns maximum possible items`() { - val list = LinkedList<Int>() - list += 0..9 - assertEquals(9, Collections.selectRandom(9, list).size) + assertEquals(9, Collections.selectRandom(9, listOf(0..9)).size) } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/ConversationServiceTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/ConversationServiceTest.kt index f960086..8abaf03 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/ConversationServiceTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/ConversationServiceTest.kt @@ -24,9 +24,8 @@ import ch.dissem.bitmessage.entity.valueobject.extended.Message import ch.dissem.bitmessage.ports.MessageRepository import ch.dissem.bitmessage.utils.TestUtils.RANDOM import com.nhaarman.mockito_kotlin.* -import org.hamcrest.Matchers.`is` -import org.junit.Assert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Test import java.util.* class ConversationServiceTest : TestBase() { @@ -45,7 +44,7 @@ class ConversationServiceTest : TestBase() { doReturn(expected).whenever(conversationService).getConversation(any<UUID>()) val actual = conversationService.getConversation(UUID.randomUUID()) - assertThat(actual, `is`(expected)) + Assertions.assertEquals(expected, actual) } companion object { @@ -53,57 +52,69 @@ class ConversationServiceTest : TestBase() { fun conversation(alice: BitmessageAddress, bob: BitmessageAddress): List<Plaintext> { val result = LinkedList<Plaintext>() - val older = plaintext(alice, bob, + val older = plaintext( + alice, bob, Message.Builder() .subject("hey there") .body("does it work?") .build(), - Plaintext.Status.SENT) + Plaintext.Status.SENT + ) result.add(older) - val root = plaintext(alice, bob, + val root = plaintext( + alice, bob, Message.Builder() .subject("new test") .body("There's a new test in town!") .build(), - Plaintext.Status.SENT) + Plaintext.Status.SENT + ) result.add(root) result.add( - plaintext(bob, alice, + plaintext( + bob, alice, Message.Builder() .subject("Re: new test (1a)") .body("Nice!") .addParent(root) .build(), - Plaintext.Status.RECEIVED) + Plaintext.Status.RECEIVED + ) ) - val latest = plaintext(bob, alice, + val latest = plaintext( + bob, alice, Message.Builder() .subject("Re: new test (2b)") .body("PS: it did work!") .addParent(root) .addParent(older) .build(), - Plaintext.Status.RECEIVED) + Plaintext.Status.RECEIVED + ) result.add(latest) result.add( - plaintext(alice, bob, + plaintext( + alice, bob, Message.Builder() .subject("Re: new test (2)") .body("") .addParent(latest) .build(), - Plaintext.Status.DRAFT) + Plaintext.Status.DRAFT + ) ) return result } - fun plaintext(from: BitmessageAddress, to: BitmessageAddress, - content: ExtendedEncoding, status: Plaintext.Status): Plaintext { + fun plaintext( + from: BitmessageAddress, to: BitmessageAddress, + content: ExtendedEncoding, status: Plaintext.Status + ): Plaintext { val builder = Plaintext.Builder(MSG) .IV(TestUtils.randomInventoryVector()) .from(from) diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/DecodeTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/DecodeTest.kt index d80f5f8..c0f8576 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/DecodeTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/DecodeTest.kt @@ -16,8 +16,8 @@ package ch.dissem.bitmessage.utils -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/EncodeTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/EncodeTest.kt index 0fa4361..26c2185 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/EncodeTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/EncodeTest.kt @@ -16,8 +16,9 @@ package ch.dissem.bitmessage.utils -import org.junit.Assert.assertEquals -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assumptions +import org.junit.jupiter.api.Test import java.io.ByteArrayOutputStream class EncodeTest { @@ -111,7 +112,8 @@ class EncodeTest { fun checkBytes(stream: ByteArrayOutputStream, vararg bytes: Int) { - assertEquals(bytes.size, stream.size()) + Assumptions.assumeTrue(bytes.size == stream.size()) + val streamBytes = stream.toByteArray() for (i in bytes.indices) { diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/SqlStringsTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/SqlStringsTest.kt index 1097e01..5e15c4d 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/SqlStringsTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/SqlStringsTest.kt @@ -16,14 +16,12 @@ package ch.dissem.bitmessage.utils -import org.junit.Test - -import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class SqlStringsTest { @Test fun `ensure join works with long array`() { - val test = longArrayOf(1L, 2L) - assertEquals("1, 2", SqlStrings.join(*test)) + assertEquals("1, 2", SqlStrings.join(1L, 2L)) } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/StringsTest.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/StringsTest.kt index 815d8e8..60db9a0 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/StringsTest.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/StringsTest.kt @@ -16,9 +16,8 @@ package ch.dissem.bitmessage.utils -import org.junit.Test - -import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class StringsTest { @Test diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestBase.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestBase.kt index 90e2b2d..0e191f2 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestBase.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestBase.kt @@ -17,15 +17,16 @@ package ch.dissem.bitmessage.utils import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography -import org.junit.BeforeClass +import org.junit.jupiter.api.BeforeAll /** * @author Christian Basler */ open class TestBase { companion object { - @BeforeClass - @JvmStatic fun setUpClass() { + @BeforeAll + @JvmStatic + fun setUpClass() { Singleton.initialize(BouncyCryptography()) } } diff --git a/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestUtils.kt b/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestUtils.kt index fb80804..8dde45d 100644 --- a/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestUtils.kt +++ b/core/src/test/kotlin/ch/dissem/bitmessage/utils/TestUtils.kt @@ -29,7 +29,7 @@ import ch.dissem.bitmessage.factory.Factory import ch.dissem.bitmessage.ports.* import com.nhaarman.mockito_kotlin.mock import com.nhaarman.mockito_kotlin.spy -import org.junit.Assert.assertEquals +import org.junit.jupiter.api.Assertions.assertEquals import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.InputStream @@ -54,7 +54,8 @@ object TestUtils { fun loadObjectMessage(version: Int, resourceName: String): ObjectMessage { val data = getBytes(resourceName) val input = ByteArrayInputStream(data) - return Factory.getObjectMessage(version, input, data.size) ?: throw NoSuchElementException("error loading object message") + return Factory.getObjectMessage(version, input, data.size) + ?: throw NoSuchElementException("error loading object message") } @JvmStatic diff --git a/cryptography-bc/build.gradle b/cryptography-bc/build.gradle index 8fb1175..7fd5d35 100644 --- a/cryptography-bc/build.gradle +++ b/cryptography-bc/build.gradle @@ -13,7 +13,8 @@ uploadArchives { dependencies { compile project(':core') compile 'org.bouncycastle:bcprov-jdk15on' - testCompile 'junit:junit' testCompile 'com.nhaarman:mockito-kotlin' + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' testCompile project(path: ':core', configuration: 'testArtifacts') } diff --git a/cryptography-bc/src/test/kotlin/ch/dissem/bitmessage/security/CryptographyTest.kt b/cryptography-bc/src/test/kotlin/ch/dissem/bitmessage/security/CryptographyTest.kt index 6d4a05d..f14ae39 100644 --- a/cryptography-bc/src/test/kotlin/ch/dissem/bitmessage/security/CryptographyTest.kt +++ b/cryptography-bc/src/test/kotlin/ch/dissem/bitmessage/security/CryptographyTest.kt @@ -22,14 +22,15 @@ import ch.dissem.bitmessage.entity.payload.GenericPayload import ch.dissem.bitmessage.entity.valueobject.PrivateKey import ch.dissem.bitmessage.exception.InsufficientProofOfWorkException import ch.dissem.bitmessage.ports.MultiThreadedPOWEngine -import ch.dissem.bitmessage.ports.ProofOfWorkEngine -import ch.dissem.bitmessage.utils.* +import ch.dissem.bitmessage.utils.CallbackWaiter +import ch.dissem.bitmessage.utils.Singleton +import ch.dissem.bitmessage.utils.TestUtils +import ch.dissem.bitmessage.utils.UnixTime import ch.dissem.bitmessage.utils.UnixTime.DAY import ch.dissem.bitmessage.utils.UnixTime.MINUTE import ch.dissem.bitmessage.utils.UnixTime.now -import org.hamcrest.CoreMatchers.`is` -import org.junit.Assert.* -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test import java.io.ByteArrayInputStream import javax.xml.bind.DatatypeConverter @@ -63,7 +64,7 @@ class CryptographyTest { assertArrayEquals(crypto.sha512(TEST_SHA512), crypto.doubleSha512(TEST_VALUE)) } - @Test(expected = InsufficientProofOfWorkException::class) + @Test fun `ensure exception for insufficient proof of work`() { val objectMessage = ObjectMessage.Builder() .nonce(ByteArray(8)) @@ -71,7 +72,9 @@ class CryptographyTest { .objectType(0) .payload(GenericPayload.read(0, 1, ByteArrayInputStream(ByteArray(0)), 0)) .build() - crypto.checkProofOfWork(objectMessage, 1000, 1000) + assertThrows(InsufficientProofOfWorkException::class.java) { + crypto.checkProofOfWork(objectMessage, 1000, 1000) + } } @Test @@ -94,7 +97,7 @@ class CryptographyTest { try { crypto.checkProofOfWork(objectMessage, 1000, 1000) } catch (e: InsufficientProofOfWorkException) { - fail(e.message) + fail<Unit>(e.message) } } @@ -108,12 +111,14 @@ class CryptographyTest { assertArrayEquals(data, decrypted) } - @Test(expected = IllegalArgumentException::class) + @Test fun `ensure decryption fails with invalid cypher text`() { val data = crypto.randomBytes(128) val key_e = crypto.randomBytes(32) val iv = crypto.randomBytes(16) - crypto.crypt(false, data, key_e, iv) + assertThrows(IllegalArgumentException::class.java) { + crypto.crypt(false, data, key_e, iv) + } } @Test @@ -132,7 +137,7 @@ class CryptographyTest { val data = crypto.randomBytes(100) val privateKey = PrivateKey(false, 1, 1000, 1000) val signature = crypto.getSignature(data, privateKey) - assertThat(crypto.isSignatureValid(data, signature, privateKey.pubkey), `is`(true)) + assertTrue(crypto.isSignatureValid(data, signature, privateKey.pubkey)) } @Test @@ -141,18 +146,24 @@ class CryptographyTest { val privateKey = PrivateKey(false, 1, 1000, 1000) val signature = crypto.getSignature(data, privateKey) data[0]++ - assertThat(crypto.isSignatureValid(data, signature, privateKey.pubkey), `is`(false)) + assertFalse(crypto.isSignatureValid(data, signature, privateKey.pubkey)) } companion object { val TEST_VALUE = "teststring".toByteArray() - val TEST_SHA1 = DatatypeConverter.parseHexBinary("" - + "b8473b86d4c2072ca9b08bd28e373e8253e865c4") - val TEST_SHA512 = DatatypeConverter.parseHexBinary("" - + "6253b39071e5df8b5098f59202d414c37a17d6a38a875ef5f8c7d89b0212b028" - + "692d3d2090ce03ae1de66c862fa8a561e57ed9eb7935ce627344f742c0931d72") - val TEST_RIPEMD160 = DatatypeConverter.parseHexBinary("" - + "cd566972b5e50104011a92b59fa8e0b1234851ae") + val TEST_SHA1 = DatatypeConverter.parseHexBinary( + "" + + "b8473b86d4c2072ca9b08bd28e373e8253e865c4" + ) + val TEST_SHA512 = DatatypeConverter.parseHexBinary( + "" + + "6253b39071e5df8b5098f59202d414c37a17d6a38a875ef5f8c7d89b0212b028" + + "692d3d2090ce03ae1de66c862fa8a561e57ed9eb7935ce627344f742c0931d72" + ) + val TEST_RIPEMD160 = DatatypeConverter.parseHexBinary( + "" + + "cd566972b5e50104011a92b59fa8e0b1234851ae" + ) private val crypto = BouncyCryptography() diff --git a/cryptography-sc/build.gradle b/cryptography-sc/build.gradle index 6e8085c..aec4fc7 100644 --- a/cryptography-sc/build.gradle +++ b/cryptography-sc/build.gradle @@ -13,7 +13,8 @@ uploadArchives { dependencies { compile project(':core') compile 'com.madgag.spongycastle:prov' - testCompile 'junit:junit' testCompile 'com.nhaarman:mockito-kotlin' + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' testCompile project(path: ':core', configuration: 'testArtifacts') } diff --git a/demo/build.gradle b/demo/build.gradle index 9631b6f..37eaa34 100644 --- a/demo/build.gradle +++ b/demo/build.gradle @@ -33,6 +33,7 @@ dependencies { compile 'args4j:args4j' compile 'com.h2database:h2' compile 'org.apache.commons:commons-text' - testCompile 'junit:junit' testCompile 'com.nhaarman:mockito-kotlin' + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' } diff --git a/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java b/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java deleted file mode 100644 index 238a986..0000000 --- a/demo/src/test/java/ch/dissem/bitmessage/SystemTest.java +++ /dev/null @@ -1,218 +0,0 @@ -/* - * Copyright 2016 Christian Basler - * - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -package ch.dissem.bitmessage; - -import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography; -import ch.dissem.bitmessage.entity.BitmessageAddress; -import ch.dissem.bitmessage.entity.Plaintext; -import ch.dissem.bitmessage.networking.nio.NioNetworkHandler; -import ch.dissem.bitmessage.ports.DefaultLabeler; -import ch.dissem.bitmessage.ports.Labeler; -import ch.dissem.bitmessage.repository.*; -import ch.dissem.bitmessage.utils.TTL; -import org.junit.After; -import org.junit.Before; -import org.junit.Test; -import org.slf4j.Logger; -import org.slf4j.LoggerFactory; - -import java.util.UUID; -import java.util.concurrent.TimeUnit; - -import static ch.dissem.bitmessage.entity.payload.Pubkey.Feature.DOES_ACK; -import static ch.dissem.bitmessage.utils.UnixTime.MINUTE; -import static com.nhaarman.mockito_kotlin.MockitoKt.*; -import static org.hamcrest.CoreMatchers.equalTo; -import static org.junit.Assert.assertThat; -import static org.mockito.ArgumentMatchers.any; - -/** - * @author Christian Basler - */ -public class SystemTest { - private static int port = 6000; - - private BitmessageContext alice; - private BitmessageAddress aliceIdentity; - private Labeler aliceLabeler; - - private BitmessageContext bob; - private TestListener bobListener; - private BitmessageAddress bobIdentity; - - @Before - public void setUp() { - TTL.msg(5 * MINUTE); - TTL.getpubkey(5 * MINUTE); - TTL.pubkey(5 * MINUTE); - - int alicePort = port++; - int bobPort = port++; - { - JdbcConfig aliceDB = new JdbcConfig("jdbc:h2:mem:alice;DB_CLOSE_DELAY=-1", "sa", ""); - aliceLabeler = spy(new DebugLabeler("Alice")); - TestListener aliceListener = new TestListener(); - alice = new BitmessageContext.Builder() - .addressRepo(new JdbcAddressRepository(aliceDB)) - .inventory(new JdbcInventory(aliceDB)) - .labelRepo(new JdbcLabelRepository(aliceDB)) - .messageRepo(new JdbcMessageRepository(aliceDB)) - .powRepo(new JdbcProofOfWorkRepository(aliceDB)) - .nodeRegistry(new TestNodeRegistry(bobPort)) - .networkHandler(new NioNetworkHandler()) - .cryptography(new BouncyCryptography()) - .listener(aliceListener) - .labeler(aliceLabeler) - .build(); - alice.internals().getPreferences().setPort(alicePort); - alice.startup(); - aliceIdentity = alice.createIdentity(false, DOES_ACK); - } - { - JdbcConfig bobDB = new JdbcConfig("jdbc:h2:mem:bob;DB_CLOSE_DELAY=-1", "sa", ""); - bobListener = new TestListener(); - bob = new BitmessageContext.Builder() - .addressRepo(new JdbcAddressRepository(bobDB)) - .inventory(new JdbcInventory(bobDB)) - .labelRepo(new JdbcLabelRepository(bobDB)) - .messageRepo(new JdbcMessageRepository(bobDB)) - .powRepo(new JdbcProofOfWorkRepository(bobDB)) - .nodeRegistry(new TestNodeRegistry(alicePort)) - .networkHandler(new NioNetworkHandler()) - .cryptography(new BouncyCryptography()) - .listener(bobListener) - .labeler(new DebugLabeler("Bob")) - .build(); - bob.internals().getPreferences().setPort(bobPort); - bob.startup(); - bobIdentity = bob.createIdentity(false, DOES_ACK); - } - ((DebugLabeler) alice.labeler()).init(aliceIdentity, bobIdentity); - ((DebugLabeler) bob.labeler()).init(aliceIdentity, bobIdentity); - } - - @After - public void tearDown() { - alice.shutdown(); - bob.shutdown(); - } - - @Test(timeout = 120_000) - public void ensureAliceCanSendMessageToBob() throws Exception { - String originalMessage = UUID.randomUUID().toString(); - alice.send(aliceIdentity, new BitmessageAddress(bobIdentity.getAddress()), "Subject", originalMessage); - - Plaintext plaintext = bobListener.get(2, TimeUnit.MINUTES); - - assertThat(plaintext.getType(), equalTo(Plaintext.Type.MSG)); - assertThat(plaintext.getText(), equalTo(originalMessage)); - - verify(aliceLabeler, timeout(TimeUnit.MINUTES.toMillis(2)).atLeastOnce()) - .markAsAcknowledged(any()); - } - - @Test(timeout = 30_000) - public void ensureBobCanReceiveBroadcastFromAlice() throws Exception { - String originalMessage = UUID.randomUUID().toString(); - bob.addSubscribtion(new BitmessageAddress(aliceIdentity.getAddress())); - alice.broadcast(aliceIdentity, "Subject", originalMessage); - - Plaintext plaintext = bobListener.get(15, TimeUnit.MINUTES); - - assertThat(plaintext.getType(), equalTo(Plaintext.Type.BROADCAST)); - assertThat(plaintext.getText(), equalTo(originalMessage)); - } - - private static class DebugLabeler extends DefaultLabeler { - private final Logger LOG = LoggerFactory.getLogger("Labeler"); - final String name; - String alice; - String bob; - - private DebugLabeler(String name) { - this.name = name; - } - - private void init(BitmessageAddress alice, BitmessageAddress bob) { - this.alice = alice.getAddress(); - this.bob = bob.getAddress(); - } - - @Override - public void setLabels(Plaintext msg) { - LOG.info(name + ": From " + name(msg.getFrom()) + ": Received"); - super.setLabels(msg); - } - - @Override - public void markAsDraft(Plaintext msg) { - LOG.info(name + ": From " + name(msg.getFrom()) + ": Draft"); - super.markAsDraft(msg); - } - - @Override - public void markAsSending(Plaintext msg) { - LOG.info(name + ": From " + name(msg.getFrom()) + ": Sending"); - super.markAsSending(msg); - } - - @Override - public void markAsSent(Plaintext msg) { - LOG.info(name + ": From " + name(msg.getFrom()) + ": Sent"); - super.markAsSent(msg); - } - - @Override - public void markAsAcknowledged(Plaintext msg) { - LOG.info(name + ": From " + name(msg.getFrom()) + ": Acknowledged"); - super.markAsAcknowledged(msg); - } - - @Override - public void markAsRead(Plaintext msg) { - LOG.info(name + ": From " + name(msg.getFrom()) + ": Read"); - super.markAsRead(msg); - } - - @Override - public void markAsUnread(Plaintext msg) { - LOG.info(name + ": From " + name(msg.getFrom()) + ": Unread"); - super.markAsUnread(msg); - } - - @Override - public void delete(Plaintext msg) { - LOG.info(name + ": From " + name(msg.getFrom()) + ": Cleared"); - super.delete(msg); - } - - @Override - public void archive(Plaintext msg) { - LOG.info(name + ": From " + name(msg.getFrom()) + ": Archived"); - super.archive(msg); - } - - private String name(BitmessageAddress address) { - if (alice.equals(address.getAddress())) - return "Alice"; - else if (bob.equals(address.getAddress())) - return "Bob"; - else - return "Unknown (" + address.getAddress() + ")"; - } - } -} diff --git a/demo/src/test/java/ch/dissem/bitmessage/SystemTest.kt b/demo/src/test/java/ch/dissem/bitmessage/SystemTest.kt new file mode 100644 index 0000000..e5ad40d --- /dev/null +++ b/demo/src/test/java/ch/dissem/bitmessage/SystemTest.kt @@ -0,0 +1,215 @@ +/* + * Copyright 2016 Christian Basler + * + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +package ch.dissem.bitmessage + +import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography +import ch.dissem.bitmessage.entity.BitmessageAddress +import ch.dissem.bitmessage.entity.Plaintext +import ch.dissem.bitmessage.entity.payload.Pubkey.Feature.DOES_ACK +import ch.dissem.bitmessage.networking.nio.NioNetworkHandler +import ch.dissem.bitmessage.ports.DefaultLabeler +import ch.dissem.bitmessage.ports.Labeler +import ch.dissem.bitmessage.repository.* +import ch.dissem.bitmessage.utils.TTL +import ch.dissem.bitmessage.utils.UnixTime.MINUTE +import com.nhaarman.mockito_kotlin.spy +import com.nhaarman.mockito_kotlin.timeout +import com.nhaarman.mockito_kotlin.verify +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTimeoutPreemptively +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test +import org.mockito.ArgumentMatchers.any +import org.slf4j.LoggerFactory +import java.time.Duration.ofMinutes +import java.time.Duration.ofSeconds +import java.util.* +import java.util.concurrent.TimeUnit + +/** + * @author Christian Basler + */ +class SystemTest { + + private lateinit var alice: BitmessageContext + private lateinit var aliceIdentity: BitmessageAddress + private lateinit var aliceLabeler: Labeler + + private lateinit var bob: BitmessageContext + private lateinit var bobListener: TestListener + private lateinit var bobIdentity: BitmessageAddress + + @BeforeEach + fun setUp() { + TTL.msg = 5 * MINUTE + TTL.getpubkey = 5 * MINUTE + TTL.pubkey = 5 * MINUTE + + val alicePort = port++ + val bobPort = port++ + run { + val aliceDB = JdbcConfig("jdbc:h2:mem:alice;DB_CLOSE_DELAY=-1", "sa", "") + aliceLabeler = spy(DebugLabeler("Alice")) + val aliceListener = TestListener() + alice = BitmessageContext.Builder() + .addressRepo(JdbcAddressRepository(aliceDB)) + .inventory(JdbcInventory(aliceDB)) + .labelRepo(JdbcLabelRepository(aliceDB)) + .messageRepo(JdbcMessageRepository(aliceDB)) + .powRepo(JdbcProofOfWorkRepository(aliceDB)) + .nodeRegistry(TestNodeRegistry(bobPort)) + .networkHandler(NioNetworkHandler()) + .cryptography(BouncyCryptography()) + .listener(aliceListener) + .labeler(aliceLabeler) + .build() + alice.internals.preferences.port = alicePort + alice.startup() + aliceIdentity = alice.createIdentity(false, DOES_ACK) + } + run { + val bobDB = JdbcConfig("jdbc:h2:mem:bob;DB_CLOSE_DELAY=-1", "sa", "") + bobListener = TestListener() + bob = BitmessageContext.Builder() + .addressRepo(JdbcAddressRepository(bobDB)) + .inventory(JdbcInventory(bobDB)) + .labelRepo(JdbcLabelRepository(bobDB)) + .messageRepo(JdbcMessageRepository(bobDB)) + .powRepo(JdbcProofOfWorkRepository(bobDB)) + .nodeRegistry(TestNodeRegistry(alicePort)) + .networkHandler(NioNetworkHandler()) + .cryptography(BouncyCryptography()) + .listener(bobListener) + .labeler(DebugLabeler("Bob")) + .build() + bob.internals.preferences.port = bobPort + bob.startup() + bobIdentity = bob.createIdentity(false, DOES_ACK) + } + (alice.labeler as DebugLabeler).init(aliceIdentity, bobIdentity) + (bob.labeler as DebugLabeler).init(aliceIdentity, bobIdentity) + } + + @AfterEach + fun tearDown() { + alice.shutdown() + bob.shutdown() + } + + @Test + @Throws(Exception::class) + fun ensureAliceCanSendMessageToBob() { + assertTimeoutPreemptively(ofMinutes(2)) { + val originalMessage = UUID.randomUUID().toString() + alice.send(aliceIdentity, BitmessageAddress(bobIdentity.address), "Subject", originalMessage) + + val plaintext = bobListener[2, TimeUnit.MINUTES] + + assertEquals(Plaintext.Type.MSG, plaintext.type) + assertEquals(originalMessage, plaintext.text) + + verify( + aliceLabeler, + timeout(TimeUnit.MINUTES.toMillis(2)).atLeastOnce() + ).markAsAcknowledged(any<Plaintext>()) + } + } + + @Test + @Throws(Exception::class) + fun ensureBobCanReceiveBroadcastFromAlice() { + assertTimeoutPreemptively(ofSeconds(30)) { + val originalMessage = UUID.randomUUID().toString() + bob.addSubscribtion(BitmessageAddress(aliceIdentity.address)) + alice.broadcast(aliceIdentity, "Subject", originalMessage) + + val plaintext = bobListener[15, TimeUnit.MINUTES] + + assertEquals(Plaintext.Type.BROADCAST, plaintext.type) + assertEquals(originalMessage, plaintext.text) + } + } + + internal class DebugLabeler internal constructor(private val name: String) : DefaultLabeler() { + private val LOG = LoggerFactory.getLogger("Labeler") + private lateinit var alice: String + private lateinit var bob: String + + internal fun init(alice: BitmessageAddress, bob: BitmessageAddress) { + this.alice = alice.address + this.bob = bob.address + } + + override fun setLabels(msg: Plaintext) { + LOG.info(name + ": From " + name(msg.from) + ": Received") + super.setLabels(msg) + } + + override fun markAsDraft(msg: Plaintext) { + LOG.info(name + ": From " + name(msg.from) + ": Draft") + super.markAsDraft(msg) + } + + override fun markAsSending(msg: Plaintext) { + LOG.info(name + ": From " + name(msg.from) + ": Sending") + super.markAsSending(msg) + } + + override fun markAsSent(msg: Plaintext) { + LOG.info(name + ": From " + name(msg.from) + ": Sent") + super.markAsSent(msg) + } + + override fun markAsAcknowledged(msg: Plaintext) { + LOG.info(name + ": From " + name(msg.from) + ": Acknowledged") + super.markAsAcknowledged(msg) + } + + override fun markAsRead(msg: Plaintext) { + LOG.info(name + ": From " + name(msg.from) + ": Read") + super.markAsRead(msg) + } + + override fun markAsUnread(msg: Plaintext) { + LOG.info(name + ": From " + name(msg.from) + ": Unread") + super.markAsUnread(msg) + } + + override fun delete(msg: Plaintext) { + LOG.info(name + ": From " + name(msg.from) + ": Cleared") + super.delete(msg) + } + + override fun archive(msg: Plaintext) { + LOG.info(name + ": From " + name(msg.from) + ": Archived") + super.archive(msg) + } + + private fun name(address: BitmessageAddress): String { + return when { + alice == address.address -> "Alice" + bob == address.address -> "Bob" + else -> "Unknown (" + address.address + ")" + } + } + } + + companion object { + private var port = 6000 + } +} diff --git a/exports/build.gradle b/exports/build.gradle index e4dbd29..7021321 100644 --- a/exports/build.gradle +++ b/exports/build.gradle @@ -15,9 +15,10 @@ dependencies { compile 'org.slf4j:slf4j-api' compile 'com.beust:klaxon' - testCompile 'junit:junit' - testCompile 'org.hamcrest:hamcrest-library' testCompile 'com.nhaarman:mockito-kotlin' + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' + testCompile project(path: ':core', configuration: 'testArtifacts') testCompile project(':cryptography-bc') } diff --git a/exports/src/test/kotlin/ch/dissem/bitmessage/exports/ContactExportTest.kt b/exports/src/test/kotlin/ch/dissem/bitmessage/exports/ContactExportTest.kt index dd3aa5f..4982681 100644 --- a/exports/src/test/kotlin/ch/dissem/bitmessage/exports/ContactExportTest.kt +++ b/exports/src/test/kotlin/ch/dissem/bitmessage/exports/ContactExportTest.kt @@ -19,10 +19,8 @@ package ch.dissem.bitmessage.exports import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography import ch.dissem.bitmessage.entity.BitmessageAddress import ch.dissem.bitmessage.utils.TestUtils -import org.hamcrest.CoreMatchers.`is` -import org.hamcrest.CoreMatchers.nullValue -import org.junit.Assert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.Test class ContactExportTest { @@ -42,7 +40,7 @@ class ContactExportTest { ) val export = ContactExport.exportContacts(contacts) print(export.toJsonString(true)) - assertThat(ContactExport.importContacts(export), `is`(contacts)) + assertEquals(contacts, ContactExport.importContacts(export)) } @Test @@ -53,9 +51,9 @@ class ContactExportTest { val export = ContactExport.exportContacts(contacts) print(export.toJsonString(true)) val import = ContactExport.importContacts(export) - assertThat(import.size, `is`(1)) - assertThat(import[0].isChan, `is`(true)) - assertThat(import[0].privateKey, `is`(nullValue())) + assertEquals(1, import.size) + assertTrue(import[0].isChan) + assertNull(import[0].privateKey) } @Test @@ -66,8 +64,9 @@ class ContactExportTest { val export = ContactExport.exportContacts(contacts, true) print(export.toJsonString(true)) val import = ContactExport.importContacts(export) - assertThat(import.size, `is`(1)) - assertThat(import[0].isChan, `is`(true)) - assertThat(import[0].privateKey, `is`(contacts[0].privateKey)) + + assertEquals(1, import.size) + assertTrue(import[0].isChan) + assertEquals(contacts[0].privateKey, import[0].privateKey) } } diff --git a/exports/src/test/kotlin/ch/dissem/bitmessage/exports/MessageExportTest.kt b/exports/src/test/kotlin/ch/dissem/bitmessage/exports/MessageExportTest.kt index 04fd799..616935e 100644 --- a/exports/src/test/kotlin/ch/dissem/bitmessage/exports/MessageExportTest.kt +++ b/exports/src/test/kotlin/ch/dissem/bitmessage/exports/MessageExportTest.kt @@ -23,23 +23,22 @@ import ch.dissem.bitmessage.entity.valueobject.Label import ch.dissem.bitmessage.utils.ConversationServiceTest import ch.dissem.bitmessage.utils.Singleton import ch.dissem.bitmessage.utils.TestUtils -import org.hamcrest.CoreMatchers.`is` -import org.junit.Assert.assertThat -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test class MessageExportTest { - val inbox = Label("Inbox", Label.Type.INBOX, 0x0000ff) - val outbox = Label("Outbox", Label.Type.OUTBOX, 0x00ff00) - val unread = Label("Unread", Label.Type.UNREAD, 0x000000) - val trash = Label("Trash", Label.Type.TRASH, 0x555555) + private val inbox = Label("Inbox", Label.Type.INBOX, 0x0000ff) + private val outbox = Label("Outbox", Label.Type.OUTBOX, 0x00ff00) + private val unread = Label("Unread", Label.Type.UNREAD, 0x000000) + private val trash = Label("Trash", Label.Type.TRASH, 0x555555) - val labels = listOf( + private val labels = listOf( inbox, outbox, unread, trash ) - val labelMap = MessageExport.createLabelMap(labels) + private val labelMap = MessageExport.createLabelMap(labels) init { TestUtils.mockedInternalContext(cryptography = BouncyCryptography()) @@ -49,7 +48,7 @@ class MessageExportTest { fun `ensure labels are exported`() { val export = MessageExport.exportLabels(labels) print(export.toJsonString(true)) - assertThat(MessageExport.importLabels(export), `is`(labels)) + assertEquals(labels, MessageExport.importLabels(export)) } @Test @@ -84,6 +83,6 @@ class MessageExportTest { ) val export = MessageExport.exportMessages(messages) print(export.toJsonString(true)) - assertThat(MessageExport.importMessages(export, labelMap), `is`(messages)) + assertEquals(messages, MessageExport.importMessages(export, labelMap)) } } diff --git a/extensions/build.gradle b/extensions/build.gradle index 2ec242e..dde5877 100644 --- a/extensions/build.gradle +++ b/extensions/build.gradle @@ -28,9 +28,10 @@ uploadArchives { dependencies { compile project(':core') - testCompile 'junit:junit' testCompile 'org.slf4j:slf4j-simple' testCompile 'com.nhaarman:mockito-kotlin' + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' testCompile project(path: ':core', configuration: 'testArtifacts') testCompile project(':cryptography-bc') } diff --git a/extensions/src/test/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessageTest.kt b/extensions/src/test/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessageTest.kt index d24ed0e..4950b9f 100644 --- a/extensions/src/test/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessageTest.kt +++ b/extensions/src/test/kotlin/ch/dissem/bitmessage/extensions/CryptoCustomMessageTest.kt @@ -21,17 +21,15 @@ import ch.dissem.bitmessage.entity.CustomMessage import ch.dissem.bitmessage.entity.payload.GenericPayload import ch.dissem.bitmessage.entity.valueobject.PrivateKey import ch.dissem.bitmessage.extensions.pow.ProofOfWorkRequest +import ch.dissem.bitmessage.utils.Singleton.cryptography import ch.dissem.bitmessage.utils.TestBase import ch.dissem.bitmessage.utils.TestUtils -import org.junit.Test - +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Test import java.io.ByteArrayInputStream import java.io.ByteArrayOutputStream import java.io.InputStream -import ch.dissem.bitmessage.utils.Singleton.cryptography -import org.junit.Assert.assertEquals - class CryptoCustomMessageTest : TestBase() { @Test fun `ensure encrypt then decrypt yields same object`() { @@ -40,7 +38,10 @@ class CryptoCustomMessageTest : TestBase() { val payloadBefore = GenericPayload(0, 1, cryptography().randomBytes(100)) val messageBefore = CryptoCustomMessage(payloadBefore) - messageBefore.signAndEncrypt(sendingIdentity, cryptography().createPublicKey(sendingIdentity.publicDecryptionKey)) + messageBefore.signAndEncrypt( + sendingIdentity, + cryptography().createPublicKey(sendingIdentity.publicDecryptionKey) + ) val out = ByteArrayOutputStream() messageBefore.writer().write(out) @@ -48,10 +49,10 @@ class CryptoCustomMessageTest : TestBase() { val customMessage = CustomMessage.read(input, out.size()) val messageAfter = CryptoCustomMessage.read(customMessage, - object : CryptoCustomMessage.Reader<GenericPayload> { - override fun read(sender: BitmessageAddress, input: InputStream) = - GenericPayload.read(0, 1, input, 100) - }) + object : CryptoCustomMessage.Reader<GenericPayload> { + override fun read(sender: BitmessageAddress, input: InputStream) = + GenericPayload.read(0, 1, input, 100) + }) val payloadAfter = messageAfter.decrypt(sendingIdentity.publicDecryptionKey) assertEquals(payloadBefore, payloadAfter) @@ -62,11 +63,16 @@ class CryptoCustomMessageTest : TestBase() { val privateKey = PrivateKey.read(TestUtils.getResource("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8.privkey")) val sendingIdentity = BitmessageAddress(privateKey) - val requestBefore = ProofOfWorkRequest(sendingIdentity, cryptography().randomBytes(64), - ProofOfWorkRequest.Request.CALCULATE) + val requestBefore = ProofOfWorkRequest( + sendingIdentity, cryptography().randomBytes(64), + ProofOfWorkRequest.Request.CALCULATE + ) val messageBefore = CryptoCustomMessage(requestBefore) - messageBefore.signAndEncrypt(sendingIdentity, cryptography().createPublicKey(sendingIdentity.publicDecryptionKey)) + messageBefore.signAndEncrypt( + sendingIdentity, + cryptography().createPublicKey(sendingIdentity.publicDecryptionKey) + ) val out = ByteArrayOutputStream() @@ -74,8 +80,10 @@ class CryptoCustomMessageTest : TestBase() { val input = ByteArrayInputStream(out.toByteArray()) val customMessage = CustomMessage.read(input, out.size()) - val messageAfter = CryptoCustomMessage.read(customMessage, - ProofOfWorkRequest.Reader(sendingIdentity)) + val messageAfter = CryptoCustomMessage.read( + customMessage, + ProofOfWorkRequest.Reader(sendingIdentity) + ) val requestAfter = messageAfter.decrypt(sendingIdentity.publicDecryptionKey) assertEquals(requestBefore, requestAfter) diff --git a/networking/build.gradle b/networking/build.gradle index 489729c..e792652 100644 --- a/networking/build.gradle +++ b/networking/build.gradle @@ -12,9 +12,10 @@ uploadArchives { dependencies { compile project(':core') - testCompile 'junit:junit' testCompile 'org.slf4j:slf4j-simple' testCompile 'com.nhaarman:mockito-kotlin' + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' testCompile project(path: ':core', configuration: 'testArtifacts') testCompile project(':cryptography-bc') } diff --git a/networking/src/test/kotlin/ch/dissem/bitmessage/networking/NetworkHandlerTest.kt b/networking/src/test/kotlin/ch/dissem/bitmessage/networking/NetworkHandlerTest.kt index cd030f5..c793247 100644 --- a/networking/src/test/kotlin/ch/dissem/bitmessage/networking/NetworkHandlerTest.kt +++ b/networking/src/test/kotlin/ch/dissem/bitmessage/networking/NetworkHandlerTest.kt @@ -29,17 +29,11 @@ import ch.dissem.bitmessage.testutils.TestInventory import ch.dissem.bitmessage.utils.Property import ch.dissem.bitmessage.utils.Singleton.cryptography import com.nhaarman.mockito_kotlin.mock -import org.hamcrest.Matchers.`is` -import org.hamcrest.Matchers.notNullValue -import org.junit.After -import org.junit.Assert.assertEquals -import org.junit.Assert.assertThat -import org.junit.Before -import org.junit.Rule -import org.junit.Test -import org.junit.rules.DisableOnDebug -import org.junit.rules.TestRule -import org.junit.rules.Timeout +import org.junit.jupiter.api.AfterEach +import org.junit.jupiter.api.Assertions +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import org.slf4j.LoggerFactory /** @@ -58,11 +52,7 @@ class NetworkHandlerTest { private lateinit var peerNetworkHandler: NetworkHandler private lateinit var nodeNetworkHandler: NetworkHandler - @JvmField - @Rule - val timeout: TestRule = DisableOnDebug(Timeout.seconds(60)) - - @Before + @BeforeEach fun setUp() { peerInventory = TestInventory() peerNetworkHandler = NioNetworkHandler() @@ -125,7 +115,7 @@ class NetworkHandlerTest { } } - @After + @AfterEach fun cleanUp() { shutdown(peer) shutdown(node) @@ -148,8 +138,8 @@ class NetworkHandlerTest { val nodeStatus = waitForNetworkStatus(node) val peerStatus = waitForNetworkStatus(peer) - assertEquals(1, nodeStatus.getProperty("outgoing")!!.value) - assertEquals(1, peerStatus.getProperty("incoming")!!.value) + assertEquals(1, nodeStatus.getProperty("outgoing")?.value) + assertEquals(1, peerStatus.getProperty("incoming")?.value) } @Test @@ -161,22 +151,20 @@ class NetworkHandlerTest { val response = nodeNetworkHandler.send(peerAddress.toInetAddress(), peerAddress.port, request) - assertThat(response, notNullValue()) - assertThat(response.customCommand, `is`("test response")) - assertThat(response.getData(), `is`(data)) + assertNotNull(response) + assertEquals("test response", response.customCommand) + assertEquals(data, response.getData()) } - @Test(expected = NodeException::class) + @Test fun `ensure CustomMessage without response yields exception`() { - val data = cryptography().randomBytes(8) - data[0] = 0.toByte() - val request = CustomMessage("test request", data) + assertThrows(NodeException::class.java) { + val data = cryptography().randomBytes(8) + data[0] = 0.toByte() + val request = CustomMessage("test request", data) - val response = nodeNetworkHandler.send(peerAddress.toInetAddress(), peerAddress.port, request) - - assertThat(response, notNullValue()) - assertThat(response.customCommand, `is`("test response")) - assertThat(response.getData(), `is`(request.getData())) + nodeNetworkHandler.send(peerAddress.toInetAddress(), peerAddress.port, request) + } } @Test diff --git a/repositories/build.gradle b/repositories/build.gradle index ce3b83a..eeca0ab 100644 --- a/repositories/build.gradle +++ b/repositories/build.gradle @@ -13,9 +13,11 @@ uploadArchives { dependencies { compile project(':core') compile 'org.flywaydb:flyway-core' - testCompile 'junit:junit' testCompile 'com.h2database:h2' + testCompile 'org.hamcrest:java-hamcrest' testCompile 'com.nhaarman:mockito-kotlin' + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' testCompile project(path: ':core', configuration: 'testArtifacts') testCompile project(':cryptography-bc') } diff --git a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcAddressRepositoryTest.kt b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcAddressRepositoryTest.kt index 5e05aac..4144593 100644 --- a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcAddressRepositoryTest.kt +++ b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcAddressRepositoryTest.kt @@ -19,9 +19,9 @@ package ch.dissem.bitmessage.repository import ch.dissem.bitmessage.entity.BitmessageAddress import ch.dissem.bitmessage.entity.payload.Pubkey.Feature.DOES_ACK import ch.dissem.bitmessage.entity.valueobject.PrivateKey -import org.junit.Assert.* -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test class JdbcAddressRepositoryTest : TestBase() { private val CONTACT_A = "BM-2cW7cD5cDQJDNkE7ibmyTxfvGAmnPqa9Vt" @@ -34,7 +34,7 @@ class JdbcAddressRepositoryTest : TestBase() { private lateinit var config: TestJdbcConfig private lateinit var repo: JdbcAddressRepository - @Before + @BeforeEach fun setUp() { config = TestJdbcConfig() config.reset() diff --git a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcInventoryTest.kt b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcInventoryTest.kt index 4aa6e69..04e1fde 100644 --- a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcInventoryTest.kt +++ b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcInventoryTest.kt @@ -26,9 +26,9 @@ import ch.dissem.bitmessage.entity.valueobject.InventoryVector import ch.dissem.bitmessage.ports.Inventory import ch.dissem.bitmessage.utils.UnixTime.DAY import ch.dissem.bitmessage.utils.UnixTime.now -import org.junit.Assert.* -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import java.util.* class JdbcInventoryTest : TestBase() { @@ -39,7 +39,7 @@ class JdbcInventoryTest : TestBase() { private lateinit var inventoryVector2: InventoryVector private lateinit var inventoryVectorIgnore: InventoryVector - @Before + @BeforeEach fun setUp() { config = TestJdbcConfig() config.reset() diff --git a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcLabelRepositoryTest.kt b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcLabelRepositoryTest.kt index f7822ec..2557fed 100644 --- a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcLabelRepositoryTest.kt +++ b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcLabelRepositoryTest.kt @@ -18,15 +18,15 @@ package ch.dissem.bitmessage.repository import ch.dissem.bitmessage.entity.valueobject.Label import ch.dissem.bitmessage.ports.LabelRepository -import org.junit.Assert.assertEquals -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test class JdbcLabelRepositoryTest : TestBase() { private lateinit var repo: LabelRepository - @Before + @BeforeEach fun setUp() { val config = TestJdbcConfig() config.reset() diff --git a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcMessageRepositoryTest.kt b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcMessageRepositoryTest.kt index 8d078e0..af24a01 100644 --- a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcMessageRepositoryTest.kt +++ b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcMessageRepositoryTest.kt @@ -35,10 +35,12 @@ import org.hamcrest.BaseMatcher import org.hamcrest.CoreMatchers.`is` import org.hamcrest.Description import org.hamcrest.Matcher +import org.hamcrest.MatcherAssert.assertThat import org.hamcrest.Matchers.* -import org.junit.Assert.* -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertNotNull +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import java.util.* class JdbcMessageRepositoryTest : TestBase() { @@ -54,7 +56,7 @@ class JdbcMessageRepositoryTest : TestBase() { private lateinit var drafts: Label private lateinit var unread: Label - @Before + @BeforeEach fun setUp() { val config = TestJdbcConfig() config.reset() @@ -230,6 +232,7 @@ class JdbcMessageRepositoryTest : TestBase() { val parent = storeConversation() val responses = repo.findResponses(parent) + assertThat(responses, hasSize<Plaintext>(2)) assertThat(responses, hasItem(hasMessage("Re: new test", "Nice!"))) assertThat(responses, hasItem(hasMessage("Re: new test", "PS: it did work!"))) diff --git a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.kt b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.kt index 2b604d4..5ff172c 100644 --- a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.kt +++ b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcNodeRegistryTest.kt @@ -19,11 +19,10 @@ package ch.dissem.bitmessage.repository import ch.dissem.bitmessage.entity.valueobject.NetworkAddress import ch.dissem.bitmessage.ports.NodeRegistry import ch.dissem.bitmessage.utils.UnixTime.now -import org.hamcrest.Matchers.empty -import org.junit.Assert.assertEquals -import org.junit.Assert.assertThat -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assertions.assertTrue +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import java.util.* /** @@ -34,23 +33,25 @@ class JdbcNodeRegistryTest : TestBase() { private lateinit var config: TestJdbcConfig private lateinit var registry: NodeRegistry - @Before + @BeforeEach fun setUp() { config = TestJdbcConfig() config.reset() registry = JdbcNodeRegistry(config) - registry.offerAddresses(Arrays.asList( + registry.offerAddresses( + Arrays.asList( createAddress(1, 8444, 1, now), createAddress(2, 8444, 1, now), createAddress(3, 8444, 1, now), createAddress(4, 8444, 2, now) - )) + ) + ) } @Test fun `ensure getKnownNodes() without streams yields empty`() { - assertThat(registry.getKnownAddresses(10), empty<NetworkAddress>()) + assertTrue(registry.getKnownAddresses(10).isEmpty()) } @Test @@ -71,11 +72,13 @@ class JdbcNodeRegistryTest : TestBase() { @Test fun `ensure offered addresses are added`() { - registry.offerAddresses(Arrays.asList( + registry.offerAddresses( + Arrays.asList( createAddress(1, 8444, 1, now), createAddress(10, 8444, 1, now), createAddress(11, 8444, 1, now) - )) + ) + ) var knownAddresses = registry.getKnownAddresses(1000, 1) assertEquals(5, knownAddresses.size.toLong()) @@ -88,10 +91,10 @@ class JdbcNodeRegistryTest : TestBase() { private fun createAddress(lastByte: Int, port: Int, stream: Long, time: Long): NetworkAddress { return NetworkAddress.Builder() - .ipv6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, lastByte) - .port(port) - .stream(stream) - .time(time) - .build() + .ipv6(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, lastByte) + .port(port) + .stream(stream) + .time(time) + .build() } } diff --git a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcProofOfWorkRepositoryTest.kt b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcProofOfWorkRepositoryTest.kt index 1e08859..af82893 100644 --- a/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcProofOfWorkRepositoryTest.kt +++ b/repositories/src/test/kotlin/ch/dissem/bitmessage/repository/JdbcProofOfWorkRepositoryTest.kt @@ -22,9 +22,6 @@ import ch.dissem.bitmessage.entity.Plaintext import ch.dissem.bitmessage.entity.Plaintext.Type.MSG import ch.dissem.bitmessage.entity.payload.GenericPayload import ch.dissem.bitmessage.entity.payload.GetPubkey -import ch.dissem.bitmessage.entity.payload.ObjectPayload -import ch.dissem.bitmessage.entity.payload.Pubkey -import ch.dissem.bitmessage.entity.valueobject.PrivateKey import ch.dissem.bitmessage.ports.AddressRepository import ch.dissem.bitmessage.ports.MessageRepository import ch.dissem.bitmessage.ports.ProofOfWorkRepository.Item @@ -32,11 +29,9 @@ import ch.dissem.bitmessage.utils.Singleton.cryptography import ch.dissem.bitmessage.utils.TestUtils import ch.dissem.bitmessage.utils.UnixTime import ch.dissem.bitmessage.utils.UnixTime.MINUTE -import org.hamcrest.CoreMatchers.* -import org.junit.Assert.assertThat -import org.junit.Assert.assertTrue -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test import kotlin.properties.Delegates /** @@ -51,7 +46,7 @@ class JdbcProofOfWorkRepositoryTest : TestBase() { private var initialHash1: ByteArray by Delegates.notNull<ByteArray>() private var initialHash2: ByteArray by Delegates.notNull<ByteArray>() - @Before + @BeforeEach fun setUp() { config = TestJdbcConfig() config.reset() @@ -66,9 +61,11 @@ class JdbcProofOfWorkRepositoryTest : TestBase() { cryptography = cryptography() ) - repo.putObject(ObjectMessage.Builder() - .payload(GetPubkey(BitmessageAddress("BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn"))).build(), - 1000, 1000) + repo.putObject( + ObjectMessage.Builder() + .payload(GetPubkey(BitmessageAddress("BM-2DAjcCFrqFrp88FUxExhJ9kPqHdunQmiyn"))).build(), + 1000, 1000 + ) initialHash1 = repo.getItems()[0] val sender = TestUtils.loadIdentity("BM-2cSqjfJ8xK6UUn5Rw3RpdGQ9RsDkBhWnS8") @@ -84,21 +81,25 @@ class JdbcProofOfWorkRepositoryTest : TestBase() { .build() messageRepo.save(plaintext) initialHash2 = cryptography().getInitialHash(plaintext.ackMessage!!) - repo.putObject(Item( - plaintext.ackMessage!!, - 1000, 1000, - UnixTime.now + 10 * MINUTE, - plaintext - )) + repo.putObject( + Item( + plaintext.ackMessage!!, + 1000, 1000, + UnixTime.now + 10 * MINUTE, + plaintext + ) + ) } @Test fun `ensure object is stored`() { val sizeBefore = repo.getItems().size - repo.putObject(ObjectMessage.Builder() - .payload(GetPubkey(BitmessageAddress("BM-2D9U2hv3YBMHM1zERP32anKfVKohyPN9x2"))).build(), - 1000, 1000) - assertThat(repo.getItems().size, `is`(sizeBefore + 1)) + repo.putObject( + ObjectMessage.Builder() + .payload(GetPubkey(BitmessageAddress("BM-2D9U2hv3YBMHM1zERP32anKfVKohyPN9x2"))).build(), + 1000, 1000 + ) + assertEquals(sizeBefore + 1, repo.getItems().size) } @Test @@ -116,40 +117,42 @@ class JdbcProofOfWorkRepositoryTest : TestBase() { .status(Plaintext.Status.DOING_PROOF_OF_WORK) .build() messageRepo.save(plaintext) - repo.putObject(Item( - plaintext.ackMessage!!, - 1000, 1000, - UnixTime.now + 10 * MINUTE, - plaintext - )) - assertThat(repo.getItems().size, `is`(sizeBefore + 1)) + repo.putObject( + Item( + plaintext.ackMessage!!, + 1000, 1000, + UnixTime.now + 10 * MINUTE, + plaintext + ) + ) + assertEquals(sizeBefore + 1, repo.getItems().size) } @Test fun `ensure item can be retrieved`() { val item = repo.getItem(initialHash1) - assertThat(item, notNullValue()) - assertThat<ObjectPayload>(item.objectMessage.payload, instanceOf<ObjectPayload>(GetPubkey::class.java)) - assertThat(item.nonceTrialsPerByte, `is`(1000L)) - assertThat(item.extraBytes, `is`(1000L)) + assertTrue(item.objectMessage.payload is GetPubkey) + assertEquals(1000L, item.nonceTrialsPerByte) + assertEquals(1000L, item.extraBytes) } @Test fun `ensure ack item can be retrieved`() { val item = repo.getItem(initialHash2) - assertThat(item, notNullValue()) - assertThat<ObjectPayload>(item.objectMessage.payload, instanceOf<ObjectPayload>(GenericPayload::class.java)) - assertThat(item.nonceTrialsPerByte, `is`(1000L)) - assertThat(item.extraBytes, `is`(1000L)) - assertThat(item.expirationTime, not<Number>(0)) - assertThat(item.message, notNullValue()) - assertThat<PrivateKey>(item.message?.from?.privateKey, notNullValue()) - assertThat<Pubkey>(item.message?.to?.pubkey, notNullValue()) + assertTrue(item.objectMessage.payload is GenericPayload) + assertEquals(1000L, item.nonceTrialsPerByte) + assertEquals(1000L, item.extraBytes) + assertTrue(item.expirationTime ?: 0L > 0L) + assertNotNull(item.message) + assertNotNull(item.message?.from?.privateKey) + assertNotNull(item.message?.to?.pubkey) } - @Test(expected = RuntimeException::class) + @Test fun `ensure retrieving nonexisting item causes exception`() { - repo.getItem(ByteArray(0)) + assertThrows(RuntimeException::class.java) { + repo.getItem(ByteArray(0)) + } } @Test diff --git a/wif/build.gradle b/wif/build.gradle index aa7cbb9..5aa39e2 100644 --- a/wif/build.gradle +++ b/wif/build.gradle @@ -13,7 +13,8 @@ uploadArchives { dependencies { compile project(':core') compile 'org.ini4j:ini4j' - testCompile 'junit:junit' testCompile 'com.nhaarman:mockito-kotlin' + testCompile 'org.junit.jupiter:junit-jupiter-api' + testRuntime 'org.junit.jupiter:junit-jupiter-engine' testCompile project(':cryptography-bc') } diff --git a/wif/src/test/kotlin/ch/dissem/bitmessage/wif/WifExporterTest.kt b/wif/src/test/kotlin/ch/dissem/bitmessage/wif/WifExporterTest.kt index 2244ef8..f1546db 100644 --- a/wif/src/test/kotlin/ch/dissem/bitmessage/wif/WifExporterTest.kt +++ b/wif/src/test/kotlin/ch/dissem/bitmessage/wif/WifExporterTest.kt @@ -37,9 +37,10 @@ import ch.dissem.bitmessage.cryptography.bc.BouncyCryptography import ch.dissem.bitmessage.ports.AddressRepository import com.nhaarman.mockito_kotlin.mock import com.nhaarman.mockito_kotlin.whenever -import org.junit.Assert.assertEquals -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.assertEquals +import org.junit.jupiter.api.Assumptions.assumeTrue +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test class WifExporterTest { private val repo = mock<AddressRepository>() @@ -47,7 +48,7 @@ class WifExporterTest { private lateinit var importer: WifImporter private lateinit var exporter: WifExporter - @Before + @BeforeEach fun setUp() { ctx = BitmessageContext.build { cryptography = BouncyCryptography() @@ -61,7 +62,7 @@ class WifExporterTest { listener {} } importer = WifImporter(ctx, javaClass.classLoader.getResourceAsStream("nuked.dat")) - assertEquals(81, importer.getIdentities().size) + assumeTrue(importer.getIdentities().size == 81) exporter = WifExporter(ctx) } @@ -70,10 +71,7 @@ class WifExporterTest { whenever(repo.getIdentities()).thenReturn(importer.getIdentities()) exporter.addAll() val result = exporter.toString() - var count = 0 - for (i in 0..result.length - 1) { - if (result[i] == '[') count++ - } + val count = result.count { it == '[' } assertEquals(importer.getIdentities().size, count) } @@ -81,10 +79,7 @@ class WifExporterTest { fun `ensure all from a collection are added`() { exporter.addAll(importer.getIdentities()) val result = exporter.toString() - var count = 0 - for (i in 0..result.length - 1) { - if (result[i] == '[') count++ - } + val count = result.count { it == '[' } assertEquals(importer.getIdentities().size, count) } diff --git a/wif/src/test/kotlin/ch/dissem/bitmessage/wif/WifImporterTest.kt b/wif/src/test/kotlin/ch/dissem/bitmessage/wif/WifImporterTest.kt index 758bb39..7c6b44a 100644 --- a/wif/src/test/kotlin/ch/dissem/bitmessage/wif/WifImporterTest.kt +++ b/wif/src/test/kotlin/ch/dissem/bitmessage/wif/WifImporterTest.kt @@ -39,16 +39,16 @@ import com.nhaarman.mockito_kotlin.any import com.nhaarman.mockito_kotlin.mock import com.nhaarman.mockito_kotlin.times import com.nhaarman.mockito_kotlin.verify -import org.junit.Assert.* -import org.junit.Before -import org.junit.Test +import org.junit.jupiter.api.Assertions.* +import org.junit.jupiter.api.BeforeEach +import org.junit.jupiter.api.Test class WifImporterTest { private val repo = mock<AddressRepository>() private lateinit var ctx: BitmessageContext private lateinit var importer: WifImporter - @Before + @BeforeEach fun setUp() { ctx = BitmessageContext.build { cryptography = BouncyCryptography() @@ -81,7 +81,7 @@ class WifImporterTest { assertEquals("Nuked Address", identity.alias) assertEquals(320L, identity.pubkey?.nonceTrialsPerByte) assertEquals(14000L, identity.pubkey?.extraBytes) - assertNotNull("Private key", identity.privateKey) + assertNotNull(identity.privateKey, "Private key") assertEquals(32, identity.privateKey?.privateEncryptionKey?.size) assertEquals(32, identity.privateKey?.privateSigningKey?.size) assertFalse(identity.isChan)