Ejemplo n.º 1
0
 def test_range_size(self) -> None:
     self.assertEqual(RangeInteger('UINT32',
                                   Number(0),
                                   Sub(Pow(Number(2), Number(32)), Number(1)),
                                   Number(32)
                                   ).size,
                      Number(32))
Ejemplo n.º 2
0
def parse_type(string: str, location: int, tokens: ParseResults) -> Type:
    try:
        name = tokens[1]
        full_name = f"__PACKAGE__.{name}"
        if tokens[3] == "mod":
            return ModularInteger(full_name, *tokens[4:6])
        if tokens[3] == "range":
            tokens[6] = tokens[6]["size"]
            return RangeInteger(full_name, *tokens[4:7])
        if tokens[3] == "message":
            return MessageSpec(full_name, tokens[4])
        if tokens[3] == "null message":
            return MessageSpec(full_name, [])
        if tokens[3] == "(":
            elements = dict(tokens[4:-2])
            aspects = tokens[-1]
            if len(elements) < len(tokens[4:-2]):
                raise ModelError(f'"{name}" contains duplicate elements')
            if "always_valid" not in aspects:
                aspects["always_valid"] = False
            return Enumeration(full_name, elements, aspects["size"],
                               aspects["always_valid"])
        if tokens[3] == "new":
            return DerivationSpec(full_name, tokens[4])
        if tokens[3] == "array of":
            return Array(
                full_name,
                Reference(tokens[4] if "." in
                          tokens[4] else f"__PACKAGE__.{tokens[4]}"))
    except ModelError as e:
        raise ParseFatalException(string, location, e)
    raise ParseFatalException(string, location, "unexpected type")
Ejemplo n.º 3
0
def test_range_invalid_size_exceeds_limit() -> None:
    # ISSUE: Componolit/RecordFlux#238
    assert_type_error(
        RangeInteger("P.T", Number(0), Number(256), Number(128),
                     Location((50, 3))),
        r'^<stdin>:50:3: model: error: size of "T" exceeds limit \(2\*\*64\)$',
    )
Ejemplo n.º 4
0
def test_integer_type_spec() -> None:
    spec = {
        "Integer_Type": Specification(
            ContextSpec([]),
            PackageSpec(
                "Integer_Type",
                [
                    RangeInteger("__PACKAGE__.Page_Num", Number(1), Number(2000), Number(16)),
                    RangeInteger("__PACKAGE__.Line_Size", Number(0), Number(255), Number(8)),
                    ModularInteger("__PACKAGE__.Byte", Number(256)),
                    ModularInteger("__PACKAGE__.Hash_Index", Number(64)),
                ],
            ),
        )
    }
    assert_specifications_files([f"{TESTDIR}/integer_type.rflx"], spec)
Ejemplo n.º 5
0
def parse_type(string: str, location: int, tokens: list) -> Type:
    try:
        if tokens[3] == 'mod':
            return ModularInteger(tokens[1], *tokens[4:6])
        if tokens[3] == 'range':
            tokens[6] = tokens[6]['size']
            return RangeInteger(tokens[1], *tokens[4:7])
        if tokens[3] == 'message':
            return Message(tokens[1], tokens[4])
        if tokens[3] == '(':
            elements = dict(tokens[4:-2])
            aspects = tokens[-1]
            if len(elements) < len(tokens[4:-2]):
                raise ModelError(f'"{tokens[1]}" contains duplicate elements')
            if 'always_valid' not in aspects:
                aspects['always_valid'] = False
            return Enumeration(tokens[1], elements, aspects['size'],
                               aspects['always_valid'])
        if tokens[3] == 'new':
            if len(tokens) == 7:
                tokens.append(TRUE)
            return Refinement(tokens[1], *tokens[4:])
        if tokens[3] == 'array of':
            return Array(tokens[1], tokens[4])
    except ModelError as e:
        raise ParseFatalException(string, location, e)
    raise ParseFatalException(string, location, 'unexpected type')
Ejemplo n.º 6
0
def test_range_size() -> None:
    assert_equal(
        RangeInteger("P.T", Number(0),
                     Sub(Pow(Number(2), Number(32)), Number(1)),
                     Number(32)).size,
        Number(32),
    )
