/usr/share/pyshared/tftp/test/test_netascii.py is in python-txtftp 0.1~bzr38-0ubuntu2.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 | '''
@author: shylent
'''
from cStringIO import StringIO
from tftp.netascii import (from_netascii, to_netascii, NetasciiReceiverProxy,
NetasciiSenderProxy)
from twisted.internet.defer import inlineCallbacks
from twisted.trial import unittest
import re
import tftp
class FromNetascii(unittest.TestCase):
def setUp(self):
self._orig_nl = tftp.netascii.NL
def test_lf_newline(self):
tftp.netascii.NL = '\x0a'
self.assertEqual(from_netascii('\x0d\x00'), '\x0d')
self.assertEqual(from_netascii('\x0d\x0a'), '\x0a')
self.assertEqual(from_netascii('foo\x0d\x0a\x0abar'), 'foo\x0a\x0abar')
self.assertEqual(from_netascii('foo\x0d\x0a\x0abar'), 'foo\x0a\x0abar')
# freestanding CR should not occur, but handle it anyway
self.assertEqual(from_netascii('foo\x0d\x0a\x0dbar'), 'foo\x0a\x0dbar')
def test_cr_newline(self):
tftp.netascii.NL = '\x0d'
self.assertEqual(from_netascii('\x0d\x00'), '\x0d')
self.assertEqual(from_netascii('\x0d\x0a'), '\x0d')
self.assertEqual(from_netascii('foo\x0d\x0a\x0abar'), 'foo\x0d\x0abar')
self.assertEqual(from_netascii('foo\x0d\x0a\x00bar'), 'foo\x0d\x00bar')
self.assertEqual(from_netascii('foo\x0d\x00\x0abar'), 'foo\x0d\x0abar')
def test_crlf_newline(self):
tftp.netascii.NL = '\x0d\x0a'
self.assertEqual(from_netascii('\x0d\x00'), '\x0d')
self.assertEqual(from_netascii('\x0d\x0a'), '\x0d\x0a')
self.assertEqual(from_netascii('foo\x0d\x00\x0abar'), 'foo\x0d\x0abar')
def tearDown(self):
tftp.netascii.NL = self._orig_nl
class ToNetascii(unittest.TestCase):
def setUp(self):
self._orig_nl = tftp.netascii.NL
self._orig_nl_regex = tftp.netascii.re_to_netascii
def test_lf_newline(self):
tftp.netascii.NL = '\x0a'
tftp.netascii.re_to_netascii = re.compile(tftp.netascii._re_to_netascii %
tftp.netascii.NL)
self.assertEqual(to_netascii('\x0d'), '\x0d\x00')
self.assertEqual(to_netascii('\x0a'), '\x0d\x0a')
self.assertEqual(to_netascii('\x0a\x0d'), '\x0d\x0a\x0d\x00')
self.assertEqual(to_netascii('\x0d\x0a'), '\x0d\x00\x0d\x0a')
def test_cr_newline(self):
tftp.netascii.NL = '\x0d'
tftp.netascii.re_to_netascii = re.compile(tftp.netascii._re_to_netascii %
tftp.netascii.NL)
self.assertEqual(to_netascii('\x0d'), '\x0d\x0a')
self.assertEqual(to_netascii('\x0a'), '\x0a')
self.assertEqual(to_netascii('\x0d\x0a'), '\x0d\x0a\x0a')
self.assertEqual(to_netascii('\x0a\x0d'), '\x0a\x0d\x0a')
def test_crlf_newline(self):
tftp.netascii.NL = '\x0d\x0a'
tftp.netascii.re_to_netascii = re.compile(tftp.netascii._re_to_netascii %
tftp.netascii.NL)
self.assertEqual(to_netascii('\x0d\x0a'), '\x0d\x0a')
self.assertEqual(to_netascii('\x0d'), '\x0d\x00')
self.assertEqual(to_netascii('\x0d\x0a\x0d'), '\x0d\x0a\x0d\x00')
self.assertEqual(to_netascii('\x0d\x0d\x0a'), '\x0d\x00\x0d\x0a')
def tearDown(self):
tftp.netascii.NL = self._orig_nl
tftp.netascii.re_to_netascii = self._orig_nl_regex
class ReceiverProxy(unittest.TestCase):
test_data = """line1
line2
line3
"""
def setUp(self):
self.source = StringIO(to_netascii(self.test_data))
self.sink = StringIO()
@inlineCallbacks
def test_conversion(self):
p = NetasciiReceiverProxy(self.sink)
chunk = self.source.read(2)
while chunk:
yield p.write(chunk)
chunk = self.source.read(2)
self.sink.seek(0) # !!!
self.assertEqual(self.sink.read(), self.test_data)
@inlineCallbacks
def test_conversion_byte_by_byte(self):
p = NetasciiReceiverProxy(self.sink)
chunk = self.source.read(1)
while chunk:
yield p.write(chunk)
chunk = self.source.read(1)
self.sink.seek(0) # !!!
self.assertEqual(self.sink.read(), self.test_data)
@inlineCallbacks
def test_conversion_normal(self):
p = NetasciiReceiverProxy(self.sink)
chunk = self.source.read(1)
while chunk:
yield p.write(chunk)
chunk = self.source.read(5)
self.sink.seek(0) # !!!
self.assertEqual(self.sink.read(), self.test_data)
class SenderProxy(unittest.TestCase):
test_data = """line1
line2
line3
"""
def setUp(self):
self.source = StringIO(self.test_data)
self.sink = StringIO()
@inlineCallbacks
def test_conversion_normal(self):
p = NetasciiSenderProxy(self.source)
chunk = yield p.read(5)
self.assertEqual(len(chunk), 5)
self.sink.write(chunk)
last_chunk = False
while chunk:
chunk = yield p.read(5)
# If a terminating chunk (len < blocknum) was already sent, there should
# be no more data (means, we can only yield empty lines from now on)
if last_chunk and chunk:
print "LEN: %s" % len(chunk)
self.fail("Last chunk (with len < blocksize) was already yielded, "
"but there is more data.")
if len(chunk) < 5:
last_chunk = True
self.sink.write(chunk)
self.sink.seek(0)
self.assertEqual(self.sink.read(), to_netascii(self.test_data))
@inlineCallbacks
def test_conversion_byte_by_byte(self):
p = NetasciiSenderProxy(self.source)
chunk = yield p.read(1)
self.assertEqual(len(chunk), 1)
self.sink.write(chunk)
last_chunk = False
while chunk:
chunk = yield p.read(1)
# If a terminating chunk (len < blocknum) was already sent, there should
# be no more data (means, we can only yield empty lines from now on)
if last_chunk and chunk:
print "LEN: %s" % len(chunk)
self.fail("Last chunk (with len < blocksize) was already yielded, "
"but there is more data.")
if len(chunk) < 1:
last_chunk = True
self.sink.write(chunk)
self.sink.seek(0)
self.assertEqual(self.sink.read(), to_netascii(self.test_data))
@inlineCallbacks
def test_conversion(self):
p = NetasciiSenderProxy(self.source)
chunk = yield p.read(2)
self.assertEqual(len(chunk), 2)
self.sink.write(chunk)
last_chunk = False
while chunk:
chunk = yield p.read(2)
# If a terminating chunk (len < blocknum) was already sent, there should
# be no more data (means, we can only yield empty lines from now on)
if last_chunk and chunk:
print "LEN: %s" % len(chunk)
self.fail("Last chunk (with len < blocksize) was already yielded, "
"but there is more data.")
if len(chunk) < 2:
last_chunk = True
self.sink.write(chunk)
self.sink.seek(0)
self.assertEqual(self.sink.read(), to_netascii(self.test_data))
|