public void Encrypter_should_fail_verification_of_a_modified_signed_document() { // Arrange var keyFile = Path.GetTempFileName(); Encrypter.CreateKey(keyFile); var newKey = Encrypter.ReadKey(keyFile); var doc = new XmlDocument(); doc.LoadXml(@"<root><element /></root>"); Encrypter.SignXml(doc, newKey); doc.DocumentElement.AppendChild(doc.CreateElement("Foo")); // change document after signing var docFile = Path.GetTempFileName(); doc.Save(docFile); // Act bool result; using (var docStream = new FileStream(docFile, FileMode.Open)) { result = Encrypter.VerifyXml(docStream, keyFile); } // Absterge File.Delete(keyFile); File.Delete(docFile); // Assert Assert.IsFalse(result); }
public void Encrypter_should_verify_unchanged_signed_document_with_persisted_key() { // Arrange var keyFile = Path.GetTempFileName(); Encrypter.CreateKey(keyFile); var newKey = Encrypter.ReadKey(keyFile); var doc = new XmlDocument(); doc.LoadXml(@"<root><element /></root>"); Encrypter.SignXml(doc, newKey); var docFile = Path.GetTempFileName(); doc.Save(docFile); // Act bool result; using (var docStream = new FileStream(docFile, FileMode.Open)) { result = Encrypter.VerifyXml(docStream, keyFile); } // Absterge File.Delete(keyFile); File.Delete(docFile); // Assert Assert.IsTrue(result); }
public void Encrypter_should_verify_document_signed_by_PowerShell_script() { // Arrange var signScriptPath = Path.Combine(Path.GetDirectoryName(typeof(Encrypter).Assembly.Location), "Sign-DeploymentMappings.ps1"); var mappingsPath = Path.GetTempFileName(); File.WriteAllText(mappingsPath, @"<root><element /></root>"); var keyPath = Path.GetTempFileName(); Encrypter.CreateKey(keyPath); var arguments = string.Format("-command & '{0}' -DeploymentMappingsPath '{1}' -KeyPath '{2}'", signScriptPath, mappingsPath, keyPath); var process = Process.Start("powershell.exe", arguments); if (!process.WaitForExit(5000)) { process.Kill(); } // Act bool result; using (var stream = File.OpenRead(mappingsPath)) { result = Encrypter.VerifyXml(stream, keyPath); } // Absterge File.Delete(mappingsPath); File.Delete(keyPath); // Assert Assert.IsTrue(result); }
public void Encrypter_should_not_add_another_signature_to_a_signed_document() { // Arrange var keyFile = Path.GetTempFileName(); Encrypter.CreateKey(keyFile); var mappingsPath = Path.GetTempFileName(); File.WriteAllText(mappingsPath, @"<root><element /></root>"); Encrypter.Sign(mappingsPath, keyFile); var signedMappingsBackupPath = Path.GetTempFileName(); File.Copy(mappingsPath, signedMappingsBackupPath, true); // Act Encrypter.Sign(mappingsPath, keyFile); var result = FilesEqual(mappingsPath, signedMappingsBackupPath); // Absterge File.Delete(keyFile); File.Delete(mappingsPath); File.Delete(signedMappingsBackupPath); // Assert Assert.IsTrue(result); }
public void ConfigurationReader_should_read_mappings_from_a_signed_xml_document() { // Arrange var keyFile = Path.GetTempFileName(); Encrypter.CreateKey(keyFile); var newKey = Encrypter.ReadKey(keyFile); var doc = new XmlDocument(); doc.LoadXml(SerializedDeploymentMappings.CompleteDeployerConfiguration); Encrypter.SignXml(doc, newKey); string signedXml; using (var signedXmlStream = new MemoryStream()) { doc.Save(signedXmlStream); signedXml = Encoding.UTF8.GetString(signedXmlStream.ToArray()); } var reader = new ConfigurationReader(new StubDeploymentFileSource(signedXml), keyFile); var buildDetail = new BuildDetail { BuildDefinition = { Name = "MyBuildDefA" } }; IEnumerable <Mapping> mappings; // Act try { mappings = reader.ReadMappings(buildDetail); } finally { // Absterge File.Delete(keyFile); } // Assert Assert.IsTrue(mappings.Any()); }
public void Encrypter_should_verify_and_not_close_input_stream() { // Arrange var keyFile = Path.GetTempFileName(); Encrypter.CreateKey(keyFile); var newKey = Encrypter.ReadKey(keyFile); var doc = new XmlDocument(); doc.LoadXml(@"<root><element /></root>"); Encrypter.SignXml(doc, newKey); var docFile = Path.GetTempFileName(); doc.Save(docFile); // Act try { using (var docStream = new FileStream(docFile, FileMode.Open)) { Encrypter.VerifyXml(docStream, keyFile); docStream.Seek(0, SeekOrigin.Begin); docStream.ReadByte(); // throws an exception if stream is closed } } finally { // Absterge File.Delete(keyFile); File.Delete(docFile); } // Assert // no exception }