Home | History | Annotate | Download | only in test
      1 """
      2 Tests for uu module.
      3 Nick Mathewson
      4 """
      5 
      6 import unittest
      7 from test import test_support
      8 
      9 import sys, os, uu, cStringIO
     10 import uu
     11 
     12 plaintext = "The smooth-scaled python crept over the sleeping dog\n"
     13 
     14 encodedtext = """\
     15 M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P
     16 (:6YG(&1O9PH """
     17 
     18 encodedtextwrapped = "begin %03o %s\n" + encodedtext.replace("%", "%%") + "\n \nend\n"
     19 
     20 class UUTest(unittest.TestCase):
     21 
     22     def test_encode(self):
     23         inp = cStringIO.StringIO(plaintext)
     24         out = cStringIO.StringIO()
     25         uu.encode(inp, out, "t1")
     26         self.assertEqual(out.getvalue(), encodedtextwrapped % (0666, "t1"))
     27         inp = cStringIO.StringIO(plaintext)
     28         out = cStringIO.StringIO()
     29         uu.encode(inp, out, "t1", 0644)
     30         self.assertEqual(out.getvalue(), encodedtextwrapped % (0644, "t1"))
     31 
     32     def test_decode(self):
     33         inp = cStringIO.StringIO(encodedtextwrapped % (0666, "t1"))
     34         out = cStringIO.StringIO()
     35         uu.decode(inp, out)
     36         self.assertEqual(out.getvalue(), plaintext)
     37         inp = cStringIO.StringIO(
     38             "UUencoded files may contain many lines,\n" +
     39             "even some that have 'begin' in them.\n" +
     40             encodedtextwrapped % (0666, "t1")
     41         )
     42         out = cStringIO.StringIO()
     43         uu.decode(inp, out)
     44         self.assertEqual(out.getvalue(), plaintext)
     45 
     46     def test_truncatedinput(self):
     47         inp = cStringIO.StringIO("begin 644 t1\n" + encodedtext)
     48         out = cStringIO.StringIO()
     49         try:
     50             uu.decode(inp, out)
     51             self.fail("No exception raised")
     52         except uu.Error, e:
     53             self.assertEqual(str(e), "Truncated input file")
     54 
     55     def test_missingbegin(self):
     56         inp = cStringIO.StringIO("")
     57         out = cStringIO.StringIO()
     58         try:
     59             uu.decode(inp, out)
     60             self.fail("No exception raised")
     61         except uu.Error, e:
     62             self.assertEqual(str(e), "No valid begin line found in input file")
     63 
     64 class UUStdIOTest(unittest.TestCase):
     65 
     66     def setUp(self):
     67         self.stdin = sys.stdin
     68         self.stdout = sys.stdout
     69 
     70     def tearDown(self):
     71         sys.stdin = self.stdin
     72         sys.stdout = self.stdout
     73 
     74     def test_encode(self):
     75         sys.stdin = cStringIO.StringIO(plaintext)
     76         sys.stdout = cStringIO.StringIO()
     77         uu.encode("-", "-", "t1", 0666)
     78         self.assertEqual(
     79             sys.stdout.getvalue(),
     80             encodedtextwrapped % (0666, "t1")
     81         )
     82 
     83     def test_decode(self):
     84         sys.stdin = cStringIO.StringIO(encodedtextwrapped % (0666, "t1"))
     85         sys.stdout = cStringIO.StringIO()
     86         uu.decode("-", "-")
     87         self.assertEqual(sys.stdout.getvalue(), plaintext)
     88 
     89 class UUFileTest(unittest.TestCase):
     90 
     91     def _kill(self, f):
     92         # close and remove file
     93         try:
     94             f.close()
     95         except (SystemExit, KeyboardInterrupt):
     96             raise
     97         except:
     98             pass
     99         try:
    100             os.unlink(f.name)
    101         except (SystemExit, KeyboardInterrupt):
    102             raise
    103         except:
    104             pass
    105 
    106     def setUp(self):
    107         self.tmpin  = test_support.TESTFN + "i"
    108         self.tmpout = test_support.TESTFN + "o"
    109 
    110     def tearDown(self):
    111         del self.tmpin
    112         del self.tmpout
    113 
    114     def test_encode(self):
    115         fin = fout = None
    116         try:
    117             test_support.unlink(self.tmpin)
    118             fin = open(self.tmpin, 'wb')
    119             fin.write(plaintext)
    120             fin.close()
    121 
    122             fin = open(self.tmpin, 'rb')
    123             fout = open(self.tmpout, 'w')
    124             uu.encode(fin, fout, self.tmpin, mode=0644)
    125             fin.close()
    126             fout.close()
    127 
    128             fout = open(self.tmpout, 'r')
    129             s = fout.read()
    130             fout.close()
    131             self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin))
    132 
    133             # in_file and out_file as filenames
    134             uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0644)
    135             fout = open(self.tmpout, 'r')
    136             s = fout.read()
    137             fout.close()
    138             self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin))
    139 
    140         finally:
    141             self._kill(fin)
    142             self._kill(fout)
    143 
    144     def test_decode(self):
    145         f = None
    146         try:
    147             test_support.unlink(self.tmpin)
    148             f = open(self.tmpin, 'w')
    149             f.write(encodedtextwrapped % (0644, self.tmpout))
    150             f.close()
    151 
    152             f = open(self.tmpin, 'r')
    153             uu.decode(f)
    154             f.close()
    155 
    156             f = open(self.tmpout, 'r')
    157             s = f.read()
    158             f.close()
    159             self.assertEqual(s, plaintext)
    160             # XXX is there an xp way to verify the mode?
    161         finally:
    162             self._kill(f)
    163 
    164     def test_decode_filename(self):
    165         f = None
    166         try:
    167             test_support.unlink(self.tmpin)
    168             f = open(self.tmpin, 'w')
    169             f.write(encodedtextwrapped % (0644, self.tmpout))
    170             f.close()
    171 
    172             uu.decode(self.tmpin)
    173 
    174             f = open(self.tmpout, 'r')
    175             s = f.read()
    176             f.close()
    177             self.assertEqual(s, plaintext)
    178         finally:
    179             self._kill(f)
    180 
    181     def test_decodetwice(self):
    182         # Verify that decode() will refuse to overwrite an existing file
    183         f = None
    184         try:
    185             f = cStringIO.StringIO(encodedtextwrapped % (0644, self.tmpout))
    186 
    187             f = open(self.tmpin, 'r')
    188             uu.decode(f)
    189             f.close()
    190 
    191             f = open(self.tmpin, 'r')
    192             self.assertRaises(uu.Error, uu.decode, f)
    193             f.close()
    194         finally:
    195             self._kill(f)
    196 
    197 def test_main():
    198     test_support.run_unittest(UUTest, UUStdIOTest, UUFileTest)
    199 
    200 if __name__=="__main__":
    201     test_main()
    202