blob: 503976a0103f8f063828b1654ad37c216b34086f [file] [log] [blame]
Tim Peters48dacc62001-07-11 21:43:42 +00001"""
2Tests for uu module.
3Nick Mathewson
4"""
5
6from test_support import verify, TestFailed, verbose, TESTFN
7import 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:
63 fin = open(tmpIn, 'w')
64 fin.write(teststr)
65 fin.close()
66
67 fin = open(tmpIn, 'r')
68 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)
82 fin = open(tmpIn, 'r')
83 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')