示例#1
0
        public void Fail_Decrypt_BadIv()
        {
            // Setup
            var password      = Fakes.RandomString();
            var parameterData = new ParameterSetupData()
            {
                Value     = Fakes.RandomString(),
                DataType  = DataType.String,
                Name      = Fakes.RandomString(),
                Sensitive = true
            };


            var xml  = XmlGenerators.ProjectParamsFile(new List <ParameterSetupData>(new[] { parameterData }));
            var path = Path.Combine(_workingFolder, Guid.NewGuid().ToString("N"));

            File.WriteAllText(path, xml);
            var projectFile = new ProjectFileImpl();

            projectFile.Initialize(path, null);


            var    newProjectFile = new ProjectFileImpl();
            string encryptedXml;

            using (var stream = new MemoryStream())
            {
                projectFile.Save(stream, ProtectionLevel.EncryptSensitiveWithPassword, password);
                stream.Position = 0;

                var sr = new StreamReader(stream);
                encryptedXml = sr.ReadToEnd();
            }

            var encryptedXmlDoc = new XmlDocument();

            encryptedXmlDoc.LoadXml(encryptedXml);
            var ivAttribute = encryptedXmlDoc.SelectSingleNode("//*[@IV or @SSIS:IV]", encryptedXmlDoc.GetNameSpaceManager()).GetAttributeNode("IV");

            if (ivAttribute != null)
            {
                ivAttribute.Value = $"*{Fakes.RandomString()}"; // Added * to break Convert.FromBase64 false success
            }
            // Execute
            Exception exception;

            using (var stream = new MemoryStream())
            {
                encryptedXmlDoc.Save(stream);
                stream.Flush();
                stream.Position = 0;

                exception = Record.Exception(() => newProjectFile.Initialize(stream, password));
            }

            // Assert
            Assert.NotNull(exception);
            Assert.IsType <InvalidXmlException>(exception);
            Assert.True(exception.Message.Contains("\"IV\""));
        }
示例#2
0
        public void Fail_Save_NoPassword(ProtectionLevel protectionLevel)
        {
            // Setup
            var parameterData = new ParameterSetupData()
            {
                Value     = Fakes.RandomString(),
                DataType  = DataType.String,
                Name      = Fakes.RandomString(),
                Sensitive = true
            };


            var xml  = XmlGenerators.ProjectParamsFile(new List <ParameterSetupData>(new[] { parameterData }));
            var path = Path.Combine(_workingFolder, Guid.NewGuid().ToString("N"));

            File.WriteAllText(path, xml);
            var projectFile = new ProjectFileImpl();

            projectFile.Initialize(path, null);

            // Execute
            Exception exception;

            using (var stream = new MemoryStream())
            {
                exception = Record.Exception(() => projectFile.Save(stream, protectionLevel, null));
            }

            // Assert
            Assert.NotNull(exception);
            Assert.IsType <InvalidPaswordException>(exception);
        }
示例#3
0
        public void Pass_Save_ToFile_NoSensitive()
        {
            // Setup
            var parameterData = new ParameterSetupData()
            {
                Value     = Fakes.RandomString(),
                DataType  = DataType.String,
                Name      = Fakes.RandomString(),
                Sensitive = true
            };

            var xml  = XmlGenerators.ProjectParamsFile(new List <ParameterSetupData>(new[] { parameterData }));
            var path = Path.Combine(_workingFolder, Guid.NewGuid().ToString("N"));

            File.WriteAllText(path, xml);
            var projectFile = new ProjectFileImpl();

            projectFile.Initialize(path, null);

            var saveToPath = Path.Combine(_workingFolder, Guid.NewGuid().ToString("N"));

            // Execute - save with no sensitive info
            projectFile.Save(saveToPath);

            // Assert - should not have sensitive node
            var xmlDoc = new XmlDocument();

            xmlDoc.Load(saveToPath);
            var encryptedNode = xmlDoc.SelectSingleNode("//SSIS:Property[@SSIS:Name=\"Value\"]", xmlDoc.GetNameSpaceManager());

            Assert.Null(encryptedNode);
        }
示例#4
0
        public void Fail_Decrypt_NoPassword()
        {
            // Setup
            var password      = Fakes.RandomString();
            var parameterData = new ParameterSetupData()
            {
                Value     = Fakes.RandomString(),
                DataType  = DataType.String,
                Name      = Fakes.RandomString(),
                Sensitive = true
            };

            var xml  = XmlGenerators.ProjectParamsFile(new List <ParameterSetupData>(new[] { parameterData }));
            var path = Path.Combine(_workingFolder, Guid.NewGuid().ToString("N"));

            File.WriteAllText(path, xml);
            var projectFile = new ProjectFileImpl();

            projectFile.Initialize(path, null);


            var    newProjectFile = new ProjectFileImpl();
            string encryptedXml;

            using (var stream = new MemoryStream())
            {
                projectFile.Save(stream, ProtectionLevel.EncryptSensitiveWithPassword, password);
                stream.Position = 0;

                var sr = new StreamReader(stream);
                encryptedXml = sr.ReadToEnd();
            }

            var encryptedXmlDoc = new XmlDocument();

            encryptedXmlDoc.LoadXml(encryptedXml);

            // Execute
            Exception exception;

            using (var stream = new MemoryStream())
            {
                encryptedXmlDoc.Save(stream);
                stream.Flush();
                stream.Position = 0;

                exception = Record.Exception(() => newProjectFile.Initialize(stream, string.Empty));
            }

            // Assert
            Assert.NotNull(exception);
            Assert.IsType <InvalidPaswordException>(exception);
        }
