From 884e4af3861ba46a0e8672f34c66c3cb957c0811 Mon Sep 17 00:00:00 2001 From: Kaz Date: Thu, 27 Jun 2024 12:48:46 +0200 Subject: [PATCH 1/3] complete validation and tests for CreateRollCall data --- .../data/digitalcash/PostTransactionCoin.kt | 2 + .../message/data/rollcall/CreateRollCall.kt | 25 ++++ .../lao/event/rollcall/RollCallViewModel.kt | 8 +- .../model/network/method/BroadcastTest.kt | 18 ++- .../model/network/method/PublishTest.kt | 15 ++- .../data/rollcall/CreateRollCallTest.kt | 108 +++++++++++++++++- 6 files changed, 170 insertions(+), 6 deletions(-) diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/digitalcash/PostTransactionCoin.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/digitalcash/PostTransactionCoin.kt index ebb8dff51d..6651fab0f1 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/digitalcash/PostTransactionCoin.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/digitalcash/PostTransactionCoin.kt @@ -15,6 +15,8 @@ class PostTransactionCoin( // the transaction object // String : String = transaction.computeId() + init {} + override fun equals(other: Any?): Boolean { if (this === other) { return true diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCall.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCall.kt index 70872f0f97..0391e2fd45 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCall.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCall.kt @@ -5,6 +5,7 @@ import com.github.dedis.popstellar.model.network.method.message.data.Action import com.github.dedis.popstellar.model.network.method.message.data.Data import com.github.dedis.popstellar.model.network.method.message.data.Objects import com.github.dedis.popstellar.model.objects.RollCall +import com.github.dedis.popstellar.utility.MessageValidator import com.google.gson.annotations.SerializedName import java.util.Optional @@ -41,6 +42,8 @@ class CreateRollCall : Data { description: String?, laoId: String ) { + validate(laoId, "laoId", name, creation, proposedStart, proposedEnd, location, description) + .let { if (description != null) it.stringNotEmpty(description, "description") else it } this.name = name this.creation = creation this.proposedStart = proposedStart @@ -63,6 +66,8 @@ class CreateRollCall : Data { location: String, description: String? ) { + validate(id, "id", name, creation, proposedStart, proposedEnd, location, description) + this.id = id this.name = name this.creation = creation @@ -108,4 +113,24 @@ class CreateRollCall : Data { return "CreateRollCall{id='$id', name='$name', creation=$creation, proposedStart=$proposedStart, " + "proposedEnd=$proposedEnd, location='$location', description='$description'}" } + + private fun validate( + id: String, + idLabel: String, + name: String, + creation: Long, + proposedStart: Long, + proposedEnd: Long, + location: String, + description: String? + ): MessageValidator.MessageValidatorBuilder { + return MessageValidator.verify() + .isNotEmptyBase64(id, idLabel) + .stringNotEmpty(name, "name") + .greaterOrEqualThan(creation, 0, "creation") + .greaterOrEqualThan(proposedStart, 0, "proposedStart") + .greaterOrEqualThan(proposedEnd, 0, "proposedEnd") + .stringNotEmpty(location, "location") + .let { if (description != null) it.stringNotEmpty(description, "description") else it } + } } diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/rollcall/RollCallViewModel.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/rollcall/RollCallViewModel.kt index 4a18a8de13..efdff9a6f6 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/rollcall/RollCallViewModel.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/ui/lao/event/rollcall/RollCallViewModel.kt @@ -104,7 +104,13 @@ constructor( val createRollCall = CreateRollCall( - title, creation, proposedStart, proposedEnd, location, description, laoView.id) + title, + creation, + proposedStart, + proposedEnd, + location, + description?.ifEmpty { null }, + laoView.id) return networkManager.messageSender .publish(keyManager.mainKeyPair, laoView.channel, createRollCall) diff --git a/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/BroadcastTest.kt b/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/BroadcastTest.kt index 70a30fe59e..ef8ddbf673 100644 --- a/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/BroadcastTest.kt +++ b/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/BroadcastTest.kt @@ -5,9 +5,10 @@ import com.github.dedis.popstellar.model.network.method.message.data.rollcall.Cr import com.github.dedis.popstellar.model.objects.Channel.Companion.fromString import com.github.dedis.popstellar.testutils.Base64DataUtils import com.google.gson.Gson -import java.util.Objects import org.junit.Assert import org.junit.Test +import java.time.Instant +import java.util.Objects class BroadcastTest { @Test @@ -59,8 +60,19 @@ class BroadcastTest { companion object { private val CHANNEL = fromString("root/stuff") - private const val ID = 42 - private val DATA = CreateRollCall("title", 0, 1, 2, "EPFL", "rc", "an id") + private const val LAO_ID = "fEvAfdtNrykd9NPYl9ReHLX-6IP6SFLKTZJLeGUHZ_U=" + private const val NAME = "NAME" + private val NOW = Instant.now().epochSecond + private val END = NOW + 30L + private const val LOCATION = "Location" + private val DATA = CreateRollCall( + NAME, + NOW, + NOW, + END, + LOCATION, null, + LAO_ID + ) private val KEYPAIR = Base64DataUtils.generateKeyPair() private val MESSAGE_GENERAL = MessageGeneral(KEYPAIR, DATA, Gson()) private val BROADCAST = Broadcast(CHANNEL, MESSAGE_GENERAL) diff --git a/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/PublishTest.kt b/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/PublishTest.kt index 474cf92e6c..ba330193c3 100644 --- a/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/PublishTest.kt +++ b/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/PublishTest.kt @@ -8,6 +8,7 @@ import com.google.gson.Gson import java.util.Objects import org.junit.Assert import org.junit.Test +import java.time.Instant class PublishTest { @Test @@ -50,7 +51,19 @@ class PublishTest { companion object { private val CHANNEL = fromString("root/stuff") private const val ID = 42 - private val DATA = CreateRollCall("title", 0, 1, 2, "EPFL", "rc", "an id") + private const val LAO_ID = "fEvAfdtNrykd9NPYl9ReHLX-6IP6SFLKTZJLeGUHZ_U=" + private const val NAME = "NAME" + private val NOW = Instant.now().epochSecond + private val END = NOW + 30L + private const val LOCATION = "Location" + private val DATA = CreateRollCall( + NAME, + NOW, + NOW, + END, + LOCATION, null, + LAO_ID + ) private val KEYPAIR = Base64DataUtils.generateKeyPair() private val MESSAGE_GENERAL = MessageGeneral(KEYPAIR, DATA, Gson()) private val PUBLISH = Publish(CHANNEL, ID, MESSAGE_GENERAL) diff --git a/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCallTest.kt b/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCallTest.kt index c0e75889b7..c8c2bb69d2 100644 --- a/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCallTest.kt +++ b/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCallTest.kt @@ -5,13 +5,15 @@ import com.github.dedis.popstellar.model.network.JsonTestUtils.testData import com.github.dedis.popstellar.model.network.method.message.data.Action import com.github.dedis.popstellar.model.network.method.message.data.Objects import com.github.dedis.popstellar.model.objects.event.EventType +import com.github.dedis.popstellar.model.objects.security.Base64URLData import com.github.dedis.popstellar.utility.security.HashSHA256.hash -import java.time.Instant +import junit.framework.TestCase.assertNotNull import org.hamcrest.CoreMatchers import org.hamcrest.MatcherAssert import org.junit.Assert import org.junit.Test import org.junit.runner.RunWith +import java.time.Instant @RunWith(AndroidJUnit4::class) class CreateRollCallTest { @@ -61,6 +63,107 @@ class CreateRollCallTest { MatcherAssert.assertThat(CREATE_ROLL_CALL.location, CoreMatchers.`is`(LOCATION)) } + @Test + fun constructor1SucceedsWithValidData() { + val createRollCall = CreateRollCall(NAME, NOW, NOW, END, LOCATION, null, LAO_ID) + assertNotNull(createRollCall) + } + + fun constructor1SucceedsWithValidDataDescription() { + CreateRollCall(NAME, NOW, NOW, END, LOCATION, "super cool description", LAO_ID) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor1FailsWhenNameEmpty() { + CreateRollCall("", NOW, NOW, END, LOCATION, null, LAO_ID) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor1FailsWhenLAOIdEmpty() { + CreateRollCall(NAME, NOW, NOW, END, LOCATION, null, EMPTY_B64) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor1FailsWhenLAOIdNotB64() { + CreateRollCall(NAME, NOW, NOW, END, LOCATION, null, INVALID_B64) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor1FailsWhenCreationNegative() { + CreateRollCall(NAME, -1, NOW, END, LOCATION, null, LAO_ID) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor1FailsWhenProposedStartNegative() { + CreateRollCall(NAME, NOW, -1, END, LOCATION, null, LAO_ID) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor1FailsWhenProposedEndNegative() { + CreateRollCall(NAME, NOW, NOW, -1, LOCATION, null, LAO_ID) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor1FailsWhenLocationEmpty() { + CreateRollCall(NAME, NOW, NOW, END, EMPTY_STRING, null, LAO_ID) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor1FailsWhenDescriptionEmpty() { + CreateRollCall(NAME, NOW, NOW, END, LOCATION, EMPTY_STRING, LAO_ID) + } + + @Test + fun constructorSucceedsWithValidData() { + val createRollCall = CreateRollCall(ID, NAME, NOW, NOW, END, LOCATION, null) + assertNotNull(createRollCall) + } + + fun constructorSucceedsWithValidDataDescription() { + CreateRollCall(ID, NAME, NOW, NOW, END, LOCATION, "super cool description") + } + + @Test(expected = IllegalArgumentException::class) + fun constructor2FailsWhenNameEmpty() { + CreateRollCall(ID, "", NOW, NOW, END, LOCATION, null) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor2FailsWhenIdEmpty() { + CreateRollCall(EMPTY_B64, NAME, NOW, NOW, END, LOCATION, null) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor2FailsWhenIdNotB64() { + CreateRollCall(INVALID_B64, NAME, NOW, NOW, END, LOCATION, null) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor2FailsWhenCreationNegative() { + CreateRollCall(ID, NAME, -1, NOW, END, LOCATION, null) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor2FailsWhenProposedStartNegative() { + CreateRollCall(ID, NAME, NOW, -1, END, LOCATION, null) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor2FailsWhenProposedEndNegative() { + CreateRollCall(ID, NAME, NOW, NOW, -1, LOCATION, null) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor2FailsWhenLocationEmpty() { + CreateRollCall(ID, NAME, NOW, NOW, END, EMPTY_STRING, null) + } + + @Test(expected = IllegalArgumentException::class) + fun constructor2FailsWhenDescriptionEmpty() { + CreateRollCall(ID, NAME, NOW, NOW, END, LOCATION, EMPTY_STRING) + } + + @Test fun jsonValidationTest() { testData(CREATE_ROLL_CALL) @@ -88,6 +191,9 @@ class CreateRollCallTest { private val NOW = Instant.now().epochSecond private val END = NOW + 30L private const val LOCATION = "Location" + private const val EMPTY_STRING = "" + private val INVALID_B64 = "invalidBase64String" + private val EMPTY_B64 = Base64URLData("".toByteArray()).encoded private val CREATE_ROLL_CALL = CreateRollCall(NAME, NOW, NOW, END, LOCATION, null, LAO_ID) private val ID = hash(EventType.ROLL_CALL.suffix, LAO_ID, NOW.toString(), NAME) } From 0bc97d3ee3703e91f62ac693ba7e623eec460332 Mon Sep 17 00:00:00 2001 From: Kaz Date: Thu, 27 Jun 2024 12:58:01 +0200 Subject: [PATCH 2/3] removed forgotten empty init{} --- .../method/message/data/digitalcash/PostTransactionCoin.kt | 2 -- 1 file changed, 2 deletions(-) diff --git a/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/digitalcash/PostTransactionCoin.kt b/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/digitalcash/PostTransactionCoin.kt index 6651fab0f1..ebb8dff51d 100644 --- a/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/digitalcash/PostTransactionCoin.kt +++ b/fe2-android/app/src/main/java/com/github/dedis/popstellar/model/network/method/message/data/digitalcash/PostTransactionCoin.kt @@ -15,8 +15,6 @@ class PostTransactionCoin( // the transaction object // String : String = transaction.computeId() - init {} - override fun equals(other: Any?): Boolean { if (this === other) { return true From 8f36e239804a5ed8a41745329b497267394901ed Mon Sep 17 00:00:00 2001 From: Kaz Date: Thu, 27 Jun 2024 13:00:28 +0200 Subject: [PATCH 3/3] little refactor --- .../method/message/data/rollcall/CreateRollCallTest.kt | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCallTest.kt b/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCallTest.kt index c8c2bb69d2..cdf4ee8b21 100644 --- a/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCallTest.kt +++ b/fe2-android/app/src/test/unit/java/com/github/dedis/popstellar/model/network/method/message/data/rollcall/CreateRollCallTest.kt @@ -114,12 +114,12 @@ class CreateRollCallTest { } @Test - fun constructorSucceedsWithValidData() { + fun constructor2SucceedsWithValidData() { val createRollCall = CreateRollCall(ID, NAME, NOW, NOW, END, LOCATION, null) assertNotNull(createRollCall) } - fun constructorSucceedsWithValidDataDescription() { + fun constructor2SucceedsWithValidDataDescription() { CreateRollCall(ID, NAME, NOW, NOW, END, LOCATION, "super cool description") }