github.com/grumpyhome/grumpy@v0.3.1-0.20201208125205-7b775405bdf1/grumpy-runtime-src/third_party/stdlib/test/test_uu.py (about)

     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      def test_garbage_padding(self):
    65          # Issue #22406
    66          encodedtext = (
    67              "begin 644 file\n"
    68              # length 1; bits 001100 111111 111111 111111
    69              "\x21\x2C\x5F\x5F\x5F\n"
    70              "\x20\n"
    71              "end\n"
    72          )
    73          plaintext = "\x33"  # 00110011
    74  
    75          inp = cStringIO.StringIO(encodedtext)
    76          out = cStringIO.StringIO()
    77          uu.decode(inp, out, quiet=True)
    78          self.assertEqual(out.getvalue(), plaintext)
    79  
    80          #import codecs
    81          #decoded = codecs.decode(encodedtext, "uu_codec")
    82          #self.assertEqual(decoded, plaintext)
    83  
    84  class UUStdIOTest(unittest.TestCase):
    85  
    86      def setUp(self):
    87          self.stdin = sys.stdin
    88          self.stdout = sys.stdout
    89  
    90      def tearDown(self):
    91          sys.stdin = self.stdin
    92          sys.stdout = self.stdout
    93  
    94      def test_encode(self):
    95          sys.stdin = cStringIO.StringIO(plaintext)
    96          sys.stdout = cStringIO.StringIO()
    97          uu.encode("-", "-", "t1", 0666)
    98          self.assertEqual(
    99              sys.stdout.getvalue(),
   100              encodedtextwrapped % (0666, "t1")
   101          )
   102  
   103      def test_decode(self):
   104          sys.stdin = cStringIO.StringIO(encodedtextwrapped % (0666, "t1"))
   105          sys.stdout = cStringIO.StringIO()
   106          uu.decode("-", "-")
   107          self.assertEqual(sys.stdout.getvalue(), plaintext)
   108  
   109  class UUFileTest(unittest.TestCase):
   110  
   111      def _kill(self, f):
   112          # close and remove file
   113          try:
   114              f.close()
   115          except (SystemExit, KeyboardInterrupt):
   116              raise
   117          except:
   118              pass
   119          try:
   120              os.unlink(f.name)
   121          except (SystemExit, KeyboardInterrupt):
   122              raise
   123  #        except:
   124  #            pass
   125  
   126      def setUp(self):
   127          self.tmpin  = test_support.TESTFN + "i"
   128          self.tmpout = test_support.TESTFN + "o"
   129  
   130      def tearDown(self):
   131          del self.tmpin
   132          del self.tmpout
   133  
   134      def test_encode(self):
   135          fin = fout = None
   136          try:
   137              test_support.unlink(self.tmpin)
   138              fin = open(self.tmpin, 'wb')
   139              fin.write(plaintext)
   140              fin.close()
   141  
   142              fin = open(self.tmpin, 'rb')
   143              fout = open(self.tmpout, 'w')
   144              uu.encode(fin, fout, self.tmpin, mode=0644)
   145              fin.close()
   146              fout.close()
   147  
   148              fout = open(self.tmpout, 'r')
   149              s = fout.read()
   150              fout.close()
   151              self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin))
   152  
   153              # in_file and out_file as filenames
   154              uu.encode(self.tmpin, self.tmpout, self.tmpin, mode=0644)
   155              fout = open(self.tmpout, 'r')
   156              s = fout.read()
   157              fout.close()
   158              self.assertEqual(s, encodedtextwrapped % (0644, self.tmpin))
   159  
   160          finally:
   161              self._kill(fin)
   162              self._kill(fout)
   163  
   164      def test_decode(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              f = open(self.tmpin, 'r')
   173              uu.decode(f)
   174              f.close()
   175  
   176              f = open(self.tmpout, 'r')
   177              s = f.read()
   178              f.close()
   179              self.assertEqual(s, plaintext)
   180              # XXX is there an xp way to verify the mode?
   181          finally:
   182              self._kill(f)
   183  
   184      def test_decode_filename(self):
   185          f = None
   186          try:
   187              test_support.unlink(self.tmpin)
   188              f = open(self.tmpin, 'w')
   189              f.write(encodedtextwrapped % (0644, self.tmpout))
   190              f.close()
   191  
   192              uu.decode(self.tmpin)
   193  
   194              f = open(self.tmpout, 'r')
   195              s = f.read()
   196              f.close()
   197              self.assertEqual(s, plaintext)
   198          finally:
   199              self._kill(f)
   200  
   201      def test_decodetwice(self):
   202          # Verify that decode() will refuse to overwrite an existing file
   203          f = None
   204          try:
   205              f = cStringIO.StringIO(encodedtextwrapped % (0644, self.tmpout))
   206  
   207              f = open(self.tmpin, 'r')
   208              uu.decode(f)
   209              f.close()
   210  
   211              f = open(self.tmpin, 'r')
   212              self.assertRaises(uu.Error, uu.decode, f)
   213              f.close()
   214          finally:
   215              self._kill(f)
   216  
   217  def test_main():
   218      test_support.run_unittest(UUTest, UUStdIOTest, UUFileTest)
   219  
   220  if __name__=="__main__":
   221      test_main()