Example #1
0
        public static void TryCopyBMPString(
            PublicEncodingRules ruleSet,
            string inputHex,
            string expectedValue)
        {
            byte[] inputData = inputHex.HexToByteArray();
            char[] output    = new char[expectedValue.Length];

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

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

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

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

            copied = reader.TryCopyBMPString(
                output,
                out charsWritten);

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

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

            Assert.Equal(expectedValue, actualValue);
        }
Example #2
0
        private static void TryCopyBMPString_Throws_Helper(PublicEncodingRules ruleSet, byte[] inputData)
        {
            char[] outputData = new char[inputData.Length + 1];
            outputData[0] = 'a';

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

            Assert.Throws <CryptographicException>(
                () =>
            {
                reader.TryCopyBMPString(
                    outputData,
                    out bytesWritten);
            });

            Assert.Equal(-1, bytesWritten);
            Assert.Equal('a', outputData[0]);
        }