aboutsummaryrefslogtreecommitdiffstats
path: root/test/netlib/test_strutils.py
blob: 84a0dded669c2dd7a26b308395ab83f52a82d2f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
# coding=utf-8
import six

from netlib import strutils, tutils


def test_native():
    with tutils.raises(TypeError):
        strutils.native(42)
    if six.PY2:
        assert strutils.native(u"foo") == b"foo"
        assert strutils.native(b"foo") == b"foo"
    else:
        assert strutils.native(u"foo") == u"foo"
        assert strutils.native(b"foo") == u"foo"


def test_clean_bin():
    assert strutils.clean_bin(b"one") == b"one"
    assert strutils.clean_bin(b"\00ne") == b".ne"
    assert strutils.clean_bin(b"\nne") == b"\nne"
    assert strutils.clean_bin(b"\nne", False) == b".ne"
    assert strutils.clean_bin(u"\u2605".encode("utf8")) == b"..."

    assert strutils.clean_bin(u"one") == u"one"
    assert strutils.clean_bin(u"\00ne") == u".ne"
    assert strutils.clean_bin(u"\nne") == u"\nne"
    assert strutils.clean_bin(u"\nne", False) == u".ne"
    assert strutils.clean_bin(u"\u2605") == u"\u2605"


def test_safe_subn():
    assert strutils.safe_subn("foo", u"bar", "\xc2foo")


def test_bytes_to_escaped_str():
    assert strutils.bytes_to_escaped_str(b"foo") == "foo"
    assert strutils.bytes_to_escaped_str(b"\b") == r"\x08"
    assert strutils.bytes_to_escaped_str(br"&!?=\)") == r"&!?=\\)"
    assert strutils.bytes_to_escaped_str(b'\xc3\xbc') == r"\xc3\xbc"
    assert strutils.bytes_to_escaped_str(b"'") == r"\'"
    assert strutils.bytes_to_escaped_str(b'"') == r'"'

    with tutils.raises(ValueError):
        strutils.bytes_to_escaped_str(u"such unicode")


def test_escaped_str_to_bytes():
    assert strutils.escaped_str_to_bytes("foo") == b"foo"
    assert strutils.escaped_str_to_bytes("\x08") == b"\b"
    assert strutils.escaped_str_to_bytes("&!?=\\\\)") == br"&!?=\)"
    assert strutils.escaped_str_to_bytes("ü") == b'\xc3\xbc'
    assert strutils.escaped_str_to_bytes(u"\\x08") == b"\b"
    assert strutils.escaped_str_to_bytes(u"&!?=\\\\)") == br"&!?=\)"
    assert strutils.escaped_str_to_bytes(u"ü") == b'\xc3\xbc'

    if six.PY2:
        with tutils.raises(ValueError):
            strutils.escaped_str_to_bytes(42)
    else:
        with tutils.raises(ValueError):
            strutils.escaped_str_to_bytes(b"very byte")


def test_isBin():
    assert not strutils.isBin("testing\n\r")
    assert strutils.isBin("testing\x01")
    assert strutils.isBin("testing\x0e")
    assert strutils.isBin("testing\x7f")


def test_isXml():
    assert not strutils.isXML("foo")
    assert strutils.isXML("<foo")
    assert strutils.isXML("  \n<foo")


def test_clean_hanging_newline():
    s = "foo\n"
    assert strutils.clean_hanging_newline(s) == "foo"
    assert strutils.clean_hanging_newline("foo") == "foo"


def test_hexdump():
    assert list(strutils.hexdump(b"one\0" * 10))