updated schema to check if oneOf length, reflength or data is set.
This commit is contained in:
parent
94d24a8b19
commit
67485f775a
|
@ -2,9 +2,12 @@
|
|||
<project version="4">
|
||||
<component name="ChangeListManager">
|
||||
<list default="true" id="208c8f22-eeea-487d-8a23-c2fce1eb1434" name="Default" comment="">
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/npd.iml" afterPath="$PROJECT_DIR$/.idea/npd.iml" />
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/.idea/workspace.xml" afterPath="$PROJECT_DIR$/.idea/workspace.xml" />
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/example.yml" afterPath="$PROJECT_DIR$/example.yml" />
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/protocols/osi1-2/arp.yml" afterPath="$PROJECT_DIR$/protocols/osi1-2/arp.yml" />
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/protocols/osi1-2/eth-frame.yml" afterPath="$PROJECT_DIR$/protocols/osi1-2/eth-frame.yml" />
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/protocols/osi1-2/eth-packet.yml" afterPath="$PROJECT_DIR$/protocols/osi1-2/eth-packet.yml" />
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/protocols/osi3/icmpv4.yml" afterPath="$PROJECT_DIR$/protocols/osi3/icmpv4.yml" />
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/protocols/osi3/ipv4.yml" afterPath="$PROJECT_DIR$/protocols/osi3/ipv4.yml" />
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/schema.yml" afterPath="$PROJECT_DIR$/schema.yml" />
|
||||
<change type="MODIFICATION" beforePath="$PROJECT_DIR$/validate.py" afterPath="$PROJECT_DIR$/validate.py" />
|
||||
</list>
|
||||
|
@ -29,10 +32,10 @@
|
|||
<file leaf-file-name="validate.py" pinned="false" current-in-tab="true">
|
||||
<entry file="file://$PROJECT_DIR$/validate.py">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="216">
|
||||
<caret line="12" column="33" selection-start-line="12" selection-start-column="33" selection-end-line="12" selection-end-column="33" />
|
||||
<state relative-caret-position="410">
|
||||
<caret line="39" column="43" selection-start-line="39" selection-start-column="43" selection-end-line="39" selection-end-column="43" />
|
||||
<folding>
|
||||
<element signature="e#1#11#0" expanded="true" />
|
||||
<element signature="e#0#10#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
|
@ -130,7 +133,7 @@
|
|||
</component>
|
||||
<component name="PropertiesComponent">
|
||||
<property name="last_opened_file_path" value="$PROJECT_DIR$" />
|
||||
<property name="settings.editor.selected.configurable" value="com.jetbrains.python.configuration.PyActiveSdkModuleConfigurable" />
|
||||
<property name="settings.editor.selected.configurable" value="preferences.keymap" />
|
||||
<property name="settings.editor.splitter.proportion" value="0.2" />
|
||||
</component>
|
||||
<component name="RunManager" selected="Python.validate">
|
||||
|
@ -147,7 +150,7 @@
|
|||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<module name="npd" />
|
||||
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/validate.py" />
|
||||
<option name="PARAMETERS" value="example.yml" />
|
||||
<option name="PARAMETERS" value="--all -v" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<method />
|
||||
</configuration>
|
||||
|
@ -285,8 +288,26 @@
|
|||
<option name="USE_KEYWORD" value="false" />
|
||||
<method />
|
||||
</configuration>
|
||||
<list size="1">
|
||||
<item index="0" class="java.lang.String" itemvalue="Python.validate" />
|
||||
<configuration default="false" name="validate single file" type="PythonConfigurationType" factoryName="Python">
|
||||
<option name="INTERPRETER_OPTIONS" value="" />
|
||||
<option name="PARENT_ENVS" value="true" />
|
||||
<envs>
|
||||
<env name="PYTHONUNBUFFERED" value="1" />
|
||||
</envs>
|
||||
<option name="SDK_HOME" value="" />
|
||||
<option name="WORKING_DIRECTORY" value="$PROJECT_DIR$" />
|
||||
<option name="IS_MODULE_SDK" value="true" />
|
||||
<option name="ADD_CONTENT_ROOTS" value="true" />
|
||||
<option name="ADD_SOURCE_ROOTS" value="true" />
|
||||
<module name="npd" />
|
||||
<option name="SCRIPT_NAME" value="$PROJECT_DIR$/validate.py" />
|
||||
<option name="PARAMETERS" value="-f example.yml" />
|
||||
<option name="SHOW_COMMAND_LINE" value="false" />
|
||||
<method />
|
||||
</configuration>
|
||||
<list size="2">
|
||||
<item index="0" class="java.lang.String" itemvalue="Python.validate single file" />
|
||||
<item index="1" class="java.lang.String" itemvalue="Python.validate" />
|
||||
</list>
|
||||
<recent_temporary>
|
||||
<list size="1">
|
||||
|
@ -351,7 +372,7 @@
|
|||
<state relative-caret-position="0">
|
||||
<caret line="0" column="0" selection-start-line="0" selection-start-column="0" selection-end-line="0" selection-end-column="0" />
|
||||
<folding>
|
||||
<element signature="e#1#11#0" expanded="true" />
|
||||
<element signature="e#0#10#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
|
@ -361,7 +382,7 @@
|
|||
<state relative-caret-position="234">
|
||||
<caret line="14" column="25" selection-start-line="14" selection-start-column="25" selection-end-line="14" selection-end-column="25" />
|
||||
<folding>
|
||||
<element signature="e#1#11#0" expanded="true" />
|
||||
<element signature="e#0#10#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
|
@ -378,7 +399,7 @@
|
|||
<state relative-caret-position="234">
|
||||
<caret line="14" column="25" selection-start-line="14" selection-start-column="25" selection-end-line="14" selection-end-column="25" />
|
||||
<folding>
|
||||
<element signature="e#1#11#0" expanded="true" />
|
||||
<element signature="e#0#10#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
|
@ -395,7 +416,7 @@
|
|||
<state relative-caret-position="234">
|
||||
<caret line="14" column="25" selection-start-line="14" selection-start-column="25" selection-end-line="14" selection-end-column="25" />
|
||||
<folding>
|
||||
<element signature="e#1#11#0" expanded="true" />
|
||||
<element signature="e#0#10#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
|
@ -439,10 +460,10 @@
|
|||
</entry>
|
||||
<entry file="file://$PROJECT_DIR$/validate.py">
|
||||
<provider selected="true" editor-type-id="text-editor">
|
||||
<state relative-caret-position="216">
|
||||
<caret line="12" column="33" selection-start-line="12" selection-start-column="33" selection-end-line="12" selection-end-column="33" />
|
||||
<state relative-caret-position="410">
|
||||
<caret line="39" column="43" selection-start-line="39" selection-start-column="43" selection-end-line="39" selection-end-column="43" />
|
||||
<folding>
|
||||
<element signature="e#1#11#0" expanded="true" />
|
||||
<element signature="e#0#10#0" expanded="true" />
|
||||
</folding>
|
||||
</state>
|
||||
</provider>
|
||||
|
|
|
@ -15,4 +15,5 @@ protocol: &icmpv4
|
|||
desc: rest of header
|
||||
length: 4
|
||||
- field: data
|
||||
data: true
|
||||
optional: true
|
20
schema.yml
20
schema.yml
|
@ -26,12 +26,16 @@ definitions:
|
|||
field:
|
||||
type: object
|
||||
properties:
|
||||
length:
|
||||
type: integer
|
||||
reflength:
|
||||
type: string
|
||||
data:
|
||||
type: boolean
|
||||
field:
|
||||
type: string
|
||||
desc:
|
||||
type: string
|
||||
length:
|
||||
type: integer
|
||||
bitfield:
|
||||
type: boolean
|
||||
optional:
|
||||
|
@ -40,16 +44,18 @@ definitions:
|
|||
type: boolean
|
||||
nextlayer:
|
||||
type: boolean
|
||||
data:
|
||||
type: boolean
|
||||
reference:
|
||||
type: string
|
||||
reflength:
|
||||
type: string
|
||||
subfields:
|
||||
type: array
|
||||
items:
|
||||
$ref: "#/definitions/field"
|
||||
oneOf:
|
||||
- required:
|
||||
- length
|
||||
- required:
|
||||
- reflength
|
||||
- required:
|
||||
- data
|
||||
required:
|
||||
- field
|
||||
- length
|
||||
|
|
42
validate.py
42
validate.py
|
@ -1,24 +1,46 @@
|
|||
|
||||
import sys
|
||||
from jsonschema import validate
|
||||
import yaml
|
||||
|
||||
import argparse
|
||||
import glob
|
||||
|
||||
|
||||
def main(argv):
|
||||
schemafile = "schema.yml"
|
||||
schema = None
|
||||
parser = argparse.ArgumentParser()
|
||||
parser.add_argument("--all", help="Validate all protocols.", action="store_true")
|
||||
parser.add_argument("-f", help="Validate file.", default=None)
|
||||
parser.add_argument("-s", help="Use schema file.", default="schema.yml")
|
||||
parser.add_argument("-v", help="Verbosity...", action="store_true")
|
||||
args = parser.parse_args()
|
||||
schemafile = args.s
|
||||
schemadata = None
|
||||
with open(schemafile, "r") as f:
|
||||
schemadata = yaml.load(f)
|
||||
data = None
|
||||
with open(argv[1], "r") as f:
|
||||
data = yaml.load(f)
|
||||
# get all yml files from protocols
|
||||
# validate with
|
||||
validate(data, schemadata)
|
||||
if args.all:
|
||||
# search for files and validate them.
|
||||
print("Validating all files in protocols/osi*")
|
||||
list = glob.glob("protocols/osi*/*.yml", recursive=True)
|
||||
for file in list:
|
||||
print("Validating file: " + file)
|
||||
validateFile(file, schemadata, True if args.v else False)
|
||||
pass
|
||||
elif args.f is not None:
|
||||
validateFile(args.f, schemadata, True if args.v else False)
|
||||
pass
|
||||
|
||||
|
||||
def validateFile(file, schema, verbose=False):
|
||||
data = None
|
||||
with open(file, "r") as f:
|
||||
data = yaml.load(f)
|
||||
try:
|
||||
validate(data, schema)
|
||||
print(file + " is valid.")
|
||||
except:
|
||||
print("Failed to validate: " + file)
|
||||
if verbose:
|
||||
raise
|
||||
|
||||
|
||||
if __name__ == '__main__':
|
||||
main(sys.argv)
|
||||
|
|
Reference in New Issue