Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 10 additions & 0 deletions packages/modpoll-wot/profile.md
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,16 @@ requires a new prefix.
`conformance.tm.json`, which covers every byte and word order and each Form extension; with
`conformance.data.json` it is also what the workspace integration tests serve with modsim and read with modpoll.

### Rejection vectors

An implementation that accepts or stores Thing Models without this package, such as a web service, must refuse what
this loader refuses. `tests/fixtures/conformance.rejections.json`, next to the fingerprint vectors,
lists documents that must be refused: each vector is a JSON Patch (RFC 6902, `add`, `replace` and `remove` only)
applied to `conformance.tm.json`, with the name of the rule that refuses the result. The unchanged model must be
accepted. The rule names group the vectors for readers and are not an error code contract; `schema` marks a vector
that one of the bundled JSON Schemas already refuses. Two differences cannot be expressed as vectors, because they
disappear when JSON is parsed outside Python: duplicate object keys, and an integer written as a float such as `1.0`.

## Verification reports

A verification report records one reading of a Thing Model on a physical device. Its schema is
Expand Down
65 changes: 65 additions & 0 deletions packages/modpoll-wot/tests/fixtures/conformance.rejections.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,65 @@
{
"model": "conformance.tm.json",
"vectors": [
{"rule": "profile-version", "patch": [{"op": "replace", "path": "/modpoll:version", "value": "0.1"}]},
{"rule": "document-kind", "patch": [{"op": "remove", "path": "/@type"}]},
{"rule": "placeholder-unbound", "patch": [{"op": "replace", "path": "/description", "value": "Site {{SITE}}"}]},
{"rule": "placeholder-malformed", "patch": [{"op": "replace", "path": "/description", "value": "Site {{SITE"}]},
{"rule": "title-blank", "patch": [{"op": "replace", "path": "/title", "value": " "}]},
{"rule": "security-scheme", "patch": [{"op": "add", "path": "/securityDefinitions", "value": {"basic_sc": {"scheme": "basic", "in": "header"}}}, {"op": "add", "path": "/security", "value": "basic_sc"}]},
{"rule": "root-feature", "patch": [{"op": "add", "path": "/actions", "value": {"reset": {}}}]},
{"rule": "root-feature", "patch": [{"op": "add", "path": "/events", "value": {"alarm": {}}}]},
{"rule": "root-feature", "patch": [{"op": "add", "path": "/links", "value": [{"href": "https://example.com/manual", "rel": "related"}]}]},
{"rule": "root-feature", "patch": [{"op": "add", "path": "/base", "value": "modbus+tcp://device.example/"}]},
{"rule": "root-modbus-field", "patch": [{"op": "add", "path": "/modv:unitID", "value": 1}]},
{"rule": "root-project-extension", "patch": [{"op": "add", "path": "/modpoll:audit", "value": {}}]},
{"rule": "vocabulary-alias", "patch": [{"op": "replace", "path": "/@context/1/modv", "value": "https://example.com/modbus"}]},
{"rule": "property-protocol-metadata", "patch": [{"op": "add", "path": "/properties/u32_be/modv:address", "value": 0}]},
{"rule": "schema", "patch": [{"op": "add", "path": "/properties/u32_be/forms/0/modpoll:scale", "value": 1}]},
{"rule": "href-invalid", "patch": [{"op": "add", "path": "/properties/u32_be/forms/0/href", "value": "http://device.example/1/0?quantity=2"}]},
{"rule": "href-disagrees", "patch": [{"op": "add", "path": "/properties/u32_be/forms/0/href", "value": "modbus+tcp://device.example:502/1/5?quantity=2"}]},
{"rule": "unit-id-inconsistent", "patch": [{"op": "add", "path": "/properties/u32_be/forms/0/modv:unitID", "value": 1}, {"op": "add", "path": "/properties/u32_word_swap/forms/0/modv:unitID", "value": 2}]},
{"rule": "span", "patch": [{"op": "replace", "path": "/properties/u32_be/forms/0/modv:address", "value": 65535}]},
{"rule": "span", "patch": [{"op": "replace", "path": "/properties/label/forms/0/modv:quantity", "value": 126}, {"op": "replace", "path": "/properties/label/forms/0/modpoll:byteLength", "value": 252}]},
{"rule": "codec", "patch": [{"op": "replace", "path": "/properties/half/forms/0/modv:type", "value": "xsd:unsignedShort"}]},
{"rule": "schema", "patch": [{"op": "replace", "path": "/properties/half/forms/0/modpoll:codec", "value": "uint16"}]},
{"rule": "bit-raw-type", "patch": [{"op": "replace", "path": "/properties/flag_bit3/forms/0/modv:type", "value": "xsd:short"}]},
{"rule": "boolean-property", "patch": [{"op": "replace", "path": "/properties/flag_bit3/forms/0/modv:type", "value": "xsd:boolean"}, {"op": "remove", "path": "/properties/flag_bit3/forms/0/modpoll:bit"}, {"op": "replace", "path": "/properties/flag_bit3/type", "value": "integer"}]},
{"rule": "boolean-scalar-quantity", "patch": [{"op": "replace", "path": "/properties/packed_coils/type", "value": "boolean"}, {"op": "replace", "path": "/properties/packed_coils/forms/0/modv:type", "value": "xsd:boolean"}, {"op": "remove", "path": "/properties/packed_coils/forms/0/modpoll:bitPacking"}]},
{"rule": "boolean-multiplier", "patch": [{"op": "replace", "path": "/properties/packed_coils/type", "value": "array"}, {"op": "add", "path": "/properties/packed_coils/items", "value": {"type": "boolean"}}, {"op": "replace", "path": "/properties/packed_coils/forms/0/modv:type", "value": "xsd:boolean"}, {"op": "remove", "path": "/properties/packed_coils/forms/0/modpoll:bitPacking"}, {"op": "add", "path": "/properties/packed_coils/forms/0/modpoll:multiplier", "value": 2}]},
{"rule": "raw-type-missing", "patch": [{"op": "replace", "path": "/properties/half/forms/0/modv:type", "value": "xsd:hexBinary"}, {"op": "remove", "path": "/properties/half/forms/0/modpoll:codec"}]},
{"rule": "schema", "patch": [{"op": "replace", "path": "/properties/label/forms/0/modpoll:byteLength", "value": 0}]},
{"rule": "byte-length-not-string", "patch": [{"op": "add", "path": "/properties/u32_be/forms/0/modpoll:byteLength", "value": 4}]},
{"rule": "bit-packing-missing", "patch": [{"op": "remove", "path": "/properties/packed_coils/forms/0/modpoll:bitPacking"}]},
{"rule": "bit-packing-misplaced", "patch": [{"op": "add", "path": "/properties/u32_be/forms/0/modpoll:bitPacking", "value": "legacy-msb"}]},
{"rule": "quantity-width", "patch": [{"op": "replace", "path": "/properties/u32_be/forms/0/modv:quantity", "value": 1}]},
{"rule": "quantity-width", "patch": [{"op": "replace", "path": "/properties/label/forms/0/modpoll:byteLength", "value": 8}]},
{"rule": "media-type", "patch": [{"op": "replace", "path": "/properties/u32_be/forms/0/contentType", "value": "application/octet-stream;length=2"}]},
{"rule": "media-type", "patch": [{"op": "replace", "path": "/properties/u32_word_swap/forms/0/contentType", "value": "application/octet-stream;byteSeq=BIG_ENDIAN"}]},
{"rule": "media-type", "patch": [{"op": "replace", "path": "/properties/i16_scaled/forms/0/contentType", "value": "application/octet-stream;length=2"}]},
{"rule": "operation", "patch": [{"op": "replace", "path": "/properties/u32_be/forms/0/op", "value": ["writeproperty"]}]},
{"rule": "operation", "patch": [{"op": "add", "path": "/properties/u32_be/writeOnly", "value": true}]},
{"rule": "operation", "patch": [{"op": "add", "path": "/properties/u32_be/forms/1", "value": {"op": ["readproperty"], "contentType": "application/octet-stream", "modv:entity": "HoldingRegister", "modv:address": 0, "modv:quantity": 2, "modv:type": "xsd:unsignedInt"}}]},
{"rule": "forms-disagree", "patch": [{"op": "remove", "path": "/properties/u32_be/readOnly"}, {"op": "add", "path": "/properties/u32_be/forms/1", "value": {"op": ["writeproperty"], "contentType": "application/octet-stream", "modv:entity": "HoldingRegister", "modv:address": 2, "modv:quantity": 2, "modv:type": "xsd:unsignedInt"}}]},
{"rule": "forms-cover-access", "patch": [{"op": "remove", "path": "/properties/u32_be/readOnly"}]},
{"rule": "read-only-table", "patch": [{"op": "replace", "path": "/properties/packed_coils/forms/0/modv:entity", "value": "DiscreteInput"}, {"op": "remove", "path": "/properties/packed_coils/readOnly"}, {"op": "replace", "path": "/properties/packed_coils/forms/0/op", "value": ["readproperty", "writeproperty"]}]},
{"rule": "schema", "patch": [{"op": "replace", "path": "/properties/i16_scaled/forms/0/modpoll:multiplier", "value": "NaN"}]},
{"rule": "multiplier", "patch": [{"op": "replace", "path": "/properties/i16_scaled/forms/0/modpoll:multiplier", "value": "1e9999"}]},
{"rule": "schema", "patch": [{"op": "replace", "path": "/properties/i16_scaled/forms/0/modpoll:multiplier", "value": true}]},
{"rule": "multiplier", "patch": [{"op": "add", "path": "/properties/label/forms/0/modpoll:multiplier", "value": "2"}]},
{"rule": "multiplier", "patch": [{"op": "add", "path": "/properties/flag_bit3/forms/0/modpoll:multiplier", "value": "2"}]},
{"rule": "property-type", "patch": [{"op": "replace", "path": "/properties/i16_scaled/type", "value": "integer"}]},
{"rule": "property-type", "patch": [{"op": "replace", "path": "/properties/label/type", "value": "integer"}]},
{"rule": "property-type", "patch": [{"op": "replace", "path": "/properties/flag_bit3/type", "value": "integer"}]},
{"rule": "property-type", "patch": [{"op": "replace", "path": "/properties/half/type", "value": "integer"}]},
{"rule": "property-type", "patch": [{"op": "replace", "path": "/properties/u32_be/type", "value": "string"}]},
{"rule": "group-span", "patch": [{"op": "replace", "path": "/modpoll:acquisition/0/modv:quantity", "value": 126}]},
{"rule": "group-span", "patch": [{"op": "replace", "path": "/modpoll:acquisition/0/modv:address", "value": 65530}]},
{"rule": "group-member", "patch": [{"op": "replace", "path": "/modpoll:acquisition/0/modpoll:members/0", "value": "missing"}]},
{"rule": "group-member", "patch": [{"op": "add", "path": "/modpoll:acquisition/1/modpoll:members/1", "value": "u32_be"}]},
{"rule": "group-member-span", "patch": [{"op": "replace", "path": "/modpoll:acquisition/0/modv:address", "value": 1}]},
{"rule": "group-member-span", "patch": [{"op": "replace", "path": "/modpoll:acquisition/0/modv:quantity", "value": 15}]},
{"rule": "group-member-span", "patch": [{"op": "replace", "path": "/modpoll:acquisition/1/modv:entity", "value": "DiscreteInput"}]},
{"rule": "property-ungrouped", "patch": [{"op": "remove", "path": "/modpoll:acquisition/0/modpoll:members/0"}]}
]
}
110 changes: 110 additions & 0 deletions packages/modpoll-wot/tests/test_rejections.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,110 @@
"""Rejection vectors: documents another implementation must refuse as well."""

