Newer
Older
using System.Security.Cryptography;
using System.Security.Cryptography.Xml;
using System.Text;
using System.Xml;
using System.Xml.Linq;
using System.Xml.Schema;
using System.Xml.XPath;
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(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("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);
[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);
}
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
[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);
}
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));
}
}