public void DataElementAttributes() { var errors = new StringBuilder(); var deByType = new Dictionary <Type, DataElementAttribute>(); var deByName = new Dictionary <string, KeyValuePair <DataElementAttribute, Type> >(); foreach (var kv in ClassLoader.GetAllByAttribute <DataElementAttribute>(null)) { var attr = kv.Key; var type = kv.Value; // Verify only 1 DataElement attribute per type if (deByType.ContainsKey(type)) { var old = deByType[type]; errors.AppendLine(); errors.AppendFormat("DataElement in assembly '{0}' class '{1}' declared both '{2}' and '{3}.", type.Assembly.Location, type.FullName, attr.elementName, old.elementName); } else { deByType.Add(type, attr); } // Verify no elementName collissions if (deByName.ContainsKey(attr.elementName)) { var old = deByName[attr.elementName]; errors.AppendLine(); errors.AppendFormat("DataElement '{0}' declared in assembly '{1}' class '{2}' and in assembly {3} and class '{4}'.", attr.elementName, kv.Value.Assembly.Location, kv.Value.FullName, old.Value.Assembly.Location, old.Value.FullName); } else { deByName.Add(attr.elementName, kv); } var paramAttrs = type.GetAttributes <ParameterAttribute>(null).Select(a => a.name).ToList(); var dupes = paramAttrs.GroupBy(a => a).SelectMany(g => g.Skip(1)).Distinct().ToList(); if (dupes.Count != 0) { errors.AppendLine(); errors.AppendFormat("DataElement '{0}' declared in assembly '{1}' class '{2}' has duplicate parameters: '{3}'", attr.elementName, type.Assembly.Location, type.FullName, string.Join("', '", dupes)); } } string msg = errors.ToString(); if (!string.IsNullOrEmpty(msg)) { logger.Debug(msg); Assert.Null(msg); } }
private XmlSchemaElement[] MakeDataModel(string minOccurs, string maxOccurrs) { var ret = new List <XmlSchemaElement>(); foreach (var item in ClassLoader.GetAllByAttribute <PitParsableAttribute>((t, a) => a.topLevel)) { var name = item.Key.xmlElementName; var type = item.Value; var elem = MakeDataElement(name, type); elem.MinOccursString = minOccurs; elem.MaxOccursString = maxOccurrs; ret.Add(elem); } return(ret.ToArray()); }
public FormMain() { InitializeComponent(); foreach (var strategy in ClassLoader.GetAllByAttribute <MutationStrategyAttribute>(null)) { comboBoxFuzzingStrategy.Items.Add(strategy.Key.Name); } //tabControl.TabPages.Remove(tabPageGUI); tabControl.TabPages.Remove(tabPageFuzzing); //tabControl.TabPages.Remove(tabPageOutput); LoadPlatformAssembly(); // Check OS and load side assembly Platform.OS os = Platform.GetOS(); switch (os) { case Platform.OS.OSX: tabControl.TabPages.Remove(tabPageDebuggerLinux); tabControl.TabPages.Remove(tabPageDebuggerWin); tabControl.TabPages.Remove(tabPageGUI); richTextBoxOSX.LoadFile(Assembly.GetExecutingAssembly().GetManifestResourceStream("PeachFuzzBang.OSXDebugging.rtf"), RichTextBoxStreamType.RichText); break; case Platform.OS.Linux: tabControl.TabPages.Remove(tabPageDebuggerOSX); tabControl.TabPages.Remove(tabPageDebuggerWin); tabControl.TabPages.Remove(tabPageGUI); richTextBoxLinux.LoadFile(Assembly.GetExecutingAssembly().GetManifestResourceStream("PeachFuzzBang.LinuxDebugging.rtf"), RichTextBoxStreamType.RichText); break; case Platform.OS.Windows: { comboBoxAttachToServiceServices.Items.Clear(); foreach (ServiceController srv in ServiceController.GetServices()) { comboBoxAttachToServiceServices.Items.Add(srv.ServiceName); } textBoxAttachToProcessProcessName.Items.Clear(); foreach (Process proc in Process.GetProcesses()) { textBoxAttachToProcessProcessName.Items.Add(proc.ProcessName); proc.Close(); } tabControl.TabPages.Remove(tabPageDebuggerOSX); tabControl.TabPages.Remove(tabPageDebuggerLinux); if (!Environment.Is64BitProcess && Environment.Is64BitOperatingSystem) { MessageBox.Show("Warning: The 64bit version of Peach 3 must be used on 64 bit Operating Systems.", "Warning"); } string windbg = null; Type t = ClassLoader.FindTypeByAttribute <MonitorAttribute>((x, y) => y.Name == "WindowsDebugger"); if (t != null) { windbg = t.InvokeMember("FindWinDbg", BindingFlags.InvokeMethod, null, null, null) as string; } if (windbg != null) { textBoxDebuggerPath.Text = windbg; } else { textBoxDebuggerPath.Text = "Error, could not locate windbg!"; } } break; } if (os != Platform.OS.Windows) { // Update default settings to include full path to PeachFuzzBang // When double clicking the app to run it, the current working // directory is $HOME string cwd = Environment.CurrentDirectory + Path.DirectorySeparatorChar; string path = Path.GetDirectoryName(Assembly.GetExecutingAssembly().Location) + Path.DirectorySeparatorChar; if (path.StartsWith(cwd)) { path = path.Substring(cwd.Length); } if (!string.IsNullOrEmpty(path)) { textBoxFuzzedFile.Text = Path.Combine(path, textBoxFuzzedFile.Text); textBoxTemplateFiles.Text = Path.Combine(path, textBoxTemplateFiles.Text); textBoxLinuxArguments.Text = Path.Combine(path, textBoxLinuxArguments.Text); textBoxOSXArguments.Text = Path.Combine(path, textBoxOSXArguments.Text); } } buttonStartFuzzing.Enabled = true; buttonSaveConfiguration.Enabled = false; buttonStopFuzzing.Enabled = false; comboBoxPitDataModel.SelectedIndexChanged += new EventHandler(comboBoxPitDataModel_SelectedIndexChanged); richTextBoxIntroduction.LoadFile(Assembly.GetExecutingAssembly().GetManifestResourceStream("PeachFuzzBang.Introduction.rtf"), RichTextBoxStreamType.RichText); }
void PopulatePluginType(XmlSchemaComplexType complexType, PluginElementAttribute pluginAttr) { if (pluginAttr.Named) { var nameAttr = new XmlSchemaAttribute(); nameAttr.Name = "name"; nameAttr.Annotate("{0} name.".Fmt(pluginAttr.PluginType.Name)); nameAttr.Use = XmlSchemaUse.Optional; complexType.Attributes.Add(nameAttr); } var typeAttr = new XmlSchemaAttribute(); typeAttr.Name = pluginAttr.AttributeName; typeAttr.Use = XmlSchemaUse.Required; typeAttr.Annotate("Specify the class name of a Peach {0}. You can implement your own {1}s as needed.".Fmt( pluginAttr.PluginType.Name, pluginAttr.PluginType.Name.ToLower() )); var restrictEnum = new XmlSchemaSimpleTypeRestriction(); restrictEnum.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace); foreach (var item in ClassLoader.GetAllByAttribute <PluginAttribute>((t, a) => a.Type == pluginAttr.PluginType && a.IsDefault && !a.IsTest)) { restrictEnum.Facets.Add(MakePluginFacet(item.Key, item.Value)); } var enumType = new XmlSchemaSimpleType(); enumType.Content = restrictEnum; var restrictLen = new XmlSchemaSimpleTypeRestriction(); restrictLen.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace); restrictLen.Facets.Add(new XmlSchemaMaxLengthFacet() { Value = "1024" }); var userType = new XmlSchemaSimpleType(); userType.Content = restrictLen; var union = new XmlSchemaSimpleTypeUnion(); union.BaseTypes.Add(userType); union.BaseTypes.Add(enumType); var schemaType = new XmlSchemaSimpleType(); schemaType.Content = union; typeAttr.SchemaType = schemaType; complexType.Attributes.Add(typeAttr); if (!objElemCache.ContainsKey(typeof(PluginParam))) { AddElement("Param", typeof(PluginParam), null); } var schemaElem = new XmlSchemaElement(); schemaElem.RefName = new XmlQualifiedName("Param", schema.TargetNamespace); XmlSchemaGroupBase schemaParticle; if (pluginAttr.PluginType == typeof(Transformer)) { schemaParticle = new XmlSchemaChoice(); schemaParticle.MinOccursString = "0"; schemaParticle.MaxOccursString = "unbounded"; var transElem = new XmlSchemaElement(); transElem.RefName = new XmlQualifiedName("Transformer", schema.TargetNamespace); schemaParticle.Items.Add(transElem); } else { schemaParticle = new XmlSchemaSequence(); schemaElem.MinOccursString = "0"; schemaElem.MaxOccursString = "unbounded"; } schemaParticle.Items.Add(schemaElem); complexType.Particle = schemaParticle; }
void CombinePluginType(XmlSchemaComplexType complexType, PluginElementAttribute pluginAttr) { var addedAttrs = new Dictionary <string, XmlSchemaAttribute>(); var addedElems = new Dictionary <string, XmlSchemaElement>(); var schemaParticle = new XmlSchemaChoice(); schemaParticle.MinOccursString = "0"; schemaParticle.MaxOccursString = "unbounded"; var restrictEnum = new XmlSchemaSimpleTypeRestriction(); restrictEnum.BaseTypeName = new XmlQualifiedName("string", XmlSchema.Namespace); foreach (var item in ClassLoader.GetAllByAttribute <PluginAttribute>((t, a) => a.Type == pluginAttr.PluginType && a.IsDefault && !a.IsTest)) { restrictEnum.Facets.Add(MakePluginFacet(item.Key, item.Value)); foreach (var pi in item.Value.GetProperties()) { var attrAttr = pi.GetAttributes <XmlAttributeAttribute>().FirstOrDefault(); if (attrAttr != null) { var attr = MakeAttribute(attrAttr.AttributeName, pi); if (!addedAttrs.ContainsKey(attr.Name)) { complexType.Attributes.Add(attr); addedAttrs.Add(attr.Name, attr); } continue; } var elemAttr = pi.GetAttributes <XmlElementAttribute>().FirstOrDefault(); if (elemAttr != null) { var elems = MakeElement(elemAttr.ElementName, null, pi, null); foreach (var elem in elems) { var key = elem.Name ?? elem.RefName.Name; if (!addedElems.ContainsKey(key)) { elem.MinOccursString = null; elem.MaxOccursString = null; schemaParticle.Items.Add(elem); addedElems.Add(key, elem); } } } var anyAttr = pi.GetAttributes <XmlAnyAttributeAttribute>().FirstOrDefault(); if (anyAttr != null) { complexType.AnyAttribute = new XmlSchemaAnyAttribute() { ProcessContents = XmlSchemaContentProcessing.Skip }; continue; } } foreach (var prop in item.Value.GetAttributes <ParameterAttribute>()) { var attr = MakeAttribute(prop.name, prop); if (!addedAttrs.ContainsKey(attr.Name)) { complexType.Attributes.Add(attr); addedAttrs.Add(attr.Name, attr); } } } var enumType = new XmlSchemaSimpleType(); enumType.Content = restrictEnum; var typeAttr = new XmlSchemaAttribute(); typeAttr.Name = pluginAttr.AttributeName; typeAttr.Use = XmlSchemaUse.Required; typeAttr.SchemaType = enumType; typeAttr.Annotate("Specify the {0} of a Peach {1}.".Fmt( pluginAttr.AttributeName, pluginAttr.PluginType.Name.ToLower() )); complexType.Attributes.Add(typeAttr); if (schemaParticle.Items.Count > 0) { complexType.Particle = schemaParticle; } }
void AddDataElement(string name, Type type) { var schemaElem = MakeItem <XmlSchemaElement>(name, type, objElemCache); var complexType = new XmlSchemaComplexType(); var schemaParticle = new XmlSchemaChoice(); schemaParticle.MinOccursString = "0"; schemaParticle.MaxOccursString = "unbounded"; foreach (var prop in type.GetAttributes <ParameterAttribute>()) { var attr = MakeAttribute(prop.name, prop); complexType.Attributes.Add(attr); } var deAttr = type.GetAttributes <Peach.Core.Dom.DataElementAttribute>().First(); if (deAttr.elementTypes.HasFlag(Peach.Core.Dom.DataElementTypes.DataElements)) { foreach (var kv in ClassLoader.GetAllByAttribute <Peach.Core.Dom.DataElementAttribute>(null)) { var parents = kv.Value.GetAttributes <Peach.Core.Dom.DataElementParentSupportedAttribute>(); if (parents.Any() && !parents.Any(a => a.elementName == name)) { continue; } var elem = MakeDataElement(kv.Key.elementName, kv.Value); schemaParticle.Items.Add(elem); } } foreach (var child in type.GetAttributes <Peach.Core.Dom.DataElementChildSupportedAttribute>()) { var childType = ClassLoader.FindTypeByAttribute <Peach.Core.Dom.DataElementAttribute>((t, a) => a.elementName == child.elementName); var elem = MakeDataElement(child.elementName, childType); schemaParticle.Items.Add(elem); } if (deAttr.elementTypes.HasFlag(Peach.Core.Dom.DataElementTypes.Fixup)) { PopulateDataElement(schemaParticle, "Fixup"); } if (deAttr.elementTypes.HasFlag(Peach.Core.Dom.DataElementTypes.Hint)) { var elems = MakeElement("Hint", null, typeof(DataModel).GetProperty("Hint"), null); foreach (var elem in elems) { elem.MinOccursString = null; elem.MaxOccursString = null; schemaParticle.Items.Add(elem); } } if (deAttr.elementTypes.HasFlag(Peach.Core.Dom.DataElementTypes.Transformer)) { PopulateDataElement(schemaParticle, "Transformer"); } if (deAttr.elementTypes.HasFlag(Peach.Core.Dom.DataElementTypes.Relation)) { PopulateDataElement(schemaParticle, "Relation"); } if (deAttr.elementTypes.HasFlag(Peach.Core.Dom.DataElementTypes.Analyzer)) { PopulateDataElement(schemaParticle, "Analyzer"); } if (schemaParticle.Items.Count > 0) { complexType.Particle = schemaParticle; } schemaElem.SchemaType = complexType; }
public void PluginAttributes() { var errors = new StringBuilder(); // All plugins should have: // 1) One plugin attribute with default=true // 2) A description // 3) No duplicated parameters // 4) No plugin attribute names should conflict // 5) Plugins can only be of one type (Monitor/Publisher/Fixup) var pluginsByType = new Dictionary <Type, List <PluginAttribute> >(); var pluginsByName = new Dictionary <string, KeyValuePair <PluginAttribute, Type> >(); foreach (var kv in ClassLoader.GetAllByAttribute <Peach.Core.PluginAttribute>(null)) { var attr = kv.Key; var type = kv.Value; if (!pluginsByType.ContainsKey(type)) { pluginsByType.Add(type, new List <PluginAttribute>()); } pluginsByType[type].Add(attr); string pluginName = string.Format("{0} '{1}'", attr.Type.Name, attr.Name); // Verify #4 (no name collisions) if (pluginsByName.ContainsKey(pluginName)) { var old = pluginsByName[pluginName]; errors.AppendLine(); if (old.Value == kv.Value) { errors.AppendFormat("{0} declared more than once in assembly '{1}' class '{2}'.", pluginName, kv.Value.Assembly.Location, kv.Value.FullName); } else { errors.AppendFormat("{0} declared in assembly '{1}' class '{2}' and in assembly {3} and class '{4}'.", pluginName, kv.Value.Assembly.Location, kv.Value.FullName, old.Value.Assembly.Location, old.Value.FullName); } } else { pluginsByName.Add(pluginName, kv); } } foreach (var kv in pluginsByType) { var type = kv.Key; var attrs = kv.Value; // Verify #4 (eEnsure all plugin attributes are of the same type) var pluginTypes = attrs.Select(a => a.Type.Name).Distinct().ToList(); if (pluginTypes.Count != 1) { errors.AppendLine(); errors.AppendFormat("Plugin declared in assembly '{1}' class '{2}' has multiple types: '{3}'", attrs[0].Type.Name, type.Assembly.Location, type.FullName, string.Join("', '", pluginTypes)); } // Verify #1 (ensure there is a single default) var defaults = attrs.Where(a => a.IsDefault).Select(a => a.Name).ToList(); if (defaults.Count == 0) { errors.AppendLine(); errors.AppendFormat("{0} declared in assembly '{1}' class '{2}' has no default name.", attrs[0].Type.Name, type.Assembly.Location, type.FullName); } else if (defaults.Count != 1) { errors.AppendLine(); errors.AppendFormat("{0} declared in assembly '{1}' class '{2}' has multiple defaults: '{3}'", attrs[0].Type.Name, type.Assembly.Location, type.FullName, string.Join("', '", defaults)); } // Verify #2 (ensure there is a description) //var desc = type.GetAttributes<DescriptionAttribute>(null).FirstOrDefault(); //if (desc == null) //{ // errors.AppendLine(); // errors.AppendFormat("{0} declared in assembly '{1}' class '{2}' has no description.", // attrs[0].Type.Name, type.Assembly.Location, type.FullName); //} // Verify #3 (all the parameters must be unique in name) var paramAttrs = type.GetAttributes <ParameterAttribute>(null).Select(a => a.name).ToList(); var dupes = paramAttrs.GroupBy(a => a).SelectMany(g => g.Skip(1)).Distinct().ToList(); if (dupes.Count != 0) { errors.AppendLine(); errors.AppendFormat("{0} declared in assembly '{1}' class '{2}' has duplicate parameters: '{3}'", attrs[0].Type.Name, type.Assembly.Location, type.FullName, string.Join("', '", dupes)); } } string msg = errors.ToString(); if (!string.IsNullOrEmpty(msg)) { logger.Debug(msg); Assert.Null(msg); } }