Example #1
0
        public static void TestC14NInclusivePrefixes()
        {
            TestCase tc    = TestConfigHelper.GetTest("TestC14NInclusivePrefixes");
            int      count = 0;

            foreach (var input in tc.Inputs)
            {
                count++;
                string rwTypeStr = input.Arguments[0].Value;
                ReaderWriterFactory.ReaderWriterType rwType = (ReaderWriterFactory.ReaderWriterType)Enum.Parse(typeof(ReaderWriterFactory.ReaderWriterType), rwTypeStr, true);
                Encoding encoding        = Encoding.GetEncoding(input.Arguments[1].Value);
                bool     mustSupportV14N = input.Arguments[2].Value.ToLower() == "true";

                MemoryStream        ms     = new MemoryStream();
                XmlWriter           w      = ReaderWriterFactory.CreateXmlWriter(rwType, ms, encoding);
                XmlDictionaryWriter writer = w as XmlDictionaryWriter;
                if (writer == null)
                {
                    writer = XmlDictionaryWriter.CreateDictionaryWriter(w);
                }

                if (!writer.CanCanonicalize)
                {
                    Assert.False(mustSupportV14N,
                                 string.Format("Error, writer {0},{1} should support C14N, but it doesn't!", rwTypeStr, encoding.ToString()));
                    continue;
                }

                string myDefaultNamespace = "http://mynamespace";
                string myNamespace1       = "http://mynamespace1";
                string myNamespace2       = "http://mynamespace2";
                string myNamespace3       = "http://mynamespace3";
                string myNamespace4       = "http://mynamespace4";
                writer.WriteStartElement("Root");
                writer.WriteXmlnsAttribute("p1", myNamespace1);
                writer.WriteAttributeString("p1", "a", null, "b");
                writer.WriteStartElement("", "Element1", myDefaultNamespace);
                writer.WriteAttributeString("p3", "c", myNamespace3, "d");
                writer.WriteStartElement("Element2");

                MemoryStream canonicalStream = new MemoryStream();

                writer.StartCanonicalization(canonicalStream, false, new string[] { "p3", "p2", "p1", "" });
                writer.WriteStartElement("pre", "Element3", myNamespace2);
                writer.WriteAttributeString("pre2", "attrName", myNamespace4, "attrValue");
                writer.WriteStartElement("Element4", "");
                writer.WriteStartAttribute("attr1");
                writer.WriteQualifiedName("foo", myNamespace1);
                writer.WriteEndAttribute();

                writer.WriteStartAttribute("attr2");
                writer.WriteQualifiedName("bar", myNamespace3);
                writer.WriteEndAttribute();

                writer.WriteString("Hello world");

                writer.WriteEndElement(); // Element4
                writer.WriteEndElement(); // pre:Element3

                writer.EndCanonicalization();
                writer.WriteEndElement(); // Element2
                writer.WriteEndElement(); // Element1
                writer.WriteEndElement(); // Root
                writer.Flush();

                byte[] canonicalDoc = canonicalStream.ToArray();
                byte[] fullDoc      = ms.ToArray();

                writer.Close(); // Finished creating the document

                XmlDsigExcC14NTransform transform = new XmlDsigExcC14NTransform();
                transform.InclusiveNamespacesPrefixList = "p3 p2 p1 #default";
                transform.LoadInput(new MemoryStream(canonicalDoc));
                Stream transformedOutput  = transform.GetOutput(typeof(Stream)) as Stream;
                byte[] outputFromSecurity = StreamToByteArray(transformedOutput);
                //Finished creating the doc from the security class

                Helper.DumpToFile(fullDoc);
                Helper.DumpToFile(canonicalDoc);
                Helper.DumpToFile(outputFromSecurity);
                Assert.True(Enumerable.SequenceEqual(outputFromSecurity, canonicalDoc), $"TestC14NInclusivePrefixes test variation #{count} failed");
            }
        }
