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

Skip to content
Snippets Groups Projects
Forked from VECTO / VECTO Sim
7589 commits behind the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
VectoHashTest.cs 15.60 KiB
/*
* This file is part of VECTO.
*
* Copyright © 2012-2017 European Union
*
* Developed by Graz University of Technology,
*              Institute of Internal Combustion Engines and Thermodynamics,
*              Institute of Technical Informatics
*
* VECTO is licensed under the EUPL, Version 1.1 or - as soon they will be approved
* by the European Commission - subsequent versions of the EUPL (the "Licence");
* You may not use VECTO except in compliance with the Licence.
* You may obtain a copy of the Licence at:
*
* https://joinup.ec.europa.eu/community/eupl/og_page/eupl
*
* Unless required by applicable law or agreed to in writing, VECTO
* distributed under the Licence is distributed on an "AS IS" basis,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the Licence for the specific language governing permissions and
* limitations under the Licence.
*
* Authors:
*   Stefan Hausberger, hausberger@ivt.tugraz.at, IVT, Graz University of Technology
*   Christian Kreiner, christian.kreiner@tugraz.at, ITI, Graz University of Technology
*   Michael Krisper, michael.krisper@tugraz.at, ITI, Graz University of Technology
*   Raphael Luz, luz@ivt.tugraz.at, IVT, Graz University of Technology
*   Markus Quaritsch, markus.quaritsch@tugraz.at, IVT, Graz University of Technology
*   Martin Rexeis, rexeis@ivt.tugraz.at, IVT, Graz University of Technology
*/

using System;
using System.IO;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using NUnit.Framework;
using TUGraz.VectoCore.Utils;
using TUGraz.VectoHashing;
using VectoHashingTest.Utils;
using Assert = NUnit.Framework.Assert;

namespace VectoHashingTest
{
	[TestFixture]
	public class VectoHashTest
	{
		public const string ReferenceXMLEngine = @"Testdata\XML\Reference\vecto_engine-sample.xml";
		public const string ReferenceXMLVehicle = @"Testdata\XML\Reference\vecto_vehicle-sample_FULL.xml";

		[TestCase]
		public void TestComponentsEngineFile()
		{
			var h = VectoHash.Load(ReferenceXMLEngine);
			var components = h.GetContainigComponents().ToList();

			Assert.AreEqual(1, components.Count);
			Assert.AreEqual(VectoComponents.Engine, components[0]);
		}

		[TestCase]
		public void TestComponentsVehicleFile()
		{
			var h = VectoHash.Load(ReferenceXMLVehicle);
			var components = h.GetContainigComponents().ToList();

			Assert.AreEqual(10, components.Count);
		}

		[TestCase(ReferenceXMLEngine, VectoComponents.Engine, BasicHasingTests.HashEngineXML)]
		public void TestHashComputationSelected(string file, VectoComponents component, string expectedHash)
		{
			var h = VectoHash.Load(file);
			var hash = h.ComputeHash(component);

			Assert.AreEqual(expectedHash, hash);
		}

		[TestCase(ReferenceXMLVehicle, BasicHasingTests.HashVehicleXML),
		TestCase(ReferenceXMLEngine, BasicHasingTests.HashEngineXML)]
		public void TestHashComputation(string file, string expectedHash)
		{
			var h = VectoHash.Load(file);
			var hash = h.ComputeHash();

			Assert.AreEqual(expectedHash, hash);
		}

		[TestCase(ReferenceXMLEngine)]
		public void TestHashComputationInvalidComponent(string file)
		{
			var h = VectoHash.Load(file);
			AssertHelper.Exception<Exception>(() => h.ComputeHash(VectoComponents.Gearbox), "Component Gearbox not found");
		}

		[TestCase(ReferenceXMLEngine)]
		public void TestReadHashInvalidComponent(string file)
		{
			var h = VectoHash.Load(file);
			AssertHelper.Exception<Exception>(() => h.ReadHash(VectoComponents.Gearbox), "Component Gearbox not found");
		}

		[TestCase(ReferenceXMLVehicle, VectoComponents.Engine, "e0c253b643f7f8f09b963aca4a264d06fbfa599f"),
		TestCase(ReferenceXMLVehicle, VectoComponents.Gearbox, "d14189366134120e08fa3f2c6e3328dd13c08a23")]
		public void TestReadHash(string file, VectoComponents component, string expectedHash)
		{
			var h = VectoHash.Load(file);
			var existingHash = h.ReadHash(component);

			Assert.AreEqual(expectedHash, existingHash);
		}

