From 499ef27bf4e1c33a9a9868d568ca5814c1af4331 Mon Sep 17 00:00:00 2001 From: Markus Quaritsch <markus.quaritsch@tugraz.at> Date: Fri, 5 May 2017 12:16:29 +0200 Subject: [PATCH] adding creating hash for component --- VectoCommon/VectoHashing/IVectoHash.cs | 2 +- VectoCommon/VectoHashing/VectoComponents.cs | 24 +++++++++++ VectoCommon/VectoHashing/VectoHash.cs | 42 +++++++++++++++++-- VectoCommon/VectoHashingTest/VectoHashTest.cs | 24 ++++++++++- .../VectoHashingTest/VectoHashingTest.csproj | 6 +++ 5 files changed, 92 insertions(+), 6 deletions(-) diff --git a/VectoCommon/VectoHashing/IVectoHash.cs b/VectoCommon/VectoHashing/IVectoHash.cs index abde7e5a40..7c282717a8 100644 --- a/VectoCommon/VectoHashing/IVectoHash.cs +++ b/VectoCommon/VectoHashing/IVectoHash.cs @@ -5,7 +5,7 @@ namespace TUGraz.VectoHashing { public interface IVectoHash { - IEnumerable<VectoComponents> GetContainigComponents(); + IList<VectoComponents> GetContainigComponents(); string ComputeHash(); diff --git a/VectoCommon/VectoHashing/VectoComponents.cs b/VectoCommon/VectoHashing/VectoComponents.cs index 256599f30f..7b0ab169ce 100644 --- a/VectoCommon/VectoHashing/VectoComponents.cs +++ b/VectoCommon/VectoHashing/VectoComponents.cs @@ -43,5 +43,29 @@ namespace TUGraz.VectoHashing throw new ArgumentOutOfRangeException("VectoComponents", component, null); } } + + public static string HashIdPrefix(this VectoComponents component) + { + switch (component) { + case VectoComponents.Engine: + return "ENG-"; + case VectoComponents.Gearbox: + return "GBX-"; + case VectoComponents.Axlegear: + return "AXL-"; + case VectoComponents.Retarder: + return "RET-"; + case VectoComponents.TorqueConverter: + return "TC-"; + case VectoComponents.Angledrive: + return "ANGL-"; + case VectoComponents.Airdrag: + return "AD-"; + case VectoComponents.Tyre: + return "TYRE-"; + default: + throw new ArgumentOutOfRangeException("VectoComponents", component, null); + } + } } } \ No newline at end of file diff --git a/VectoCommon/VectoHashing/VectoHash.cs b/VectoCommon/VectoHashing/VectoHash.cs index 699c7b75b8..dbfbd75d17 100644 --- a/VectoCommon/VectoHashing/VectoHash.cs +++ b/VectoCommon/VectoHashing/VectoHash.cs @@ -1,6 +1,7 @@ using System; using System.Collections; using System.Collections.Generic; +using System.ComponentModel; using System.IO; using System.Linq; using System.Xml; @@ -50,7 +51,7 @@ namespace TUGraz.VectoHashing Helper.AddNamespaces(Manager); } - public IEnumerable<VectoComponents> GetContainigComponents() + public IList<VectoComponents> GetContainigComponents() { var retVal = new List<VectoComponents>(); foreach (var component in EnumHelper.GetValues<VectoComponents>()) { @@ -78,10 +79,43 @@ namespace TUGraz.VectoHashing public XDocument AddHash() { - var toSign = GetIdForElement(GetComponentQueryString()); - var hash = XMLHashProvider.ComputeHash(document, toSign); + var components = GetContainigComponents(); + if (components.Count > 1) { + throw new Exception("can only add hash for a single component!"); + } + if (components[0] == VectoComponents.Vehicle) { + throw new Exception("adding hash for Vehicle is not supported"); + } + var query = string.Format("//*[local-name()='{0}']/*[local-name()='Data']", components[0]); + var node = document.SelectSingleNode(query); + if (node == null) { + throw new Exception(string.Format("'Data' element for component {0} not found!", components[0])); + } + var attributes = node.Attributes; + var id = components[0].HashIdPrefix() + Guid.NewGuid().ToString("n").Substring(0, 20); + var idSet = false; + if (attributes != null && attributes[XMLNames.Component_ID_Attr] != null) { + attributes[XMLNames.Component_ID_Attr].Value = id; + idSet = true; + } + if (!idSet) { + var attr = document.CreateAttribute(XMLNames.Component_ID_Attr); + attr.Value = id; + if (node.Attributes == null) { + throw new Exception("failed to add 'id' attribute"); + } + node.Attributes.Append(attr); + } - return null; + var hash = XMLHashProvider.ComputeHash(document, id); + var sig = document.CreateElement(XMLNames.DI_Signature, node.NamespaceURI); + + if (node.ParentNode == null || hash.DocumentElement == null) { + throw new Exception("Invalid format of document and/or created hash"); + } + sig.AppendChild(document.ImportNode(hash.DocumentElement, true)); + node.ParentNode.AppendChild(sig); + return document.ToXDocument(); } public string ReadHash() diff --git a/VectoCommon/VectoHashingTest/VectoHashTest.cs b/VectoCommon/VectoHashingTest/VectoHashTest.cs index 3f1a5a438c..cc5f83c12d 100644 --- a/VectoCommon/VectoHashingTest/VectoHashTest.cs +++ b/VectoCommon/VectoHashingTest/VectoHashTest.cs @@ -1,4 +1,7 @@ -using System.Linq; +using System.IO; +using System.Linq; +using System.Text; +using System.Xml; using NUnit.Framework; using TUGraz.VectoHashing; using Assert = NUnit.Framework.Assert; @@ -89,5 +92,24 @@ namespace VectoHashingTest Assert.AreEqual(expectedHash, hash); } + + + [TestCase(@"Testdata\XML\ToHash\vecto_engine-input.xml"), + TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input.xml")] + public void TestAddHash(string file) + { + var destination = Path.GetFileNameWithoutExtension(file) + "_hashed.xml"; + + var h = VectoHash.Load(file); + var r = h.AddHash(); + + var writer = new XmlTextWriter(destination, Encoding.UTF8); + r.WriteTo(writer); + writer.Flush(); + writer.Close(); + + var h2 = VectoHash.Load(destination); + Assert.IsTrue(h2.ValidateHash()); + } } } \ No newline at end of file diff --git a/VectoCommon/VectoHashingTest/VectoHashingTest.csproj b/VectoCommon/VectoHashingTest/VectoHashingTest.csproj index 5b72742a8a..d11e7b567f 100644 --- a/VectoCommon/VectoHashingTest/VectoHashingTest.csproj +++ b/VectoCommon/VectoHashingTest/VectoHashingTest.csproj @@ -72,6 +72,12 @@ <Content Include="Testdata\XML\simple_document.xml"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> + <Content Include="Testdata\XML\ToHash\vecto_engine-input.xml"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </Content> + <Content Include="Testdata\XML\ToHash\vecto_gearbox-input.xml"> + <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> + </Content> <Content Include="Testdata\XML\Variations\vecto_engine-sample Encoding ISO 8859-15.xml"> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> </Content> -- GitLab