Ejemplo n.º 7
0
def create_array_message() -> Message:
    length_type = ModularInteger("Arrays.Length", Pow(Number(2), Number(8)))

    modular_type = ModularInteger("Arrays.Modular_Integer",
                                  Pow(Number(2), Number(16)))
    modular_vector_type = Array("Arrays.Modular_Vector", modular_type)

    range_type = RangeInteger("Arrays.Range_Integer", Number(1), Number(100),
                              Number(8))
    range_vector_type = Array("Arrays.Range_Vector", range_type)

    enum_type = Enumeration(
        "Arrays.Enumeration",
        {
            "ZERO": Number(0),
            "ONE": Number(1),
            "TWO": Number(2)
        },
        Number(8),
        False,
    )
    enum_vector_type = Array("Arrays.Enumeration_Vector", enum_type)

    av_enum_type = Enumeration(
        "Arrays.AV_Enumeration",
        {
            "AV_ZERO": Number(0),
            "AV_ONE": Number(1),
            "AV_TWO": Number(2)
        },
        Number(8),
        True,
    )
    av_enum_vector_type = Array("Arrays.AV_Enumeration_Vector", av_enum_type)

    structure = [
        Link(INITIAL, Field("Length")),
        Link(Field("Length"),
             Field("Modular_Vector"),
             length=Mul(Variable("Length"), Number(8))),
        Link(Field("Modular_Vector"), Field("Range_Vector"),
             length=Number(16)),
        Link(Field("Range_Vector"),
             Field("Enumeration_Vector"),
             length=Number(16)),
        Link(Field("Enumeration_Vector"),
             Field("AV_Enumeration_Vector"),
             length=Number(16)),
        Link(Field("AV_Enumeration_Vector"), FINAL),
    ]

    types = {
        Field("Length"): length_type,
        Field("Modular_Vector"): modular_vector_type,
        Field("Range_Vector"): range_vector_type,
        Field("Enumeration_Vector"): enum_vector_type,
        Field("AV_Enumeration_Vector"): av_enum_vector_type,
    }

    return Message("Arrays.Message", structure, types)
Ejemplo n.º 8
0
def range_integers(
    draw: Draw,
    unique_identifiers: ty.Generator[ID, None, None],
    multiple_of_8: bool = False,
    align_to_8: int = 0,
) -> RangeInteger:
    size = draw(sizes(multiple_of_8, align_to_8))
    max_value = min(2**size - 1, 2**63 - 1)
    first = draw(st.integers(min_value=0, max_value=max_value))
    last = draw(st.integers(min_value=first, max_value=max_value))
    return RangeInteger(
        next(unique_identifiers), expr.Number(first), expr.Number(last), expr.Number(size)
    )
Ejemplo n.º 9
0
def test_name_conflict_types() -> None:
    assert_model_error(
        [
            ModularInteger(ID("P::T"), Number(256), location=Location((10, 20))),
            RangeInteger(
                ID("P::T"), Number(1), Number(100), Number(8), location=Location((11, 30))
            ),
        ],
        r"^"
        r'<stdin>:11:30: model: error: name conflict for type "P::T"\n'
        r'<stdin>:10:20: model: info: previous occurrence of "P::T"'
        r"$",
    )
Ejemplo n.º 10
0
def test_value_range() -> None:
    # pylint: disable=pointless-statement
    rangetype = RangeInteger("Test.Int", Number(8), Number(16), Number(8))
    rangevalue = IntegerValue(rangetype)
    assert not rangevalue.initialized
    with pytest.raises(NotInitializedError, match="value not initialized"):
        rangevalue.value
    with pytest.raises(NotInitializedError, match="value not initialized"):
        rangevalue.expr
    rangevalue.assign(10)
    assert rangevalue.initialized
    assert rangevalue.value == 10
    assert str(rangevalue.bitstring) == "00001010"
    with pytest.raises(ValueError, match=r"value 17 not in type range 8 .. 16"):
        rangevalue.assign(17)
    with pytest.raises(ValueError, match=r"value 7 not in type range 8 .. 16"):
        rangevalue.assign(7)
