def test_valid_via_port_single_value(self): rule = RuleParser.parse('allow via-port "foo bar"') self.assertEqual( rule.via_port, DeviceAttribute(name=DeviceAttributeName.VIA_PORT, operator=None, values=['foo bar']))
def test_valid_id_value_with_generic_vendor_and_product(self): rule = RuleParser.parse('allow id *:*') self.assertEqual( rule.id, DeviceAttribute(name=DeviceAttributeName.ID, operator=None, values=[DeviceId(None, None)]))
def test_valid_serial_multi_values(self): rule = RuleParser.parse('allow serial { "foo bar" "baz" }') self.assertEqual( rule.serial, DeviceAttribute(name=DeviceAttributeName.SERIAL, operator=None, values=['foo bar', 'baz']))
def test_valid_serial_multi_values_with_operator(self): rule = RuleParser.parse('allow serial one-of { "foo bar" "baz" }') self.assertEqual( rule.serial, DeviceAttribute(name=DeviceAttributeName.SERIAL, operator=DeviceAttributeOperator.ONE_OF, values=['foo bar', 'baz']))
def test_valid_name_multi_values_with_operator(self): rule = RuleParser.parse('allow name one-of { "foo bar" "baz" }') self.assertEqual( rule.name, DeviceAttribute(name=DeviceAttributeName.NAME, operator=DeviceAttributeOperator.ONE_OF, values=['foo bar', 'baz']))
def test_valid_serial_single_value(self): rule = RuleParser.parse('allow serial "foo bar"') self.assertEqual( rule.serial, DeviceAttribute(name=DeviceAttributeName.SERIAL, operator=None, values=['foo bar']))
def test_valid_name_single_value(self): rule = RuleParser.parse('allow name "foo bar"') self.assertEqual( rule.name, DeviceAttribute(name=DeviceAttributeName.NAME, operator=None, values=['foo bar']))
def test_valid_single_valued_id_with_specific_vendor_and_product(self): rule = RuleParser.parse('allow id dead:beef') self.assertEqual( rule.id, DeviceAttribute(name=DeviceAttributeName.ID, operator=None, values=[DeviceId(57005, 48879)]))
def test_valid_parent_hash_single_value(self): rule = RuleParser.parse('allow parent-hash "foo bar"') self.assertEqual( rule.parent_hash, DeviceAttribute(name=DeviceAttributeName.PARENT_HASH, operator=None, values=['foo bar']))
def test_valid_parent_hash_multi_values_with_operator(self): rule = RuleParser.parse('allow parent-hash one-of { "foo bar" "baz" }') self.assertEqual( rule.parent_hash, DeviceAttribute(name=DeviceAttributeName.PARENT_HASH, operator=DeviceAttributeOperator.ONE_OF, values=['foo bar', 'baz']))
def test_valid_hash_multi_values(self): rule = RuleParser.parse('allow hash { "foo bar" "baz" }') self.assertEqual( rule.hash, DeviceAttribute(name=DeviceAttributeName.HASH, operator=None, values=['foo bar', 'baz']))
def test_valid_id_value_with_generic_product_only(self): rule = RuleParser.parse('allow id cafe:*') self.assertEqual( rule.id, DeviceAttribute(name=DeviceAttributeName.ID, operator=None, values=[DeviceId(0xcafe, None)]))
def test_valid_with_interface_value_with_generic_ss_pp(self): rule = RuleParser.parse('allow with-interface ab:*:*') self.assertEqual( rule.with_interface, DeviceAttribute(name=DeviceAttributeName.WITH_INTERFACE, operator=None, values=[DeviceInterfaceType(0xab, None, None)]))
def test_valid_single_valued_with_interface_with_specific_cc_ss_pp(self): rule = RuleParser.parse('allow with-interface ab:cd:ef') self.assertEqual( rule.with_interface, DeviceAttribute(name=DeviceAttributeName.WITH_INTERFACE, operator=None, values=[DeviceInterfaceType(0xab, 0xcd, 0xef)]))
def test_valid_name_multi_values(self): rule = RuleParser.parse('allow name { "foo bar" "baz" }') self.assertEqual( rule.name, DeviceAttribute(name=DeviceAttributeName.NAME, operator=None, values=['foo bar', 'baz']))
def test_valid_with_connect_type_multi_values(self): rule = RuleParser.parse('allow with-connect-type { "foo bar" "baz" }') self.assertEqual( rule.with_connect_type, DeviceAttribute(name=DeviceAttributeName.WITH_CONNECT_TYPE, operator=None, values=['foo bar', 'baz']))
def test_valid_via_port_multi_values_with_operator(self): rule = RuleParser.parse('allow via-port one-of { "foo bar" "baz" }') self.assertEqual( rule.via_port, DeviceAttribute(name=DeviceAttributeName.VIA_PORT, operator=DeviceAttributeOperator.ONE_OF, values=['foo bar', 'baz']))
def test_valid_multi_valued_id_with_operator(self): rule = RuleParser.parse('allow id one-of { dead:beef cafe:babe }') self.assertEqual( rule.id, DeviceAttribute( name=DeviceAttributeName.ID, operator=DeviceAttributeOperator.ONE_OF, values=[DeviceId(0xdead, 0xbeef), DeviceId(0xcafe, 0xbabe)]))
def test_can_parse_multi_valued_with_interface_without_operator(self): rule = RuleParser.parse('allow with-interface { 12:34:56 ab:cd:ef }') self.assertEqual( rule.with_interface, DeviceAttribute(name=DeviceAttributeName.WITH_INTERFACE, operator=None, values=[ DeviceInterfaceType(0x12, 0x34, 0x56), DeviceInterfaceType(0xab, 0xcd, 0xef), ]))
def test_all_single_valued_attributes(self): rule = RuleParser.parse(''' allow id cafe:babe hash "AbCd123" parent-hash "EfGh456" name "Foo Bar" serial "AB12CD34EF56" via-port "1-2" with-interface ab:cd:ef with-connect-type "hotplug" ''') self.assertEqual( rule.id, DeviceAttribute(name=DeviceAttributeName.ID, operator=None, values=[DeviceId(0xcafe, 0xbabe)])) self.assertEqual( rule.hash, DeviceAttribute(name=DeviceAttributeName.HASH, operator=None, values=['AbCd123'])) self.assertEqual( rule.parent_hash, DeviceAttribute(name=DeviceAttributeName.PARENT_HASH, operator=None, values=['EfGh456'])) self.assertEqual( rule.name, DeviceAttribute(name=DeviceAttributeName.NAME, operator=None, values=['Foo Bar'])) self.assertEqual( rule.serial, DeviceAttribute(name=DeviceAttributeName.SERIAL, operator=None, values=['AB12CD34EF56'])) self.assertEqual( rule.via_port, DeviceAttribute(name=DeviceAttributeName.VIA_PORT, operator=None, values=['1-2'])) self.assertEqual( rule.with_interface, DeviceAttribute(name=DeviceAttributeName.WITH_INTERFACE, operator=None, values=[DeviceInterfaceType(0xab, 0xcd, 0xef)])) self.assertEqual( rule.with_connect_type, DeviceAttribute(name=DeviceAttributeName.WITH_CONNECT_TYPE, operator=None, values=['hotplug']))
def test_invalid_id_vendor_too_long(self): with self.assertRaises(RuleParsingError): RuleParser.parse('allow id 12345:1234')
def test_invalid_id_value_with_specific_product_only(self): with self.assertRaises(RuleParsingError): RuleParser.parse('allow id *:0001')
def test_invalid_target(self): with self.assertRaises(RuleParsingError): RuleParser.parse('foo')
def test_attributes_cannot_be_repeated(self): with self.assertRaises(RuleParsingError): RuleParser.parse('allow id 1234:9876 id 5678:4321')
def test_invalid_id_without_value(self): with self.assertRaises(RuleParsingError): RuleParser.parse('allow id')
def test_invalid_attribute_name(self): with self.assertRaises(RuleParsingError): RuleParser.parse('allow foo "bar"')
def test_valid_reject_target_alone(self): rule = RuleParser.parse('reject') self.assertEqual(rule.target, RuleTarget.REJECT)
def test_valid_block_target_alone(self): rule = RuleParser.parse('block') self.assertEqual(rule.target, RuleTarget.BLOCK)
def test_valid_allow_target_alone(self): rule = RuleParser.parse('allow') self.assertEqual(rule.target, RuleTarget.ALLOW)
def test_invalid_partially_matching_target(self): with self.assertRaises(RuleParsingError): RuleParser.parse('allowed')