|
1 | 1 | // Licensed to the .NET Foundation under one or more agreements. |
2 | 2 | // The .NET Foundation licenses this file to you under the MIT license. |
3 | 3 |
|
| 4 | +using Test.Cryptography; |
4 | 5 | using Xunit; |
5 | 6 |
|
6 | 7 | namespace System.Formats.Asn1.Tests.Reader |
@@ -30,5 +31,71 @@ public static void HasDataAndThrowIfNotEmpty_StartsEmpty() |
30 | 31 | // Assert.NoThrow |
31 | 32 | reader.ThrowIfNotEmpty(); |
32 | 33 | } |
| 34 | + |
| 35 | + [Fact] |
| 36 | + public static void Clone_CopiesCurrentState() |
| 37 | + { |
| 38 | + // Sequence { |
| 39 | + // SetOf { |
| 40 | + // UtcTime 500405000012Z |
| 41 | + // Null |
| 42 | + // } |
| 43 | + // } |
| 44 | + // Verify the options are preserved in the clone by observing them: |
| 45 | + // this is an incorrectly sorted SET OF with a date of 50/04/05 that should be 2050, not 1950. |
| 46 | + ReadOnlyMemory<byte> asn = "30133111170D3530303430353030303031325A0500".HexToByteArray(); |
| 47 | + |
| 48 | + AsnReaderOptions options = new AsnReaderOptions |
| 49 | + { |
| 50 | + UtcTimeTwoDigitYearMax = 2050, |
| 51 | + SkipSetSortOrderVerification = true, |
| 52 | + }; |
| 53 | + |
| 54 | + AsnReader sequence = new AsnReader(asn, AsnEncodingRules.DER, options); |
| 55 | + AsnReader reader = sequence.ReadSequence(); |
| 56 | + sequence.ThrowIfNotEmpty(); |
| 57 | + |
| 58 | + AsnReader clone = reader.Clone(); |
| 59 | + Assert.Equal(reader.RuleSet, clone.RuleSet); |
| 60 | + |
| 61 | + AssertReader(reader); |
| 62 | + Assert.False(reader.HasData, "reader.HasData"); |
| 63 | + Assert.True(clone.HasData, "clone.HasData"); |
| 64 | + |
| 65 | + AssertReader(clone); |
| 66 | + Assert.False(clone.HasData, "clone.HasData"); |
| 67 | + |
| 68 | + static void AssertReader(AsnReader reader) |
| 69 | + { |
| 70 | + AsnReader setOf = reader.ReadSetOf(); |
| 71 | + reader.ThrowIfNotEmpty(); |
| 72 | + |
| 73 | + DateTimeOffset dateTime = setOf.ReadUtcTime(); |
| 74 | + Assert.Equal(2050, dateTime.Year); |
| 75 | + setOf.ReadNull(); |
| 76 | + setOf.ThrowIfNotEmpty(); |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + [Fact] |
| 81 | + public static void Clone_Empty() |
| 82 | + { |
| 83 | + AsnReader reader = new AsnReader(ReadOnlyMemory<byte>.Empty, AsnEncodingRules.DER); |
| 84 | + AsnReader clone = reader.Clone(); |
| 85 | + Assert.False(reader.HasData, "reader.HasData"); |
| 86 | + Assert.False(clone.HasData, "clone.HasData"); |
| 87 | + } |
| 88 | + |
| 89 | + [Fact] |
| 90 | + public static void Clone_SameUnderlyingData() |
| 91 | + { |
| 92 | + ReadOnlyMemory<byte> data = "04050102030405".HexToByteArray(); |
| 93 | + AsnReader reader = new AsnReader(data, AsnEncodingRules.DER); |
| 94 | + AsnReader clone = reader.Clone(); |
| 95 | + |
| 96 | + Assert.True(reader.TryReadPrimitiveOctetString(out ReadOnlyMemory<byte> readerData)); |
| 97 | + Assert.True(clone.TryReadPrimitiveOctetString(out ReadOnlyMemory<byte> cloneData)); |
| 98 | + Assert.True(readerData.Span == cloneData.Span, "readerData == cloneData"); |
| 99 | + } |
33 | 100 | } |
34 | 101 | } |
0 commit comments