Ejemplo n.º 11
0
def test_aggregate_equal_invalid_length_field() -> None:

    length = Field(ID("Length", Location((2, 5))))
    magic = Field(ID("Magic", Location((3, 5))))

    structure = [
        Link(INITIAL, length),
        Link(length,
             magic,
             length=Mul(Number(8),
                        Variable("Length"),
                        location=Location((6, 5)))),
        Link(
            magic,
            FINAL,
            condition=Equal(Variable("Magic"),
                            Aggregate(Number(1), Number(2)),
                            location=Location((10, 5))),
        ),
    ]
    types = {
        Field("Length"):
        RangeInteger("P.Length_Type", Number(10), Number(100), Number(8),
                     Location((5, 10))),
        Field(ID("Magic", Location((17, 3)))):
        Opaque(),
    }
    assert_message_model_error(
        structure,
        types,
        r"^"
        r'<stdin>:10:5: model: error: contradicting condition in "P.M"\n'
        r'<stdin>:2:5: model: info: on path: "Length"\n'
        r'<stdin>:3:5: model: info: on path: "Magic"\n'
        r'<stdin>:6:5: model: info: unsatisfied "Magic\'Length = 8 [*] Length"\n'
        r'<stdin>:10:5: model: info: unsatisfied "2 [*] 8 = Magic\'Length"\n'
        r'<stdin>:5:10: model: info: unsatisfied "Length >= 10"'
        r"$",
    )
Ejemplo n.º 12
0
def test_value_equal(enum_value: EnumValue) -> None:
    # pylint: disable=comparison-with-itself
    ov = OpaqueValue(Opaque())
    ev = enum_value
    rangetype = RangeInteger("Test.Int", Number(8), Number(16), Number(8))
    rv = IntegerValue(rangetype)
    modtype = ModularInteger("Test.Int", Number(2 ** 16))
    mv = IntegerValue(modtype)
    mv2 = IntegerValue(modtype)
    assert ov == ov
    assert ev == ev
    assert rv == rv
    assert mv == mv
    assert ev != rv
    assert mv == mv2
    mv.assign(2)
    assert mv != mv2
    mv2.assign(10)
    assert mv != mv2
    mv.assign(10)
    assert mv == mv2
    rv.assign(10)
    assert mv != rv
Ejemplo n.º 13
0
def create_ethernet_pdu() -> PDU:
    uint48 = ModularInteger('UINT48', Pow(Number(2), Number(48)))
    uint16 = RangeInteger('UINT16',
                          Number(0),
                          Sub(Pow(Number(2), Number(16)), Number(1)),
                          Number(16))
    payload_array = Array('Payload_Array')

    initial = InitialNode()
    destination = Node('Destination', uint48)
    source = Node('Source', uint48)
    tpid = Node('TPID', uint16)
    tci = Node('TCI', uint16)
    ether_type = Node('EtherType', uint16)
    payload = Node('Payload', payload_array)

    initial.edges = [Edge(destination)]
    destination.edges = [Edge(source)]
    source.edges = [Edge(tpid)]
    tpid.edges = [Edge(tci,
                       Equal(Value('TPID'), Number(0x8100))),
                  Edge(ether_type,
                       NotEqual(Value('TPID'), Number(0x8100)),
                       first=First('TPID'))]
    tci.edges = [Edge(ether_type)]
    ether_type.edges = [Edge(payload,
                             LessEqual(Value('EtherType'), Number(1500)),
                             Mul(LengthValue('EtherType'), Number(8))),
                        Edge(payload,
                             GreaterEqual(Value('EtherType'), Number(1536)),
                             Sub(Last('Message'), Last('EtherType')))]
    payload.edges = [Edge(FINAL,
                          And(GreaterEqual(Div(Length('Payload'), Number(8)), Number(46)),
                              LessEqual(Div(Length('Payload'), Number(8)), Number(1500))))]

    return PDU('Ethernet.Frame', initial)
