コード例 #1
0
def test_stringz__dump_multibyte_with_bom():
    """Ensure multibyte encodings work with StringZ as well and the BOM isn't
    added before the null byte."""
    field = stringlike.StringZ(encoding="utf-16")

    if sys.byteorder == "little":
        assert field.to_bytes(
            "AbCd") == b"\xff\xfeA\x00b\x00C\x00d\x00\x00\x00"
    else:
        assert field.to_bytes(
            "AbCd") == b"\xfe\xff\x00A\x00b\x00C\x00d\x00\x00"
コード例 #2
0
def test_stringz__dump_default_null_crashes():
    field = stringlike.StringZ(null_value=DEFAULT)
    with pytest.raises(errors.UnserializableValueError):
        field.to_bytes(None)
コード例 #3
0
def test_stringz__load_default_null_crashes():
    field = stringlike.StringZ(null_value=DEFAULT)
    with pytest.raises(errors.CannotDetermineNullError):
        field.from_bytes(b"\x00")
コード例 #4
0
def test_stringz__load_null_value(null_value):
    field = stringlike.StringZ(null_value=null_value)
    assert field.from_bytes(b"NULL\x00") is None
コード例 #5
0
def test_stringz_load_multibyte():
    """Test loading multibyte strings with a terminating null."""
    field = stringlike.StringZ(encoding="utf-16")
    assert field.from_bytes(b"\xff\xfeA\x00b\x00C\x00d\x00\x00\x00") == "AbCd"
    assert field.from_bytes(b"\xfe\xff\x00A\x00b\x00C\x00d\x00\x00") == "AbCd"
コード例 #6
0
def test_stringz__dump_multibyte():
    """Basic multibyte test dump."""
    field = stringlike.StringZ(encoding="utf-32-le")
    assert (field.to_bytes("AbC") ==
            b"A\x00\x00\x00b\x00\x00\x00C\x00\x00\x00\x00\x00\x00\x00")
コード例 #7
0
def test_stringz__dump_basic():
    """Basic test of StringZ dumping."""
    field = stringlike.StringZ(encoding="utf-8")
    assert field.to_bytes(
        r"¯\_(ツ)_/¯") == b"\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf\0"
コード例 #8
0
def test_stringz__load_eof_before_null():
    """Crash if we hit the end of the data before we get a null byte."""
    field = stringlike.StringZ(encoding="utf-8")
    with pytest.raises(errors.DeserializationError):
        assert field.from_bytes(b"\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf")
コード例 #9
0
def test_stringz__load_basic():
    """Basic test of StringZ loading."""
    field = stringlike.StringZ(encoding="utf-8")
    assert field.from_bytes(
        b"\xc2\xaf\\_(\xe3\x83\x84)_/\xc2\xaf\0") == r"¯\_(ツ)_/¯"