blob: 26d1a7ab4fe96f6a4d1c04804b37de1c718eb010 [file] [log] [blame]
Tim Peters48dacc62001-07-11 21:43:42 +00001"""
2Tests for uu module.
3Nick Mathewson
4"""
5
Walter Dörwald411e5a22003-05-06 08:57:41 +00006import unittest
7from test import test_support
8
9import sys, os, uu, cStringIO
Tim Peters48dacc62001-07-11 21:43:42 +000010import uu
11from StringIO import StringIO
12
Walter Dörwald411e5a22003-05-06 08:57:41 +000013plaintext = "The smooth-scaled python crept over the sleeping dog\n"
14
15encodedtext = """\
Tim Peters48dacc62001-07-11 21:43:42 +000016M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
17(:6YG(&1O9PH """
Tim Peters48dacc62001-07-11 21:43:42 +000018
Walter Dörwald411e5a22003-05-06 08:57:41 +000019encodedtextwrapped = "begin %03o %s\n" + encodedtext.replace("%", "%%") + "\n \nend\n"
Tim Peters48dacc62001-07-11 21:43:42 +000020
Walter Dörwald411e5a22003-05-06 08:57:41 +000021class UUTest(unittest.TestCase):
Tim Peters48dacc62001-07-11 21:43:42 +000022
Walter Dörwald411e5a22003-05-06 08:57:41 +000023 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 Peters48dacc62001-07-11 21:43:42 +000032
Walter Dörwald411e5a22003-05-06 08:57:41 +000033 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 Peters48dacc62001-07-11 21:43:42 +000046
Walter Dörwald411e5a22003-05-06 08:57:41 +000047 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 Peters48dacc62001-07-11 21:43:42 +000055
Walter Dörwald411e5a22003-05-06 08:57:41 +000056 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 Peters48dacc62001-07-11 21:43:42 +000064
Walter Dörwald411e5a22003-05-06 08:57:41 +000065class UUStdIOTest(unittest.TestCase):
Tim Peters48dacc62001-07-11 21:43:42 +000066
Walter Dörwald411e5a22003-05-06 08:57:41 +000067 def setUp(self):
68 self.stdin = sys.stdin
69 self.stdout = sys.stdout
Tim Peters48dacc62001-07-11 21:43:42 +000070
Walter Dörwald411e5a22003-05-06 08:57:41 +000071 def tearDown(self):
72 sys.stdin = self.stdin
73 sys.stdout = self.stdout
Barry Warsawd1795702001-08-17 20:00:11 +000074
Walter Dörwald411e5a22003-05-06 08:57:41 +000075 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
90class 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
168def test_main():
169 test_support.run_unittest(UUTest, UUStdIOTest, UUFileTest)
170
171if __name__=="__main__":
172 test_main()