Skip to content

Commit

Permalink
fix CborWriter bug when writing tagged empty collections (dotnet#39786)
Browse files Browse the repository at this point in the history
  • Loading branch information
eiriktsarpalis authored and Jacksondr5 committed Aug 10, 2020
1 parent b97c509 commit ac4b197
Show file tree
Hide file tree
Showing 3 changed files with 25 additions and 11 deletions.
4 changes: 2 additions & 2 deletions src/libraries/System.Formats.Cbor/src/Resources/Strings.resx
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@
<value>The destination is too small to hold the encoded value.</value>
</data>
<data name="Cbor_PopMajorTypeMismatch" xml:space="preserve">
<value>Cannot perform the requested operation, the current context is '{0}'.</value>
<value>Cannot perform the requested operation, the current major type context is '{0}'.</value>
</data>
<data name="Cbor_NotAtEndOfDefiniteLengthDataItem" xml:space="preserve">
<value>Not at end of the definite-length data item.</value>
Expand Down Expand Up @@ -246,4 +246,4 @@
<data name="CborContentException_DefaultMessage" xml:space="preserve">
<value>The CBOR encoding is invalid.</value>
</data>
</root>
</root>
Original file line number Diff line number Diff line change
Expand Up @@ -292,6 +292,7 @@ private void PushDataItem(CborMajorType newMajorType, int? definiteLength)
_keysRequireSorting = false;
_keyEncodingRanges = null;
_keyValuePairEncodingRanges = null;
_isTagContext = false;
}

private void PopDataItem(CborMajorType typeToPop)
Expand All @@ -313,7 +314,7 @@ private void PopDataItem(CborMajorType typeToPop)

if (_isTagContext)
{
// writer expecting value after a tag data item , cannot pop the current context
// writer expecting value after a tag data item, cannot pop the current context
throw new InvalidOperationException(SR.Format(SR.Cbor_PopMajorTypeMismatch, (int)CborMajorType.Tag));
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -101,15 +101,15 @@ public static void WriteMap_IndefiniteLength_WithPatching_NestedValues_HappyPath
[InlineData(new object[] { Map, "a", "A", -1, 2, new byte[] { }, new byte[] { 1 } }, "a3200240410161616141")]
[InlineData(new object[] { Map, new object[] { Map, 3, 4, 1, 2 }, 0, new object[] { 1, 2, 3 }, 0, new string[] { "a", "b" }, 0, new string[] { Hex, "ab", "" }, 00 }, "a441ab00626162008301020300a20102030400")]
public static void WriteMap_IndefiniteLength_WithPatching_Ctap2Sorting_HappyPath(object[] values, string expectedHexEncoding)
{
byte[] expectedEncoding = expectedHexEncoding.HexToByteArray();
var writer = new CborWriter(CborConformanceMode.Ctap2Canonical, convertIndefiniteLengthEncodings: true);
Helpers.WriteMap(writer, values, useDefiniteLengthCollections: false);
byte[] actualEncoding = writer.Encode();
AssertHelper.HexEqual(expectedEncoding, actualEncoding);
}
{
byte[] expectedEncoding = expectedHexEncoding.HexToByteArray();
var writer = new CborWriter(CborConformanceMode.Ctap2Canonical, convertIndefiniteLengthEncodings: true);
Helpers.WriteMap(writer, values, useDefiniteLengthCollections: false);
byte[] actualEncoding = writer.Encode();
AssertHelper.HexEqual(expectedEncoding, actualEncoding);
}

[Theory]
[Theory]
[InlineData(new object[] { Map, "a", 1, "b", new object[] { 2, 3 } }, "a26161016162820203")]
[InlineData(new object[] { Map, "a", new object[] { 2, 3, "b", new object[] { Map, "x", -1, "y", new object[] { "z", 0 } } } }, "a161618402036162a2617820617982617a00")]
[InlineData(new object[] { "a", new object[] { Map, "b", "c" } }, "826161a161626163")]
Expand Down Expand Up @@ -384,5 +384,18 @@ public static void WriteStartMap_IndefiniteLength_NoPatching_UnsupportedConforma
var writer = new CborWriter(conformanceMode, convertIndefiniteLengthEncodings: false);
Assert.Throws<InvalidOperationException>(() => writer.WriteStartMap(null));
}

[Fact]
public static void Write_TaggedEmptyMap_ShouldSucceed()
{
var writer = new CborWriter();

writer.WriteTag(CborTag.DateTimeString);
writer.WriteStartMap(0);
writer.WriteEndMap();

byte[] encoding = writer.Encode();
Assert.Equal("C0A0", encoding.ByteArrayToHex());
}
}
}

0 comments on commit ac4b197

Please sign in to comment.