Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 1 | """ |
| 2 | Tests for uu module. |
| 3 | Nick Mathewson |
| 4 | """ |
| 5 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 6 | import unittest |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 7 | from test import support |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 8 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 9 | import sys, os |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 10 | import uu |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 11 | from io import BytesIO |
| 12 | import io |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 13 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 14 | plaintext = b"The smooth-scaled python crept over the sleeping dog\n" |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 15 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 16 | encodedtext = b"""\ |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 17 | M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P |
| 18 | (:6YG(&1O9PH """ |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 19 | |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 20 | # Stolen from io.py |
| 21 | class FakeIO(io.TextIOWrapper): |
| 22 | """Text I/O implementation using an in-memory buffer. |
| 23 | |
| 24 | Can be a used as a drop-in replacement for sys.stdin and sys.stdout. |
| 25 | """ |
| 26 | |
| 27 | # XXX This is really slow, but fully functional |
| 28 | |
| 29 | def __init__(self, initial_value="", encoding="utf-8", |
| 30 | errors="strict", newline="\n"): |
| 31 | super(FakeIO, self).__init__(io.BytesIO(), |
| 32 | encoding=encoding, |
| 33 | errors=errors, |
| 34 | newline=newline) |
Benjamin Peterson | 4fa88fa | 2009-03-04 00:14:51 +0000 | [diff] [blame] | 35 | self._encoding = encoding |
| 36 | self._errors = errors |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 37 | if initial_value: |
| 38 | if not isinstance(initial_value, str): |
| 39 | initial_value = str(initial_value) |
| 40 | self.write(initial_value) |
| 41 | self.seek(0) |
| 42 | |
| 43 | def getvalue(self): |
| 44 | self.flush() |
| 45 | return self.buffer.getvalue().decode(self._encoding, self._errors) |
| 46 | |
| 47 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 48 | def encodedtextwrapped(mode, filename): |
| 49 | return (bytes("begin %03o %s\n" % (mode, filename), "ascii") + |
| 50 | encodedtext + b"\n \nend\n") |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 51 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 52 | class UUTest(unittest.TestCase): |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 53 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 54 | def test_encode(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 55 | inp = io.BytesIO(plaintext) |
| 56 | out = io.BytesIO() |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 57 | uu.encode(inp, out, "t1") |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 58 | self.assertEqual(out.getvalue(), encodedtextwrapped(0o666, "t1")) |
| 59 | inp = io.BytesIO(plaintext) |
| 60 | out = io.BytesIO() |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 61 | uu.encode(inp, out, "t1", 0o644) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 62 | self.assertEqual(out.getvalue(), encodedtextwrapped(0o644, "t1")) |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 63 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 64 | def test_decode(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 65 | inp = io.BytesIO(encodedtextwrapped(0o666, "t1")) |
| 66 | out = io.BytesIO() |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 67 | uu.decode(inp, out) |
| 68 | self.assertEqual(out.getvalue(), plaintext) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 69 | inp = io.BytesIO( |
| 70 | b"UUencoded files may contain many lines,\n" + |
| 71 | b"even some that have 'begin' in them.\n" + |
| 72 | encodedtextwrapped(0o666, "t1") |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 73 | ) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 74 | out = io.BytesIO() |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 75 | uu.decode(inp, out) |
| 76 | self.assertEqual(out.getvalue(), plaintext) |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 77 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 78 | def test_truncatedinput(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 79 | inp = io.BytesIO(b"begin 644 t1\n" + encodedtext) |
| 80 | out = io.BytesIO() |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 81 | try: |
| 82 | uu.decode(inp, out) |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 83 | self.fail("No exception raised") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 84 | except uu.Error as e: |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 85 | self.assertEqual(str(e), "Truncated input file") |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 86 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 87 | def test_missingbegin(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 88 | inp = io.BytesIO(b"") |
| 89 | out = io.BytesIO() |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 90 | try: |
| 91 | uu.decode(inp, out) |
Andrew Svetlov | 737fb89 | 2012-12-18 21:14:22 +0200 | [diff] [blame] | 92 | self.fail("No exception raised") |
Guido van Rossum | b940e11 | 2007-01-10 16:19:56 +0000 | [diff] [blame] | 93 | except uu.Error as e: |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 94 | self.assertEqual(str(e), "No valid begin line found in input file") |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 95 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 96 | class UUStdIOTest(unittest.TestCase): |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 97 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 98 | def setUp(self): |
| 99 | self.stdin = sys.stdin |
| 100 | self.stdout = sys.stdout |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 101 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 102 | def tearDown(self): |
| 103 | sys.stdin = self.stdin |
| 104 | sys.stdout = self.stdout |
Barry Warsaw | d179570 | 2001-08-17 20:00:11 +0000 | [diff] [blame] | 105 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 106 | def test_encode(self): |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 107 | sys.stdin = FakeIO(plaintext.decode("ascii")) |
| 108 | sys.stdout = FakeIO() |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 109 | uu.encode("-", "-", "t1", 0o666) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 110 | self.assertEqual(sys.stdout.getvalue(), |
| 111 | encodedtextwrapped(0o666, "t1").decode("ascii")) |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 112 | |
| 113 | def test_decode(self): |
Alexandre Vassalotti | 794652d | 2008-06-11 22:58:36 +0000 | [diff] [blame] | 114 | sys.stdin = FakeIO(encodedtextwrapped(0o666, "t1").decode("ascii")) |
| 115 | sys.stdout = FakeIO() |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 116 | uu.decode("-", "-") |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 117 | stdout = sys.stdout |
| 118 | sys.stdout = self.stdout |
| 119 | sys.stdin = self.stdin |
| 120 | self.assertEqual(stdout.getvalue(), plaintext.decode("ascii")) |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 121 | |
| 122 | class UUFileTest(unittest.TestCase): |
| 123 | |
| 124 | def _kill(self, f): |
| 125 | # close and remove file |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 126 | if f is None: |
| 127 | return |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 128 | try: |
| 129 | f.close() |
| 130 | except (SystemExit, KeyboardInterrupt): |
| 131 | raise |
| 132 | except: |
| 133 | pass |
| 134 | try: |
| 135 | os.unlink(f.name) |
| 136 | except (SystemExit, KeyboardInterrupt): |
| 137 | raise |
| 138 | except: |
| 139 | pass |
| 140 | |
| 141 | def setUp(self): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 142 | self.tmpin = support.TESTFN + "i" |
| 143 | self.tmpout = support.TESTFN + "o" |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 144 | |
| 145 | def tearDown(self): |
| 146 | del self.tmpin |
| 147 | del self.tmpout |
| 148 | |
| 149 | def test_encode(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 150 | fin = fout = None |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 151 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 152 | support.unlink(self.tmpin) |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 153 | fin = open(self.tmpin, 'wb') |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 154 | fin.write(plaintext) |
| 155 | fin.close() |
| 156 | |
Thomas Wouters | b213704 | 2007-02-01 18:02:27 +0000 | [diff] [blame] | 157 | fin = open(self.tmpin, 'rb') |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 158 | fout = open(self.tmpout, 'wb') |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 159 | uu.encode(fin, fout, self.tmpin, mode=0o644) |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 160 | fin.close() |
| 161 | fout.close() |
| 162 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 163 | fout = open(self.tmpout, 'rb') |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 164 | s = fout.read() |
| 165 | fout.close() |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 166 | self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) |
Walter Dörwald | 705fd47 | 2005-11-21 18:55:56 +0000 | [diff] [blame] | 167 | |
| 168 | # in_file and out_file as filenames |
Guido van Rossum | cd16bf6 | 2007-06-13 18:07:49 +0000 | [diff] [blame] | 169 | uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 170 | fout = open(self.tmpout, 'rb') |
Walter Dörwald | 705fd47 | 2005-11-21 18:55:56 +0000 | [diff] [blame] | 171 | s = fout.read() |
| 172 | fout.close() |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 173 | self.assertEqual(s, encodedtextwrapped(0o644, self.tmpin)) |
Walter Dörwald | 705fd47 | 2005-11-21 18:55:56 +0000 | [diff] [blame] | 174 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 175 | finally: |
| 176 | self._kill(fin) |
| 177 | self._kill(fout) |
| 178 | |
| 179 | def test_decode(self): |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 180 | f = None |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 181 | try: |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 182 | support.unlink(self.tmpin) |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 183 | f = open(self.tmpin, 'wb') |
| 184 | f.write(encodedtextwrapped(0o644, self.tmpout)) |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 185 | f.close() |
| 186 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 187 | f = open(self.tmpin, 'rb') |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 188 | uu.decode(f) |
| 189 | f.close() |
| 190 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 191 | f = open(self.tmpout, 'rb') |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 192 | s = f.read() |
| 193 | f.close() |
| 194 | self.assertEqual(s, plaintext) |
| 195 | # XXX is there an xp way to verify the mode? |
| 196 | finally: |
| 197 | self._kill(f) |
| 198 | |
Antoine Pitrou | f569826 | 2010-10-31 16:04:14 +0000 | [diff] [blame] | 199 | def test_decode_filename(self): |
| 200 | f = None |
| 201 | try: |
| 202 | support.unlink(self.tmpin) |
| 203 | f = open(self.tmpin, 'wb') |
| 204 | f.write(encodedtextwrapped(0o644, self.tmpout)) |
| 205 | f.close() |
| 206 | |
| 207 | uu.decode(self.tmpin) |
| 208 | |
| 209 | f = open(self.tmpout, 'rb') |
| 210 | s = f.read() |
| 211 | f.close() |
| 212 | self.assertEqual(s, plaintext) |
| 213 | finally: |
| 214 | self._kill(f) |
| 215 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 216 | def test_decodetwice(self): |
| 217 | # Verify that decode() will refuse to overwrite an existing file |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 218 | f = None |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 219 | try: |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 220 | f = io.BytesIO(encodedtextwrapped(0o644, self.tmpout)) |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 221 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 222 | f = open(self.tmpin, 'rb') |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 223 | uu.decode(f) |
| 224 | f.close() |
| 225 | |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 226 | f = open(self.tmpin, 'rb') |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 227 | self.assertRaises(uu.Error, uu.decode, f) |
| 228 | f.close() |
| 229 | finally: |
| 230 | self._kill(f) |
| 231 | |
| 232 | def test_main(): |
Benjamin Peterson | ee8712c | 2008-05-20 21:35:26 +0000 | [diff] [blame] | 233 | support.run_unittest(UUTest, |
Guido van Rossum | 34d1928 | 2007-08-09 01:03:29 +0000 | [diff] [blame] | 234 | UUStdIOTest, |
| 235 | UUFileTest, |
| 236 | ) |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 237 | |
| 238 | if __name__=="__main__": |
| 239 | test_main() |