|
| 1 | +/* |
| 2 | + * Copyright 2017-2024 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. |
| 3 | + */ |
| 4 | + |
| 5 | +package kotlinx.serialization.protobuf |
| 6 | + |
| 7 | + |
| 8 | +import kotlinx.serialization.* |
| 9 | +import kotlin.test.* |
| 10 | + |
| 11 | +class InvalidFieldNumberTest { |
| 12 | + |
| 13 | + @Serializable |
| 14 | + data class Holder(val value: Int) |
| 15 | + |
| 16 | + @Serializable |
| 17 | + data class ListHolder(val value: List<Int>) |
| 18 | + |
| 19 | + @Serializable |
| 20 | + data class ZeroProtoNumber(@ProtoNumber(0) val value: Int) |
| 21 | + |
| 22 | + @Serializable |
| 23 | + data class NegativeProtoNumber(@ProtoNumber(-5) val value: Int) |
| 24 | + |
| 25 | + @Test |
| 26 | + fun testDeserializeZeroInput() { |
| 27 | + assertFailsWithMessage<SerializationException>("0 is not allowed as the protobuf field number in kotlinx.serialization.protobuf.InvalidFieldNumberTest.Holder, the input bytes may have been corrupted") { |
| 28 | + // first value with field number = 0 |
| 29 | + val hexString = "000f" |
| 30 | + ProtoBuf.decodeFromHexString<Holder>(hexString) |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + @Test |
| 35 | + fun testDeserializeZeroInputForElement() { |
| 36 | + assertFailsWithMessage<SerializationException>("0 is not allowed as the protobuf field number in kotlinx.serialization.protobuf.InvalidFieldNumberTest.ListHolder, the input bytes may have been corrupted") { |
| 37 | + // first element with field number = 0 |
| 38 | + val hexString = "000f" |
| 39 | + ProtoBuf.decodeFromHexString<ListHolder>(hexString) |
| 40 | + } |
| 41 | + } |
| 42 | + |
| 43 | + @Test |
| 44 | + fun testSerializeZeroProtoNumber() { |
| 45 | + assertFailsWithMessage<SerializationException>("0 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.ZeroProtoNumber', because protobuf supports field numbers in range 1..2147483647") { |
| 46 | + ProtoBuf.encodeToHexString(ZeroProtoNumber(42)) |
| 47 | + } |
| 48 | + } |
| 49 | + |
| 50 | + @Test |
| 51 | + fun testDeserializeZeroProtoNumber() { |
| 52 | + assertFailsWithMessage<SerializationException>("0 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.ZeroProtoNumber', because protobuf supports field numbers in range 1..2147483647") { |
| 53 | + ProtoBuf.decodeFromHexString<ZeroProtoNumber>("000f") |
| 54 | + } |
| 55 | + } |
| 56 | + |
| 57 | + @Test |
| 58 | + fun testSerializeNegativeProtoNumber() { |
| 59 | + assertFailsWithMessage<SerializationException>("-5 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.NegativeProtoNumber', because protobuf supports field numbers in range 1..2147483647") { |
| 60 | + ProtoBuf.encodeToHexString(NegativeProtoNumber(42)) |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + @Test |
| 65 | + fun testDeserializeNegativeProtoNumber() { |
| 66 | + assertFailsWithMessage<SerializationException>("-5 is not allowed in ProtoNumber for property 'value' of 'kotlinx.serialization.protobuf.InvalidFieldNumberTest.NegativeProtoNumber', because protobuf supports field numbers in range 1..2147483647") { |
| 67 | + ProtoBuf.decodeFromHexString<NegativeProtoNumber>("000f") |
| 68 | + } |
| 69 | + } |
| 70 | +} |
0 commit comments