Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 1 | """ |
| 2 | Tests for uu module. |
| 3 | Nick Mathewson |
| 4 | """ |
| 5 | |
| 6 | from test_support import verify, TestFailed, verbose, TESTFN |
| 7 | import sys, os |
| 8 | import uu |
| 9 | from StringIO import StringIO |
| 10 | |
| 11 | teststr = "The smooth-scaled python crept over the sleeping dog\n" |
| 12 | expected = """\ |
| 13 | M5&AE('-M;V]T:\"US8V%L960@<'ET:&]N(&-R97!T(&]V97(@=&AE('-L965P |
| 14 | (:6YG(&1O9PH """ |
| 15 | encoded1 = "begin 666 t1\n"+expected+"\n \nend\n" |
| 16 | if verbose: |
| 17 | print '1. encode file->file' |
| 18 | inp = StringIO(teststr) |
| 19 | out = StringIO() |
| 20 | uu.encode(inp, out, "t1") |
| 21 | verify(out.getvalue() == encoded1) |
| 22 | inp = StringIO(teststr) |
| 23 | out = StringIO() |
| 24 | uu.encode(inp, out, "t1", 0644) |
| 25 | verify(out.getvalue() == "begin 644 t1\n"+expected+"\n \nend\n") |
| 26 | |
| 27 | if verbose: |
| 28 | print '2. decode file->file' |
| 29 | inp = StringIO(encoded1) |
| 30 | out = StringIO() |
| 31 | uu.decode(inp, out) |
| 32 | verify(out.getvalue() == teststr) |
| 33 | inp = StringIO("""UUencoded files may contain many lines, |
| 34 | even some that have 'begin' in them.\n"""+encoded1) |
| 35 | out = StringIO() |
| 36 | uu.decode(inp, out) |
| 37 | verify(out.getvalue() == teststr) |
| 38 | |
| 39 | stdinsave = sys.stdin |
| 40 | stdoutsave = sys.stdout |
| 41 | try: |
| 42 | if verbose: |
| 43 | print '3. encode stdin->stdout' |
| 44 | sys.stdin = StringIO(teststr) |
| 45 | sys.stdout = StringIO() |
| 46 | uu.encode("-", "-", "t1", 0666) |
| 47 | verify(sys.stdout.getvalue() == encoded1) |
| 48 | if verbose: |
| 49 | print >>stdoutsave, '4. decode stdin->stdout' |
| 50 | sys.stdin = StringIO(encoded1) |
| 51 | sys.stdout = StringIO() |
| 52 | uu.decode("-", "-") |
| 53 | verify(sys.stdout.getvalue() == teststr) |
| 54 | finally: |
| 55 | sys.stdin = stdinsave |
| 56 | sys.stdout = stdoutsave |
| 57 | |
| 58 | if verbose: |
| 59 | print '5. encode file->file' |
| 60 | tmpIn = TESTFN + "i" |
| 61 | tmpOut = TESTFN + "o" |
| 62 | try: |
Jack Jansen | fffd722 | 2001-08-03 13:04:03 +0000 | [diff] [blame] | 63 | fin = open(tmpIn, 'wb') |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 64 | fin.write(teststr) |
| 65 | fin.close() |
| 66 | |
Jack Jansen | fffd722 | 2001-08-03 13:04:03 +0000 | [diff] [blame] | 67 | fin = open(tmpIn, 'rb') |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 68 | fout = open(tmpOut, 'w') |
| 69 | uu.encode(fin, fout, tmpIn, mode=0644) |
| 70 | fin.close() |
| 71 | fout.close() |
| 72 | |
| 73 | fout = open(tmpOut, 'r') |
| 74 | s = fout.read() |
| 75 | fout.close() |
| 76 | verify(s == 'begin 644 ' + tmpIn + '\n' + expected + '\n \nend\n') |
| 77 | |
| 78 | os.unlink(tmpIn) |
| 79 | if verbose: |
| 80 | print '6. decode file-> file' |
| 81 | uu.decode(tmpOut) |
Jack Jansen | fffd722 | 2001-08-03 13:04:03 +0000 | [diff] [blame] | 82 | fin = open(tmpIn, 'rb') |
Tim Peters | 48dacc6 | 2001-07-11 21:43:42 +0000 | [diff] [blame] | 83 | s = fin.read() |
| 84 | fin.close() |
| 85 | verify(s == teststr) |
| 86 | # XXX is there an xp way to verify the mode? |
| 87 | |
| 88 | finally: |
| 89 | try: |
| 90 | fin.close() |
| 91 | except: |
| 92 | pass |
| 93 | try: |
| 94 | fout.close() |
| 95 | except: |
| 96 | pass |
| 97 | try: |
| 98 | os.unlink(tmpIn) |
| 99 | except: |
| 100 | pass |
| 101 | try: |
| 102 | os.unlink(tmpOut) |
| 103 | except: |
| 104 | pass |
| 105 | |
| 106 | if verbose: |
| 107 | print '7. error: truncated input' |
| 108 | inp = StringIO("begin 644 t1\n"+expected) |
| 109 | out = StringIO() |
| 110 | try: |
| 111 | uu.decode(inp, out) |
| 112 | raise TestFailed("No exception thrown") |
| 113 | except uu.Error, e: |
| 114 | verify(str(e) == 'Truncated input file') |
| 115 | |
| 116 | if verbose: |
| 117 | print '8. error: missing begin' |
| 118 | inp = StringIO("") |
| 119 | out = StringIO() |
| 120 | try: |
| 121 | uu.decode(inp, out) |
| 122 | raise TestFailed("No exception thrown") |
| 123 | except uu.Error, e: |
| 124 | verify(str(e) == 'No valid begin line found in input file') |