test_uu.py 7.39 KB
Newer Older
1 2 3 4 5
"""
Tests for uu module.
Nick Mathewson
"""

6
import unittest
7
from test import support
8

9
import sys, os
10
import uu
11 12
from io import BytesIO
import io
13

14
plaintext = b"The smooth-scaled python crept over the sleeping dog\n"
15

16
encodedtext = b"""\
17 18
M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
(:6YG(&1O9PH """
19

20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
# Stolen from io.py
class FakeIO(io.TextIOWrapper):
    """Text I/O implementation using an in-memory buffer.

    Can be a used as a drop-in replacement for sys.stdin and sys.stdout.
    """

    # XXX This is really slow, but fully functional

    def __init__(self, initial_value="", encoding="utf-8",
                 errors="strict", newline="\n"):
        super(FakeIO, self).__init__(io.BytesIO(),
                                     encoding=encoding,
                                     errors=errors,
                                     newline=newline)
35 36
        self._encoding = encoding
        self._errors = errors
37 38 39 40 41 42 43 44 45 46 47
        if initial_value:
            if not isinstance(initial_value, str):
                initial_value = str(initial_value)
            self.write(initial_value)
            self.seek(0)

    def getvalue(self):
        self.flush()
        return self.buffer.getvalue().decode(self._encoding, self._errors)


48 49 50
def encodedtextwrapped(mode, filename):
    return (bytes("begin %03o %s\n" % (mode, filename), "ascii") +
            encodedtext + b"\n \nend\n")
51 52 53 54

class UUTest(unittest.TestCase):

    def test_encode(self):
55 56
        inp = io.BytesIO(plaintext)
        out = io.BytesIO()
57
        uu.encode(inp, out, "t1")
58 59 60
        self.assertEqual(out.getvalue(), encodedtextwrapped(0o666, "t1"))
        inp = io.BytesIO(plaintext)
        out = io.BytesIO()
61
        uu.encode(inp, out, "t1", 0o644)
62
        self.assertEqual(out.getvalue(), encodedtextwrapped(0o644, "t1"))
63 64

    def test_decode(self):
65 66
        inp = io.BytesIO(encodedtextwrapped(0o666, "t1"))
        out = io.BytesIO()
67 68
        uu.decode(inp, out)
        self.assertEqual(out.getvalue(), plaintext)
69 70 71 72
        inp = io.BytesIO(
            b"UUencoded files may contain many lines,\n" +
            b"even some that have 'begin' in them.\n" +
            encodedtextwrapped(0o666, "t1")
73
        )
74
        out = io.BytesIO()
75 76 77 78
        uu.decode(inp, out)
        self.assertEqual(out.getvalue(), plaintext)

    def test_truncatedinput(self):
79 80
        inp = io.BytesIO(b"begin 644 t1\n" + encodedtext)
        out = io.BytesIO()
81 82
        try:
            uu.decode(inp, out)
83
            self.fail("No exception raised")
84
        except uu.Error as e:
85 86 87
            self.assertEqual(str(e), "Truncated input file")

    def test_missingbegin(self):
88 89
        inp = io.BytesIO(b"")
        out = io.BytesIO()
90 91
        try:
            uu.decode(inp, out)
92
            self.fail("No exception raised")
93
        except uu.Error as e:
94 95
            self.assertEqual(str(e), "No valid begin line found in input file")

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117
    def test_garbage_padding(self):
        # Issue #22406
        encodedtext = (
            b"begin 644 file\n"
            # length 1; bits 001100 111111 111111 111111
            b"\x21\x2C\x5F\x5F\x5F\n"
            b"\x20\n"
            b"end\n"
        )
        plaintext = b"\x33"  # 00110011

        with self.subTest("uu.decode()"):
            inp = io.BytesIO(encodedtext)
            out = io.BytesIO()
            uu.decode(inp, out, quiet=True)
            self.assertEqual(out.getvalue(), plaintext)

        with self.subTest("uu_codec"):
            import codecs
            decoded = codecs.decode(encodedtext, "uu_codec")
            self.assertEqual(decoded, plaintext)

