From 7e1640e2a46952a855fa5b910555efc206dc4e4d Mon Sep 17 00:00:00 2001 From: Markus Quaritsch <markus.quaritsch@tugraz.at> Date: Tue, 19 Feb 2019 09:36:01 +0100 Subject: [PATCH] adding testcase for digest value in sum-file --- .../VectoCore/Utils/DataIntegrityHelper.cs | 22 +++++ VectoCore/VectoCore/VectoCore.csproj | 1 + .../Algorithms/CSVDigestValueTest.cs | 94 +++++++++++++++++++ VectoCore/VectoCoreTest/VectoCoreTest.csproj | 1 + 4 files changed, 118 insertions(+) create mode 100644 VectoCore/VectoCore/Utils/DataIntegrityHelper.cs create mode 100644 VectoCore/VectoCoreTest/Algorithms/CSVDigestValueTest.cs diff --git a/VectoCore/VectoCore/Utils/DataIntegrityHelper.cs b/VectoCore/VectoCore/Utils/DataIntegrityHelper.cs new file mode 100644 index 0000000000..2f4ad7fcdd --- /dev/null +++ b/VectoCore/VectoCore/Utils/DataIntegrityHelper.cs @@ -0,0 +1,22 @@ +using System.Linq; +using System.Security.Cryptography; +using System.Text; + +namespace TUGraz.VectoCore.Utils +{ + public class DataIntegrityHelper + { + public static string ComputeDigestValue(string[] lines) + { + var hash = System.Convert.ToBase64String(GetHash(string.Join("\n", lines))); + + return string.Format("SHA256: {0}", hash); + } + + public static byte[] GetHash(string inputString) + { + HashAlgorithm algorithm = SHA256.Create(); + return algorithm.ComputeHash(Encoding.UTF8.GetBytes(inputString)); + } + } +} diff --git a/VectoCore/VectoCore/VectoCore.csproj b/VectoCore/VectoCore/VectoCore.csproj index 8cb11f2181..745ae5e45e 100644 --- a/VectoCore/VectoCore/VectoCore.csproj +++ b/VectoCore/VectoCore/VectoCore.csproj @@ -222,6 +222,7 @@ <Compile Include="OutputData\XML\XMLDeclarationWriter.cs" /> <Compile Include="OutputData\XML\XMLEngineeringWriter.cs" /> <Compile Include="OutputData\XML\XMLManufacturerReport.cs" /> + <Compile Include="Utils\DataIntegrityHelper.cs" /> <Compile Include="Utils\MeanShiftClustering.cs" /> <Compile Include="Utils\ProviderExtensions.cs" /> <Compile Include="Models\Declaration\AirDrag.cs" /> diff --git a/VectoCore/VectoCoreTest/Algorithms/CSVDigestValueTest.cs b/VectoCore/VectoCoreTest/Algorithms/CSVDigestValueTest.cs new file mode 100644 index 0000000000..dbb3dfbff9 --- /dev/null +++ b/VectoCore/VectoCoreTest/Algorithms/CSVDigestValueTest.cs @@ -0,0 +1,94 @@ +using System.Collections.Generic; +using System.Data; +using System.IO; +using System.Linq; +using NUnit.Framework; +using NUnit.Framework.Internal; +using TUGraz.VectoCommon.Utils; +using TUGraz.VectoCore.Utils; + +namespace TUGraz.VectoCore.Tests.Algorithms +{ + [TestFixture] + public class CSVDigestValueTest + { + + [TestCase] + public void TestDigestValueCreation() + { + var tbl = CreateDataTable(new[] { "t", "dt", "v" }, 5); + + var str = new MemoryStream(); + var writer = new StreamWriter(str); + VectoCSVFile.Write(writer, tbl, true, true); + + writer.Flush(); + str.Flush(); + str.Seek(0, SeekOrigin.Begin); + + var reader = new StreamReader(str); + var lines = new List<string>(); + while (!reader.EndOfStream) + lines.Add(reader.ReadLine()); + + var last = lines.Last(); + + Assert.IsTrue(last.StartsWith("#@"), "Digest Identifier not found"); + Assert.IsTrue(last.Contains("SHA256"), "Digest descriptor SHA256 not found"); + } + + [TestCase] + public void TestDigestValueValidation() + { + var tbl = CreateDataTable(new[] { "t", "dt", "v" }, 5); + + var str = new MemoryStream(); + var writer = new StreamWriter(str); + VectoCSVFile.Write(writer, tbl, true, true); + + writer.Flush(); + str.Flush(); + str.Seek(0, SeekOrigin.Begin); + + var reader = new StreamReader(str); + var lines = new List<string>(); + while (!reader.EndOfStream) + lines.Add(reader.ReadLine()); + + var last = lines.Last(); + + Assert.IsTrue(last.StartsWith("#@"), "Digest Identifier not found"); + Assert.IsTrue(last.Contains("SHA256"), "Digest descriptor SHA256 not found"); + + var otherLines = lines.Where(x => !x.StartsWith("#@")).ToArray(); + + var digest = DataIntegrityHelper.ComputeDigestValue(otherLines); + + Assert.AreEqual(string.Format("#@ {0}", digest), last); + } + + private static DataTable CreateDataTable(string[] cols, int numRows) + { + var tbl = new DataTable(); + + foreach (var col in cols) { + tbl.Columns.Add(col, typeof(double)); + } + + var rnd = new Randomizer(873); + for (var i = 0; i < numRows; i++) { + var row = tbl.NewRow(); + foreach (var col in cols) { + row[col] = rnd.NextDouble(100); + } + + tbl.Rows.Add(row); + } + + return tbl; + } + + + + } +} diff --git a/VectoCore/VectoCoreTest/VectoCoreTest.csproj b/VectoCore/VectoCoreTest/VectoCoreTest.csproj index 994ec739ed..0a26d5437b 100644 --- a/VectoCore/VectoCoreTest/VectoCoreTest.csproj +++ b/VectoCore/VectoCoreTest/VectoCoreTest.csproj @@ -77,6 +77,7 @@ <Otherwise /> </Choose> <ItemGroup> + <Compile Include="Algorithms\CSVDigestValueTest.cs" /> <Compile Include="Algorithms\MeanShiftClusteringTest.cs" /> <Compile Include="Dummy\EngineFLDTest.cs" /> <Compile Include="Exceptions\ExceptionTests.cs" /> -- GitLab