Ejemplo n.º 14
0
def parse_type(string: str, location: int, tokens: ParseResults) -> Type:
    package = ID("__PACKAGE__")
    name = tokens[1]

    locn = parser_location(tokens[0], tokens[-1], string)

    identifier = package * name

    if tokens[3] == "mod":
        return ModularInteger(identifier, tokens[4], locn)
    if tokens[3] == "range":
        tokens[6] = tokens[6]["size"]
        return RangeInteger(identifier, tokens[4], tokens[5], tokens[6], locn)
    if tokens[3] == "message":
        return MessageSpec(identifier, tokens[4], locn)
    if tokens[3] == "null message":
        return MessageSpec(identifier, [], locn)
    if tokens[3] == "(":
        elements = tokens[4:-3]
        aspects = tokens[-2]
        if "always_valid" not in aspects:
            aspects["always_valid"] = False
        enumeration = Enumeration(identifier, elements, aspects["size"],
                                  aspects["always_valid"], locn)
        return enumeration
    if tokens[3] == "new":
        return DerivationSpec(identifier, tokens[4], locn)
    if tokens[3] == "array of":
        return ArraySpec(
            identifier,
            ReferenceSpec(qualified_type_name(tokens[4], package),
                          tokens[4].location),
            locn,
        )

    raise ParseFatalException(string, location, "unexpected type")
Ejemplo n.º 15
0
def test_range_invalid_last_exceeds_limit() -> None:
    assert_type_error(
        RangeInteger("P.T", Number(1), Pow(Number(2), Number(63)), Number(64)),
        r'^model: error: last of "T" exceeds limit \(2\*\*63 - 1\)$',
    )
Ejemplo n.º 16
0
def test_range_invalid_first_variable() -> None:
    assert_type_error(
        RangeInteger("P.T", Add(Number(1), Variable("X")), Number(15),
                     Number(4), Location((5, 3))),
        r'^<stdin>:5:3: model: error: first of "T" contains variable$',
    )
Ejemplo n.º 17
0
def test_range_invalid_last_variable() -> None:
    assert_type_error(
        RangeInteger("P.T", Number(1), Add(Number(1), Variable("X")),
                     Number(4), Location((80, 6))),
        r'^<stdin>:80:6: model: error: last of "T" contains variable$',
    )
Ejemplo n.º 18
0
def create_ethernet_frame() -> Message:
    address_type = ModularInteger("Ethernet.Address",
                                  Pow(Number(2), Number(48)))
    type_length_type = RangeInteger("Ethernet.Type_Length", Number(46),
                                    Sub(Pow(Number(2), Number(16)), Number(1)),
                                    Number(16))
    tpid_type = RangeInteger("Ethernet.TPID", Number(0x8100, 16),
                             Number(0x8100, 16), Number(16))
    tci_type = ModularInteger("Ethernet.TCI", Pow(Number(2), Number(16)))

    structure = [
        Link(INITIAL, Field("Destination")),
        Link(Field("Destination"), Field("Source")),
        Link(Field("Source"), Field("Type_Length_TPID")),
        Link(
            Field("Type_Length_TPID"),
            Field("TPID"),
            Equal(Variable("Type_Length_TPID"), Number(0x8100, 16)),
            first=First("Type_Length_TPID"),
        ),
        Link(
            Field("Type_Length_TPID"),
            Field("Type_Length"),
            NotEqual(Variable("Type_Length_TPID"), Number(0x8100, 16)),
            first=First("Type_Length_TPID"),
        ),
        Link(Field("TPID"), Field("TCI")),
        Link(Field("TCI"), Field("Type_Length")),
        Link(
            Field("Type_Length"),
            Field("Payload"),
            LessEqual(Variable("Type_Length"), Number(1500)),
            Mul(Variable("Type_Length"), Number(8)),
        ),
        Link(
            Field("Type_Length"),
            Field("Payload"),
            GreaterEqual(Variable("Type_Length"), Number(1536)),
            Sub(Last("Message"), Last("Type_Length")),
        ),
        Link(
            Field("Payload"),
            FINAL,
            And(
                GreaterEqual(Div(Length("Payload"), Number(8)), Number(46)),
                LessEqual(Div(Length("Payload"), Number(8)), Number(1500)),
            ),
        ),
    ]

    types = {
        Field("Destination"): address_type,
        Field("Source"): address_type,
        Field("Type_Length_TPID"): type_length_type,
        Field("TPID"): tpid_type,
        Field("TCI"): tci_type,
        Field("Type_Length"): type_length_type,
        Field("Payload"): Payload(),
    }

    return Message("Ethernet.Frame", structure, types)
