diff --git a/core/commonMain/src/kotlinx/serialization/internal/Enums.kt b/core/commonMain/src/kotlinx/serialization/internal/Enums.kt index 5644bd4c5..1e6201546 100644 --- a/core/commonMain/src/kotlinx/serialization/internal/Enums.kt +++ b/core/commonMain/src/kotlinx/serialization/internal/Enums.kt @@ -67,16 +67,22 @@ internal class EnumSerializer>( override fun serialize(encoder: Encoder, value: T) { val index = values.indexOf(value) - check(index != -1) { - "$value is not a valid enum ${descriptor.serialName}, must be one of ${values.contentToString()}" + if (index == -1) { + throw SerializationException( + "$value is not a valid enum ${descriptor.serialName}, " + + "must be one of ${values.contentToString()}" + ) } encoder.encodeEnum(descriptor, index) } override fun deserialize(decoder: Decoder): T { val index = decoder.decodeEnum(descriptor) - check(index in values.indices) { - "$index is not among valid $${descriptor.serialName} enum values, values size is ${values.size}" + if (index !in values.indices) { + throw SerializationException( + "$index is not among valid ${descriptor.serialName} enum values, " + + "values size is ${values.size}" + ) } return values[index] } diff --git a/formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/internal/Streams.kt b/formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/internal/Streams.kt index 540b7b46d..da712fa40 100644 --- a/formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/internal/Streams.kt +++ b/formats/protobuf/commonMain/src/kotlinx/serialization/protobuf/internal/Streams.kt @@ -97,7 +97,7 @@ internal class ByteArrayInput(private var array: ByteArray, private val endIndex private fun readVarint64SlowPath(): Long { var result = 0L var shift = 0 - while (shift != 64) { + while (shift < 64) { val byte = read() result = result or ((byte and 0x7F).toLong() shl shift) if (byte and 0x80 == 0) { @@ -105,13 +105,13 @@ internal class ByteArrayInput(private var array: ByteArray, private val endIndex } shift += 7 } - throw SerializationException("Varint too long: exceeded 64 bits") + throw SerializationException("Input stream is malformed: Varint too long (exceeded 64 bits)") } private fun readVarint32SlowPath(): Int { var result = 0 var shift = 0 - while (shift != 32) { + while (shift < 32) { val byte = read() result = result or ((byte and 0x7F) shl shift) if (byte and 0x80 == 0) { @@ -119,7 +119,7 @@ internal class ByteArrayInput(private var array: ByteArray, private val endIndex } shift += 7 } - throw SerializationException("Varint too long: exceeded 32 bits") + throw SerializationException("Input stream is malformed: Varint too long (exceeded 32 bits)") } } diff --git a/formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufEnumTest.kt b/formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufEnumTest.kt new file mode 100644 index 000000000..188fb93a6 --- /dev/null +++ b/formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufEnumTest.kt @@ -0,0 +1,39 @@ +/* + * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. + */ + +package kotlinx.serialization.protobuf + +import kotlinx.serialization.* +import kotlin.test.* + +class ProtobufEnumTest { + + enum class SomeEnum { ALPHA, BETA, GAMMA } + + @Serializable + data class EnumWithUnion(@ProtoNumber(5) val s: String, + @ProtoNumber(6) val e: SomeEnum = SomeEnum.ALPHA, + @ProtoNumber(7) val i: Int = 42) + + @Test + fun testEnumWithUnion() { + val data = EnumWithUnion("foo", SomeEnum.BETA) + val hex = ProtoBuf.encodeToHexString(EnumWithUnion.serializer(), data) + val restored = ProtoBuf.decodeFromHexString(EnumWithUnion.serializer(), hex) + assertEquals(data, restored) + } + + @Serializable + class EnumHolder(val e: SomeEnum) + + @Test + fun testUnknownValue() { + val bytes = ProtoBuf.encodeToByteArray(EnumHolder(SomeEnum.ALPHA)) + bytes[1] = 3 + assertFailsWith { ProtoBuf.decodeFromByteArray(bytes) } + bytes[1] = -1 + assertFailsWith { ProtoBuf.decodeFromByteArray(bytes) } + + } +} diff --git a/formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufUnionEnumTest.kt b/formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufUnionEnumTest.kt deleted file mode 100644 index 23cc2c0d4..000000000 --- a/formats/protobuf/commonTest/src/kotlinx/serialization/protobuf/ProtobufUnionEnumTest.kt +++ /dev/null @@ -1,26 +0,0 @@ -/* - * Copyright 2017-2020 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. - */ - -package kotlinx.serialization.protobuf - -import kotlinx.serialization.* -import kotlin.test.* - -class ProtobufUnionEnumTest { - - enum class SomeEnum { ALPHA, BETA, GAMMA } - - @Serializable - data class WithUnions(@ProtoNumber(5) val s: String, - @ProtoNumber(6) val e: SomeEnum = SomeEnum.ALPHA, - @ProtoNumber(7) val i: Int = 42) - - @Test - fun testEnum() { - val data = WithUnions("foo", SomeEnum.BETA) - val hex = ProtoBuf.encodeToHexString(WithUnions.serializer(), data) - val restored = ProtoBuf.decodeFromHexString(WithUnions.serializer(), hex) - assertEquals(data, restored) - } -}