Skip to content

Commit a5d57b7

Browse files
committed
javatime.kt: fail fast if the generated epoch seconds is out of range.
1 parent fb858e4 commit a5d57b7

File tree

1 file changed

+17
-8
lines changed
  • firebase-dataconnect/testutil/src/main/kotlin/com/google/firebase/dataconnect/testutil/property/arbitrary

1 file changed

+17
-8
lines changed

firebase-dataconnect/testutil/src/main/kotlin/com/google/firebase/dataconnect/testutil/property/arbitrary/javatime.kt

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,6 @@ import io.kotest.property.arbitrary.enum
3232
import io.kotest.property.arbitrary.int
3333
import io.kotest.property.arbitrary.of
3434
import io.kotest.property.arbitrary.orNull
35-
import kotlin.random.nextInt
3635
import org.threeten.bp.Instant
3736
import org.threeten.bp.OffsetDateTime
3837
import org.threeten.bp.ZoneOffset
@@ -154,7 +153,11 @@ private fun Instant.toFdcFieldRegex(): Regex {
154153
return Regex(pattern)
155154
}
156155

157-
data class Nanoseconds(val nanoseconds: Int, val string: String)
156+
data class Nanoseconds(
157+
val nanoseconds: Int,
158+
val string: String,
159+
val digitCounts: JavaTimeArbs.NanosecondComponents
160+
)
158161

159162
sealed interface TimeOffset {
160163

@@ -242,6 +245,16 @@ object JavaTimeArbs {
242245
)
243246
.toInstant()
244247

248+
// The valid range below was copied from:
249+
// com.google.firebase.Timestamp.Timestamp.validateRange()
250+
require(instant.epochSecond in -62_135_596_800 until 253_402_300_800) {
251+
"internal error weppxzqj2y: " +
252+
"instant.epochSecond out of range: ${instant.epochSecond} (" +
253+
"year=$year, month=$month, day=$day, " +
254+
"hour=$hour, minute=$minute, second=$second, " +
255+
"nanosecond=$nanosecond timeOffset=$timeOffset)"
256+
}
257+
245258
val string = buildString {
246259
append(year)
247260
append('-')
@@ -350,15 +363,11 @@ object JavaTimeArbs {
350363
"$nanosecondsInt (digitCounts=$digitCounts)"
351364
}
352365

353-
Nanoseconds(nanosecondsInt, nanosecondsString)
366+
Nanoseconds(nanosecondsInt, nanosecondsString, digitCounts)
354367
}
355368
}
356369

357-
private data class NanosecondComponents(
358-
val leadingZeroes: Int,
359-
val proper: Int,
360-
val trailingZeroes: Int
361-
)
370+
data class NanosecondComponents(val leadingZeroes: Int, val proper: Int, val trailingZeroes: Int)
362371

363372
private fun nanosecondComponents(): Arb<NanosecondComponents> =
364373
arbitrary(

0 commit comments

Comments
 (0)