from copy import deepcopy
import json
from pathlib import Path

import pytest

from modpoll_wot import AcquisitionError
from modpoll_wot.profile import instantiate
from modpoll_wot.validation import ModelValidationError

FIXTURES = Path(__file__).parent / "fixtures"
VECTORS = json.loads((FIXTURES / "conformance.rejections.json").read_bytes())
MODEL = json.loads((FIXTURES / VECTORS["model"]).read_bytes())
# What the loader answers for each rule. A vector that is refused for another reason than its
# own rule would stop testing that rule, so the answer is part of the check.
ANSWERS = {
"profile-version": "unsupported executable profile",
"document-kind": "'href' is a required property",
"placeholder-unbound": "unbound TM placeholder",
"placeholder-malformed": "malformed TM placeholder",
"title-blank": "title cannot be empty",
"security-scheme": "supports only nosec",
"root-feature": "unsupported executable profile feature",
"root-modbus-field": "root Modbus fields are unsupported",
"root-project-extension": "unsupported root project extension",
"vocabulary-alias": "unsupported fixed vocabulary aliases",
"property-protocol-metadata": "protocol metadata belongs in Forms",
"href-invalid": "invalid concrete Modbus TCP href",
"href-disagrees": "href disagrees with",
"unit-id-inconsistent": "inconsistent Unit IDs",
"span": "invalid Modbus span",
"codec": "unsupported or redundant codec",
"bit-raw-type": "bit extraction requires a register unsignedShort",
"boolean-property": "invalid boolean property",
"boolean-scalar-quantity": "scalar boolean requires quantity 1",
"boolean-multiplier": "booleans cannot be multiplied",
"raw-type-missing": "unsupported or missing raw type",
"byte-length-not-string": "byteLength only applies to strings",
"bit-packing-missing": "requires explicit legacy-msb packing",
"bit-packing-misplaced": "bitPacking is only for",
"quantity-width": "quantity disagrees with raw codec width",
"media-type": "binary media parameter",
"operation": "ambiguous or invalid operation",
"forms-disagree": "read/write Forms disagree",
"forms-cover-access": "Forms must cover property access",
"read-only-table": "read-only table requires readOnly",
"multiplier": "multiplier",
"property-type": "property type disagrees",
"group-span": "invalid group span",
"group-member": "unknown or duplicate member",
"group-member-span": "member outside group or wrong entity",
"property-ungrouped": "exactly one acquisition group",
}


