Skip to content

Commit

Permalink
Register fixed lengths for octet strings
Browse files Browse the repository at this point in the history
For the purpose of generating and parsing formatted OIDs, pysnmp needs
to know whether an OCTET STRING typed object has a fixed length, and if
so, which length. Before the switch to Jinja2 templates, pysmi assigned
such fixed lengths as "fixedLength" attributes to types. With the
switch, that functionality got lost, thereby also breaking pysnmp's
proper handling of formatted OIDs.

This commit restores the assignment of "fixedLength" attributes. Note
that as of writing, this change is necessary but not sufficient to fix
the corresponding funtionality in pysnmp.

The test set is extended with extra tests for this functionality, and
some previously missing general tests on constraints. As part of that, a
bug is fixed in an existing test wherein naming conflicts caused
various bulk-generated test methods to overwrite each other.
  • Loading branch information
dcvmoole committed Nov 2, 2024
1 parent e470a8f commit 99eae42
Show file tree
Hide file tree
Showing 3 changed files with 397 additions and 30 deletions.
17 changes: 16 additions & 1 deletion pysmi/codegen/intermediate.py
Original file line number Diff line number Diff line change
Expand Up @@ -880,7 +880,22 @@ def gen_octetstring_subtype(self, data):
size["max"] = vmax
sizes.append(size)

return {"size": sizes}
outDict = {"size": sizes}

# If the union of ranges consists of a single size, then store that
# information as well, so that pysnmp (which needs to know) does not
# have to compute it itself. We take a slightly elaborate approach so
# that strange ranges such as SIZE(4 | 4) also produce a fixed size.
#
# Note that as per RFC 2578 Sec. 9 point (3), derived types may only
# ever reduce the ranges of their base types, so we never need to
# override a base type's fixed length to be "non-fixed" again.
minSize = min(size["min"] for size in sizes)
maxSize = max(size["max"] for size in sizes)
if minSize == maxSize:
outDict["fixed"] = minSize

return outDict

# noinspection PyUnusedLocal
def gen_oid(self, data):
Expand Down
3 changes: 3 additions & 0 deletions pysmi/codegen/templates/pysnmp/mib-definitions.j2
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ if mibBuilder.loadTexts:
ValueSizeConstraint({{ range['min'] }}, {{ range['max'] }}),
{% endfor %}
)
{% if 'fixed' in spec %}
fixedLength = {{ spec['fixed'] }}
{% endif %}
{% endif %}
{% endmacro -%}

Expand Down
Loading

0 comments on commit 99eae42

Please sign in to comment.