Ejemplo n.º 19
0
 def test_range_invalid_first_negative(self) -> None:
     with self.assertRaises(ModelError):
         RangeInteger("P.T", Number(-1), Number(0), Number(1))
Ejemplo n.º 20
0
    def test_pdu_fields_ethernet(self) -> None:
        expected = OrderedDict([
            ('Destination',
             Field('Destination',
                   ModularInteger('UINT48', Pow(Number(2), Number(48))),
                   TRUE,
                   {
                       '0':
                       Variant(
                           [],
                           TRUE,
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47)
                           })
                   })),
            ('Source',
             Field('Source',
                   ModularInteger('UINT48', Pow(Number(2), Number(48))),
                   TRUE,
                   {
                       '00':
                       Variant(
                           [
                               ('Destination', '0')
                           ],
                           TRUE,
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95)
                           })
                   })),
            ('TPID',
             Field('TPID',
                   RangeInteger('UINT16',
                                Number(0),
                                Sub(Pow(Number(2), Number(16)), Number(1)),
                                Number(16)),
                   Or(NotEqual(Value('TPID'), Number(0x8100)),
                      Equal(Value('TPID'), Number(0x8100))),
                   {
                       '000':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00')
                           ],
                           TRUE,
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111)
                           })
                   })),
            ('TCI',
             Field('TCI',
                   RangeInteger('UINT16',
                                Number(0),
                                Sub(Pow(Number(2), Number(16)), Number(1)),
                                Number(16)),
                   TRUE,
                   {
                       '0000':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000')
                           ],
                           Equal(Value('TPID'), Number(0x8100)),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('TCI'): Number(16),
                               First('TCI'): Number(112),
                               Last('TCI'): Number(127)
                           })
                   })),
            ('EtherType',
             Field('EtherType',
                   RangeInteger('UINT16',
                                Number(0),
                                Sub(Pow(Number(2), Number(16)), Number(1)),
                                Number(16)),
                   Or(GreaterEqual(Value('EtherType'), Number(1536)),
                      LessEqual(Value('EtherType'), Number(1500))),
                   {
                       '00000':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000'),
                               ('TCI', '0000')
                           ],
                           TRUE,
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('TCI'): Number(16),
                               First('TCI'): Number(112),
                               Last('TCI'): Number(127),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(128),
                               Last('EtherType'): Number(143)
                           }),
                       '0001':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000')
                           ],
                           NotEqual(Value('TPID'), Number(0x8100)),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(96),
                               Last('EtherType'): Number(111)
                           })
                   })),
            ('Payload',
             Field('Payload',
                   Array('Payload_Array'),
                   And(GreaterEqual(Div(Length('Payload'), Number(8)), Number(46)),
                       LessEqual(Div(Length('Payload'), Number(8)), Number(1500))),
                   {
                       '000000':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000'),
                               ('TCI', '0000'),
                               ('EtherType', '00000')
                           ],
                           LessEqual(Value('EtherType'), Number(1500)),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('TCI'): Number(16),
                               First('TCI'): Number(112),
                               Last('TCI'): Number(127),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(128),
                               Last('EtherType'): Number(143),
                               Length('Payload'): Mul(LengthValue('EtherType'), Number(8)),
                               First('Payload'): Number(144),
                               Last('Payload'): Add(Mul(LengthValue('EtherType'), Number(8)),
                                                    Number(143))
                           }),
                       '000001':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000'),
                               ('TCI', '0000'),
                               ('EtherType', '00000')
                           ],
                           GreaterEqual(Value('EtherType'), Number(1536)),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('TCI'): Number(16),
                               First('TCI'): Number(112),
                               Last('TCI'): Number(127),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(128),
                               Last('EtherType'): Number(143),
                               Length('Payload'): Add(Last('Message'), Number(-143)),
                               First('Payload'): Number(144),
                               Last('Payload'): Last('Message')
                           }),
                       '00010':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000'),
                               ('EtherType', '0001')
                           ],
                           LessEqual(Value('EtherType'), Number(1500)),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(96),
                               Last('EtherType'): Number(111),
                               Length('Payload'): Mul(LengthValue('EtherType'), Number(8)),
                               First('Payload'): Number(112),
                               Last('Payload'): Add(Mul(LengthValue('EtherType'), Number(8)),
                                                    Number(111))
                           }),
                       '00011':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000'),
                               ('EtherType', '0001')
                           ],
                           GreaterEqual(Value('EtherType'), Number(1536)),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(96),
                               Last('EtherType'): Number(111),
                               Length('Payload'): Add(Last('Message'), Number(-111)),
                               First('Payload'): Number(112),
                               Last('Payload'): Last('Message')
                           })
                   })),
            ('FINAL',
             Field('FINAL',
                   Null(),
                   TRUE,
                   {
                       '0000000':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000'),
                               ('TCI', '0000'),
                               ('EtherType', '00000'),
                               ('Payload', '000000')
                           ],
                           And(GreaterEqual(Div(Length('Payload'), Number(8)), Number(46)),
                               LessEqual(Div(Length('Payload'), Number(8)), Number(1500))),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('TCI'): Number(16),
                               First('TCI'): Number(112),
                               Last('TCI'): Number(127),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(128),
                               Last('EtherType'): Number(143),
                               Length('Payload'): Mul(LengthValue('EtherType'), Number(8)),
                               First('Payload'): Number(144),
                               Last('Payload'): Add(Mul(LengthValue('EtherType'), Number(8)),
                                                    Number(143))
                           }),
                       '0000010':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000'),
                               ('TCI', '0000'),
                               ('EtherType', '00000'),
                               ('Payload', '000001')
                           ],
                           And(GreaterEqual(Div(Length('Payload'), Number(8)), Number(46)),
                               LessEqual(Div(Length('Payload'), Number(8)), Number(1500))),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('TCI'): Number(16),
                               First('TCI'): Number(112),
                               Last('TCI'): Number(127),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(128),
                               Last('EtherType'): Number(143),
                               Length('Payload'): Add(Last('Message'), Number(-143)),
                               First('Payload'): Number(144),
                               Last('Payload'): Last('Message')
                           }),
                       '000100':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000'),
                               ('EtherType', '0001'),
                               ('Payload', '00010')
                           ],
                           And(GreaterEqual(Div(Length('Payload'), Number(8)), Number(46)),
                               LessEqual(Div(Length('Payload'), Number(8)), Number(1500))),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(96),
                               Last('EtherType'): Number(111),
                               Length('Payload'): Mul(LengthValue('EtherType'), Number(8)),
                               First('Payload'): Number(112),
                               Last('Payload'): Add(Mul(LengthValue('EtherType'), Number(8)),
                                                    Number(111))
                           }),
                       '000110':
                       Variant(
                           [
                               ('Destination', '0'),
                               ('Source', '00'),
                               ('TPID', '000'),
                               ('EtherType', '0001'),
                               ('Payload', '00011')
                           ],
                           And(GreaterEqual(Div(Length('Payload'), Number(8)), Number(46)),
                               LessEqual(Div(Length('Payload'), Number(8)), Number(1500))),
                           {
                               Length('Destination'): Number(48),
                               First('Destination'): Number(0),
                               Last('Destination'): Number(47),
                               Length('Source'): Number(48),
                               First('Source'): Number(48),
                               Last('Source'): Number(95),
                               Length('TPID'): Number(16),
                               First('TPID'): Number(96),
                               Last('TPID'): Number(111),
                               Length('EtherType'): Number(16),
                               First('EtherType'): Number(96),
                               Last('EtherType'): Number(111),
                               Length('Payload'): Add(Last('Message'), Number(-111)),
                               First('Payload'): Number(112),
                               Last('Payload'): Last('Message')
                           })
                   }))
        ])

        self.assertEqual(ETHERNET_PDU.fields(), expected)
