File: test_comm.py

package info (click to toggle)
python-oslo.privsep 3.8.0-1
  • links: PTS, VCS
  • area: main
  • in suites: experimental
  • size: 472 kB
  • sloc: python: 1,517; makefile: 28; sh: 12
file content (104 lines) | stat: -rw-r--r-- 2,662 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
# Copyright 2015 Rackspace Inc.
#
#    Licensed under the Apache License, Version 2.0 (the "License"); you may
#    not use this file except in compliance with the License. You may obtain
#    a copy of the License at
#
#         http://www.apache.org/licenses/LICENSE-2.0
#
#    Unless required by applicable law or agreed to in writing, software
#    distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
#    WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
#    License for the specific language governing permissions and limitations
#    under the License.

import io

from oslotest import base

from oslo_privsep import comm


class BufSock:
    def __init__(self):
        self.readpos = 0
        self.buf = io.BytesIO()

    def recv(self, bufsize):
        if self.buf.closed:
            return b''
        self.buf.seek(self.readpos, 0)
        data = self.buf.read(bufsize)
        self.readpos += len(data)
        return data

    def sendall(self, data):
        self.buf.seek(0, 2)
        self.buf.write(data)

    def shutdown(self, _flag):
        self.buf.close()


class TestSerialization(base.BaseTestCase):
    def setUp(self):
        super().setUp()

        sock = BufSock()

        self.input = comm.Serializer(sock)
        self.output = iter(comm.Deserializer(sock))

    def send(self, data):
        self.input.send(data)
        return next(self.output)

    def assertSendable(self, value):
        self.assertEqual(value, self.send(value))

    def test_none(self):
        self.assertSendable(None)

    def test_bool(self):
        self.assertSendable(True)
        self.assertSendable(False)

    def test_int(self):
        self.assertSendable(42)
        self.assertSendable(-84)

    def test_bytes(self):
        data = b'\x00\x01\x02\xfd\xfe\xff'
        self.assertSendable(data)

    def test_unicode(self):
        data = '\u4e09\u9df9\udc82'
        self.assertSendable(data)

    def test_tuple(self):
        self.assertSendable((1, 'foo'))

    def test_list(self):
        # NB! currently lists get converted to tuples by serialization.
        self.assertEqual((1, 'foo'), self.send([1, 'foo']))

    def test_dict(self):
        self.assertSendable(
            {
                'a': 'b',
                1: 2,
                None: None,
                (1, 2): (3, 4),
            }
        )

    def test_badobj(self):
        class UnknownClass:
            pass

        obj = UnknownClass()
        self.assertRaises(TypeError, self.send, obj)

    def test_eof(self):
        self.input.close()
        self.assertRaises(StopIteration, next, self.output)