0001 """ 0002 Tests for uu module. 0003 Nick Mathewson 0004 """ 0005 0006 import unittest 0007 from test import test_support 0008 0009 import sys, os, uu, cStringIO 0010 import uu 0011 from StringIO import StringIO 0012 0013 plaintext = "The smooth-scaled python crept over the sleeping dog\n" 0014 0015 encodedtext = """\ 0016 M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P 0017 (:6YG(&1O9PH """ 0018 0019 encodedtextwrapped = "begin %03o %s\n" + encodedtext.replace("%", "%%") + "\n \nend\n" 0020 0021 class UUTest(unittest.TestCase): 0022 0023 def test_encode(self): 0024 inp = cStringIO.StringIO(plaintext) 0025 out = cStringIO.StringIO() 0026 uu.encode(inp, out, "t1") 0027 self.assertEqual(out.getvalue(), encodedtextwrapped % (0666, "t1")) 0028 inp = cStringIO.StringIO(plaintext) 0029 out = cStringIO.StringIO() 0030 uu.encode(inp, out, "t1", 0644) 0031 self.assertEqual(out.getvalue(), encodedtextwrapped % (0644, "t1")) 0032 0033 def test_decode(self): 0034 inp = cStringIO.StringIO(encodedtextwrapped % (0666, "t1")) 0035 out = cStringIO.StringIO() 0036 uu.decode(inp, out) 0037 self.assertEqual(out.getvalue(), plaintext) 0038 inp = cStringIO.StringIO( 0039 "UUencoded files may contain many lines,\n" + 0040 "even some that have 'begin' in them.\n" + 0041 encodedtextwrapped % (0666, "t1") 0042 ) 0043 out = cStringIO.StringIO() 0044 uu.decode(inp, out) 0045 self.assertEqual(out.getvalue(), plaintext) 0046 0047 def test_truncatedinput(self): 0048 inp = cStringIO.StringIO("begin 644 t1\n" + encodedtext) 0049 out = cStringIO.StringIO() 0050 try: 0051 uu.decode(inp, out) 0052 self.fail("No exception thrown") 0053 except uu.Error, e: 0054 self.assertEqual(str(e), "Truncated input file") 0055 0056 def test_missingbegin(self): 0057 inp = cStringIO.StringIO("") 0058 out = cStringIO.StringIO() 0059 try: 0060 uu.decode(inp, out) 0061 self.fail("No exception thrown") 0062 except uu.Error, e: 0063 self.assertEqual(str(e), "No valid begin line found in input file") 0064 0065 class UUStdIOTest(unittest.TestCase): 0066 0067 def setUp(self): 0068 self.stdin = sys.stdin 0069 self.stdout = sys.stdout 0070 0071 def tearDown(self): 0072 sys.stdin = self.stdin 0073 sys.stdout = self.stdout 0074 0075 def test_encode(self): 0076 sys.stdin = cStringIO.StringIO(plaintext) 0077 sys.stdout = cStringIO.StringIO() 0078 uu.encode("-", "-", "t1", 0666) 0079 self.assertEqual( 0080 sys.stdout.getvalue(), 0081 encodedtextwrapped % (0666, "t1") 0082 ) 0083 0084 def test_decode(self): 0085 sys.stdin = cStringIO.StringIO(encodedtextwrapped % (0666, "t1")) 0086 sys.stdout = cStringIO.StringIO() 0087 uu.decode("-", "-") 0088 self.assertEqual(sys.stdout.getvalue(), plaintext) 0089 0090 class UUFileTest(unittest.TestCase): 0091 0092 def _kill(self, f): 0093 # close and remove file 0094 try: 0095 f.close() 0096 except (SystemExit, KeyboardInterrupt): 0097 raise 0098 except: 0099 pass 0100 try: 0101 os.unlink(f.name) 0102 except (SystemExit, KeyboardInterrupt): 0103 raise 0104 except: 0105 pass 0106 0107 def setUp(self): 0108 self.tmpin = test_support.TESTFN + "i" 0109 self.tmpout = test_support.TESTFN + "o" 0110 0111 def tearDown(self): 0112 del self.tmpin 0113 del self.tmpout 0114 0115 def test_encode(self): 0116 try: 0117 fin = open(self.tmpin, 'wb') 0118 fin.write(plaintext) 0119 fin.close() 0120 0121 fin = open(self.tmpin, 'rb') 0122 fout = open(self.tmpout, 'w') 0123 uu.encode(fin, fout, self.tmpin, mode=0644) 0124 fin.close() 0125 fout.close() 0126 0127 fout = open(self.tmpout, 'r') 0128 s = fout.read() 0129 fout.close() 0130 self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin)) 0131 finally: 0132 self._kill(fin) 0133 self._kill(fout) 0134 0135 def test_decode(self): 0136 try: 0137 f = open(self.tmpin, 'wb') 0138 f.write(encodedtextwrapped % (0644, self.tmpout)) 0139 f.close() 0140 0141 f = open(self.tmpin, 'rb') 0142 uu.decode(f) 0143 f.close() 0144 0145 f = open(self.tmpout, 'r') 0146 s = f.read() 0147 f.close() 0148 self.assertEqual(s, plaintext) 0149 # XXX is there an xp way to verify the mode? 0150 finally: 0151 self._kill(f) 0152 0153 def test_decodetwice(self): 0154 # Verify that decode() will refuse to overwrite an existing file 0155 try: 0156 f = cStringIO.StringIO(encodedtextwrapped % (0644, self.tmpout)) 0157 0158 f = open(self.tmpin, 'rb') 0159 uu.decode(f) 0160 f.close() 0161 0162 f = open(self.tmpin, 'rb') 0163 self.assertRaises(uu.Error, uu.decode, f) 0164 f.close() 0165 finally: 0166 self._kill(f) 0167 0168 def test_main(): 0169 test_support.run_unittest(UUTest, UUStdIOTest, UUFileTest) 0170 0171 if __name__=="__main__": 0172 test_main() 0173
Generated by PyXR 0.9.4