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 |
| 7 | from test import test_support |
| 8 | |
| 9 | import sys, os, uu, cStringIO |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 10 | import uu |
| 11 | from StringIO import StringIO |
| 12 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 13 | plaintext = "The smooth-scaled python crept over the sleeping dog\n" |
| 14 | |
| 15 | encodedtext = """\ |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 16 | M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P |
| 17 | (:6YG(&1O9PH """ |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 18 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 19 | encodedtextwrapped = "begin %03o %s\n" + encodedtext.replace("%", "%%") + "\n \nend\n" |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 20 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 21 | class UUTest(unittest.TestCase): |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 22 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 23 | def test_encode(self): |
| 24 | inp = cStringIO.StringIO(plaintext) |
| 25 | out = cStringIO.StringIO() |
| 26 | uu.encode(inp, out, "t1") |
| 27 | self.assertEqual(out.getvalue(), encodedtextwrapped % (0666, "t1")) |
| 28 | inp = cStringIO.StringIO(plaintext) |
| 29 | out = cStringIO.StringIO() |
| 30 | uu.encode(inp, out, "t1", 0644) |
| 31 | self.assertEqual(out.getvalue(), encodedtextwrapped % (0644, "t1")) |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 32 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 33 | def test_decode(self): |
| 34 | inp = cStringIO.StringIO(encodedtextwrapped % (0666, "t1")) |
| 35 | out = cStringIO.StringIO() |
| 36 | uu.decode(inp, out) |
| 37 | self.assertEqual(out.getvalue(), plaintext) |
| 38 | inp = cStringIO.StringIO( |
| 39 | "UUencoded files may contain many lines,\n" + |
| 40 | "even some that have 'begin' in them.\n" + |
| 41 | encodedtextwrapped % (0666, "t1") |
| 42 | ) |
| 43 | out = cStringIO.StringIO() |
| 44 | uu.decode(inp, out) |
| 45 | self.assertEqual(out.getvalue(), plaintext) |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 46 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 47 | def test_truncatedinput(self): |
| 48 | inp = cStringIO.StringIO("begin 644 t1\n" + encodedtext) |
| 49 | out = cStringIO.StringIO() |
| 50 | try: |
| 51 | uu.decode(inp, out) |
| 52 | self.fail("No exception thrown") |
| 53 | except uu.Error, e: |
| 54 | self.assertEqual(str(e), "Truncated input file") |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 55 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 56 | def test_missingbegin(self): |
| 57 | inp = cStringIO.StringIO("") |
| 58 | out = cStringIO.StringIO() |
| 59 | try: |
| 60 | uu.decode(inp, out) |
| 61 | self.fail("No exception thrown") |
| 62 | except uu.Error, e: |
| 63 | self.assertEqual(str(e), "No valid begin line found in input file") |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 64 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 65 | class UUStdIOTest(unittest.TestCase): |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 66 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 67 | def setUp(self): |
| 68 | self.stdin = sys.stdin |
| 69 | self.stdout = sys.stdout |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 70 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 71 | def tearDown(self): |
| 72 | sys.stdin = self.stdin |
| 73 | sys.stdout = self.stdout |
Barry Warsaw | d179570 | 2001-08-17 20:00:11 +0000 | [diff] [blame] | 74 | |
Walter Dörwald | 411e5a2 | 2003-05-06 08:57:41 +0000 | [diff] [blame] | 75 | def test_encode(self): |
| 76 | sys.stdin = cStringIO.StringIO(plaintext) |
| 77 | sys.stdout = cStringIO.StringIO() |
| 78 | uu.encode("-", "-", "t1", 0666) |
| 79 | self.assertEqual( |
| 80 | sys.stdout.getvalue(), |
| 81 | encodedtextwrapped % (0666, "t1") |
| 82 | ) |
| 83 | |
| 84 | def test_decode(self): |
| 85 | sys.stdin = cStringIO.StringIO(encodedtextwrapped % (0666, "t1")) |
| 86 | sys.stdout = cStringIO.StringIO() |
| 87 | uu.decode("-", "-") |
| 88 | self.assertEqual(sys.stdout.getvalue(), plaintext) |
| 89 | |
| 90 | class UUFileTest(unittest.TestCase): |
| 91 | |
| 92 | def _kill(self, f): |
| 93 | # close and remove file |
| 94 | try: |
| 95 | f.close() |
| 96 | except (SystemExit, KeyboardInterrupt): |
| 97 | raise |
| 98 | except: |
| 99 | pass |
| 100 | try: |
| 101 | os.unlink(f.name) |
| 102 | except (SystemExit, KeyboardInterrupt): |
| 103 | raise |
| 104 | except: |
| 105 | pass |
| 106 | |
| 107 | def setUp(self): |
| 108 | self.tmpin = test_support.TESTFN + "i" |
| 109 | self.tmpout = test_support.TESTFN + "o" |
| 110 | |
| 111 | def tearDown(self): |
| 112 | del self.tmpin |
| 113 | del self.tmpout |
| 114 | |
| 115 | def test_encode(self): |
| 116 | try: |
| 117 | fin = open(self.tmpin, 'wb') |
| 118 | fin.write(plaintext) |
| 119 | fin.close() |
| 120 | |
| 121 | fin = open(self.tmpin, 'rb') |
| 122 | fout = open(self.tmpout, 'w') |
| 123 | uu.encode(fin, fout, self.tmpin, mode=0644) |
| 124 | fin.close() |
| 125 | fout.close() |
| 126 | |
| 127 | fout = open(self.tmpout, 'r') |
| 128 | s = fout.read() |
| 129 | fout.close() |
| 130 | self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin)) |
| 131 | finally: |
| 132 | self._kill(fin) |
| 133 | self._kill(fout) |
| 134 | |
| 135 | def test_decode(self): |
| 136 | try: |
| 137 | f = open(self.tmpin, 'wb') |
| 138 | f.write(encodedtextwrapped % (0644, self.tmpout)) |
| 139 | f.close() |
| 140 | |
| 141 | f = open(self.tmpin, 'rb') |
| 142 | uu.decode(f) |
| 143 | f.close() |
| 144 | |
| 145 | f = open(self.tmpout, 'r') |
| 146 | s = f.read() |
| 147 | f.close() |
| 148 | self.assertEqual(s, plaintext) |
| 149 | # XXX is there an xp way to verify the mode? |
| 150 | finally: |
| 151 | self._kill(f) |
| 152 | |
| 153 | def test_decodetwice(self): |
| 154 | # Verify that decode() will refuse to overwrite an existing file |
| 155 | try: |
| 156 | f = cStringIO.StringIO(encodedtextwrapped % (0644, self.tmpout)) |
| 157 | |
| 158 | f = open(self.tmpin, 'rb') |
| 159 | uu.decode(f) |
| 160 | f.close() |
| 161 | |
| 162 | f = open(self.tmpin, 'rb') |
| 163 | self.assertRaises(uu.Error, uu.decode, f) |
| 164 | f.close() |
| 165 | finally: |
| 166 | self._kill(f) |
| 167 | |
| 168 | def test_main(): |
| 169 | test_support.run_unittest(UUTest, UUStdIOTest, UUFileTest) |
| 170 | |
| 171 | if __name__=="__main__": |
| 172 | test_main() |