Ejemplo n.º 1
0
        public async Task PerformXdtTransformAsync_InSecureXmlFailsToTransform_Throws()
        {
            using (var test = new XdtTransformerTest("<a xmlns:xdt=\"http://schemas.microsoft.com/XML-Document-Transform\"><x><y xdt:Transform=\"Insert\"><z>$c$</z></y></x></a>"))
            {
                var projectFileOriginalContent =
                    @"<?xml version=""1.0""?>
<!DOCTYPE a [
   <!ENTITY greeting ""Hello"">
   <!ENTITY name ""NuGet Client "">
   <!ENTITY sayhello ""&greeting; &name;"">
]>
<a><x name=""&sayhello;"" /></a>";

                File.WriteAllText(test.TargetFile.FullName, projectFileOriginalContent);

                test.ProjectSystem.SetupGet(x => x.ProjectFullPath)
                .Returns(test.TargetFile.DirectoryName);
                test.ProjectSystem.SetupGet(x => x.ProjectName)
                .Returns("ProjectName");
                test.ProjectSystem.Setup(x => x.GetPropertyValue(It.IsNotNull <string>()))
                .Returns("d");

                var exception = await Assert.ThrowsAsync <InvalidDataException>(
                    () => XdtTransformer.PerformXdtTransformAsync(
                        test.StreamTaskFactory,
                        test.TargetFile.Name,
                        test.ProjectSystem.Object,
                        CancellationToken.None));

                Assert.IsType <XmlException>(exception.InnerException);
            }
        }
Ejemplo n.º 2
0
        public async Task RevertFileAsync_RevertsFile()
        {
            var projectFileContent = string.Format(
                CultureInfo.InvariantCulture,
                "<a>{0}  <x>{0}    <b>d</b>{0}    <y>{0}      <z>d</z>{0}    </y>{0}  </x>{0}</a>",
                Environment.NewLine);

            using (var test = new XdtTransformerTest("<a xmlns:xdt=\"http://schemas.microsoft.com/XML-Document-Transform\"><x><y xdt:Transform=\"Remove\"><z>$c$</z></y></x></a>"))
            {
                var zipArchiveFilePath = Path.Combine(test.TestDirectory.Path, "archive.zip");
                var zipFileInfo        = new InternalZipFileInfo(zipArchiveFilePath, "install.xdt");

                using (var zipFileStream = File.OpenWrite(zipArchiveFilePath))
                    using (var zipArchive = new ZipArchive(zipFileStream, ZipArchiveMode.Create))
                    {
                        var content = Encoding.UTF8.GetBytes("<a xmlns:xdt=\"http://schemas.microsoft.com/XML-Document-Transform\"><x><b xdt:Transform=\"Insert\">$c$</b></x></a>");

                        zipArchive.AddEntry(zipFileInfo.ZipArchiveEntryFullName, content);
                    }

                File.WriteAllText(test.TargetFile.FullName, projectFileContent);

                test.ProjectSystem.SetupGet(x => x.ProjectFullPath)
                .Returns(test.TargetFile.DirectoryName);
                test.ProjectSystem.SetupGet(x => x.ProjectName)
                .Returns("ProjectName");
                test.ProjectSystem.Setup(x => x.GetPropertyValue(It.IsNotNull <string>()))
                .Returns("d");
                test.ProjectSystem.Setup(x => x.AddFile(It.IsNotNull <string>(), It.IsNotNull <Stream>()))
                .Callback <string, Stream>(
                    (targetFilePath, stream) =>
                {
                    Assert.Equal(test.TargetFile.Name, targetFilePath);

                    stream.Seek(offset: 0, origin: SeekOrigin.Begin);

                    using (var reader = new StreamReader(stream))
                    {
                        var actualResult   = reader.ReadToEnd();
                        var expectedResult = string.Format(
                            CultureInfo.InvariantCulture,
                            "<a>{0}  <x>{0}    <b>d</b>{0}  </x>{0}</a>",
                            Environment.NewLine);

                        Assert.Equal(expectedResult, actualResult);
                    }
                });

                await test.Transformer.RevertFileAsync(
                    test.StreamTaskFactory,
                    test.TargetFile.Name,
                    new[] { zipFileInfo },
                    test.ProjectSystem.Object,
                    CancellationToken.None);
            }
        }
        public async Task TransformFileAsync_WithPUACharactersInPathTransformsFile_Success()
        {
            using var test = new XdtTransformerTest("<a xmlns:xdt=\"http://schemas.microsoft.com/XML-Document-Transform\"><x><y xdt:Transform=\"Insert\"><z>$c$</z></y></x></a>");

            var projectFileOriginalContent = "<a><x /></a>";

            File.WriteAllText(test.TargetFile.FullName, projectFileOriginalContent);

            string pathWithPAUChars = Path.Combine(test.TargetFile.DirectoryName, "U1[]U2[]U3[]");

            Directory.CreateDirectory(pathWithPAUChars);
            string newTargetFile = Path.Combine(pathWithPAUChars, "target.file");

            File.Move(test.TargetFile.FullName, newTargetFile);

            test.ProjectSystem.SetupGet(x => x.ProjectFullPath)
            .Returns(test.TargetFile.DirectoryName);
            test.ProjectSystem.SetupGet(x => x.ProjectName)
            .Returns("ProjectName");
            test.ProjectSystem.Setup(x => x.GetPropertyValue(It.IsNotNull <string>()))
            .Returns("d");
            test.ProjectSystem.Setup(x => x.AddFile(It.IsNotNull <string>(), It.IsNotNull <Stream>()))
            .Callback <string, Stream>(
                (targetFilePath, stream) =>
            {
                Assert.Equal(newTargetFile, targetFilePath);

                stream.Seek(offset: 0, origin: SeekOrigin.Begin);

                using (var reader = new StreamReader(stream))
                {
                    var actualResult   = reader.ReadToEnd();
                    var expectedResult = "<a>\r\n  <x>\r\n    <y>\r\n      <z>d</z>\r\n    </y>\r\n  </x>\r\n</a>";

                    Assert.Equal(expectedResult, actualResult);
                }
            });

            await XdtTransformer.PerformXdtTransformAsync(
                test.StreamTaskFactory,
                newTargetFile,
                test.ProjectSystem.Object,
                CancellationToken.None);
        }
