class Hash(xso.XSO): """ Represent a single hash digest. .. attribute:: algo The hash algorithm used. The name is as specified in :xep:`300`. .. attribute:: digest The digest as :class:`bytes`. """ TAG = namespaces.xep0300_hashes2, "hash" algo = xso.Attr("algo", ) digest = xso.Text(type_=xso.Base64Binary()) def __init__(self, algo, digest): super().__init__() self.algo = algo self.digest = digest def get_impl(self): """ Return a new :mod:`hashlib` hash for the :attr:`algo` set on this object. See :func:`hash_from_algo` for details and exceptions. """ return hash_from_algo(self.algo)
class Data(xso.XSO): TAG = (namespaces.xep0047, "data") seq = xso.Attr("seq", type_=xso.Integer()) sid = xso.Attr("sid", type_=xso.String()) content = xso.Text(type_=xso.Base64Binary()) def __init__(self, sid, seq, content): self.seq = seq self.sid = sid self.content = content
class Data(xso.XSO): """ A data node, as used to publish and receive the avatar image data as image/png. .. attribute:: data The binary image data. """ TAG = (namespaces.xep0084_data, "data") data = xso.Text(type_=xso.Base64Binary()) def __init__(self, image_data): self.data = image_data
def test_coerce_accepts_bytes_bytearray_array(self): t = xso.Base64Binary() import array array_value = array.array("h") array_value.append(1234) array_value.append(5678) array_value.append(910) values = [ b"foobar", bytearray(b"baz"), ] for value in values: result = t.coerce(value) self.assertEqual(bytes(value), result) self.assertIsInstance(result, bytes)
def test_coerce_passes_bytes(self): t = xso.Base64Binary() value = b"foo" self.assertIs(value, t.coerce(value))
def test_coerce_rejects_int(self): t = xso.Base64Binary() with self.assertRaisesRegex(TypeError, "must be convertible to bytes"): t.coerce(12)
def test_format_long(self): t = xso.Base64Binary() self.assertEqual( "Zm5vcmRmbm9yZGZub3JkZm5vcmRmbm9yZGZub3JkZm5vcmRmbm9yZG" "Zub3JkZm5vcmRmbm9yZGZub3JkZm5vcmRmbm9yZGZub3JkZm5vcmRm" "bm9yZGZub3JkZm5vcmRmbm9yZA==", t.format(b"fnord" * 20))
def test_format_empty_with_empty_as_equal_flag(self): t = xso.Base64Binary(empty_as_equal=True) self.assertEqual("=", t.format(b""))
def test_format_empty_default(self): t = xso.Base64Binary() self.assertEqual("", t.format(b""))
def test_format(self): t = xso.Base64Binary() self.assertEqual("Zm5vcmQ=", t.format(b"fnord"))
def test_parse_empty(self): t = xso.Base64Binary() self.assertEqual(b"", t.parse("")) self.assertEqual(b"", t.parse("="))
def test_parse(self): t = xso.Base64Binary() self.assertEqual(b"fnord", t.parse("Zm5vcmQ="))
def test_is_abstract_type(self): self.assertIsInstance(xso.Base64Binary(), xso.AbstractType)