def guess_response(content_type, data, ssl2=False): """Guess which kind of message is in the record layer payload""" if content_type == ContentType.change_cipher_spec: if len(data) != 1: return "ChangeCipherSpec(invalid size)" return "ChangeCipherSpec()" elif content_type == ContentType.alert: if len(data) < 2: return "Alert(invalid size)" return "Alert({0}, {1})".format(AlertLevel.toStr(data[0]), AlertDescription.toStr(data[1])) elif content_type == ContentType.handshake: if not data: return "Handshake(invalid size)" if ssl2: return "Handshake({0})".format(SSL2HandshakeType.toStr(data[0])) else: return "Handshake({0})".format(HandshakeType.toStr(data[0])) elif content_type == ContentType.application_data: return "ApplicationData(len={0})".format(len(data)) else: return ("Message(content_type={0}, first_byte={1}, " "len={2})").format(ContentType.toStr(content_type), data[0], len(data))
def __format__(self, formatstr): """Advanced formatting for messages.""" hexify = False verbose = "" if "h" in formatstr: hexify = True if "v" in formatstr: verbose = "ContentType." if hexify: data = b2a_hex(self.data) else: data = repr(self.data) return "Message(contentType={0}{1}, data={2})".format(verbose, ContentType.toStr(self.contentType), data)
def __format__(self, formatstr): """Advanced formatting for messages.""" hexify = False verbose = "" if 'h' in formatstr: hexify = True if 'v' in formatstr: verbose = "ContentType." if hexify: data = b2a_hex(self.data) else: data = repr(self.data) return "Message(contentType={0}{1}, data={2})"\ .format(verbose, ContentType.toStr(self.contentType), data)
def guess_response(content_type, data): """Guess which kind of message is in the record layer payload""" if content_type == ContentType.change_cipher_spec: if len(data) != 1: return "ChangeCipherSpec(invalid size)" return "ChangeCipherSpec()" elif content_type == ContentType.alert: if len(data) < 2: return "Alert(invalid size)" return "Alert({0}, {1})".format(AlertLevel.toStr(data[0]), AlertDescription.toStr(data[1])) elif content_type == ContentType.handshake: if not data: return "Handshake(invalid size)" return "Handshake({0})".format(HandshakeType.toStr(data[0])) elif content_type == ContentType.application_data: return "ApplicationData(len={0})".format(len(data)) else: return ("Message(content_type={0}, first_byte={1}, " "len={2})").format(ContentType.toStr(content_type), data[0], len(data))
def test_toStr_with_invalid_value(self): self.assertEqual(ContentType.toStr((20, 21, 22, 23)), "(20, 21, 22, 23)")
def test_toRepr_with_invalid_value(self): self.assertIsNone(ContentType.toRepr((20, 21, 22, 23)))
def test_toStr_with_invalid_value(self): self.assertEqual(ContentType.toStr((20, 21, 22, 23)), '(20, 21, 22, 23)')