Skip to content

Commit

Permalink
Implement AsnReader.Clone (#86913)
Browse files Browse the repository at this point in the history
* Implement AsnReader.Clone

* Test that the clone is created from the correct slice
  • Loading branch information
vcsjones authored Jun 2, 2023
1 parent 443e592 commit 2855dd2
Show file tree
Hide file tree
Showing 3 changed files with 77 additions and 0 deletions.
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");
}
}
}

0 comments on commit 2855dd2

Please sign in to comment.