def test_encode_decode_attribute_with_encrypt(self): secret_key = '1234567890' authenticator = os.urandom(16) tests = ( ('User-Name', 'test'), # Testing encode/decode simple string attribute # Testing encode/decode User-Password attribute ('User-Password', 'blafoo_vary_vary_vary_vary_vary_vary_vary_vary_vary_vary_vary_vary_vary_vary_long' ), ) for ATTRIBUTE, VALUE in tests: attribute_for_encode = AttributeType.get_attribute_for_encode( ATTRIBUTE) if attribute_for_encode.encrypt == USER_PASSWORD_CRYPT_TYPE: value_ = UserPasswordCrypt.encrypt(VALUE, secret_key, authenticator) else: value_ = VALUE encoded_ = attribute_for_encode.encode(value_) attribute_for_decode = AttributeType.get_attribute_for_decode( encoded_) decoded_name, decoded_value = attribute_for_decode.decode(encoded_) if attribute_for_decode.encrypt == USER_PASSWORD_CRYPT_TYPE: decoded_value = UserPasswordCrypt.decrypt( decoded_value, secret_key, authenticator) self.assertEqual(attribute_for_encode, attribute_for_decode) self.assertEqual(decoded_name, ATTRIBUTE) self.assertEqual(decoded_value, VALUE)
def setUp(self): self.dictionary = dictionary.Dictionary(quit=True) self.dictionary.merge( os.path.join(CURRENT_DIR, 'fixtures/dictionaries/dictionary.cisco')) self.dictionary.merge( os.path.join(CURRENT_DIR, 'fixtures/dictionaries/dictionary.dhcp')) AttributeType.set_dictionary(self.dictionary)
def _verify_attributes(self): attributes_names = self.attributes.names() for name in attributes_names: attr = AttributeType.get_attribute_for_encode(name) if attr.is_vendor_specific() or name == 'Proxy-State': continue else: raise PacketError("Attribute '{}' is rejected for 'Accounting-Response' packet".format(name))
def _verify_attributes(self): attributes_names = self.attributes.names() for name in attributes_names: attr = AttributeType.get_attribute_for_encode(name) # Vendor-Specific is allowed if attr.is_vendor_specific(): continue if name not in self.ACCEPTED_ATTRIBUTES: self.attributes.remove(name)
def _verify_attributes(self): attributes_names = self.attributes.names() for name in attributes_names: attr = AttributeType.get_attribute_for_encode(name) # Vendor-Specific is allowed if attr.is_vendor_specific(): continue if name not in self.__ACCEPTED_ATTRIBUTES: raise PacketError("Attribute '{}' is rejected for 'Access-Challenge' packet".format(name))
def test_encode_decode_vsa_attribute(self): tests = ( ('Cisco-AVPair', 'bla=foo'), # Testing encode/decode VSA string attribute ('DHCP-Your-IP-Address', ipaddress.IPv4Address('192.168.0.1') ), # Testing encode/decode vsa ipv4addr ('DHCP-Hardware-Type', 'Ethernet' ) # # Testing encode/decode VSA attribute with value ) for ATTRIBUTE, VALUE in tests: attribute_for_encode = AttributeType.get_attribute_for_encode( ATTRIBUTE) encoded_ = attribute_for_encode.encode(VALUE) attribute_for_decode = AttributeType.get_attribute_for_decode( encoded_) decoded_name, decoded_value = attribute_for_decode.decode(encoded_) self.assertEqual(attribute_for_encode, attribute_for_decode) self.assertEqual(decoded_name, ATTRIBUTE) self.assertEqual(decoded_value, VALUE)
def test_encode_decode_simple_attribute(self): tests = ( ('User-Name', 'My name is test' ), # Testing encode/decode simple string attribute ('NAS-IP-Address', ipaddress.IPv4Address('192.168.0.1') ), # Testing encode/decode simple ipv4addr attribute ('Service-Type', 'Login-User' ) # Testing encode/decode simple attribute with value ) for ATTRIBUTE, VALUE in tests: attribute_for_encode = AttributeType.get_attribute_for_encode( ATTRIBUTE) encoded_ = attribute_for_encode.encode(VALUE) attribute_for_decode = AttributeType.get_attribute_for_decode( encoded_) decoded_name, decoded_value = attribute_for_decode.decode(encoded_) self.assertEqual(attribute_for_encode, attribute_for_decode) self.assertEqual(decoded_name, ATTRIBUTE) self.assertEqual(decoded_value, VALUE)
def setUp(self): dictionary = Dictionary(quit=False) dictionary.merge(os.path.join(CURRENT_DIR, 'fixtures/dictionaries/dictionary.cisco')) dictionary.merge(os.path.join(CURRENT_DIR, 'fixtures/dictionaries/dictionary.dhcp')) AttributeType.set_dictionary(dictionary)