-
Notifications
You must be signed in to change notification settings - Fork 622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add protobuf conformance tests v1 #2404
Merged
sandwwraith
merged 4 commits into
Kotlin:dev
from
Dogacel:dogac/add-protobuf-conformance-tests-v1
Oct 10, 2023
Merged
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
16b56c1
add conformance tests using test_messages_proto3 construct google pro…
Dogacel a33e69c
Merge branch 'dev' into dogac/add-protobuf-conformance-tests-v1
Dogacel 8a89a96
update tests and descriptions
Dogacel 85e8f67
refactor tests slightly
Dogacel File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
90 changes: 90 additions & 0 deletions
90
formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/conformance/Proto3EnumTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* | ||
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf.conformance | ||
|
||
import com.google.protobuf_test_messages.proto3.* | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.protobuf.* | ||
import kotlin.test.* | ||
|
||
@Serializable | ||
data class KTestMessagesProto3Enum( | ||
@ProtoNumber(21) val optionalNestedEnum: KNestedEnum = KNestedEnum.FOO, | ||
@ProtoNumber(22) val optionalForeignEnum: KForeignEnum = KForeignEnum.FOREIGN_FOO, | ||
@ProtoNumber(23) val optionalAliasedEnum: KAliasedEnum = KAliasedEnum.ALIAS_FOO, | ||
) { | ||
enum class KNestedEnum { | ||
@ProtoNumber(0) | ||
FOO, | ||
|
||
@ProtoNumber(1) | ||
BAR, | ||
|
||
@ProtoNumber(2) | ||
BAZ, | ||
|
||
@ProtoNumber(-1) | ||
NEG; | ||
|
||
fun toProto() = TestMessagesProto3.TestAllTypesProto3.NestedEnum.valueOf(this.name) | ||
} | ||
|
||
|
||
enum class KAliasedEnum { | ||
@ProtoNumber(0) | ||
ALIAS_FOO, | ||
|
||
@ProtoNumber(1) | ||
ALIAS_BAR, | ||
|
||
@ProtoNumber(2) | ||
ALIAS_BAZ, | ||
|
||
@ProtoNumber(2) | ||
MOO, | ||
|
||
@ProtoNumber(2) | ||
moo, | ||
|
||
@ProtoNumber(2) | ||
bAz; | ||
|
||
fun toProto() = TestMessagesProto3.TestAllTypesProto3.AliasedEnum.valueOf(this.name) | ||
} | ||
} | ||
|
||
enum class KForeignEnum { | ||
@ProtoNumber(0) | ||
FOREIGN_FOO, | ||
|
||
@ProtoNumber(1) | ||
FOREIGN_BAR, | ||
|
||
@ProtoNumber(2) | ||
FOREIGN_BAZ; | ||
|
||
fun toProto() = TestMessagesProto3.ForeignEnum.valueOf(this.name) | ||
} | ||
|
||
class Proto3EnumTest { | ||
@Test | ||
fun default() { | ||
val message = KTestMessagesProto3Enum( | ||
optionalNestedEnum = KTestMessagesProto3Enum.KNestedEnum.NEG, | ||
optionalForeignEnum = KForeignEnum.FOREIGN_BAR, | ||
optionalAliasedEnum = KTestMessagesProto3Enum.KAliasedEnum.ALIAS_BAR | ||
) | ||
|
||
val bytes = ProtoBuf.encodeToByteArray(message) | ||
val restored = TestMessagesProto3.TestAllTypesProto3.parseFrom(bytes) | ||
|
||
assertEquals(message.optionalNestedEnum.toProto(), restored.optionalNestedEnum) | ||
assertEquals(message.optionalForeignEnum.toProto(), restored.optionalForeignEnum) | ||
assertEquals(message.optionalAliasedEnum.toProto(), restored.optionalAliasedEnum) | ||
|
||
val restoredMessage = ProtoBuf.decodeFromByteArray<KTestMessagesProto3Enum>(restored.toByteArray()) | ||
assertEquals(message, restoredMessage) | ||
} | ||
} |
154 changes: 154 additions & 0 deletions
154
formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/conformance/Proto3MapTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,154 @@ | ||
/* | ||
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf.conformance | ||
|
||
import com.google.protobuf_test_messages.proto3.* | ||
import io.kotlintest.properties.* | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.protobuf.* | ||
import kotlin.test.* | ||
|
||
@Serializable | ||
data class KTestMessagesProto3Map( | ||
@ProtoNumber(56) val mapInt32Int32: Map<Int, Int> = emptyMap(), | ||
@ProtoNumber(57) val mapInt64Int64: Map<Long, Long> = emptyMap(), | ||
@ProtoNumber(58) val mapUint32Uint32: Map<UInt, UInt> = emptyMap(), | ||
@ProtoNumber(59) val mapUint64Uint64: Map<ULong, ULong> = emptyMap(), | ||
@ProtoNumber(60) val mapSint32Sint32: Map<Int, Int> = emptyMap(), | ||
@ProtoNumber(61) val mapSint64Sint64: Map<Long, Long> = emptyMap(), | ||
@ProtoNumber(62) val mapFixed32Fixed32: Map<Int, Int> = emptyMap(), | ||
@ProtoNumber(63) val mapFixed64Fixed64: Map<Long, Long> = emptyMap(), | ||
@ProtoNumber(64) val mapSfixed32Sfixed32: Map<Int, Int> = emptyMap(), | ||
@ProtoNumber(65) val mapSfixed64Sfixed64: Map<Long, Long> = emptyMap(), | ||
@ProtoNumber(66) val mapInt32Float: Map<Int, Float> = emptyMap(), | ||
@ProtoNumber(67) val mapInt32Double: Map<Int, Double> = emptyMap(), | ||
@ProtoNumber(68) val mapBoolBool: Map<Boolean, Boolean> = emptyMap(), | ||
@ProtoNumber(69) val mapStringString: Map<String, String> = emptyMap(), | ||
@ProtoNumber(70) val mapStringBytes: Map<String, ByteArray> = emptyMap(), | ||
@ProtoNumber(71) val mapStringNestedMessage: Map<String, KTestMessagesProto3Message.KNestedMessage> = emptyMap(), | ||
@ProtoNumber(72) val mapStringForeignMessage: Map<String, KForeignMessage> = emptyMap(), | ||
@ProtoNumber(73) val mapStringNestedEnum: Map<String, KTestMessagesProto3Enum.KNestedEnum> = emptyMap(), | ||
@ProtoNumber(74) val mapStringForeignEnum: Map<String, KForeignEnum> = emptyMap(), | ||
) | ||
|
||
class Proto3MapTest { | ||
@Test | ||
fun default() { | ||
val message = KTestMessagesProto3Map( | ||
mapInt32Int32 = Gen.map(Gen.int(), Gen.int()).generate(), | ||
mapInt64Int64 = Gen.map(Gen.long(), Gen.long()).generate(), | ||
mapUint32Uint32 = Gen.map(Gen.int().map { it.toUInt() }, Gen.int().map { it.toUInt() }).generate(), | ||
mapUint64Uint64 = Gen.map(Gen.int().map { it.toULong() }, Gen.int().map { it.toULong() }).generate(), | ||
mapInt32Float = Gen.map(Gen.int(), Gen.float()).generate(), | ||
mapInt32Double = Gen.map(Gen.int(), Gen.double()).generate(), | ||
mapBoolBool = Gen.map(Gen.bool(), Gen.bool()).generate(), | ||
mapStringString = Gen.map(Gen.string(), Gen.string()).generate(), | ||
mapStringBytes = Gen.map(Gen.string(), Gen.string().map { it.toByteArray() }).generate(), | ||
mapStringNestedMessage = mapOf( | ||
"asd_1" to KTestMessagesProto3Message.KNestedMessage( | ||
1, | ||
null | ||
), | ||
"asi_#" to KTestMessagesProto3Message.KNestedMessage( | ||
2, | ||
KTestMessagesProto3Message( | ||
KTestMessagesProto3Message.KNestedMessage(3, null), | ||
) | ||
) | ||
), | ||
mapStringForeignMessage = mapOf( | ||
"" to KForeignMessage(1), | ||
"-2" to KForeignMessage(-12), | ||
), | ||
mapStringNestedEnum = Gen.map( | ||
Gen.string(), Gen.oneOf( | ||
KTestMessagesProto3Enum.KNestedEnum.entries, | ||
) | ||
).generate(), | ||
mapStringForeignEnum = Gen.map( | ||
Gen.string(), Gen.oneOf( | ||
KForeignEnum.entries, | ||
) | ||
).generate(), | ||
) | ||
|
||
val bytes = ProtoBuf.encodeToByteArray(message) | ||
val restored = TestMessagesProto3.TestAllTypesProto3.parseFrom(bytes) | ||
|
||
|
||
assertEquals(message.mapInt32Int32, restored.mapInt32Int32Map) | ||
assertEquals(message.mapInt64Int64, restored.mapInt64Int64Map) | ||
assertEquals( | ||
message.mapUint32Uint32, | ||
restored.mapUint32Uint32Map.map { it.key.toUInt() to it.value.toUInt() }.toMap() | ||
) | ||
assertEquals( | ||
message.mapUint64Uint64, | ||
restored.mapUint64Uint64Map.map { it.key.toULong() to it.value.toULong() }.toMap() | ||
) | ||
assertEquals(message.mapInt32Float, restored.mapInt32FloatMap) | ||
assertEquals(message.mapInt32Double, restored.mapInt32DoubleMap) | ||
assertEquals(message.mapBoolBool, restored.mapBoolBoolMap) | ||
assertEquals(message.mapStringString, restored.mapStringStringMap) | ||
assertContentEquals( | ||
message.mapStringBytes.mapValues { it.value.toString(Charsets.UTF_32) }.entries.toList(), | ||
restored.mapStringBytesMap.mapValues { it.value.toByteArray().toString(Charsets.UTF_32) }.entries.toList() | ||
) | ||
assertEquals( | ||
message.mapStringNestedMessage.mapValues { it.value.toProto() }, | ||
restored.mapStringNestedMessageMap | ||
) | ||
assertEquals( | ||
message.mapStringForeignMessage.mapValues { it.value.toProto() }, | ||
restored.mapStringForeignMessageMap | ||
) | ||
assertEquals( | ||
message.mapStringNestedEnum.mapValues { it.value.name }, | ||
restored.mapStringNestedEnumMap.mapValues { it.value.name }, | ||
) | ||
assertEquals( | ||
message.mapStringForeignEnum.mapValues { it.value.name }, | ||
restored.mapStringForeignEnumMap.mapValues { it.value.name } | ||
) | ||
|
||
val restoredMessage = ProtoBuf.decodeFromByteArray<KTestMessagesProto3Map>(restored.toByteArray()) | ||
assertEquals(message.copy(mapStringBytes = mapOf()), restoredMessage.copy(mapStringBytes = mapOf())) | ||
} | ||
|
||
@Test | ||
@Ignore | ||
// Issue: https://github.com/Kotlin/kotlinx.serialization/issues/2417 | ||
fun signedAndFixed() { | ||
val message = KTestMessagesProto3Map( | ||
mapSint32Sint32 = Gen.map(Gen.int(), Gen.int()).generate(), | ||
mapSint64Sint64 = Gen.map(Gen.long(), Gen.long()).generate(), | ||
mapFixed32Fixed32 = Gen.map(Gen.int(), Gen.int()).generate(), | ||
mapFixed64Fixed64 = Gen.map(Gen.long(), Gen.long()).generate(), | ||
mapSfixed32Sfixed32 = Gen.map(Gen.int(), Gen.int()).generate(), | ||
mapSfixed64Sfixed64 = Gen.map(Gen.long(), Gen.long()).generate(), | ||
) | ||
|
||
val bytes = ProtoBuf.encodeToByteArray(message) | ||
val restored = TestMessagesProto3.TestAllTypesProto3.parseFrom(bytes) | ||
|
||
|
||
assertContentEquals(message.mapSint32Sint32.entries.toList(), restored.mapSint32Sint32Map.entries.toList()) | ||
assertContentEquals(message.mapSint64Sint64.entries.toList(), restored.mapSint64Sint64Map.entries.toList()) | ||
assertContentEquals(message.mapFixed32Fixed32.entries.toList(), restored.mapFixed32Fixed32Map.entries.toList()) | ||
assertContentEquals(message.mapFixed64Fixed64.entries.toList(), restored.mapFixed64Fixed64Map.entries.toList()) | ||
assertContentEquals( | ||
message.mapSfixed32Sfixed32.entries.toList(), | ||
restored.mapSfixed32Sfixed32Map.entries.toList() | ||
) | ||
assertContentEquals( | ||
message.mapSfixed64Sfixed64.entries.toList(), | ||
restored.mapSfixed64Sfixed64Map.entries.toList() | ||
) | ||
|
||
|
||
val restoredMessage = ProtoBuf.decodeFromByteArray<KTestMessagesProto3Map>(restored.toByteArray()) | ||
assertEquals(message, restoredMessage) | ||
} | ||
} |
65 changes: 65 additions & 0 deletions
65
formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/conformance/Proto3MessageTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,65 @@ | ||
/* | ||
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf.conformance | ||
|
||
import com.google.protobuf_test_messages.proto3.* | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.protobuf.* | ||
import kotlin.test.* | ||
|
||
@Serializable | ||
data class KTestMessagesProto3Message( | ||
@ProtoNumber(18) val optionalNestedMessage: KNestedMessage? = null, | ||
@ProtoNumber(19) val optionalForeignMessage: KForeignMessage? = null, | ||
) { | ||
@Serializable | ||
data class KNestedMessage( | ||
@ProtoNumber(1) val a: Int = 0, | ||
@ProtoNumber(2) val corecursive: KTestMessagesProto3Message? = null, | ||
) { | ||
fun toProto(): TestMessagesProto3.TestAllTypesProto3.NestedMessage = | ||
TestMessagesProto3.TestAllTypesProto3.NestedMessage.parseFrom( | ||
ProtoBuf.encodeToByteArray(this) | ||
) | ||
} | ||
} | ||
|
||
@Serializable | ||
data class KForeignMessage( | ||
@ProtoNumber(1) val c: Int = 0, | ||
) { | ||
fun toProto(): TestMessagesProto3.ForeignMessage = | ||
TestMessagesProto3.ForeignMessage.parseFrom( | ||
ProtoBuf.encodeToByteArray(this) | ||
) | ||
} | ||
|
||
class Proto3MessageTest { | ||
@Test | ||
fun default() { | ||
val message = KTestMessagesProto3Message( | ||
optionalNestedMessage = KTestMessagesProto3Message.KNestedMessage( | ||
a = 150, | ||
corecursive = KTestMessagesProto3Message( | ||
optionalNestedMessage = KTestMessagesProto3Message.KNestedMessage( | ||
a = 42, | ||
) | ||
) | ||
), | ||
optionalForeignMessage = KForeignMessage( | ||
c = 150, | ||
) | ||
) | ||
|
||
val bytes = ProtoBuf.encodeToByteArray(message) | ||
val restored = TestMessagesProto3.TestAllTypesProto3.parseFrom(bytes) | ||
assertEquals(message.optionalNestedMessage?.a, restored.optionalNestedMessage.a) | ||
assertEquals( | ||
message.optionalNestedMessage?.corecursive?.optionalNestedMessage?.a, | ||
restored.optionalNestedMessage.corecursive.optionalNestedMessage.a | ||
) | ||
assertEquals(message.optionalForeignMessage?.c, restored.optionalForeignMessage.c) | ||
} | ||
} |
75 changes: 75 additions & 0 deletions
75
formats/protobuf/jvmTest/src/kotlinx/serialization/protobuf/conformance/Proto3OneofTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
/* | ||
* Copyright 2017-2023 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license. | ||
*/ | ||
|
||
package kotlinx.serialization.protobuf.conformance | ||
|
||
import com.google.protobuf_test_messages.proto3.* | ||
import kotlinx.serialization.* | ||
import kotlinx.serialization.protobuf.* | ||
import kotlin.test.* | ||
|
||
@Serializable | ||
data class KTestMessageProto3Oneof( | ||
@ProtoNumber(111) val oneofUint32: UInt? = null, | ||
@ProtoNumber(112) val oneofNestedMessage: KTestMessagesProto3Message.KNestedMessage? = null, | ||
@ProtoNumber(113) val oneofString: String? = null, | ||
@ProtoNumber(114) val oneofBytes: ByteArray? = null, | ||
@ProtoNumber(115) val oneofBool: Boolean? = null, | ||
@ProtoNumber(116) val oneofUint64: ULong? = null, | ||
@ProtoNumber(117) val oneofFloat: Float? = null, | ||
@ProtoNumber(118) val oneofDouble: Double? = null, | ||
@ProtoNumber(119) val oneofEnum: KTestMessagesProto3Enum.KNestedEnum? = null, | ||
) { | ||
init { | ||
require( | ||
listOf( | ||
oneofUint32, | ||
oneofNestedMessage, | ||
oneofString, | ||
oneofBytes, | ||
oneofBool, | ||
oneofUint64, | ||
oneofFloat, | ||
oneofDouble, | ||
oneofEnum, | ||
).count { it != null } == 1 | ||
) | ||
} | ||
} | ||
|
||
class Proto3OneofTest { | ||
@Test | ||
fun default() { | ||
listOf( | ||
KTestMessageProto3Oneof(oneofUint32 = 150u), | ||
KTestMessageProto3Oneof(oneofNestedMessage = KTestMessagesProto3Message.KNestedMessage(a = 150)), | ||
KTestMessageProto3Oneof(oneofString = "150"), | ||
KTestMessageProto3Oneof(oneofBytes = "150".toByteArray()), | ||
KTestMessageProto3Oneof(oneofBool = true), | ||
KTestMessageProto3Oneof(oneofUint64 = 150uL), | ||
KTestMessageProto3Oneof(oneofFloat = 150f), | ||
KTestMessageProto3Oneof(oneofDouble = 150.0), | ||
KTestMessageProto3Oneof(oneofEnum = KTestMessagesProto3Enum.KNestedEnum.BAR), | ||
).forEach { message -> | ||
val bytes = ProtoBuf.encodeToByteArray(message) | ||
val restored = TestMessagesProto3.TestAllTypesProto3.parseFrom(bytes) | ||
if (message.oneofUint32 != null) assertEquals(message.oneofUint32, restored.oneofUint32.toUInt()) | ||
if (message.oneofNestedMessage != null) assertEquals( | ||
message.oneofNestedMessage.a, | ||
restored.oneofNestedMessage?.a | ||
) | ||
if (message.oneofString != null) assertEquals(message.oneofString, restored.oneofString) | ||
if (message.oneofBytes != null) assertContentEquals(message.oneofBytes, restored.oneofBytes.toByteArray()) | ||
if (message.oneofBool != null) assertEquals(message.oneofBool, restored.oneofBool) | ||
if (message.oneofUint64 != null) assertEquals(message.oneofUint64, restored.oneofUint64.toULong()) | ||
if (message.oneofFloat != null) assertEquals(message.oneofFloat, restored.oneofFloat) | ||
if (message.oneofDouble != null) assertEquals(message.oneofDouble, restored.oneofDouble) | ||
if (message.oneofEnum != null) assertEquals(message.oneofEnum.name, restored.oneofEnum?.name) | ||
|
||
val restoredMessage = ProtoBuf.decodeFromByteArray<KTestMessageProto3Oneof>(restored.toByteArray()) | ||
assertEquals(message, restoredMessage.copy(oneofBytes = message.oneofBytes)) | ||
assertContentEquals(message.oneofBytes, restoredMessage.oneofBytes) | ||
} | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Rather than testing all types in a single function it would make sense to have the inner loop in a separate function and just have separate test functions for the different data members. This would help with testing individual types, and brings more visibility (each actual test function would just call the actual implementation with the message as parameter). It would also allow testing each type even in case of some failure.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I agree with that. This would allow to determine what exact instance of
KTestMessageProto3Oneof
caused failure.Or, If you prefer a different way, you can add an assertion message to each assertion, but this looks much more boilerplate-ish.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I tried to reduce boilerplate by creating some verification functions. Let me know how it looks now.