Example #2
0
        public static void ReaderWriter_C14N_DifferentReadersWriters()
        {
            int          count   = 0;
            var          params1 = TestConfigHelper.GetTest("ReaderWriter_C14N_DifferentReadersWriters_ParamGroup1");
            var          params2 = TestConfigHelper.GetTest("ReaderWriter_C14N_DifferentReadersWriters_ParamGroup2");
            var          params3 = TestConfigHelper.GetTest("ReaderWriter_C14N_DifferentReadersWriters_ParamGroup3");
            var          params4 = TestConfigHelper.GetTest("ReaderWriter_C14N_DifferentReadersWriters_ParamGroup4");
            Transform    transform;
            MemoryStream canonicalStream;
            MemoryStream ms;
            Stream       transformedOutput;

            byte[] outputFromSecurity;
            byte[] outputFromIndigo;

            //TestC14NInMultipleWriters
            foreach (var input in params1.Inputs)
            {
                foreach (var input2 in params2.Inputs)
                {
                    foreach (var input3 in params3.Inputs)
                    {
                        count++;
                        string rwTypeStr = input.Arguments[0].Value;
                        ReaderWriterFactory.ReaderWriterType rwType = (ReaderWriterFactory.ReaderWriterType)Enum.Parse(typeof(ReaderWriterFactory.ReaderWriterType), rwTypeStr, true);
                        Encoding encoding          = Encoding.GetEncoding((string)input.Arguments[1].Value);
                        string   sampleXmlFileName = input2.Arguments[0].Value;
                        bool     mustSupportV14N   = input.Arguments[2].Value == "true";
                        string   baselineFileName  = input2.Arguments[1].Value;

                        bool testWithComments = input3.Arguments[0].Value == "true";

                        XmlDocument xmlDoc = new XmlDocument();
                        xmlDoc.PreserveWhitespace = true;

                        if (testWithComments)
                        {
                            transform = new XmlDsigExcC14NWithCommentsTransform();
                        }
                        else
                        {
                            transform = new XmlDsigExcC14NTransform();
                        }
                        xmlDoc.Load(baselineFileName);
                        transform.LoadInput(xmlDoc);
                        transformedOutput  = transform.GetOutput(typeof(Stream)) as Stream;
                        outputFromSecurity = StreamToByteArray(transformedOutput);

                        byte[] sampleXmlFileBytes = File.ReadAllBytes(sampleXmlFileName);

                        ms = new MemoryStream();
                        XmlWriter w = ReaderWriterFactory.CreateXmlWriter(rwType, ms, encoding);

                        canonicalStream = new MemoryStream();

                        XmlDictionaryWriter dicWriter = w as XmlDictionaryWriter;
                        if (dicWriter == null)
                        {
                            dicWriter = XmlDictionaryWriter.CreateDictionaryWriter(w);
                        }

                        if (!dicWriter.CanCanonicalize)
                        {
                            Assert.False(mustSupportV14N, "Error, writer should support C14N, but it doesn't!");
                            continue;
                        }

                        dicWriter.WriteStartElement("MyRoot");
                        dicWriter.StartCanonicalization(canonicalStream, testWithComments, null);
                        FileStream fs            = File.OpenRead(sampleXmlFileName);
                        XmlReader  webdataReader = XmlReader.Create(fs);
                        CopyXmlToWriter(webdataReader, dicWriter);
                        dicWriter.EndCanonicalization();
                        dicWriter.WriteEndElement();
                        dicWriter.Flush();
                        webdataReader.Close();
                        fs.Close();

                        outputFromIndigo = canonicalStream.ToArray();

                        Helper.DumpToFile(outputFromSecurity);
                        Helper.DumpToFile(outputFromIndigo);
                        Assert.True(Enumerable.SequenceEqual(outputFromSecurity, outputFromIndigo), $"ReaderWriter_C14N_DifferentReadersWriters test variation #{count} failed");
                    }
                }
            }

            //TestC14NInReader
            foreach (var input in params4.Inputs)
            {
                count++;
                string sampleXmlFileName = input.Arguments[3].Value;
                string rwTypeStr         = input.Arguments[0].Value;
                ReaderWriterFactory.ReaderWriterType rwType = (ReaderWriterFactory.ReaderWriterType)Enum.Parse(typeof(ReaderWriterFactory.ReaderWriterType), rwTypeStr, true);
                Encoding encoding = Encoding.GetEncoding((string)input.Arguments[1].Value);

                bool   mustSupportV14N  = input.Arguments[2].Value == "true";
                string baselineFileName = "ReaderWriter_C14N_BaselineXML_OnlyLF.xml";

                XmlDocument xmlDoc = new XmlDocument();
                xmlDoc.PreserveWhitespace = true;
                transform = new XmlDsigExcC14NTransform();
                xmlDoc.Load(baselineFileName);
                transform.LoadInput(xmlDoc);
                transformedOutput  = transform.GetOutput(typeof(Stream)) as Stream;
                outputFromSecurity = StreamToByteArray(transformedOutput);
                byte[] sampleXmlFileBytes = File.ReadAllBytes(sampleXmlFileName);

                XmlReader           r         = ReaderWriterFactory.CreateXmlReader(rwType, sampleXmlFileBytes, encoding);
                XmlDictionaryReader dicReader = r as XmlDictionaryReader;
                if (dicReader == null)
                {
                    dicReader = XmlDictionaryReader.CreateDictionaryReader(r);
                }

                canonicalStream = new MemoryStream();

                if (!dicReader.CanCanonicalize)
                {
                    Assert.False(mustSupportV14N, "Error, reader should support C14N, but it doesn't!");
                    continue;
                }

                dicReader.StartCanonicalization(canonicalStream, false, null);

                canonicalStream.Position = 0;
                string str = new StreamReader(canonicalStream).ReadToEnd();
                canonicalStream.Position = 0;
                while (dicReader.Read())
                {
                    ;                      // simply read it all into the C14N writer
                }
                dicReader.EndCanonicalization();
                dicReader.Close();

                outputFromIndigo = canonicalStream.ToArray();
                Helper.DumpToFile(outputFromSecurity);
                Helper.DumpToFile(outputFromIndigo);
                Assert.True(Enumerable.SequenceEqual(outputFromSecurity, outputFromIndigo), $"ReaderWriter_C14N_DifferentReadersWriters test variation #{count} failed");
            }

            //TestC14NWriterWithManyAttributes
            int           numberOfAttributes = 1000;
            int           seed   = (int)DateTime.Now.Ticks;
            Random        rndGen = new Random(seed);
            StringBuilder sb     = new StringBuilder();

            sb.Append("<Root><Element");
            int prefixIndex = 0;

            for (int i = 0; i < numberOfAttributes; i++)
            {
                string namespaceUri = null;
                string prefix       = null;
                if ((rndGen.Next() % 5) == 0)
                {
                    prefix       = "p" + (prefixIndex++);
                    namespaceUri = "http://namespace_" + i;
                }

                string localName = "attr" + i;
                string value     = "attrValue" + i;
                if (prefix == null)
                {
                    sb.Append($" {localName}=\"{value}\"");
                }
                else
                {
                    sb.Append($" {prefix}:{localName}=\"{2}\" xmlns:{value}=\"{namespaceUri}\"");
                }
            }
            sb.Append(">Hello world</Element></Root>");
            string      xmlString = sb.ToString();
            XmlDocument doc       = new XmlDocument();

            doc.LoadXml(xmlString);

            ms = new MemoryStream();
            XmlDictionaryWriter writer = XmlDictionaryWriter.CreateTextWriter(ms);

            canonicalStream = new MemoryStream();
            writer.StartCanonicalization(canonicalStream, false, null);
            doc.WriteTo(writer);
            writer.Flush();
            writer.EndCanonicalization();
            outputFromIndigo = canonicalStream.ToArray();
            byte[] nonCanonicalOutput = ms.ToArray();

            XmlDsigExcC14NTransform transform2 = new XmlDsigExcC14NTransform();

            transform2.LoadInput(doc);
            transformedOutput  = transform2.GetOutput(typeof(Stream)) as Stream;
            outputFromSecurity = StreamToByteArray(transformedOutput);

            Helper.DumpToFile(outputFromSecurity);
            Helper.DumpToFile(outputFromIndigo);
            Helper.DumpToFile(nonCanonicalOutput);

            Assert.True(Enumerable.SequenceEqual(outputFromSecurity, outputFromIndigo), $"ReaderWriter_C14N_DifferentReadersWriters test variation #{count} failed");
            count++;
            Assert.Equal(params1.Inputs.Count * params2.Inputs.Count * params3.Inputs.Count + params4.Inputs.Count + 1, count);
        }