		[TestCase(ReferenceXMLVehicle, VectoComponents.Tyre, 0, "5074334bb2c090c5e258e9a664f5d19689a3f13d"),
		TestCase(ReferenceXMLVehicle, VectoComponents.Tyre, 1, "6074334bb2c090c5e258e9a664f5d19689a3f13d")]
		public void TestReadHashIdx(string file, VectoComponents component, int index, string expectedHash)
		{
			var h = VectoHash.Load(file);
			var existingHash = h.ReadHash(component, index);

			Assert.AreEqual(expectedHash, existingHash);
		}

		[TestCase]
		public void TestReadTyres1Index()
		{
			var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle1.xml";
			var h = VectoHash.Load(file);
			var expectedHash = new[] {
				"5074334bb2c090c5e258e9a664f5d19689a3f13d",
				"6074334bb2c090c5e258e9a664f5d19689a3f13d",
				"6074334bb2c090c5e258e9a664f5d19689a3f13d"
			};

			for (int i = 0; i < expectedHash.Length; i++) {
				var existingHash = h.ReadHash(VectoComponents.Tyre, i);

				Assert.AreEqual(expectedHash[i], existingHash);
			}
		}

		[TestCase]
		public void TestReadTyres2Index()
		{
			var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle2.xml";
			var h = VectoHash.Load(file);
			var expectedHash = new[] {
				"5074334bb2c090c5e258e9a664f5d19689a3f13d",
				"5074334bb2c090c5e258e9a664f5d19689a3f13d",
				"6074334bb2c090c5e258e9a664f5d19689a3f13d"
			};

			for (int i = 0; i < expectedHash.Length; i++) {
				var existingHash = h.ReadHash(VectoComponents.Tyre, i);

				Assert.AreEqual(expectedHash[i], existingHash);
			}

			AssertHelper.Exception<Exception>(() => h.ReadHash(VectoComponents.Tyre, 3),
				"index exceeds number of components found! index: 3, #components: 3");
		}

		[TestCase]
		public void TestComputeTyres1Index()
		{
			var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle1.xml";
			var h = VectoHash.Load(file);

			var hash1 = h.ComputeHash(VectoComponents.Tyre, 1);
			var hash2 = h.ComputeHash(VectoComponents.Tyre, 2);

			Assert.AreEqual(hash1, hash2);

			AssertHelper.Exception<Exception>(() => h.ComputeHash(VectoComponents.Tyre, 3),
				"index exceeds number of components found! index: 3, #components: 3");
		}

		[TestCase]
		public void TestComputeTyres2Index()
		{
			var file = @"Testdata\XML\ToHash\vecto_vehicle-sample_3axle2.xml";
			var h = VectoHash.Load(file);

			var hash1 = h.ComputeHash(VectoComponents.Tyre, 0);
			var hash2 = h.ComputeHash(VectoComponents.Tyre, 1);

			Assert.AreEqual(hash1, hash2);

			AssertHelper.Exception<Exception>(() => h.ComputeHash(VectoComponents.Tyre, 3),
				"index exceeds number of components found! index: 3, #components: 3");
		}