示例#5
0
        public void Fail_Save_NotInitialized()
        {
            // Setup
            var parameterData = new ParameterSetupData()
            {
                Value     = Fakes.RandomString(),
                DataType  = DataType.String,
                Name      = Fakes.RandomString(),
                Sensitive = true
            };

            var projectFile = new ProjectFileImpl();

            // Execute
            var exception = Record.Exception(() => projectFile.Save(Fakes.RandomString()));

            // Assert
            Assert.NotNull(exception);
            Assert.IsType <ProjectNotInitializedException>(exception);
        }
示例#6
0
        public void Pass_Save_ToStream_NoSensitive()
        {
            // Setup
            // Use ProjectParams xml in this test
            var parameterData = new ParameterSetupData()
            {
                Value     = Fakes.RandomString(),
                DataType  = DataType.String,
                Name      = Fakes.RandomString(),
                Sensitive = true
            };

            var xml  = XmlGenerators.ProjectParamsFile(new List <ParameterSetupData>(new[] { parameterData }));
            var path = Path.Combine(_workingFolder, Guid.NewGuid().ToString("N"));

            File.WriteAllText(path, xml);
            var projectFile = new ProjectFileImpl();

            projectFile.Initialize(path, null);

            // Execute
            string encryptedXml;

            using (var stream = new MemoryStream())
            {
                // Save with DontSaveSensitive
                projectFile.Save(stream);
                stream.Position = 0;
                var sr = new StreamReader(stream);
                encryptedXml = sr.ReadToEnd();
            }


            // Assert -- sensitive node should be gone
            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(encryptedXml);
            var encryptedNode = xmlDoc.SelectSingleNode("//SSIS:Property[@SSIS:Name=\"Value\"]", xmlDoc.GetNameSpaceManager());

            Assert.Null(encryptedNode);
        }
示例#7
0
        public void Pass_Encrypt()
        {
            // Setup
            var password      = Fakes.RandomString();
            var parameterData = new ParameterSetupData()
            {
                Value     = Fakes.RandomString(),
                DataType  = DataType.String,
                Name      = Fakes.RandomString(),
                Sensitive = true
            };


            var xml  = XmlGenerators.ProjectParamsFile(new List <ParameterSetupData>(new[] { parameterData }));
            var path = Path.Combine(_workingFolder, Guid.NewGuid().ToString("N"));

            File.WriteAllText(path, xml);
            var projectFile = new ProjectFileImpl();

            projectFile.Initialize(path, null);

            // Execute
            string encryptedXml;

            using (var stream = new MemoryStream())
            {
                projectFile.Save(stream, ProtectionLevel.EncryptSensitiveWithPassword, password);
                stream.Position = 0;
                var sr = new StreamReader(stream);
                encryptedXml = sr.ReadToEnd();
            }
            var xmlDoc = new XmlDocument();

            xmlDoc.LoadXml(encryptedXml);
            var encryptedNode = xmlDoc.SelectSingleNode("//SSIS:Property[@SSIS:Name=\"Value\"]", xmlDoc.GetNameSpaceManager());

            // Assert - should have encrypted node and Salt attribute
            Assert.True(encryptedNode?.Attributes?["Salt", XmlHelpers.Schemas.SSIS]?.Value != null);
        }
示例#8
0
        public void Pass_Decrypt_FromFile(ProtectionLevel protectionLevel)
        {
            // Setup
            var password      = Fakes.RandomString();
            var parameterData = new ParameterSetupData()
            {
                Value     = Fakes.RandomString(),
                DataType  = DataType.String,
                Name      = Fakes.RandomString(),
                Sensitive = true
            };


            var xml  = XmlGenerators.ProjectParamsFile(new List <ParameterSetupData>(new[] { parameterData }));
            var path = Path.Combine(_workingFolder, Guid.NewGuid().ToString("N"));

            File.WriteAllText(path, xml);
            var projectFile = new ProjectFileImpl();

            projectFile.Initialize(path, null);


            var saveToPath = Path.Combine(_workingFolder, Guid.NewGuid().ToString("D"));

            projectFile.Save(saveToPath, protectionLevel, password);

            // Execute
            var newProjectFile = new ProjectFileImpl();

            newProjectFile.Initialize(saveToPath, password);

            // Assert
            var decryptedNodeValue =
                newProjectFile.FileXmlDocumentPublic.SelectSingleNode("//SSIS:Property[@SSIS:Name=\"Value\"]", newProjectFile.FileXmlDocumentPublic.GetNameSpaceManager())?.InnerText;

            Assert.Equal(parameterData.Value, decryptedNodeValue);
        }