/usr/lib/python2.7/dist-packages/pygopherd/gopherentryTest.py is in pygopherd 2.0.18.5.
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 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 | #!/usr/bin/python
# Python-based gopher server
# Module: test of gopherentry
# COPYRIGHT #
# Copyright (C) 2002 John Goerzen
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; version 2 of the License.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
# END OF COPYRIGHT #
import unittest, os, stat, re
from pygopherd import testutil
from pygopherd.gopherentry import GopherEntry
fields = ['selector', 'config', 'fspath', 'type', 'name', 'host', 'port',
'mimetype', 'encodedmimetype', 'size', 'encoding',
'populated', 'language', 'ctime', 'mtime', 'num', 'gopherpsupport']
class GopherEntryTestCase(unittest.TestCase):
def setUp(self):
self.config = testutil.getconfig()
self.root = self.config.get("pygopherd", "root")
def assertEntryMatches(self, conditions, entry, testname):
for field, value in conditions.items():
self.assertEquals(value, getattr(entry, field),
"%s: Field '%s' expected '%s' but was '%s'" % \
(testname, field, value, getattr(entry, field)))
def testinit(self):
entry = GopherEntry('/NONEXISTANT', self.config)
conditions = {'selector' : '/NONEXISTANT',
'config' : self.config,
'populated' : 0,
'gopherpsupport' : 0
}
for x in ['fspath', 'type', 'name', 'host', 'port', 'mimetype',
'encodedmimetype', 'size', 'encoding', 'language', 'ctime',
'mtime']:
conditions[x] = None
self.assertEntryMatches(conditions, entry, 'testinit')
def testpopulate_basic(self):
fspath = '/testfile.txt'
statval = os.stat(self.root + fspath)
conditions = {'selector' : '/testfile.txt',
'config' : self.config,
'fspath' : fspath,
'type' : '0',
'name' : 'testfile.txt',
'host' : None,
'port' : None,
'mimetype' : 'text/plain',
'encodedmimetype' : None,
'encoding' : None,
'populated' : 1,
'language' : None,
'gopherpsupport' : 1,
'ctime' : statval[9],
'mtime' : statval[8],
'size' : 5,
'num' : 0}
entry = GopherEntry('/testfile.txt', self.config)
entry.populatefromfs(fspath)
self.assertEntryMatches(conditions, entry, 'testpopulate_basic')
# Also try it with passed statval.
entry = GopherEntry('/testfile.txt', self.config)
entry.populatefromfs(fspath, statval)
self.assertEntryMatches(conditions, entry,
'testpopulate_basic with cached stat')
# Make sure it's a no-op if it's already populated.
entry = GopherEntry('/NONEXISTANT', self.config)
entry.populated = 1
entry.populatefromfs(fspath)
assert entry.gettype() == None
def testpopulate_encoded(self):
fspath = '/testfile.txt.gz'
entry = GopherEntry('/testfile.txt.gz', self.config)
entry.populatefromfs(fspath)
self.assertEquals(entry.gettype(), '9')
self.assertEquals(entry.getmimetype(), 'application/octet-stream')
self.assertEquals(entry.getencoding(), 'gzip')
self.assertEquals(entry.getencodedmimetype(), 'text/plain')
self.assertEquals(entry.geteadict(),
{'ABSTRACT': "This is the abstract\nfor testfile.txt.gz"})
def testpopulate_dir(self):
fspath = self.root + '/'
entry = GopherEntry('/', self.config)
entry.populatefromfs('/')
conditions = {
'selector' : '/',
'config' : self.config,
'fspath' : '/',
'type' : '1',
'name' : '',
'host' : None,
'port' : None,
'mimetype' : 'application/gopher-menu',
'encodedmimetype' : None,
'encoding' : None,
'populated' : 1,
'language' : None,
'gopherpsupport' : 1}
self.assertEntryMatches(conditions, entry,
"testpopulate_dir")
self.assertEqual(entry.geteadict(),
{'ABSTRACT':
'This is the abstract for the testdata directory.'})
def testpopulate_remote(self):
"""Asserts that population is not done on remote objects."""
selector = '/testfile.txt'
fspath = self.root + selector
entry = GopherEntry(selector, self.config)
entry.host = 'gopher.nowhere'
entry.populatefromfs(fspath)
assert entry.gettype() == None
entry.populated = 0
entry.host = None
entry.port = 70
entry.populatefromfs(fspath)
assert entry.gettype() == None
entry.populated = 0
entry.host = 'gopher.nowhere'
entry.populatefromfs(fspath)
assert entry.gettype() == None
def testpopulate_untouched(self):
"""Asserts that populatefromfs does not touch data that has already
been set."""
selector = '/testfile.txt'
fspath = selector
entry = GopherEntry(selector, self.config)
entry.name = 'FAKE NAME'
entry.ctime = 1
entry.mtime = 2
entry.populatefromfs(fspath)
self.assertEntryMatches({'name' : 'FAKE NAME', 'ctime':1, 'mtime':2},
entry, 'testpopulate_untouched')
# Reset for the next batch.
entry = GopherEntry('/', self.config)
# Test type for a dir.
entry.type = '2'
entry.mimetype = 'FAKEMIMETYPE'
entry.populatefromfs(self.root)
self.assertEquals(entry.gettype(), '2')
self.assertEquals(entry.getmimetype(), 'FAKEMIMETYPE')
# Test mime type handling. First, regular file.
entry = GopherEntry(selector, self.config)
entry.mimetype = 'fakemimetype'
entry.populatefromfs(fspath)
self.assertEquals(entry.getmimetype(), 'fakemimetype')
# The guesstype will not find fakemimetype and so it'll set it to 0
self.assertEquals(entry.gettype(), '0')
# Now, an encoded file.
entry = GopherEntry(selector + '.gz', self.config)
entry.mimetype = 'fakemime'
entry.populatefromfs(fspath + '.gz')
self.assertEquals(entry.getmimetype(), 'fakemime')
self.assertEquals(entry.getencoding(), 'gzip')
self.assertEquals(entry.getencodedmimetype(), 'text/plain')
self.assertEquals(entry.gettype(), '0') # again from fakemime
# More details.
selector = '/testarchive.tgz'
fspath = selector
entry = GopherEntry(selector, self.config)
entry.mimetype = 'foo1234'
entry.encoding = 'bar'
entry.populatefromfs(fspath)
self.assertEquals(entry.getmimetype(), 'foo1234')
self.assertEquals(entry.getencoding(), 'bar')
self.assertEquals(entry.getencodedmimetype(), 'application/x-tar')
self.assertEquals(entry.gettype(), '0')
# And overriding only the encoding.
entry = GopherEntry(selector, self.config)
entry.encoding = 'fakeencoding'
entry.populatefromfs(fspath)
self.assertEquals(entry.getencoding(), 'fakeencoding')
self.assertEquals(entry.gettype(), '9')
self.assertEquals(entry.getmimetype(), 'application/octet-stream')
# And finally -- overriding the encoded mime type.
entry = GopherEntry(selector, self.config)
entry.encodedmimetype = 'fakeencoded'
entry.populatefromfs(fspath)
self.assertEquals(entry.getencodedmimetype(), 'fakeencoded')
self.assertEquals(entry.getmimetype(), 'application/octet-stream')
def test_guesstype(self):
entry = GopherEntry('/NONEXISTANT', self.config)
expected = {'text/plain': '0',
'application/gopher-menu': '1',
'application/gopher+-menu' : '1',
'text/html' : 'h',
'image/gif' : 'g',
'image/jpeg' : 'I',
'application/pdf' : '9',
'application/msword' : '9',
'audio/aiff' : 's'}
for mimetype, type in expected.items():
entry.mimetype = mimetype
self.assertEquals(entry.guesstype(), type,
"Failure for %s: got %s, expected %s" % \
(mimetype, entry.guesstype(), type))
def test_gets_sets(self):
"""Tests a bunch of gets that operate on values that are None
to start with, and take a default."""
entry = GopherEntry('/NONEXISTANT', self.config)
# Initialize the rest of them to None.
entry.selector = None
entry.config = None
entry.populated = None
entry.num = None
entry.gopherpsupport = None
for field in fields:
getfunc = getattr(entry, 'get' + field)
setfunc = getattr(entry, 'set' + field)
self.assertEquals(getfunc(), None)
self.assertEquals(getfunc('DEFAULT' + field), 'DEFAULT' + field)
setfunc('NewValue' + field)
self.assertEquals(getfunc(), 'NewValue' + field)
self.assertEquals(getfunc('DEFAULT'), 'NewValue' + field)
def testgeturl(self):
expected = {
'/URL:http://www.complete.org/%20/': 'http://www.complete.org/%20/',
'URL:telnet://foo.com/%20&foo=bar': 'telnet://foo.com/%20&foo=bar',
'/foo' : 'gopher://MISSINGHOST:70/0/foo',
'/About Me.txt' : 'gopher://MISSINGHOST:70/0/About%20Me.txt',
'/' : 'gopher://MISSINGHOST:70/0/'}
for selector, url in expected.items():
entry = GopherEntry(selector, self.config)
entry.settype('0')
self.assertEquals(url, entry.geturl())
self.assertEquals(re.sub('MISSINGHOST', 'NEWHOST', url),
entry.geturl('NEWHOST'))
self.assertEquals(re.sub('70', '10101', url),
entry.geturl(defaultport = 10101))
entry.sethost('newhost')
self.assertEquals(re.sub('MISSINGHOST', 'newhost', url),
entry.geturl())
entry.setport(80)
self.assertEquals(re.sub('MISSINGHOST:70', 'newhost:80', url),
entry.geturl())
|