Esempio n. 1
0
 public static bool TryCopyBMPString(
     this AsnReader reader,
     Span <char> destination,
     out int charsWritten)
 {
     return(reader.TryReadCharacterString(
                destination,
                UniversalTagNumber.BMPString,
                out charsWritten));
 }
        private static void TryCopyUTF8String_Throws_Helper(AsnEncodingRules ruleSet, byte[] inputData)
        {
            char[] outputData = new char[inputData.Length + 1];
            outputData[0] = 'a';

            int       bytesWritten = -1;
            AsnReader reader       = new AsnReader(inputData, ruleSet);

            Assert.Throws <AsnContentException>(
                () => reader.TryReadCharacterString(outputData, UniversalTagNumber.UTF8String, out bytesWritten));

            Assert.Equal(-1, bytesWritten);
            Assert.Equal('a', outputData[0]);
        }
Esempio n. 3
0
        public static void TryCopyT61String(
            AsnEncodingRules ruleSet,
            string inputHex,
            string expectedValue)
        {
            byte[] inputData = inputHex.HexToByteArray();
            char[] output    = new char[expectedValue.Length];

            AsnReader reader = new AsnReader(inputData, ruleSet);
            bool      copied;
            int       charsWritten;

            if (output.Length > 0)
            {
                output[0] = 'a';

                copied = reader.TryReadCharacterString(
                    output.AsSpan(0, expectedValue.Length - 1),
                    UniversalTagNumber.T61String,
                    out charsWritten);

                Assert.False(copied, "reader.TryCopyT61String - too short");
                Assert.Equal(0, charsWritten);
                Assert.Equal('a', output[0]);
            }

            copied = reader.TryReadCharacterString(
                output,
                UniversalTagNumber.T61String,
                out charsWritten);

            Assert.True(copied, "reader.TryCopyT61String");

            string actualValue = new string(output, 0, charsWritten);

            Assert.Equal(expectedValue, actualValue);
        }
Esempio n. 4
0
        public static void ExpectedTag_IgnoresConstructed(
            AsnEncodingRules ruleSet,
            string inputHex,
            TagClass tagClass,
            int tagValue)
        {
            byte[]    inputData   = inputHex.HexToByteArray();
            Asn1Tag   correctCons = new Asn1Tag(tagClass, tagValue, true);
            Asn1Tag   correctPrim = new Asn1Tag(tagClass, tagValue, false);
            AsnReader reader      = new AsnReader(inputData, ruleSet);

            Assert.True(
                reader.TryGetIA5StringBytes(
                    correctCons,
                    out ReadOnlyMemory <byte> val1));

            Assert.False(reader.HasData);

            reader = new AsnReader(inputData, ruleSet);

            Assert.True(
                reader.TryGetIA5StringBytes(
                    correctPrim,
                    out ReadOnlyMemory <byte> val2));

            Assert.False(reader.HasData);

            Assert.Equal(val1.ByteArrayToHex(), val2.ByteArrayToHex());

#if NETCOREAPP
            string expected = Encoding.ASCII.GetString(val1.Span);
#else
            string expected = Encoding.ASCII.GetString(val1.ToArray());
#endif

            reader = new AsnReader(inputData, ruleSet);
            Assert.Equal(expected, reader.ReadCharacterString(UniversalTagNumber.IA5String, correctPrim));

            reader = new AsnReader(inputData, ruleSet);
            Assert.Equal(expected, reader.ReadCharacterString(UniversalTagNumber.IA5String, correctCons));

            char[] output = new char[28];

            reader = new AsnReader(inputData, ruleSet);

            Assert.True(
                reader.TryReadCharacterString(
                    output.AsSpan(1),
                    UniversalTagNumber.IA5String,
                    out int charsWritten,
                    correctPrim));

            Assert.Equal(expected, output.AsSpan(1, charsWritten).ToString());

            reader = new AsnReader(inputData, ruleSet);

            Assert.True(
                reader.TryReadCharacterString(
                    output.AsSpan(2),
                    UniversalTagNumber.IA5String,
                    out charsWritten,
                    correctCons));

            Assert.Equal(expected, output.AsSpan(2, charsWritten).ToString());
        }