Skip to content

Implement AsnReader.Clone #86913

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

Merged
merged 3 commits into from
Jun 2, 2023
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
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,7 @@ public partial class AsnReader
public AsnReader(System.ReadOnlyMemory<byte> data, System.Formats.Asn1.AsnEncodingRules ruleSet, System.Formats.Asn1.AsnReaderOptions options = default(System.Formats.Asn1.AsnReaderOptions)) { }
public bool HasData { get { throw null; } }
public System.Formats.Asn1.AsnEncodingRules RuleSet { get { throw null; } }
public System.Formats.Asn1.AsnReader Clone() { throw null; }
public System.ReadOnlyMemory<byte> PeekContentBytes() { throw null; }
public System.ReadOnlyMemory<byte> PeekEncodedValue() { throw null; }
public System.Formats.Asn1.Asn1Tag PeekTag() { throw null; }
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -807,6 +807,15 @@ public ReadOnlyMemory<byte> ReadEncodedValue()
return encodedValue;
}

/// <summary>
/// Clones the current reader.
/// </summary>
/// <returns>A clone of the current reader.</returns>
/// <remarks>
/// This does not create a clone of the ASN.1 data, only the reader's state is cloned.
/// </remarks>
public AsnReader Clone() => new AsnReader(_data, RuleSet, _options);

private AsnReader CloneAtSlice(int start, int length)
{
return new AsnReader(_data.Slice(start, length), RuleSet, _options);
Expand Down
67 changes: 67 additions & 0 deletions src/libraries/System.Formats.Asn1/tests/Reader/ReaderStateTests.cs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.

using Test.Cryptography;
using Xunit;

namespace System.Formats.Asn1.Tests.Reader
Expand Down Expand Up @@ -30,5 +31,71 @@ public static void HasDataAndThrowIfNotEmpty_StartsEmpty()
// Assert.NoThrow
reader.ThrowIfNotEmpty();
}

[Fact]
public static void Clone_CopiesCurrentState()
{
// Sequence {
// SetOf {
// UtcTime 500405000012Z
// Null
// }
// }
// Verify the options are preserved in the clone by observing them:
// this is an incorrectly sorted SET OF with a date of 50/04/05 that should be 2050, not 1950.
ReadOnlyMemory<byte> asn = "30133111170D3530303430353030303031325A0500".HexToByteArray();

AsnReaderOptions options = new AsnReaderOptions
{
UtcTimeTwoDigitYearMax = 2050,
SkipSetSortOrderVerification = true,
};

AsnReader sequence = new AsnReader(asn, AsnEncodingRules.DER, options);
AsnReader reader = sequence.ReadSequence();
sequence.ThrowIfNotEmpty();

AsnReader clone = reader.Clone();
Assert.Equal(reader.RuleSet, clone.RuleSet);

AssertReader(reader);
Assert.False(reader.HasData, "reader.HasData");
Assert.True(clone.HasData, "clone.HasData");

AssertReader(clone);
Assert.False(clone.HasData, "clone.HasData");

static void AssertReader(AsnReader reader)
{
AsnReader setOf = reader.ReadSetOf();
reader.ThrowIfNotEmpty();

DateTimeOffset dateTime = setOf.ReadUtcTime();
Assert.Equal(2050, dateTime.Year);
setOf.ReadNull();
setOf.ThrowIfNotEmpty();
}
}

[Fact]
public static void Clone_Empty()
{
AsnReader reader = new AsnReader(ReadOnlyMemory<byte>.Empty, AsnEncodingRules.DER);
AsnReader clone = reader.Clone();
Assert.False(reader.HasData, "reader.HasData");
Assert.False(clone.HasData, "clone.HasData");
}

[Fact]
public static void Clone_SameUnderlyingData()
{
ReadOnlyMemory<byte> data = "04050102030405".HexToByteArray();
AsnReader reader = new AsnReader(data, AsnEncodingRules.DER);
AsnReader clone = reader.Clone();

Assert.True(reader.TryReadPrimitiveOctetString(out ReadOnlyMemory<byte> readerData));
Assert.True(clone.TryReadPrimitiveOctetString(out ReadOnlyMemory<byte> cloneData));
Assert.True(readerData.Span == cloneData.Span, "readerData == cloneData");
}
}
}