test_uu.py 6.22 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 35 36 37 38 39 40 41 42 43 44 45
# 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)
        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)


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

class UUTest(unittest.TestCase):

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

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

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

    def test_missingbegin(self):
86 87
        inp = io.BytesIO(b"")
        out = io.BytesIO()
88 89 90
        try:
            uu.decode(inp, out)
            self.fail("No exception thrown")
91
        except uu.Error as e:
92 93 94 95 96 97 98 99 100 101 102 103 104
            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):
105 106
        sys.stdin = FakeIO(plaintext.decode("ascii"))
        sys.stdout = FakeIO()
107
        uu.encode("-", "-", "t1", 0o666)
108 109
        self.assertEqual(sys.stdout.getvalue(),
                         encodedtextwrapped(0o666, "t1").decode("ascii"))
110 111

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

class UUFileTest(unittest.TestCase):

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

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

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

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

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

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

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

173 174 175 176 177
        finally:
            self._kill(fin)
            self._kill(fout)

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

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

189
            f = open(self.tmpout, 'rb')
190 191 192 193 194 195 196 197 198
            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
199
        f = None
200
        try:
201
            f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout))
202

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

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

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

if __name__=="__main__":
    test_main()