Skip to content

Commit

Permalink
added tests for CloseRollCall data validation
Browse files Browse the repository at this point in the history
  • Loading branch information
Kaz-ookid committed Jun 3, 2024
1 parent c8e7e38 commit bf479ad
Show file tree
Hide file tree
Showing 5 changed files with 87 additions and 11 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,11 @@ class CloseRollCall(

init {
verify()
.stringListIsSorted(attendees.map { toString() }, "attendees")
.stringListIsSorted(attendees, "attendees")
.validPastTimes(closedAt)
.greaterOrEqualThan(closedAt, 0, "closedAt")
.isNotEmptyBase64(laoId, "laoId")
.isNotEmptyBase64(closes, "closes")
.greaterOrEqualThan(closedAt, 0, "closedAt")

this.updateId = generateCloseRollCallId(laoId, closes, closedAt)
this.attendees = ArrayList(attendees)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import android.view.View
import android.view.ViewGroup
import androidx.annotation.VisibleForTesting
import androidx.appcompat.content.res.AppCompatResources
import androidx.core.content.ContentProviderCompat.requireContext
import androidx.lifecycle.MutableLiveData
import com.github.dedis.popstellar.R
import com.github.dedis.popstellar.databinding.RollCallFragmentBinding
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -306,10 +306,8 @@ object MessageValidator {
}

/** Helper method to check that a string list is sorted */
fun stringListIsSorted(list: List<String>, field: String): MessageValidatorBuilder {
for (i in 0 until list.size - 1) {
require(list[i + 1] >= list[i]) { "$field must be sorted" }
}
fun stringListIsSorted(list: List<*>, field: String): MessageValidatorBuilder {
require(list == list.sortedBy{it.toString()}) { "$field must be sorted" }
return this
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -128,11 +128,12 @@ constructor(

val currentAttendees: Set<PublicKey>
if (closeRollCall.attendees.containsAll(existingRollCall.attendees)) {
// closeRollCall.attendees is sorted, so we prefer to use it if we can
// closeRollCall.attendees is sorted, so we prefer to use it if we can
currentAttendees = closeRollCall.attendees.toMutableSet()
} else {
// if both lists have different attendees, we merge them even though we lose the order
// We are not ordering it because it is important to keep the order that we received to know if we face de-anonymization
// if both lists have different attendees, we merge them even though we lose the order
// We are not ordering it because it is important to keep the order that we received to know
// if we face de-anonymization
currentAttendees = existingRollCall.attendees
currentAttendees.addAll(closeRollCall.attendees)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,18 @@ 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.EventState
import com.github.dedis.popstellar.model.objects.event.EventType
import com.github.dedis.popstellar.model.objects.security.Base64URLData
import com.github.dedis.popstellar.testutils.Base64DataUtils
import com.github.dedis.popstellar.utility.MessageValidator
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
import java.util.Collections

@RunWith(AndroidJUnit4::class)
class CloseRollCallTest {
Expand Down Expand Up @@ -80,6 +85,57 @@ class CloseRollCallTest {
)
}

@Test
fun constructorSucceedsWithValidData() {
val closeRollCall = CloseRollCall(validBase64LaoId, validBase64Closes, pastTime, validAttendees)
assertNotNull(closeRollCall)
}

@Test(expected = IllegalArgumentException::class)
fun constructorFailsWhenAttendeesNotSorted() {
val unsortedAttendees = ArrayList(attendees).toMutableList()
if (unsortedAttendees[0].toString() > unsortedAttendees[1].toString()) {
Collections.swap(unsortedAttendees, 0, 1)
}

CloseRollCall(validBase64LaoId, validBase64Closes, pastTime, unsortedAttendees)
}

@Test(expected = IllegalArgumentException::class)
fun constructorFailsWhenClosedAtInFuture() {
CloseRollCall(validBase64LaoId, validBase64Closes, futureTime, validAttendees)
}

@Test(expected = IllegalArgumentException::class)
fun constructorFailsWhenTooLongPastTime() {
CloseRollCall(validBase64LaoId, validBase64Closes, tooLongPastTime, validAttendees)
}

@Test(expected = IllegalArgumentException::class)
fun constructorFailsWhenLaoIdNotBase64() {
CloseRollCall(invalidBase64, validBase64Closes, pastTime, validAttendees)
}

@Test(expected = IllegalArgumentException::class)
fun constructorFailsWhenLaoIdEmpty() {
CloseRollCall(emptyBase64, validBase64Closes, pastTime, validAttendees)
}

@Test(expected = IllegalArgumentException::class)
fun constructorFailsWhenClosesNotBase64() {
CloseRollCall(validBase64LaoId, invalidBase64, pastTime, validAttendees)
}

@Test(expected = IllegalArgumentException::class)
fun constructorFailsWhenClosesEmpty() {
CloseRollCall(validBase64LaoId, emptyBase64, pastTime, validAttendees)
}

@Test(expected = IllegalArgumentException::class)
fun constructorFailsWhenClosedAtNegative() {
CloseRollCall(validBase64LaoId, validBase64Closes, negativeClosedAt, validAttendees)
}

companion object {
private val LAO_ID = hash("LAO_ID")
private const val NAME = "NAME"
Expand All @@ -88,5 +144,27 @@ class CloseRollCallTest {
private val CREATE_ROLL_CALL = CreateRollCall(NAME, TIME, TIME, TIME, LOCATION, null, LAO_ID)
private val OPEN_ROLL_CALL = OpenRollCall(LAO_ID, CREATE_ROLL_CALL.id, TIME, EventState.CREATED)
private val CLOSE_ROLL_CALL = CloseRollCall(LAO_ID, OPEN_ROLL_CALL.updateId, TIME, ArrayList())

private val attendees = listOf(
Base64DataUtils.generatePublicKey(),
Base64DataUtils.generatePublicKey(),
Base64DataUtils.generatePublicKey(),
Base64DataUtils.generatePublicKey(),
Base64DataUtils.generatePublicKey(),
Base64DataUtils.generatePublicKey()
)

private val validAttendees = attendees.sortedBy { it.toString() }

private val pastTime = Instant.now().epochSecond - 1000
private val futureTime = Instant.now().epochSecond + 10000
private val tooLongPastTime = Instant.now().epochSecond - MessageValidator.MessageValidatorBuilder.VALID_PAST_DELAY
private val negativeClosedAt = (-1).toLong()

private val invalidBase64 = "invalidBase64String"
private val emptyBase64 = Base64URLData("".toByteArray()).encoded
private val validBase64LaoId = Base64URLData("validLaoId".toByteArray()).encoded
private val validBase64Closes = Base64URLData("validCloses".toByteArray()).encoded

}
}

0 comments on commit bf479ad

Please sign in to comment.