118 119 120 121 122 123 124 125 126 127 128
class UUStdIOTest(unittest.TestCase):

    def setUp(self):
        self.stdin = sys.stdin
        self.stdout = sys.stdout

    def tearDown(self):
        sys.stdin = self.stdin
        sys.stdout = self.stdout

    def test_encode(self):
129 130
        sys.stdin = FakeIO(plaintext.decode("ascii"))
        sys.stdout = FakeIO()
131
        uu.encode("-", "-", "t1", 0o666)
132 133
        self.assertEqual(sys.stdout.getvalue(),
                         encodedtextwrapped(0o666, "t1").decode("ascii"))
134 135

    def test_decode(self):
136 137
        sys.stdin = FakeIO(encodedtextwrapped(0o666, "t1").decode("ascii"))
        sys.stdout = FakeIO()
138
        uu.decode("-", "-")
139 140 141 142
        stdout = sys.stdout
        sys.stdout = self.stdout
        sys.stdin = self.stdin
        self.assertEqual(stdout.getvalue(), plaintext.decode("ascii"))
143 144 145 146 147

class UUFileTest(unittest.TestCase):

    def _kill(self, f):
        # close and remove file
148 149
        if f is None:
            return
150 151 152 153 154 155 156 157 158 159 160 161 162 163
        try:
            f.close()
        except (SystemExit, KeyboardInterrupt):
            raise
        except:
            pass
        try:
            os.unlink(f.name)
        except (SystemExit, KeyboardInterrupt):
            raise
        except:
            pass

    def setUp(self):
164 165
        self.tmpin  = support.TESTFN + "i"
        self.tmpout = support.TESTFN + "o"
166 167 168 169 170 171

    def tearDown(self):
        del self.tmpin
        del self.tmpout

    def test_encode(self):
172
        fin = fout = None
173
        try:
174
            support.unlink(self.tmpin)
175
            fin = open(self.tmpin, 'wb')
176 177 178
            fin.write(plaintext)
            fin.close()

179
            fin = open(self.tmpin, 'rb')
180
            fout = open(self.tmpout, 'wb')
181
            uu.encode(fin, fout, self.tmpin, mode=0o644)
182 183 184
            fin.close()
            fout.close()

185
            fout = open(self.tmpout, 'rb')
186 187
            s = fout.read()
            fout.close()
188
            self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
189 190

            # in_file and out_file as filenames
191
            uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644)
192
            fout = open(self.tmpout, 'rb')
193 194
            s = fout.read()
            fout.close()
195
            self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
196

197 198 199 200 201
        finally:
            self._kill(fin)
            self._kill(fout)

    def test_decode(self):
202
        f = None
203
        try:
204
            support.unlink(self.tmpin)
205 206
            f = open(self.tmpin, 'wb')
            f.write(encodedtextwrapped(0o644, self.tmpout))
207 208
            f.close()

209
            f = open(self.tmpin, 'rb')
210 211 212
            uu.decode(f)
            f.close()

213
            f = open(self.tmpout, 'rb')
214 215 216 217 218 219 220
            s = f.read()
            f.close()
            self.assertEqual(s, plaintext)
            # XXX is there an xp way to verify the mode?
        finally:
            self._kill(f)

221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
    def test_decode_filename(self):
        f = None
        try:
            support.unlink(self.tmpin)
            f = open(self.tmpin, 'wb')
            f.write(encodedtextwrapped(0o644, self.tmpout))
            f.close()

            uu.decode(self.tmpin)

            f = open(self.tmpout, 'rb')
            s = f.read()
            f.close()
            self.assertEqual(s, plaintext)
        finally:
            self._kill(f)

238 239
    def test_decodetwice(self):
        # Verify that decode() will refuse to overwrite an existing file
240
        f = None
241
        try:
242
            f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout))
243

244
            f = open(self.tmpin, 'rb')
245 246 247
            uu.decode(f)
            f.close()

248
            f = open(self.tmpin, 'rb')
249 250 251 252 253 254
            self.assertRaises(uu.Error, uu.decode, f)
            f.close()
        finally:
            self._kill(f)

def test_main():
255
    support.run_unittest(UUTest,
256 257 258
                              UUStdIOTest,
                              UUFileTest,
                              )
259 260 261

if __name__=="__main__":
    test_main()