Skip to content

Commit

Permalink
CBOR: fix default instance writing and verifying tags
Browse files Browse the repository at this point in the history
  • Loading branch information
JesusMcCloud committed Jul 2, 2024
1 parent 3d8768c commit f4b43e7
Show file tree
Hide file tree
Showing 4 changed files with 267 additions and 36 deletions.
4 changes: 3 additions & 1 deletion docs/formats.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,7 +250,9 @@ When serializing, `ObjectTags` will always be encoded directly before to the dat
value-tagged property of an object-tagged type will have the value tags preceding the object tags.
Writing and verifying object tags can be toggled using the `writeObjectTags` and `verifyObjectTags` configuration
switches. Note that verifying only value tags can result in some data with superfluous tags to still deserialize
successfully, since in this case - by definition - only a partial validation of tags happens.
successfully, since in this case - by definition - only a partial validation of tags happens.
A predefined Cbor instance (in addition to the default [`Cbor.Default`](Cbor.kt) one) is available, writing and verifying
all tags as [`Cbor.Tagged`](Cbor.kt).

In addition, CBOR supports keys of all types which work just as `SerialName`s.
COSE restricts this again to strings and numbers and calls these restricted map keys *labels*. String labels can be
Expand Down
30 changes: 25 additions & 5 deletions formats/cbor/commonMain/src/kotlinx/serialization/cbor/Cbor.kt
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,30 @@ public sealed class Cbor(
) : BinaryFormat {

/**
* The default instance of [Cbor]
* The default instance of [Cbor]. Neither writes nor verifies tags. Uses indefinite length encoding by default.
*/
public companion object Default :
Cbor(false, false, true, true, true, true, true, false, false, true, false, EmptySerializersModule())
public companion object Default:
Cbor(false, false, false, false, false, false, false, false, false, false, false, EmptySerializersModule()) {

/**
* Stricter instance of [Cbor]. Writes and verifies all tags. Uses indefinite length encoding by default.
*/
public val Tagging: Cbor =
Cbor {
encodeDefaults = false
ignoreUnknownKeys = false
writeKeyTags = true
writeValueTags = true
writeObjectTags = true
verifyKeyTags = true
verifyValueTags = true
verifyObjectTags = true
writeDefiniteLengths = false
preferCborLabelsOverNames = false
alwaysUseByteString = false
serializersModule = EmptySerializersModule()
}
}

override fun <T> encodeToByteArray(serializer: SerializationStrategy<T>, value: T): ByteArray {
val output = ByteArrayOutput()
Expand Down Expand Up @@ -89,7 +109,7 @@ private class CborImpl(
writeObjectTags: Boolean,
verifyKeyTags: Boolean,
verifyValueTags: Boolean,
verifyObjectAndArrayTags: Boolean,
verifyObjectTags: Boolean,
writeDefiniteLengths: Boolean,
preferCborLabelsOverNames: Boolean,
alwaysUseByteString: Boolean,
Expand All @@ -103,7 +123,7 @@ private class CborImpl(
writeObjectTags,
verifyKeyTags,
verifyValueTags,
verifyObjectAndArrayTags,
verifyObjectTags,
writeDefiniteLengths,
preferCborLabelsOverNames,
alwaysUseByteString,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ class CborArrayTest {
val referenceHexString = "8126"
val reference = ClassAs1Array(alg = -7)

val cbor = Cbor {
val cbor = Cbor(from = Cbor.Tagging) {
writeDefiniteLengths = true
}
assertEquals(referenceHexString, cbor.encodeToHexString(ClassAs1Array.serializer(), reference))
Expand All @@ -34,7 +34,7 @@ class CborArrayTest {
val referenceHexString = "c8822663666f6f"
val reference = ClassAs2Array(alg = -7, kid = "foo")

val cbor = Cbor {
val cbor = Cbor(from = Cbor.Tagging) {
writeDefiniteLengths = true
}
assertEquals(referenceHexString, cbor.encodeToHexString(ClassAs2Array.serializer(), reference))
Expand All @@ -54,7 +54,7 @@ class CborArrayTest {
val referenceHexString = "842663626172f6a0"
val reference = ClassAs4ArrayNullable(alg = -7, kid = "bar", iv = null, array = null)

val cbor = Cbor {
val cbor = Cbor(from = Cbor.Tagging) {
writeDefiniteLengths = true
}

Expand All @@ -77,7 +77,7 @@ class CborArrayTest {
val referenceHexString = "a1656172726179c8822663626172"
val reference = ClassWithArray(array = ClassAs2Array(alg = -7, kid = "bar"))

val cbor = Cbor {
val cbor = Cbor(from = Cbor.Tagging) {
writeDefiniteLengths = true
}
assertEquals(referenceHexString, cbor.encodeToHexString(ClassWithArray.serializer(), reference))
Expand All @@ -103,7 +103,7 @@ class CborArrayTest {
val referenceHexString = "a1656172726179c9c8822663626172"
val reference = DoubleTaggedClassWithArray(array = ClassAs2Array(alg = -7, kid = "bar"))

val cbor = Cbor {
val cbor = Cbor(from = Cbor.Tagging) {
writeDefiniteLengths = true
}
assertEquals(referenceHexString, cbor.encodeToHexString(DoubleTaggedClassWithArray.serializer(), reference))
Expand Down
Loading

0 comments on commit f4b43e7

Please sign in to comment.