Skip to content
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

Fix CborWriter bug when writing tagged empty collections #39786

Merged
merged 1 commit into from
Jul 22, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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());
}
}
}