Fix: error on invalid value numbers

This commit is contained in:
Istvan Ruzman
2020-08-07 16:39:17 +02:00
parent 9d7d40806a
commit dc2a24402b
2 changed files with 107 additions and 62 deletions

View File

@@ -460,7 +460,15 @@ class Dictionary:
)
(attr_name, key, vvalue) = tokens[1:]
value = _parse_number(vvalue)
try:
if '.' in vvalue:
# quick and dirty way to make floats values an error
raise ValueError
value = _parse_number(vvalue)
except ValueError:
raise ParseError(filename,
f"Invalid number {vvalue} for VALUE {key}",
line_num)
attribute = self.attrindex[attr_name]
try:

View File

@@ -86,15 +86,11 @@ def test_valid_attribute_numbers(number):
@pytest.mark.parametrize(
"number",
[
"ATTRIBUTE NAME 1234 byte",
"ATTRIBUTE NAME ABCD byte",
"ATTRIBUTE NAME -1 byte",
],
"invalid_number",
["ABCD", "-1", "inf", "INF", "-INF", "2e4", "2.5e3"],
)
def test_invalid_attribute_numbers(number):
dictionary = StringIO(number)
def test_invalid_attribute_numbers(invalid_number):
dictionary = StringIO(f"ATTRIBUTE NAME {invalid_number} integer64")
with pytest.raises(ParseError):
Dictionary("", dictionary)
@@ -120,17 +116,11 @@ def test_attribute_number_limits(type_length):
Dictionary("", dictionary)
@pytest.mark.parametrize(
"value_definition",
[
"VALUE TEST-ATTRIBUTE TEST-VALUE 1",
"VALUE TEST-ATTRIBUTE TEST-VALUE 0x1",
"VALUE TEST-ATTRIBUTE TEST-VALUE 0o1",
],
)
def test_value_definition(value_definition):
@pytest.mark.parametrize("value", ["1", "0x1", "0o1"])
def test_value_definition(value):
dictionary = StringIO(
"\n".join(["ATTRIBUTE TEST-ATTRIBUTE 1 byte", value_definition])
"ATTRIBUTE TEST-ATTRIBUTE 1 byte\n"
f"VALUE TEST-ATTRIBUTE TEST-VALUE {value}"
)
Dictionary("", dictionary)
@@ -152,14 +142,17 @@ def test_value_definition(value_definition):
)
def test_value_number_within_limit(value_num, attr_type):
dictionary = StringIO(
"\n".join(
[
f"ATTRIBUTE TEST-ATTRIBUTE 1 {attr_type}",
f"VALUE TEST-ATTRIBUTE TEST-VALUE {value_num}",
]
)
f"ATTRIBUTE TEST-ATTRIBUTE 1 {attr_type}\n"
f"VALUE TEST-ATTRIBUTE TEST-VALUE {value_num}"
)
Dictionary("", dictionary)
dictionary = StringIO(
"VENDOR TEST-VENDOR 1234\n"
"BEGIN-VENDOR TEST-VENDOR\n"
f"ATTRIBUTE TEST-ATTRIBUTE 1 {attr_type}\n"
f"VALUE TEST-ATTRIBUTE TEST-VALUE {value_num}\n"
"END-VENDOR TEST-VEDNOR"
)
@pytest.mark.parametrize(
@@ -179,60 +172,104 @@ def test_value_number_within_limit(value_num, attr_type):
)
def test_value_number_out_of_limit(value_num, attr_type):
dictionary = StringIO(
"\n".join(
[
f"ATTRIBUTE TEST-ATTRIBUTE 1 {attr_type}",
f"VALUE TEST-ATTRIBUTE TEST-VALUE {value_num}",
]
)
f"ATTRIBUTE TEST-ATTRIBUTE 1 {attr_type}\n"
f"VALUE TEST-ATTRIBUTE TEST-VALUE {value_num}"
)
with pytest.raises(ParseError):
Dictionary("", dictionary)
dictionary = StringIO(
"VENDOR TEST-VENDOR 1234\n"
"BEGIN-VENDOR TEST-VENDOR\n"
f"ATTRIBUTE TEST-ATTRIBUTE 1 {attr_type}\n"
f"VALUE TEST-ATTRIBUTE TEST-VALUE {value_num}\n"
"END-VENDOR TEST-VEDNOR"
)
with pytest.raises(ParseError):
Dictionary("", dictionary)
@pytest.mark.parametrize("datatype", [
"string", "octets", "abinary", "byte", "short",
"integer", "signed", "integer64", "ipaddr",
"ipv4prefix", "ipv6addr", "ipv6prefix", "combo-ip",
"ifid", "ether", "concat", "tlv", "extended",
"long-extended", "evs",
])
@pytest.mark.parametrize(
"datatype",
[
"string",
"octets",
"abinary",
"byte",
"short",
"integer",
"signed",
"integer64",
"ipaddr",
"ipv4prefix",
"ipv6addr",
"ipv6prefix",
"combo-ip",
"ifid",
"ether",
"concat",
"tlv",
"extended",
"long-extended",
"evs",
],
)
def test_all_datatypes_rfc_space(datatype):
dictionary = StringIO(
f"ATTRIBUTE TEST-ATTRIBUTE 1 {datatype}\n"
)
dictionary = StringIO(f"ATTRIBUTE TEST-ATTRIBUTE 1 {datatype}\n")
Dictionary("", dictionary)
@pytest.mark.parametrize("datatype", [
"string", "octets", "abinary", "byte", "short",
"integer", "signed", "integer64", "ipaddr",
"ipv4prefix", "ipv6addr", "ipv6prefix", "combo-ip",
"ifid", "ether", "tlv",
])
@pytest.mark.parametrize(
"datatype",
[
"string",
"octets",
"abinary",
"byte",
"short",
"integer",
"signed",
"integer64",
"ipaddr",
"ipv4prefix",
"ipv6addr",
"ipv6prefix",
"combo-ip",
"ifid",
"ether",
"tlv",
],
)
def test_valid_datatypes_in_vendor_space(datatype):
dictionary = StringIO(
"VENDOR TEST 1234\n"
"BEGIN-VENDOR TEST\n"
f"ATTRIBUTE TEST-ATTRIBUTE 1 {datatype}\n"
"END-VENDOR TEST\n"
)
"VENDOR TEST 1234\n"
"BEGIN-VENDOR TEST\n"
f"ATTRIBUTE TEST-ATTRIBUTE 1 {datatype}\n"
"END-VENDOR TEST\n"
)
Dictionary("", dictionary)
@pytest.mark.parametrize("datatype", [
"concat", "extended", "long-extended", "evs",
])
@pytest.mark.parametrize(
"datatype", ["concat", "extended", "long-extended", "evs",]
)
def test_invalid_datatypes_in_vendor_space(datatype):
dictionary = StringIO(
"VENDOR TEST 1234\n"
"BEGIN-VENDOR TEST\n"
f"ATTRIBUTE TEST-ATTRIBUTE 1 {datatype}\n"
"END-VENDOR TEST\n"
)
"VENDOR TEST 1234\n"
"BEGIN-VENDOR TEST\n"
f"ATTRIBUTE TEST-ATTRIBUTE 1 {datatype}\n"
"END-VENDOR TEST\n"
)
with pytest.raises(ParseError):
Dictionary("", dictionary)
@pytest.mark.parametrize(
"invalid_number",
["ABCD", "-1", "inf", "INF", "-INF", "0.1", "2e4", "2.5e3"],
)
def test_invalid_attribute_numbers(invalid_number):
dictionary = StringIO(
f"ATTRIBUTE TEST-ATTRIBUTE 1 integer\n"
f"VALUE TEST-ATTRIBUTE TEST-VALUE {invalid_number}")
with pytest.raises(ParseError):
Dictionary("", dictionary)