PageRenderTime 46ms CodeModel.GetById 18ms RepoModel.GetById 0ms app.codeStats 0ms

/contrib/python-x86_64/lib/python2.7/test/test_uu.py

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