		[TestCase("vecto_vehicle-sample_FULL_Comments.xml", BasicHasingTests.HashVehicleXML),
		TestCase("vecto_vehicle-sample_FULL_Entry_Order.xml", BasicHasingTests.HashVehicleXML),
		TestCase("vecto_vehicle-sample_FULL_Newlines_Linux_LF.xml", BasicHasingTests.HashVehicleXML),
		TestCase("vecto_vehicle-sample_FULL_Newlines_Mac_CR.xml", BasicHasingTests.HashVehicleXML),
		TestCase("vecto_vehicle-sample_FULL_Newlines_Windows_CRLF.xml", BasicHasingTests.HashVehicleXML),
		TestCase("vecto_engine-sample Encoding ISO 8859-15.xml", BasicHasingTests.HashEngineXML),
		TestCase("vecto_engine-sample Encoding UTF-8 BOM.xml", BasicHasingTests.HashEngineXML),
		TestCase("vecto_engine-sample Encoding UTF-8.xml", BasicHasingTests.HashEngineXML),
		TestCase("vecto_engine-sample Encoding UTF-16 BE BOM.xml", BasicHasingTests.HashEngineXML),
		TestCase("vecto_engine-sample Encoding UTF-16 LE.xml", BasicHasingTests.HashEngineXML),
		TestCase("vecto_engine-sample Encoding windows-1292.xml", BasicHasingTests.HashEngineXML),
		TestCase("vecto_engine-sample_Whitespaces.xml", BasicHasingTests.HashEngineXML),
		]
		public void TestHashComputationVariations(string file, string expectedHash)
		{
			var h = VectoHash.Load(@"Testdata\XML\Variations\" + file);
			var hash = h.ComputeHash();

			Assert.AreEqual(expectedHash, hash);
		}


		[TestCase(@"Testdata\XML\Validation\vecto_engine_valid.xml"),
		TestCase(@"Testdata\XML\Validation\vecto_gearbox_valid.xml")]
		public void TestValidation(string file)
		{
			var h = VectoHash.Load(file);
			Assert.IsTrue(h.ValidateHash());
		}

		[TestCase(@"Testdata\XML\Validation\vecto_engine_invalid.xml"),
		TestCase(@"Testdata\XML\Validation\vecto_gearbox_invalid.xml")]
		public void TestValidationInvalid(string file)
		{
			var h = VectoHash.Load(file);
			Assert.IsFalse(h.ValidateHash());
		}

		[TestCase(VectoComponents.Engine),
		TestCase(VectoComponents.Gearbox),
		]
		public void TestValidationComponentValid(VectoComponents component)
		{
			var file = @"Testdata\XML\Validation\vecto_vehicle_components_valid-engine_gbx.xml";
			var h = VectoHash.Load(file);

			Assert.IsTrue(h.ValidateHash(component));
		}

		[TestCase(VectoComponents.Engine),
		TestCase(VectoComponents.Gearbox),
		TestCase(VectoComponents.Axlegear),
		TestCase(VectoComponents.Angledrive),
		TestCase(VectoComponents.Retarder),
		TestCase(VectoComponents.TorqueConverter),
		TestCase(VectoComponents.Tyre),
		TestCase(VectoComponents.Airdrag),
		]
		public void TestValidationComponentInvalid(VectoComponents component)
		{
			var file = @"Testdata\XML\Validation\vecto_vehicle_components_invalid.xml";
			var h = VectoHash.Load(file);

			Assert.IsFalse(h.ValidateHash(component));
		}

		[TestCase(@"Testdata\XML\ToHash\vecto_engine-input.xml"),
		TestCase(@"Testdata\XML\ToHash\vecto_engine_withid-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());
		}

		[TestCase(@"Testdata\XML\ToHash\vecto_engine_withhash-input.xml", "input data already contains a signature element"),
		TestCase(@"Testdata\XML\ToHash\vecto_vehicle-sample.xml", "adding hash for Vehicle is not supported"),
		TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input_nodata.xml", "'Data' element for component 'Gearbox' not found!"),
		TestCase(@"Testdata\XML\ToHash\multiple_components.xml", "input must not contain multiple components!"),
		]
		public void TestAddHashException(string file, string expectedExceptionMsg)
		{
			var destination = Path.GetFileNameWithoutExtension(file) + "_hashed.xml";

			var h = VectoHash.Load(file);
			AssertHelper.Exception<Exception>(() => { var r = h.AddHash(); }, expectedExceptionMsg);
		}

		[TestCase]
		public void TestDuplicateSigElement()
		{
			var filename = @"Testdata\XML\Invalid\duplicate-sig.xml";
			var h = VectoHash.Load(filename);

			AssertHelper.Exception<Exception>(() => { var r = h.ReadHash(); }, "Multiple DigestValue elements found!");
		}


		[TestCase()]
		public void TestLoadFromStream()
		{
			var fs = new FileStream(BasicHasingTests.ReferenceXMLVehicle, FileMode.Open);
			var h = VectoHash.Load(fs);

			var hash = h.ComputeHash();
			Assert.AreEqual(BasicHasingTests.HashVehicleXML, hash);
			fs.Close();
		}

		[TestCase(WhitespaceHandling.All),
		TestCase(WhitespaceHandling.None),
		TestCase(WhitespaceHandling.Significant)]
		public void TestLoadXmlDocument(WhitespaceHandling whitespace)
		{
			var xml = new XmlDocument();
			var reader = new XmlTextReader(BasicHasingTests.ReferenceXMLVehicle);
			reader.WhitespaceHandling = whitespace;
			xml.Load(reader);
			var h = VectoHash.Load(xml);

			var hash = h.ComputeHash();
			Assert.AreEqual(BasicHasingTests.HashVehicleXML, hash);
		}

		[TestCase(@"Testdata\XML\ToHash\vecto_engine-input.xml"),
		TestCase(@"Testdata\XML\ToHash\vecto_engine_withid-input.xml"),
		TestCase(@"Testdata\XML\ToHash\vecto_gearbox-input.xml")]
		public void TestHashedComponentIsValid(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());

			// re-load generated XML and perform XSD validation
			var settings = new XmlReaderSettings() {
				ValidationType = ValidationType.Schema,
				ValidationFlags = XmlSchemaValidationFlags.ProcessInlineSchema |
								//XmlSchemaValidationFlags.ProcessSchemaLocation |
								XmlSchemaValidationFlags.ReportValidationWarnings
			};
			settings.ValidationEventHandler += new ValidationEventHandler(ValidationCallBack);
			settings.Schemas.Add(GetXMLSchema(false));
			var xmlValidator = XmlReader.Create(destination, settings);
			var xmlDoc = XDocument.Load(xmlValidator);
		}


		[TestCase("vecto_vehicle-namespace_prefix.xml", BasicHasingTests.HashVehicleXML)]
		public void TestNamespacePrefixVariations(string file, string expectedHash)
		{
			var h = VectoHash.Load(@"Testdata\XML\Variations\" + file);
			var hash = h.ComputeHash();

			Assert.AreEqual(expectedHash, hash);
		}

		[TestCase()]
		public void TestInvalidXMLAsFile()
		{
			var file = @"Testdata\XML\Invalid\invalid-comp.xml";

			AssertHelper.Exception<Exception>(() => VectoHash.Load(file), "failed to read XML document");
		}

		[TestCase()]
		public void TestInvalidXMLAsStream()
		{
			var file = @"Testdata\XML\Invalid\invalid-comp.xml";
			var stream = File.Open(file, FileMode.Open);
			AssertHelper.Exception<Exception>(() => VectoHash.Load(stream), "failed to read XML document");
		}

		[TestCase()]
		public void TestComputeHashNoComponentInXML()
		{
			var xml = @"<VectoInputDeclaration/>";
			var stream = new MemoryStream();
			var writer = new StreamWriter(stream);
			writer.Write(xml);
			writer.Flush();
			stream.Seek(0, SeekOrigin.Begin);

			var h = VectoHash.Load(stream);
			AssertHelper.Exception<Exception>(() => h.ComputeHash(), "No component found");
		}

		[TestCase()]
		public void TestReadHashNoComponentInXML()
		{
			var xml = @"<VectoInputDeclaration/>";
			var stream = new MemoryStream();
			var writer = new StreamWriter(stream);
			writer.Write(xml);
			writer.Flush();
			stream.Seek(0, SeekOrigin.Begin);

			var h = VectoHash.Load(stream);
			AssertHelper.Exception<Exception>(() => h.ReadHash(), "No component found");
		}

		[TestCase(VectoComponents.Engine, "ENG-"),
		TestCase(VectoComponents.Gearbox, "GBX-"),
		TestCase(VectoComponents.Axlegear, "AXL-"),
		TestCase(VectoComponents.Retarder, "RET-"),
		TestCase(VectoComponents.TorqueConverter, "TC-"),
		TestCase(VectoComponents.Angledrive, "ANGL-"),
		TestCase(VectoComponents.Airdrag, "AD-"),
		TestCase(VectoComponents.Tyre, "TYRE-"),
		]
		public void TestIdPrefix(VectoComponents component, string expectedPrefix)
		{
			Assert.AreEqual(expectedPrefix, component.HashIdPrefix());
		}

		[TestCase()]
		public void TestInvalidComponentXMLName()
		{
			AssertHelper.Exception<ArgumentOutOfRangeException>(() => ((VectoComponents)9999).XMLElementName());
		}

		[TestCase()]
		public void TestInvalidComponentPrefix()
		{
			AssertHelper.Exception<ArgumentOutOfRangeException>(() => ((VectoComponents)9999).HashIdPrefix());
		}

		private static XmlSchemaSet GetXMLSchema(bool job)
		{
			var resource = RessourceHelper.LoadResourceAsStream(RessourceHelper.ResourceType.XMLSchema,
				job ? "VectoInput.xsd" : "VectoComponent.xsd");
			var xset = new XmlSchemaSet() { XmlResolver = new XmlResourceResolver() };
			var reader = XmlReader.Create(resource, new XmlReaderSettings(), "schema://");
			xset.Add(XmlSchema.Read(reader, null));
			xset.Compile();
			return xset;
		}

		private static void ValidationCallBack(object sender, ValidationEventArgs args)
		{
			if (args.Severity == XmlSeverityType.Error) {
				throw new Exception(string.Format("Validation error: {0}" + Environment.NewLine +
												"Line: {1}", args.Message, args.Exception.LineNumber));
			}
		}
	}
}