|
1 """ |
|
2 Tests for uu module. |
|
3 Nick Mathewson |
|
4 """ |
|
5 |
|
6 import unittest |
|
7 from test import test_support |
|
8 |
|
9 import sys, os, uu, cStringIO |
|
10 import uu |
|
11 from StringIO import StringIO |
|
12 |
|
13 plaintext = "The smooth-scaled python crept over the sleeping dog\n" |
|
14 |
|
15 encodedtext = """\ |
|
16 M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P |
|
17 (:6YG(&1O9PH """ |
|
18 |
|
19 encodedtextwrapped = "begin %03o %s\n" + encodedtext.replace("%", "%%") + "\n \nend\n" |
|
20 |
|
21 class UUTest(unittest.TestCase): |
|
22 |
|
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")) |
|
32 |
|
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) |
|
46 |
|
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") |
|
55 |
|
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") |
|
64 |
|
65 class UUStdIOTest(unittest.TestCase): |
|
66 |
|
67 def setUp(self): |
|
68 self.stdin = sys.stdin |
|
69 self.stdout = sys.stdout |
|
70 |
|
71 def tearDown(self): |
|
72 sys.stdin = self.stdin |
|
73 sys.stdout = self.stdout |
|
74 |
|
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 |
|
132 # in_file and out_file as filenames |
|
133 uu.encode(self.tmpin, self.tmpout, mode=0644) |
|
134 fout = open(self.tmpout, 'r') |
|
135 s = fout.read() |
|
136 fout.close() |
|
137 self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin)) |
|
138 |
|
139 finally: |
|
140 self._kill(fin) |
|
141 self._kill(fout) |
|
142 |
|
143 def test_decode(self): |
|
144 try: |
|
145 f = open(self.tmpin, 'wb') |
|
146 f.write(encodedtextwrapped % (0644, self.tmpout)) |
|
147 f.close() |
|
148 |
|
149 f = open(self.tmpin, 'rb') |
|
150 uu.decode(f) |
|
151 f.close() |
|
152 |
|
153 f = open(self.tmpout, 'r') |
|
154 s = f.read() |
|
155 f.close() |
|
156 self.assertEqual(s, plaintext) |
|
157 # XXX is there an xp way to verify the mode? |
|
158 finally: |
|
159 self._kill(f) |
|
160 |
|
161 def test_decodetwice(self): |
|
162 # Verify that decode() will refuse to overwrite an existing file |
|
163 try: |
|
164 f = cStringIO.StringIO(encodedtextwrapped % (0644, self.tmpout)) |
|
165 |
|
166 f = open(self.tmpin, 'rb') |
|
167 uu.decode(f) |
|
168 f.close() |
|
169 |
|
170 f = open(self.tmpin, 'rb') |
|
171 self.assertRaises(uu.Error, uu.decode, f) |
|
172 f.close() |
|
173 finally: |
|
174 self._kill(f) |
|
175 |
|
176 def test_main(): |
|
177 test_support.run_unittest(UUTest, UUStdIOTest, UUFileTest) |
|
178 |
|
179 if __name__=="__main__": |
|
180 test_main() |