test_uu.py 6.29 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 83
        try:
            uu.decode(inp, out)
            self.fail("No exception thrown")
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 92
        try:
            uu.decode(inp, out)
            self.fail("No exception thrown")
93
        except uu.Error as e:
94 95 96 97 98 99 100 101 102 103 104 105 106
            self.assertEqual(str(e), "No valid begin line found in input file")

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):
107 108
        sys.stdin = FakeIO(plaintext.decode("ascii"))
        sys.stdout = FakeIO()
109
        uu.encode("-", "-", "t1", 0o666)
110 111
        self.assertEqual(sys.stdout.getvalue(),
                         encodedtextwrapped(0o666, "t1").decode("ascii"))
112 113

    def test_decode(self):
114 115
        sys.stdin = FakeIO(encodedtextwrapped(0o666, "t1").decode("ascii"))
        sys.stdout = FakeIO()
116
        uu.decode("-", "-")
117 118 119 120
        stdout = sys.stdout
        sys.stdout = self.stdout
        sys.stdin = self.stdin
        self.assertEqual(stdout.getvalue(), plaintext.decode("ascii"))
121 122 123 124 125

class UUFileTest(unittest.TestCase):

    def _kill(self, f):
        # close and remove file
126 127
        if f is None:
            return
128 129 130 131 132 133 134 135 136 137 138 139 140 141
        try:
            f.close()
        except (SystemExit, KeyboardInterrupt):
            raise
        except:
            pass
        try:
            os.unlink(f.name)
        except (SystemExit, KeyboardInterrupt):
            raise
        except:
            pass

    def setUp(self):
142 143
        self.tmpin  = support.TESTFN + "i"
        self.tmpout = support.TESTFN + "o"
144 145 146 147 148 149

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

    def test_encode(self):
150
        fin = fout = None
151
        try:
152
            support.unlink(self.tmpin)
153
            fin = open(self.tmpin, 'wb')
154 155 156
            fin.write(plaintext)
            fin.close()

157
            fin = open(self.tmpin, 'rb')
158
            fout = open(self.tmpout, 'wb')
159
            uu.encode(fin, fout, self.tmpin, mode=0o644)
160 161 162
            fin.close()
            fout.close()

163
            fout = open(self.tmpout, 'rb')
164 165
            s = fout.read()
            fout.close()
166
            self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
167 168

            # in_file and out_file as filenames
169
            uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644)
170
            fout = open(self.tmpout, 'rb')
171 172
            s = fout.read()
            fout.close()
173
            self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin))
174

175 176 177 178 179
        finally:
            self._kill(fin)
            self._kill(fout)

    def test_decode(self):
180
        f = None
181
        try:
182
            support.unlink(self.tmpin)
183 184
            f = open(self.tmpin, 'wb')
            f.write(encodedtextwrapped(0o644, self.tmpout))
185 186
            f.close()

187
            f = open(self.tmpin, 'rb')
188 189 190
            uu.decode(f)
            f.close()

191
            f = open(self.tmpout, 'rb')
192 193 194 195 196 197 198 199 200
            s = f.read()
            f.close()
            self.assertEqual(s, plaintext)
            # XXX is there an xp way to verify the mode?
        finally:
            self._kill(f)

    def test_decodetwice(self):
        # Verify that decode() will refuse to overwrite an existing file
201
        f = None
202
        try:
203
            f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout))
204

205
            f = open(self.tmpin, 'rb')
206 207 208
            uu.decode(f)
            f.close()

209
            f = open(self.tmpin, 'rb')
210 211 212 213 214 215
            self.assertRaises(uu.Error, uu.decode, f)
            f.close()
        finally:
            self._kill(f)

def test_main():
216
    support.run_unittest(UUTest,
217 218 219
                              UUStdIOTest,
                              UUFileTest,
                              )
220 221 222

if __name__=="__main__":
    test_main()