From df4df8a8f1e0115d83ad6e6ed59dd1034e28d7cb Mon Sep 17 00:00:00 2001
From: Mohamed Metwalli <mohamed.metwalli@sap.com>
Date: Thu, 11 Mar 2021 19:22:21 +0100
Subject: [PATCH] Use SignedTraceLocation

---
 .../qrcode/DefaultQRCodeVerifierTest.kt        |  6 +++---
 .../checkin/VerifiedTraceLocationKtTest.kt     |  6 ++++--
 .../checkins/qrcode/DefaultQRCodeVerifier.kt   | 18 +++++++++---------
 .../checkins/qrcode/QRCodeVerifyResult.kt      | 16 ++++++++++------
 .../attendee/checkin/VerifiedTraceLocation.kt  |  6 +++---
 5 files changed, 29 insertions(+), 23 deletions(-)

diff --git a/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifierTest.kt b/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifierTest.kt
index a87914a5e..e407a29e9 100644
--- a/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifierTest.kt
+++ b/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifierTest.kt
@@ -36,7 +36,7 @@ class DefaultQRCodeVerifierTest : BaseTestInstrumentation() {
         shouldNotThrowAny {
             val verifyResult = qrCodeVerifier.verify(ENCODED_EVENT)
             verifyResult.apply {
-                singedTraceLocation.event.description shouldBe "CWA Launch Party"
+                singedTraceLocation.location.description shouldBe "CWA Launch Party"
                 verifyResult.isBeforeStartTime(instant) shouldBe false
                 verifyResult.isAfterEndTime(instant) shouldBe false
             }
@@ -50,7 +50,7 @@ class DefaultQRCodeVerifierTest : BaseTestInstrumentation() {
         shouldNotThrowAny {
             val verifyResult = qrCodeVerifier.verify(ENCODED_EVENT)
             verifyResult.apply {
-                singedTraceLocation.event.description shouldBe "CWA Launch Party"
+                singedTraceLocation.location.description shouldBe "CWA Launch Party"
             }
             verifyResult.isBeforeStartTime(instant) shouldBe true
             verifyResult.isAfterEndTime(instant) shouldBe false
@@ -63,7 +63,7 @@ class DefaultQRCodeVerifierTest : BaseTestInstrumentation() {
         shouldNotThrowAny {
             val verifyResult = qrCodeVerifier.verify(ENCODED_EVENT)
             verifyResult.apply {
-                singedTraceLocation.event.description shouldBe "CWA Launch Party"
+                singedTraceLocation.location.description shouldBe "CWA Launch Party"
             }
             verifyResult.isBeforeStartTime(instant) shouldBe false
             verifyResult.isAfterEndTime(instant) shouldBe true
diff --git a/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkin/VerifiedTraceLocationKtTest.kt b/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkin/VerifiedTraceLocationKtTest.kt
index 9ae34bda6..ebae0addf 100644
--- a/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkin/VerifiedTraceLocationKtTest.kt
+++ b/Corona-Warn-App/src/androidTest/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkin/VerifiedTraceLocationKtTest.kt
@@ -2,7 +2,7 @@ package de.rki.coronawarnapp.ui.eventregistration.attendee.checkin
 
 import de.rki.coronawarnapp.eventregistration.checkins.qrcode.QRCodeVerifyResult
 import de.rki.coronawarnapp.eventregistration.common.decodeBase32
-import de.rki.coronawarnapp.server.protocols.internal.evreg.SignedEventOuterClass
+import de.rki.coronawarnapp.server.protocols.internal.pt.TraceLocationOuterClass
 import io.kotest.assertions.throwables.shouldNotThrowAny
 import io.kotest.matchers.shouldBe
 import org.joda.time.Instant
@@ -18,7 +18,9 @@ class VerifiedTraceLocationKtTest : BaseTestInstrumentation() {
     fun testVerifiedTraceLocationMapping() {
         shouldNotThrowAny {
             val signedTraceLocation =
-                SignedEventOuterClass.SignedEvent.parseFrom(DECODED_TRACE_LOCATION.decodeBase32().toByteArray())
+                TraceLocationOuterClass.SignedTraceLocation.parseFrom(
+                    DECODED_TRACE_LOCATION.decodeBase32().toByteArray()
+                )
             val verifiedTraceLocation =
                 QRCodeVerifyResult(singedTraceLocation = signedTraceLocation).toVerifiedTraceLocation()
             verifiedTraceLocation shouldBe VerifiedTraceLocation(
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifier.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifier.kt
index e0f2dc582..db0a9559c 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifier.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/DefaultQRCodeVerifier.kt
@@ -1,7 +1,7 @@
 package de.rki.coronawarnapp.eventregistration.checkins.qrcode
 
 import de.rki.coronawarnapp.eventregistration.common.decodeBase32
-import de.rki.coronawarnapp.server.protocols.internal.evreg.SignedEventOuterClass
+import de.rki.coronawarnapp.server.protocols.internal.pt.TraceLocationOuterClass
 import de.rki.coronawarnapp.util.security.SignatureValidation
 import timber.log.Timber
 import javax.inject.Inject
@@ -10,20 +10,20 @@ class DefaultQRCodeVerifier @Inject constructor(
     private val signatureValidation: SignatureValidation
 ) : QRCodeVerifier {
 
-    override suspend fun verify(encodedEvent: String): QRCodeVerifyResult {
-        Timber.tag(TAG).v("Verifying: %s", encodedEvent)
+    override suspend fun verify(encodedTraceLocation: String): QRCodeVerifyResult {
+        Timber.tag(TAG).v("Verifying: %s", encodedTraceLocation)
 
-        val signedEvent = try {
-            SignedEventOuterClass.SignedEvent.parseFrom(encodedEvent.decodeBase32().toByteArray())
+        val signedTraceLocation = try {
+            TraceLocationOuterClass.SignedTraceLocation.parseFrom(encodedTraceLocation.decodeBase32().toByteArray())
         } catch (e: Exception) {
             throw InvalidQRCodeDataException(cause = e, message = "QR-code data could not be parsed.")
         }
-        Timber.tag(TAG).d("Parsed to signed event: %s", signedEvent)
+        Timber.tag(TAG).d("Parsed to signed location: %s", signedTraceLocation)
 
         val isValid = try {
             signatureValidation.hasValidSignature(
-                signedEvent.event.toByteArray(),
-                sequenceOf(signedEvent.signature.toByteArray())
+                signedTraceLocation.location.toByteArray(),
+                sequenceOf(signedTraceLocation.signature.toByteArray())
             )
         } catch (e: Exception) {
             throw InvalidQRCodeDataException(cause = e, message = "Verification failed.")
@@ -33,7 +33,7 @@ class DefaultQRCodeVerifier @Inject constructor(
             throw InvalidQRCodeSignatureException(message = "QR-code did not match signature.")
         }
 
-        return QRCodeVerifyResult(signedEvent)
+        return QRCodeVerifyResult(signedTraceLocation)
     }
 
     companion object {
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/QRCodeVerifyResult.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/QRCodeVerifyResult.kt
index 99f87f3c2..24b3a2430 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/QRCodeVerifyResult.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/eventregistration/checkins/qrcode/QRCodeVerifyResult.kt
@@ -1,15 +1,19 @@
 package de.rki.coronawarnapp.eventregistration.checkins.qrcode
 
-import de.rki.coronawarnapp.server.protocols.internal.evreg.SignedEventOuterClass
+import de.rki.coronawarnapp.server.protocols.internal.pt.TraceLocationOuterClass
 import de.rki.coronawarnapp.util.TimeAndDateExtensions.seconds
 import org.joda.time.Instant
 
 data class QRCodeVerifyResult(
-    val singedTraceLocation: SignedEventOuterClass.SignedEvent
+    val singedTraceLocation: TraceLocationOuterClass.SignedTraceLocation
 ) {
-    fun isBeforeStartTime(now: Instant): Boolean =
-        singedTraceLocation.event.start != 0L && singedTraceLocation.event.start > now.seconds
+    fun isBeforeStartTime(now: Instant): Boolean {
+        val startTimestamp = singedTraceLocation.location.startTimestamp
+        return startTimestamp != 0L && startTimestamp > now.seconds
+    }
 
-    fun isAfterEndTime(now: Instant): Boolean =
-        singedTraceLocation.event.end != 0L && singedTraceLocation.event.end < now.seconds
+    fun isAfterEndTime(now: Instant): Boolean {
+        val endTimestamp = singedTraceLocation.location.endTimestamp
+        return endTimestamp != 0L && endTimestamp < now.seconds
+    }
 }
diff --git a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkin/VerifiedTraceLocation.kt b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkin/VerifiedTraceLocation.kt
index b8730e425..b8c61317d 100644
--- a/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkin/VerifiedTraceLocation.kt
+++ b/Corona-Warn-App/src/main/java/de/rki/coronawarnapp/ui/eventregistration/attendee/checkin/VerifiedTraceLocation.kt
@@ -18,11 +18,11 @@ data class VerifiedTraceLocation(
 ) : Parcelable
 
 fun QRCodeVerifyResult.toVerifiedTraceLocation() =
-    with(singedTraceLocation.event) {
+    with(singedTraceLocation.location) {
         VerifiedTraceLocation(
             guid = guid.toByteArray().toByteString().base64(),
-            start = start.toInstant(),
-            end = end.toInstant(),
+            start = startTimestamp.toInstant(),
+            end = endTimestamp.toInstant(),
             description = description,
             defaultCheckInLengthInMinutes = defaultCheckInLengthInMinutes
         )
-- 
GitLab