Пример #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);
            }
        }
        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);
        }