blob: bb553a151ae78ea81c39404cb7e937548c2132b5 [file] [log] [blame]
Tim Peters48dacc62001-07-11 21:43:42 +00001"""
2Tests for uu module.
3Nick Mathewson
4"""
5
Barry Warsaw04f357c2002-07-23 19:04:11 +00006from test.test_support import verify, TestFailed, verbose, TESTFN
Tim Peters48dacc62001-07-11 21:43:42 +00007import sys, os
8import uu
9from StringIO import StringIO
10
11teststr = "The smooth-scaled python crept over the sleeping dog\n"
12expected = """\
13M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
14(:6YG(&1O9PH """
15encoded1 = "begin 666 t1\n"+expected+"\n \nend\n"
16if verbose:
17 print '1. encode file->file'
18inp = StringIO(teststr)
19out = StringIO()
20uu.encode(inp, out, "t1")
21verify(out.getvalue() == encoded1)
22inp = StringIO(teststr)
23out = StringIO()
24uu.encode(inp, out, "t1", 0644)
25verify(out.getvalue() == "begin 644 t1\n"+expected+"\n \nend\n")
26
27if verbose:
28 print '2. decode file->file'
29inp = StringIO(encoded1)
30out = StringIO()
31uu.decode(inp, out)
32verify(out.getvalue() == teststr)
33inp = StringIO("""UUencoded files may contain many lines,
34 even some that have 'begin' in them.\n"""+encoded1)
35out = StringIO()
36uu.decode(inp, out)
37verify(out.getvalue() == teststr)
38
39stdinsave = sys.stdin
40stdoutsave = sys.stdout
41try:
42 if verbose:
43 print '3. encode stdin->stdout'
44 sys.stdin = StringIO(teststr)
45 sys.stdout = StringIO()
46 uu.encode("-", "-", "t1", 0666)
47 verify(sys.stdout.getvalue() == encoded1)
48 if verbose:
49 print >>stdoutsave, '4. decode stdin->stdout'
50 sys.stdin = StringIO(encoded1)
51 sys.stdout = StringIO()
52 uu.decode("-", "-")
53 verify(sys.stdout.getvalue() == teststr)
54finally:
55 sys.stdin = stdinsave
56 sys.stdout = stdoutsave
57
58if verbose:
59 print '5. encode file->file'
60tmpIn = TESTFN + "i"
61tmpOut = TESTFN + "o"
62try:
Jack Jansenfffd7222001-08-03 13:04:03 +000063 fin = open(tmpIn, 'wb')
Tim Peters48dacc62001-07-11 21:43:42 +000064 fin.write(teststr)
65 fin.close()
66
Jack Jansenfffd7222001-08-03 13:04:03 +000067 fin = open(tmpIn, 'rb')
Tim Peters48dacc62001-07-11 21:43:42 +000068 fout = open(tmpOut, 'w')
69 uu.encode(fin, fout, tmpIn, mode=0644)
70 fin.close()
71 fout.close()
72
73 fout = open(tmpOut, 'r')
74 s = fout.read()
75 fout.close()
76 verify(s == 'begin 644 ' + tmpIn + '\n' + expected + '\n \nend\n')
77
78 os.unlink(tmpIn)
79 if verbose:
80 print '6. decode file-> file'
81 uu.decode(tmpOut)
Jack Jansenfffd7222001-08-03 13:04:03 +000082 fin = open(tmpIn, 'rb')
Tim Peters48dacc62001-07-11 21:43:42 +000083 s = fin.read()
84 fin.close()
85 verify(s == teststr)
86 # XXX is there an xp way to verify the mode?
87
88finally:
89 try:
90 fin.close()
91 except:
92 pass
93 try:
94 fout.close()
95 except:
96 pass
97 try:
98 os.unlink(tmpIn)
99 except:
100 pass
101 try:
102 os.unlink(tmpOut)
103 except:
104 pass
105
106if verbose:
107 print '7. error: truncated input'
108inp = StringIO("begin 644 t1\n"+expected)
109out = StringIO()
110try:
111 uu.decode(inp, out)
112 raise TestFailed("No exception thrown")
113except uu.Error, e:
114 verify(str(e) == 'Truncated input file')
115
116if verbose:
117 print '8. error: missing begin'
118inp = StringIO("")
119out = StringIO()
120try:
121 uu.decode(inp, out)
122 raise TestFailed("No exception thrown")
123except uu.Error, e:
124 verify(str(e) == 'No valid begin line found in input file')
Barry Warsawd1795702001-08-17 20:00:11 +0000125
126# Test to verify that decode() will refuse to overwrite an existing file
Guido van Rossum3b0a3292002-08-09 16:38:32 +0000127outfile = TESTFN + "out"
Barry Warsawd1795702001-08-17 20:00:11 +0000128inp = StringIO('Here is a message to be uuencoded')
129out = StringIO()
130uu.encode(inp, out, outfile)
131out.seek(0)
132try:
133 if verbose:
134 print '9. decode w/file not exists is okay'
135 uu.decode(out)
136 if not os.path.exists(outfile):
137 raise TestFailed('uudecode w/ out_file=None failed')
138 fp = open(outfile)
139 data = fp.read()
140 fp.close()
141 if data <> inp.getvalue():
142 raise TestFailed('uudecode stored something weird')
143 # Try to write it again, which should cause a failure
144 if verbose:
145 print '10. uudecode w/file exists fails'
146 out.seek(0)
147 try:
148 uu.decode(out)
149 except uu.Error:
150 pass
151 else:
152 raise TestFailed('expected to get a "file exists" error')
153finally:
154 try:
155 os.unlink(outfile)
156 except OSError:
157 pass