Code development platform for open source projects from the European Union institutions

Skip to content
Snippets Groups Projects
Commit 499ef27b authored by Markus QUARITSCH's avatar Markus QUARITSCH
Browse files

adding creating hash for component

parent 6adbd4ef
No related branches found
No related tags found
No related merge requests found
......@@ -5,7 +5,7 @@ namespace TUGraz.VectoHashing
{
public interface IVectoHash
{
IEnumerable<VectoComponents> GetContainigComponents();
IList<VectoComponents> GetContainigComponents();
string ComputeHash();
......
......@@ -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
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()
......
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
......@@ -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>
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment