| 1 |
""" |
| 2 |
Tests for uu module. |
| 3 |
Nick Mathewson |
| 4 |
""" |
| 5 |
|
| 6 |
import unittest |
| 7 |
from test import test_support as support |
| 8 |
|
| 9 |
import cStringIO |
| 10 |
import sys |
| 11 |
import uu |
| 12 |
import io |
| 13 |
|
| 14 |
plaintext = "The smooth-scaled python crept over the sleeping dog\n" |
| 15 |
|
| 16 |
encodedtext = """\ |
| 17 |
M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P |
| 18 |
(:6YG(&1O9PH """ |
| 19 |
|
| 20 |
encodedtextwrapped = "begin %03o %s\n" + encodedtext.replace("%", "%%") + "\n \nend\n" |
| 21 |
|
| 22 |
class UUTest(unittest.TestCase): |
| 23 |
|
| 24 |
def test_encode(self): |
| 25 |
inp = cStringIO.StringIO(plaintext) |
| 26 |
out = cStringIO.StringIO() |
| 27 |
uu.encode(inp, out, "t1") |
| 28 |
self.assertEqual(out.getvalue(), encodedtextwrapped % (0666, "t1")) |
| 29 |
inp = cStringIO.StringIO(plaintext) |
| 30 |
out = cStringIO.StringIO() |
| 31 |
uu.encode(inp, out, "t1", 0644) |
| 32 |
self.assertEqual(out.getvalue(), encodedtextwrapped % (0644, "t1")) |
| 33 |
|
| 34 |
def test_decode(self): |
| 35 |
inp = cStringIO.StringIO(encodedtextwrapped % (0666, "t1")) |
| 36 |
out = cStringIO.StringIO() |
| 37 |
uu.decode(inp, out) |
| 38 |
self.assertEqual(out.getvalue(), plaintext) |
| 39 |
inp = cStringIO.StringIO( |
| 40 |
"UUencoded files may contain many lines,\n" + |
| 41 |
"even some that have 'begin' in them.\n" + |
| 42 |
encodedtextwrapped % (0666, "t1") |
| 43 |
) |
| 44 |
out = cStringIO.StringIO() |
| 45 |
uu.decode(inp, out) |
| 46 |
self.assertEqual(out.getvalue(), plaintext) |
| 47 |
|
| 48 |
def test_truncatedinput(self): |
| 49 |
inp = cStringIO.StringIO("begin 644 t1\n" + encodedtext) |
| 50 |
out = cStringIO.StringIO() |
| 51 |
try: |
| 52 |
uu.decode(inp, out) |
| 53 |
self.fail("No exception raised") |
| 54 |
except uu.Error, e: |
| 55 |
self.assertEqual(str(e), "Truncated input file") |
| 56 |
|
| 57 |
def test_missingbegin(self): |
| 58 |
inp = cStringIO.StringIO("") |
| 59 |
out = cStringIO.StringIO() |
| 60 |
try: |
| 61 |
uu.decode(inp, out) |
| 62 |
self.fail("No exception raised") |
| 63 |
except uu.Error, e: |
| 64 |
self.assertEqual(str(e), "No valid begin line found in input file") |
| 65 |
|
| 66 |
def test_garbage_padding(self): |
| 67 |
|
| 68 |
encodedtext = ( |
| 69 |
"begin 644 file\n" |
| 70 |
|
| 71 |
"\x21\x2C\x5F\x5F\x5F\n" |
| 72 |
"\x20\n" |
| 73 |
"end\n" |
| 74 |
) |
| 75 |
plaintext = "\x33" |
| 76 |
|
| 77 |
inp = cStringIO.StringIO(encodedtext) |
| 78 |
out = cStringIO.StringIO() |
| 79 |
uu.decode(inp, out, quiet=True) |
| 80 |
self.assertEqual(out.getvalue(), plaintext) |
| 81 |
|
| 82 |
import codecs |
| 83 |
decoded = codecs.decode(encodedtext, "uu_codec") |
| 84 |
self.assertEqual(decoded, plaintext) |
| 85 |
|
| 86 |
def test_newlines_escaped(self): |
| 87 |
|
| 88 |
inp = io.BytesIO(plaintext) |
| 89 |
out = io.BytesIO() |
| 90 |
filename = "test.txt\n\roverflow.txt" |
| 91 |
safefilename = b"test.txt\\n\\roverflow.txt" |
| 92 |
uu.encode(inp, out, filename) |
| 93 |
self.assertIn(safefilename, out.getvalue()) |
| 94 |
|
| 95 |
class UUStdIOTest(unittest.TestCase): |
| 96 |
|
| 97 |
def setUp(self): |
| 98 |
self.stdin = sys.stdin |
| 99 |
self.stdout = sys.stdout |
| 100 |
|
| 101 |
def tearDown(self): |
| 102 |
sys.stdin = self.stdin |
| 103 |
sys.stdout = self.stdout |
| 104 |
|
| 105 |
def test_encode(self): |
| 106 |
sys.stdin = cStringIO.StringIO(plaintext) |
| 107 |
sys.stdout = cStringIO.StringIO() |
| 108 |
uu.encode("-", "-", "t1", 0666) |
| 109 |
self.assertEqual( |
| 110 |
sys.stdout.getvalue(), |
| 111 |
encodedtextwrapped % (0666, "t1") |
| 112 |
) |
| 113 |
|
| 114 |
def test_decode(self): |
| 115 |
sys.stdin = cStringIO.StringIO(encodedtextwrapped % (0666, "t1")) |
| 116 |
sys.stdout = cStringIO.StringIO() |
| 117 |
uu.decode("-", "-") |
| 118 |
self.assertEqual(sys.stdout.getvalue(), plaintext) |
| 119 |
|
| 120 |
class UUFileTest(unittest.TestCase): |
| 121 |
|
| 122 |
def setUp(self): |
| 123 |
self.tmpin = support.TESTFN + "i" |
| 124 |
self.tmpout = support.TESTFN + "o" |
| 125 |
self.addCleanup(support.unlink, self.tmpin) |
| 126 |
self.addCleanup(support.unlink, self.tmpout) |
| 127 |
|
| 128 |
def test_encode(self): |
| 129 |
with open(self.tmpin, 'wb') as fin: |
| 130 |
fin.write(plaintext) |
| 131 |
|
| 132 |
with open(self.tmpin, 'rb') as fin: |
| 133 |
with open(self.tmpout, 'w') as fout: |
| 134 |
uu.encode(fin, fout, self.tmpin, mode=0o644) |
| 135 |
|
| 136 |
with open(self.tmpout, 'r') as fout: |
| 137 |
s = fout.read() |
| 138 |
self.assertEqual(s, encodedtextwrapped % (0o644, self.tmpin)) |
| 139 |
|
| 140 |
|
| 141 |
uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0o644) |
| 142 |
with open(self.tmpout, 'r') as fout: |
| 143 |
s = fout.read() |
| 144 |
self.assertEqual(s, encodedtextwrapped % (0o644, self.tmpin)) |
| 145 |
|
| 146 |
def test_decode(self): |
| 147 |
with open(self.tmpin, 'w') as f: |
| 148 |
f.write(encodedtextwrapped % (0o644, self.tmpout)) |
| 149 |
|
| 150 |
with open(self.tmpin, 'r') as f: |
| 151 |
uu.decode(f) |
| 152 |
|
| 153 |
with open(self.tmpout, 'r') as f: |
| 154 |
s = f.read() |
| 155 |
self.assertEqual(s, plaintext) |
| 156 |
|
| 157 |
|
| 158 |
def test_decode_filename(self): |
| 159 |
with open(self.tmpin, 'w') as f: |
| 160 |
f.write(encodedtextwrapped % (0o644, self.tmpout)) |
| 161 |
|
| 162 |
uu.decode(self.tmpin) |
| 163 |
|
| 164 |
with open(self.tmpout, 'r') as f: |
| 165 |
s = f.read() |
| 166 |
self.assertEqual(s, plaintext) |
| 167 |
|
| 168 |
def test_decodetwice(self): |
| 169 |
|
| 170 |
with open(self.tmpin, 'wb') as f: |
| 171 |
f.write(encodedtextwrapped % (0o644, self.tmpout)) |
| 172 |
with open(self.tmpin, 'r') as f: |
| 173 |
uu.decode(f) |
| 174 |
|
| 175 |
with open(self.tmpin, 'r') as f: |
| 176 |
self.assertRaises(uu.Error, uu.decode, f) |
| 177 |
|
| 178 |
def test_main(): |
| 179 |
support.run_unittest(UUTest, UUStdIOTest, UUFileTest) |
| 180 |
|
| 181 |
if __name__=="__main__": |
| 182 |
test_main() |