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:] (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] attribute = self.attrindex[attr_name]
try: try:

View File

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