Ejemplo n.º 21
0
 def test_range_invalid_first_variable(self) -> None:
     with self.assertRaises(ModelError):
         RangeInteger("P.T", Add(Number(1), Variable("X")), Number(15), Number(4))
Ejemplo n.º 22
0
def test_range_invalid_first_negative() -> None:
    assert_type_error(
        RangeInteger("P.T", Number(-1), Number(0), Number(1), Location(
            (6, 4))),
        r'^<stdin>:6:4: model: error: first of "T" negative$',
    )
Ejemplo n.º 23
0
    },
    checksums={ID("Checksum"): [Variable("Tag"), Size("Value"), Variable("Value")]},
    skip_proof=True,
)
TLV_WITH_CHECKSUM_MODEL = Model(
    [TLV_WITH_CHECKSUM_TAG, TLV_WITH_CHECKSUM_LENGTH, TLV_WITH_CHECKSUM_MESSAGE]
)

NULL_MESSAGE_IN_TLV_MESSAGE = Refinement("In_TLV", TLV_MESSAGE, Field("Value"), NULL_MESSAGE)
NULL_MESSAGE_IN_TLV_MESSAGE_MODEL = Model(
    [TLV_TAG, TLV_LENGTH, TLV_MESSAGE, NULL_MESSAGE, NULL_MESSAGE_IN_TLV_MESSAGE]
)

