示例#1
0
        public void Should_parse_integer_to_bytes()
        {
            // Arrange
            var asn1Input = new[]
            {
                new byte[] {2, 1, 0},
                new byte[] {2, 1, 127},
                new byte[] {2, 2, 0, 127},
                new byte[] {2, 2, 0, 128},
                new byte[] {2, 3, 1, 0, 0},
                new byte[] {2, 0},
                new byte[] {2, 7, 0, 165, 163, 214, 2, 169, 62}
            }.Select(b => new MemoryStream(b));

            var expectedParsedValues = new[]
            {
                new byte[] {0},
                new byte[] {127},
                new byte[] {0, 127},
                new byte[] {128},
                new byte[] {1, 0, 0},
                new byte[0],
                new byte[] {165, 163, 214, 2, 169, 62}   //TODO as far as I undertand, it is correct that the parser removes the leading zero here. However, when parsing a PEM this must be taken into account and the parser must pad the parsed value with a leading zero
            };

            var sut = new Asn1Parser();

            // Act
            var result = asn1Input.SelectMany(sut.Parse).Cast<Integer>().Select(i => i.UnencodedValue).ToArray();

            // Assert
            result.Length.Should().Be(expectedParsedValues.Length);
            for (int i = 0; i < expectedParsedValues.Length; i++)
            {
                result[i].Should().Equal(expectedParsedValues[i]);
            }
        }