def test_expr_variables(self) -> None: self.assertEqual( Or( Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42))) ).variables(), [Variable("Y"), Variable("X")], ) self.assertEqual( Or( Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42))) ).variables(), [Variable("Y"), Variable("X")], ) self.assertEqual( Or( Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42))) ).variables(), [Variable("Y"), Variable("X")], ) self.assertEqual( Or( Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(1))) ).variables(), [Variable("Y"), Variable("X")], )
def test_message_field_condition(self) -> None: self.assertEqual(ETHERNET_FRAME.field_condition(INITIAL), TRUE) self.assertEqual( ETHERNET_FRAME.field_condition(Field("TPID")), Equal(Variable("Type_Length_TPID"), Number(33024, 16)), ) self.assertEqual( ETHERNET_FRAME.field_condition(Field("Type_Length")), Or( NotEqual(Variable("Type_Length_TPID"), Number(33024, 16)), Equal(Variable("Type_Length_TPID"), Number(33024, 16)), ), ) self.assertEqual( ETHERNET_FRAME.field_condition(Field("Payload")), Or( And( Or( NotEqual(Variable("Type_Length_TPID"), Number(33024, 16)), Equal(Variable("Type_Length_TPID"), Number(33024, 16)), ), LessEqual(Variable("Type_Length"), Number(1500)), ), And( Or( NotEqual(Variable("Type_Length_TPID"), Number(33024, 16)), Equal(Variable("Type_Length_TPID"), Number(33024, 16)), ), GreaterEqual(Variable("Type_Length"), Number(1536)), ), ), )
def test_expr_variables_duplicates(self) -> None: self.assertEqual( And(Variable("X"), Variable("Y"), Variable("X")).variables(), [Variable("X"), Variable("Y")], ) self.assertEqual( Or(Variable("X"), Variable("Y"), Variable("X")).variables(), [Variable("X"), Variable("Y")], ) self.assertEqual( Add(Variable("X"), Variable("Y"), Variable("X")).variables(), [Variable("X"), Variable("Y")], ) self.assertEqual( Mul(Variable("X"), Variable("Y"), Variable("X")).variables(), [Variable("X"), Variable("Y")], ) self.assertEqual(Sub(Variable("X"), Variable("X")).variables(), [Variable("X")]) self.assertEqual(Div(Variable("X"), Variable("X")).variables(), [Variable("X")]) self.assertEqual( Or( Greater(Variable("X"), Number(42)), And(TRUE, Less(Variable("X"), Number(1))) ).variables(), [Variable("X")], )
def test_or() -> None: assert_equal( Or(TRUE, FALSE, TRUE).z3expr(), z3.Or(z3.BoolVal(True), z3.BoolVal(False), z3.BoolVal(True)), ) assert_equal( Or(TRUE, TRUE, TRUE).z3expr(), z3.Or(z3.BoolVal(True), z3.BoolVal(True), z3.BoolVal(True)), ) assert_equal(Or(TRUE, TRUE).z3expr(), z3.Or(z3.BoolVal(True), z3.BoolVal(True)))
def test_or(self) -> None: self.assertEqual( Or(TRUE, FALSE, TRUE).z3expr(), z3.Or(z3.BoolVal(True), z3.BoolVal(False), z3.BoolVal(True)), ) self.assertEqual( Or(TRUE, TRUE, TRUE).z3expr(), z3.Or(z3.BoolVal(True), z3.BoolVal(True), z3.BoolVal(True)), ) self.assertEqual( Or(TRUE, TRUE).z3expr(), z3.Or(z3.BoolVal(True), z3.BoolVal(True)))
def test_expr_contains() -> None: assert Variable("X") in Or( Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42))) ) assert Variable("Z") not in Or( Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42))) ) assert Less(Variable("X"), Number(42)) in Or( Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42))) ) assert Less(Variable("Z"), Number(42)) not in Or( Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(1))) )
def __prove_reachability(self) -> None: for f in (*self.__fields, FINAL): reachability = Or( *[ And(*[self.__with_constraints(l.condition) for l in path]) for path in self.__paths[f] ] ) result = reachability.exists() if result != ProofResult.sat: message = str(reachability).replace("\n", "") raise ModelError( f'unreachable field "{f.name}" in "{self.full_name}"' f" ({result}: {message})" )
def create_structural_valid_function() -> UnitPart: specification = FunctionSpecification( "Structural_Valid", "Boolean", [Parameter(["Ctx"], "Context"), Parameter(["Fld"], "Field")], ) return UnitPart( [SubprogramDeclaration(specification)], [ ExpressionFunctionDeclaration( specification, And( Or(*[ Equal( Selected( Indexed(Variable("Ctx.Cursors"), Variable("Fld")), "State"), Variable(s), ) for s in ("S_Valid", "S_Structural_Valid") ])), ) ], )
def constraints(self, name: str, proof: bool = False) -> Expr: if proof: return And( And(*[Equal(Variable(l), v) for l, v in self.literals.items()]), Or(*[Equal(Variable(name), Variable(l)) for l in self.literals.keys()]), ) return TRUE
def create_invalid_function() -> UnitPart: specification = FunctionSpecification( "Invalid", "Boolean", [Parameter(["Ctx"], "Context"), Parameter(["Fld"], "Field")]) return UnitPart( [SubprogramDeclaration(specification)], [ ExpressionFunctionDeclaration( specification, Or( Equal( Selected( Indexed(Variable("Ctx.Cursors"), Variable("Fld")), "State"), Variable("S_Invalid"), ), Equal( Selected( Indexed(Variable("Ctx.Cursors"), Variable("Fld")), "State"), Variable("S_Incomplete"), ), ), ) ], )
def bounded_composite_setter_preconditions(message: Message, field: Field) -> Sequence[Expr]: return [ Call( "Field_Condition", [ Variable("Ctx"), NamedAggregate(("Fld", Variable(field.affixed_name))) ] + ([Variable("Length")] if common.length_dependent_condition(message) else []), ), GreaterEqual( Call("Available_Space", [Variable("Ctx"), Variable(field.affixed_name)]), Variable("Length"), ), LessEqual( Add( Call("Field_First", [Variable("Ctx"), Variable(field.affixed_name)]), Variable("Length"), ), Div(Last(const.TYPES_BIT_INDEX), Number(2)), ), Or(*[ And( *[ Call("Valid", [Variable("Ctx"), Variable(field.affixed_name)]) for field in message.fields if Variable(field.name) in l.condition.variables() ], l.condition.substituted( mapping={ Variable(field.name): Call(f"Get_{field.name}", [Variable("Ctx")]) for field in message.fields if Variable(field.name) in l.condition.variables() }), ) for l in message.incoming(field) if Last("Message") in l.length ]), Equal( Mod( Call("Field_First", [Variable("Ctx"), Variable(field.affixed_name)]), Size(const.TYPES_BYTE), ), Number(1), ), Equal( Mod(Variable("Length"), Size(const.TYPES_BYTE)), Number(0), ), ]
def test_expr_contains(self) -> None: self.assertTrue( Variable("X") in Or(Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42)))) ) self.assertFalse( Variable("Z") in Or(Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42)))) ) self.assertTrue( Less(Variable("X"), Number(42)) in Or(Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(42)))) ) self.assertFalse( Less(Variable("Z"), Number(42)) in Or(Greater(Variable("Y"), Number(42)), And(TRUE, Less(Variable("X"), Number(1)))) )
def disjunction(cond: List[LogExpr]) -> LogExpr: if cond: res = cond.pop() for c in cond: res = Or(res, c) else: res = TRUE return res
def test_tlv_message_with_not_operator_exhausting() -> None: message = Message( "TLV::Message_With_Not_Operator_Exhausting", [ Link(INITIAL, Field("Tag")), Link( Field("Tag"), Field("Length"), Not(Not(Not(NotEqual(Variable("Tag"), Variable("Msg_Data"))))), ), Link( Field("Tag"), FINAL, reduce( lambda acc, f: f(acc), [Not, Not] * 16, Not( Or( Not( Not( Equal(Variable("Tag"), Variable("Msg_Data")))), Not(Equal(Variable("Tag"), Variable("Msg_Error"))), )), ), ), Link(Field("Length"), Field("Value"), size=Mul(Variable("Length"), Number(8))), Link(Field("Value"), FINAL), ], { Field("Tag"): TLV_TAG, Field("Length"): TLV_LENGTH, Field("Value"): OPAQUE }, ) with pytest.raises( FatalError, match=re.escape( "failed to simplify complex expression `not (not (not (not " "(not (not (not (not (not (not (not (not (not (not (not (not " "(not (not (not (not (not (not (not (not (not (not (not (not " "(not (not (not (not (not (not (not (Tag = TLV::Msg_Data))\n" " " "or not (Tag = TLV::Msg_Error))))))))))))))))))))))))))))))))))` " "after `16` iterations, best effort: " "`not (not (not (not (not (not (not (not (not (not (not (not (not " "(not (not (not (not (Tag = TLV::Msg_Data\n" " or Tag /= TLV::Msg_Error)))))))))))))))))`"), ): model = PyRFLX(model=Model([TLV_TAG, TLV_LENGTH, message])) pkg = model.package("TLV") msg = pkg.new_message("Message_With_Not_Operator_Exhausting") test_bytes = b"\x01\x00\x04\x00\x00\x00\x00" msg.parse(test_bytes)
def __compute_field_condition(self, final: Field) -> Expr: if final == INITIAL: return TRUE return Or( *[ And(self.__compute_field_condition(l.source), l.condition) for l in self.incoming(final) ] )
def message_validation_function(variants: List[Variant]) -> Subprogram: expr: LogExpr = FALSE for variant in variants: condition = variant_condition(variant) expr = condition if expr == FALSE else Or(expr, condition) return ExpressionFunction('Is_Valid', 'Boolean', [('Buffer', 'Types.Bytes')], expr, [Precondition(COMMON_PRECONDITION)])
def field_validation_function(field: Field) -> Subprogram: variants: List[LogExpr] = list(valid_variants(field)) expr = variants.pop() for e in variants: if e is not TRUE: expr = Or(expr, e) return ExpressionFunction(f'Valid_{field.name}', 'Boolean', [('Buffer', 'Types.Bytes')], expr, [Precondition(COMMON_PRECONDITION)])
def test_ass_expr_substituted() -> None: assert_equal( And(Equal(Variable("X"), Number(1)), Variable("Y")).substituted( lambda x: Variable(f"P_{x}") if isinstance(x, Variable) else x ), And(Equal(Variable("P_X"), Number(1)), Variable("P_Y")), ) assert_equal( Mul(Variable("X"), Number(1)).substituted( lambda x: Variable("Y") if x == Mul(Variable("X"), Number(1)) else x ), Variable("Y"), ) assert_equal( And(Equal(Variable("X"), Number(1)), Variable("Y")).substituted( lambda x: Variable(f"P_{x}") if isinstance(x, Variable) else (Or(*x.terms) if isinstance(x, And) else x) ), Or(Equal(Variable("P_X"), Number(1)), Variable("P_Y")), )
def test_expr_str() -> None: assert_equal( str( And( If([(Variable("X"), Number(1)), (Variable("Y"), Number(2))], Number(3)), Variable("A"), Or(Variable("B"), Variable("C")), Variable("D"), ) ), multilinestr( """(if X then 1 elsif Y then 2 else 3) and A and (B or C) and D""" ), ) assert_equal( str( ForAllOf( "X", Variable("Z"), If([(Variable("X"), Number(1)), (Variable("Y"), Number(2))], Number(3)), ) ), multilinestr( """(for all X of Z => (if X then 1 elsif Y then 2 else 3))""" ), )
def valid_predecessors_invariant() -> Expr: return AndThen( *[ If( [ ( Call( "Structural_Valid", [Indexed(Variable("Cursors"), Variable(f.affixed_name))], ), Or( *[ AndThen( Call( "Structural_Valid" if l.source in composite_fields else "Valid", [ Indexed( Variable("Cursors"), Variable(l.source.affixed_name), ) ], ), Equal( Selected( Indexed( Variable("Cursors"), Variable(f.affixed_name), ), "Predecessor", ), Variable(l.source.affixed_name), ), l.condition.substituted( substitution(message, embedded=True) ), ).simplified() for l in message.incoming(f) ] ), ) ] ) for f in message.fields if f not in message.direct_successors(INITIAL) ] )
def valid_message_condition(message: Message, field: Field = INITIAL, structural: bool = False) -> Expr: if not message.outgoing(field): return TRUE return Or(*[ l.condition if l.target == FINAL else AndThen( Call( "Structural_Valid" if structural and isinstance( message.types[l.target], Composite) else "Valid", [Variable("Ctx" ), Variable(l.target.affixed_name)], ), l.condition, valid_message_condition(message, l.target, structural), ) for l in message.outgoing(field) ])
def parse_logical_expression(string: str, location: int, tokens: list) -> LogExpr: result: List[LogExpr] = tokens[0] while len(result) > 1: left = result.pop(0) operator = result.pop(0) right = result.pop(0) expression: LogExpr if operator == 'and': expression = And(left, right) elif operator == 'or': expression = Or(left, right) else: raise ParseFatalException(string, location, 'unexpected logical operator') result.insert(0, expression) return result[0]
def parse_logical_expression(string: str, location: int, tokens: ParseResults) -> Expr: result: List[Expr] = tokens[0] while len(result) > 1: left = result.pop(0) operator = result.pop(0) right = result.pop(0) expression: Expr if operator == "and": expression = And(left, right) elif operator == "or": expression = Or(left, right) else: raise ParseFatalException(string, location, "unexpected logical operator") result.insert(0, expression) return result[0]
def test_tlv_message_with_not_operator() -> None: message = Message( "TLV::Message_With_Not_Operator", [ Link(INITIAL, Field("Tag")), Link( Field("Tag"), Field("Length"), Not(Not(Not(NotEqual(Variable("Tag"), Variable("Msg_Data"))))), ), Link( Field("Tag"), FINAL, Not( Not( Not( Or( Not( Not( Equal(Variable("Tag"), Variable("Msg_Data")))), Not( Equal(Variable("Tag"), Variable("Msg_Error"))), )))), ), Link(Field("Length"), Field("Value"), size=Mul(Variable("Length"), Number(8))), Link(Field("Value"), FINAL), ], { Field("Tag"): TLV_TAG, Field("Length"): TLV_LENGTH, Field("Value"): OPAQUE }, ) model = PyRFLX(model=Model([TLV_TAG, TLV_LENGTH, message])) pkg = model.package("TLV") msg = pkg.new_message("Message_With_Not_Operator") test_bytes = b"\x01\x00\x04\x00\x00\x00\x00" msg.parse(test_bytes) assert msg.valid_message assert msg.bytestring == test_bytes
def create_incomplete_message_function(message: Message) -> UnitPart: specification = FunctionSpecification("Incomplete_Message", "Boolean", [Parameter(["Ctx"], "Context")]) return UnitPart( [SubprogramDeclaration(specification)], [ ExpressionFunctionDeclaration( specification, Or(*[ Call("Incomplete", [Variable("Ctx"), Variable(f.affixed_name)]) for f in message.fields ]), ) ], )
def test_log_expr_str() -> None: assert_equal( str(And(Variable("A"), Or(Variable("B"), Variable("C")), Variable("D"))), multilinestr( """A and (B or C) and D""" ), ) assert_equal( str(AndThen(Variable("A"), OrElse(Variable("B"), Variable("C")), Variable("D"))), multilinestr( """A and then (B or else C) and then D""" ), )
def bounded_composite_setter_preconditions( self, message: Message, field: Field ) -> Sequence[Expr]: return [ Call( "Field_Condition", [Name("Ctx"), NamedAggregate(("Fld", Name(field.affixed_name)))] + ([Name("Length")] if length_dependent_condition(message) else []), ), GreaterEqual( Call("Available_Space", [Name("Ctx"), Name(field.affixed_name)]), Name("Length"), ), LessEqual( Add(Call("Field_First", [Name("Ctx"), Name(field.affixed_name)]), Name("Length"),), Div(Last(self.types.bit_index), Number(2)), ), Or( *[ And( *[ Call("Valid", [Name("Ctx"), Name(field.affixed_name)]) for field in message.fields if Variable(field.name) in l.condition.variables() ], l.condition.simplified( { Variable(field.name): Call(f"Get_{field.name}", [Name("Ctx")]) for field in message.fields if Variable(field.name) in l.condition.variables() } ), ) for l in message.incoming(field) if Last("Message") in l.length ] ), ]
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)
UNIVERSAL_OPTIONS, UNIVERSAL_MESSAGE, UNIVERSAL_REFINEMENT, ] ) FIXED_SIZE_MESSAGE = Message( "Fixed_Size::Message", [ Link(INITIAL, Field("Message_Type")), Link( Field("Message_Type"), Field("Data"), condition=Or( Equal(Variable("Message_Type"), Variable("Universal.MT_Null")), Equal(Variable("Message_Type"), Variable("Universal.MT_Data")), Equal(Variable("Message_Type"), Variable("Universal.MT_Values")), Equal(Variable("Message_Type"), Variable("Universal.MT_Options")), ), size=Number(64), ), Link( Field("Data"), Field("Values"), size=Mul(Number(8), Size("Universal::Value")), ), Link( Field("Values"), Field("Options"), size=Number(64), ), Link(
def test_or_simplified(self) -> None: self.assertEqual(Or(TRUE, TRUE).simplified(), TRUE) self.assertEqual(Or(TRUE, SOME_LOG_EXPR).simplified(), TRUE) self.assertEqual(Or(SOME_LOG_EXPR, TRUE).simplified(), TRUE)