Ejemplo n.º 4
0
        public async Task TransformFileAsync_TransformsFile()
        {
            using (var test = new XdtTransformerTest("<a xmlns:xdt=\"http://schemas.microsoft.com/XML-Document-Transform\"><x><y xdt:Transform=\"Insert\"><z>$c$</z></y></x></a>"))
            {
                var projectFileOriginalContent = "<a><x /></a>";

                File.WriteAllText(test.TargetFile.FullName, projectFileOriginalContent);

                test.ProjectSystem.SetupGet(x => x.ProjectFullPath)
                .Returns(test.TargetFile.DirectoryName);
                test.ProjectSystem.SetupGet(x => x.ProjectName)
                .Returns("ProjectName");
                test.ProjectSystem.Setup(x => x.GetPropertyValue(It.IsNotNull <string>()))
                .Returns("d");
                test.ProjectSystem.Setup(x => x.AddFile(It.IsNotNull <string>(), It.IsNotNull <Stream>()))
                .Callback <string, Stream>(
                    (targetFilePath, stream) =>
                {
                    Assert.Equal(test.TargetFile.Name, targetFilePath);

                    stream.Seek(offset: 0, origin: SeekOrigin.Begin);

                    using (var reader = new StreamReader(stream))
                    {
                        var actualResult   = reader.ReadToEnd();
                        var expectedResult = "<a>\r\n  <x>\r\n    <y>\r\n      <z>d</z>\r\n    </y>\r\n  </x>\r\n</a>";

                        Assert.Equal(expectedResult, actualResult);
                    }
                });

                await test.Transformer.TransformFileAsync(
                    test.StreamTaskFactory,
                    test.TargetFile.Name,
                    test.ProjectSystem.Object,
                    CancellationToken.None);
            }
        }