File: utils_test.py

package info (click to toggle)
python-mitogen 0.3.26-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,456 kB
  • sloc: python: 22,134; sh: 183; makefile: 74; perl: 19; ansic: 18
file content (143 lines) | stat: -rw-r--r-- 4,823 bytes parent folder | download | duplicates (2)
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
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
import mitogen.core
import mitogen.master
import mitogen.utils
from mitogen.core import b

import testlib


def func0(router):
    return router


@mitogen.utils.with_router
def func(router):
    "Docstring of func"
    return router


class RunWithRouterTest(testlib.TestCase):
    # test_shutdown_on_exception
    # test_shutdown_on_success

    def test_run_with_broker(self):
        router = mitogen.utils.run_with_router(func0)
        self.assertIsInstance(router, mitogen.master.Router)
        self.assertFalse(testlib.threading__thread_is_alive(router.broker._thread))


class WithRouterTest(testlib.TestCase):
    def test_with_broker(self):
        router = func()
        self.assertIsInstance(router, mitogen.master.Router)
        self.assertFalse(testlib.threading__thread_is_alive(router.broker._thread))

    def test_with_broker_preserves_attributes(self):
        self.assertEqual(func.__doc__, 'Docstring of func')
        self.assertEqual(func.__name__, 'func')


class Dict(dict): pass
class List(list): pass
class Tuple(tuple): pass
class Unicode(mitogen.core.UnicodeType): pass
class Bytes(mitogen.core.BytesType): pass


class StubbornBytes(mitogen.core.BytesType):
    """
    A binary string type that persists through `bytes(...)`.

    Stand-in for `AnsibleUnsafeBytes()` in Ansible 7-9 (core 2.14-2.16), after
    fixes/mitigations for CVE-2023-5764.
    """
    if mitogen.core.PY3:
        def __bytes__(self): return self
        def __str__(self): return self.decode()
    else:
        def __str__(self): return self
        def __unicode__(self): return self.decode()

    def decode(self, encoding='utf-8', errors='strict'):
        s = super(StubbornBytes).encode(encoding=encoding, errors=errors)
        return StubbornText(s)


class StubbornText(mitogen.core.UnicodeType):
    """
    A text string type that persists through `unicode(...)` or `str(...)`.

    Stand-in for `AnsibleUnsafeText()` in Ansible 7-9 (core 2.14-2.16), after
    following fixes/mitigations for CVE-2023-5764.
    """
    if mitogen.core.PY3:
        def __bytes__(self): return self.encode()
        def __str__(self): return self
    else:
        def __str__(self): return self.encode()
        def __unicode__(self): return self

    def encode(self, encoding='utf-8', errors='strict'):
        s = super(StubbornText).encode(encoding=encoding, errors=errors)
        return StubbornBytes(s)


class CastTest(testlib.TestCase):
    def test_dict(self):
        self.assertEqual(type(mitogen.utils.cast({})), dict)
        self.assertEqual(type(mitogen.utils.cast(Dict())), dict)

    def test_nested_dict(self):
        specimen = mitogen.utils.cast(Dict({'k': Dict({'k2': 'v2'})}))
        self.assertEqual(type(specimen), dict)
        self.assertEqual(type(specimen['k']), dict)

    def test_list(self):
        self.assertEqual(type(mitogen.utils.cast([])), list)
        self.assertEqual(type(mitogen.utils.cast(List())), list)

    def test_nested_list(self):
        specimen = mitogen.utils.cast(List((0, 1, List((None,)))))
        self.assertEqual(type(specimen), list)
        self.assertEqual(type(specimen[2]), list)

    def test_tuple(self):
        self.assertEqual(type(mitogen.utils.cast(())), list)
        self.assertEqual(type(mitogen.utils.cast(Tuple())), list)

    def test_nested_tuple(self):
        specimen = mitogen.utils.cast(Tuple((0, 1, Tuple((None,)))))
        self.assertEqual(type(specimen), list)
        self.assertEqual(type(specimen[2]), list)

    def assertUnchanged(self, v):
        self.assertIs(mitogen.utils.cast(v), v)

    def test_passthrough(self):
        self.assertUnchanged(0)
        self.assertUnchanged(0.0)
        self.assertUnchanged(float('inf'))
        self.assertUnchanged(True)
        self.assertUnchanged(False)
        self.assertUnchanged(None)

    def test_unicode(self):
        self.assertEqual(type(mitogen.utils.cast(u'')), mitogen.core.UnicodeType)
        self.assertEqual(type(mitogen.utils.cast(Unicode())), mitogen.core.UnicodeType)

    def test_bytes(self):
        self.assertEqual(type(mitogen.utils.cast(b(''))), mitogen.core.BytesType)
        self.assertEqual(type(mitogen.utils.cast(Bytes())), mitogen.core.BytesType)

    def test_stubborn_types_raise(self):
        stubborn_bytes = StubbornBytes(b('abc'))
        self.assertIs(stubborn_bytes, mitogen.core.BytesType(stubborn_bytes))
        self.assertRaises(TypeError, mitogen.utils.cast, stubborn_bytes)

        stubborn_text = StubbornText(u'abc')
        self.assertIs(stubborn_text, mitogen.core.UnicodeType(stubborn_text))
        self.assertRaises(TypeError, mitogen.utils.cast, stubborn_text)

    def test_unknown(self):
        self.assertRaises(TypeError, mitogen.utils.cast, set())
        self.assertRaises(TypeError, mitogen.utils.cast, 4j)