ETHERNET_ADDRESS = ModularInteger("Ethernet::Address", Pow(Number(2), Number(48)))
ETHERNET_TYPE_LENGTH = RangeInteger(
    "Ethernet::Type_Length", Number(46), Sub(Pow(Number(2), Number(16)), Number(1)), Number(16)
)
ETHERNET_TPID = RangeInteger("Ethernet::TPID", Number(0x8100, 16), Number(0x8100, 16), Number(16))
ETHERNET_TCI = ModularInteger("Ethernet::TCI", Pow(Number(2), Number(16)))
ETHERNET_FRAME = Message(
    "Ethernet::Frame",
    [
        Link(INITIAL, Field("Destination")),
        Link(Field("Destination"), Field("Source")),
        Link(Field("Source"), Field("Type_Length_TPID")),
        Link(
            Field("Type_Length_TPID"),
            Field("TPID"),
            Equal(Variable("Type_Length_TPID"), Number(0x8100, 16)),
            first=First("Type_Length_TPID"),
        ),
Ejemplo n.º 24
0
 def test_range_invalid_last_variable(self) -> None:
     with self.assertRaises(ModelError):
         RangeInteger('X', Number(1), Add(Number(1), Value('X')), Number(4))
Ejemplo n.º 25
0
def test_range_invalid_range() -> None:
    assert_type_error(
        RangeInteger("P.T", Number(1), Number(0), Number(1), Location(
            (10, 5))),
        r'^<stdin>:10:5: model: error: range of "T" negative$',
    )
Ejemplo n.º 26
0
 def test_range_invalid_size_variable(self) -> None:
     with self.assertRaises(ModelError):
         RangeInteger('X', Number(0), Number(256), Add(Number(8), Value('X')))
Ejemplo n.º 27
0
 def test_range_invalid_range(self) -> None:
     with self.assertRaises(ModelError):
         RangeInteger('X', Number(1), Number(0), Number(1))
Ejemplo n.º 28
0
def test_range_invalid_size_variable() -> None:
    assert_type_error(
        RangeInteger("P.T", Number(0), Number(256),
                     Add(Number(8), Variable("X")), Location((22, 4))),
        r'^<stdin>:22:4: model: error: size of "T" contains variable$',
    )
Ejemplo n.º 29
0
 def test_range_invalid_size_too_small(self) -> None:
     with self.assertRaises(ModelError):
         RangeInteger('X', Number(0), Number(256), Number(8))
Ejemplo n.º 30
0
def test_range_invalid_size_too_small() -> None:
    assert_type_error(
        RangeInteger("P.T", Number(0), Number(256), Number(8), Location(
            (10, 4))),
        r'^<stdin>:10:4: model: error: size of "T" too small$',
    )