def patched(document, patch):
"""Apply the add, replace and remove operations of a JSON Patch (RFC 6902)."""
document = deepcopy(document)
for operation in patch:
*parents, last = [
token.replace("~1", "/").replace("~0", "~")
for token in operation["path"].split("/")[1:]
]
node = document
for token in parents:
node = node[int(token)] if isinstance(node, list) else node[token]
if isinstance(node, list):
index = int(last)
if operation["op"] == "add":
node.insert(index, operation["value"])
elif operation["op"] == "replace":
node[index] = operation["value"]
else:
del node[index]
elif operation["op"] == "remove":
del node[last]
else:
assert (last in node) == (operation["op"] == "replace"), operation
node[last] = operation["value"]
return document


def test_the_unchanged_model_is_accepted():
instantiate(deepcopy(MODEL))
instantiate(deepcopy(MODEL), host="device.example")


# A TCP host binds more placeholders but must never turn a refusal into an acceptance.
@pytest.mark.parametrize(
"instance", [{}, {"host": "device.example"}], ids=["unbound", "tcp"]
)
@pytest.mark.parametrize(
"vector",
VECTORS["vectors"],
ids=[f"{i}-{vector['rule']}" for i, vector in enumerate(VECTORS["vectors"])],
)
def test_each_vector_is_rejected_by_its_rule(vector, instance):
document = patched(MODEL, vector["patch"])
with pytest.raises(AcquisitionError) as refusal:
instantiate(document, **instance)
if vector["rule"] == "schema":
assert isinstance(refusal.value, ModelValidationError)
else:
assert ANSWERS[vector["rule"]] in str(refusal.value)


def test_every_rule_has_a_vector():
assert {vector["rule"] for vector in VECTORS["vectors"]} == {*ANSWERS, "schema"}
Loading