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

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

more sanity checking

parent a3114141
No related branches found
No related tags found
No related merge requests found
......@@ -16,22 +16,25 @@ namespace HashingCmd
public delegate void HashingAction(string filename, VectoHash h);
private const string Usage = @"
hashingcmd.exe -v <file.xml>
hashingcmd.exe (-h | [-v] [[-s] -x] [-c] [-r]) <file.xml> <file2.xml> <file3.xml>
";
private const string Help = @"
hashingcmd.exe
-h: help
-h: print help
-v: verify hashed file
-s: create hashed file file
-s: create hashed file
-x: validate generated XML against VECTO XML schema
-c: compute hash and write to stdout
-r: read hash from file and write to stdout
";
static Dictionary<string, HashingAction> actions = new Dictionary<string, HashingAction>();
static bool _validateXML = false;
static int Main(string[] args)
{
try {
......@@ -45,7 +48,16 @@ hashingcmd.exe
actions["-r"] = ReadHashAction;
actions["-s"] = CreateHashedFileAction;
var fileList = args.Except(actions.Keys);
if (args.Contains("-x")) {
_validateXML = true;
}
var fileList = args.Except(actions.Keys.Concat(new[] { "-x" })).ToArray();
if (fileList.Length == 0 || !args.Intersect(actions.Keys.ToArray()).Any()) {
ShowVersionInformation();
Console.Write(Usage);
return 0;
}
foreach (var file in fileList) {
WriteLine("processing " + Path.GetFileName(file));
if (!File.Exists(Path.GetFullPath(file))) {
......@@ -60,6 +72,9 @@ hashingcmd.exe
} catch (Exception e) {
Console.ForegroundColor = ConsoleColor.Red;
Console.Error.WriteLine(e.Message);
if (e.InnerException != null) {
Console.Error.WriteLine(e.InnerException.Message);
}
Console.ResetColor();
}
}
......@@ -115,7 +130,7 @@ hashingcmd.exe
}
for (var i = 0; i < component.Count; i++) {
var readHash = h.ReadHash(component.Entry, i);
WriteLine(" " + component.Entry.XMLElementName() + "\t ... >" + readHash + "<");
WriteLine(" " + component.Entry.XMLElementName() + "\t ... " + readHash + "");
}
}
}
......@@ -135,14 +150,14 @@ hashingcmd.exe
}
for (var i = 0; i < component.Count; i++) {
var computedHash = h.ComputeHash(component.Entry, i);
WriteLine(" " + component.Entry.XMLElementName() + "\t ... >" + computedHash + "<");
WriteLine(" " + component.Entry.XMLElementName() + "\t ... " + computedHash + "");
}
}
var jobHash = h.ComputeHash();
WriteLine(" job file\t ... >" + jobHash + "<");
WriteLine(" job file\t ... " + jobHash + "");
} else {
var hash = h.ComputeHash();
WriteLine(" computed hash: >" + hash + "<");
WriteLine(" computed hash: " + hash + "");
}
}
......
......@@ -19,14 +19,22 @@ namespace TUGraz.VectoHashing
public static VectoHash Load(string filename)
{
var doc = new XmlDocument();
doc.Load(new XmlTextReader(filename));
try {
doc.Load(new XmlTextReader(filename));
} catch (Exception e) {
throw new Exception("failed to read XML document", e);
}
return new VectoHash(doc);
}
public static VectoHash Load(Stream stream)
{
var doc = new XmlDocument();
doc.Load(new XmlTextReader(stream));
try {
doc.Load(new XmlTextReader(stream));
} catch (Exception e) {
throw new Exception("failed to read XML document", e);
}
return new VectoHash(doc);
}
......@@ -77,12 +85,15 @@ namespace TUGraz.VectoHashing
if (components.Count > 1) {
throw new Exception("input must not contain multiple components!");
}
var query = string.Format("//*[local-name()='{0}']/*[local-name()='Data']", components[0]);
if (components.Count == 0) {
throw new Exception("input does not contain a known component!");
}
var query = string.Format("//*[local-name()='{0}']/*[local-name()='Data']", components[0].XMLElementName());
var node = Document.SelectSingleNode(query);
if (node == null) {
throw new Exception(string.Format("'Data' element for component '{0}' not found!", components[0]));
throw new Exception(string.Format("'Data' element for component '{0}' not found!", components[0].XMLElementName()));
}
query = string.Format("//*[local-name()='{0}']/*[local-name()='Signature']", components[0]);
query = string.Format("//*[local-name()='{0}']/*[local-name()='Signature']", components[0].XMLElementName());
var sigNodes = Document.SelectNodes(query);
if (sigNodes != null && sigNodes.Count > 0) {
throw new Exception("input data already contains a signature element");
......@@ -160,8 +171,15 @@ namespace TUGraz.VectoHashing
private static string GetHashValue(XmlDocument hashed, string elementToHash)
{
var node = hashed.SelectSingleNode("//*[@URI='#" + elementToHash + "']/*[local-name() = 'DigestValue']");
return node == null ? null : node.InnerText;
//var node = hashed.SelectSingleNode("//*[@URI='#" + elementToHash + "']/*[local-name() = 'DigestValue']");
var nodes = hashed.SelectNodes("//*[@URI='#" + elementToHash + "']/*[local-name() = 'DigestValue']");
if (nodes == null || nodes.Count == 0) {
return null;
}
if (nodes.Count > 1) {
throw new Exception("Multiple DigestValue elements found!");
}
return nodes[0].InnerText;
}
}
}
\ No newline at end of file
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