/usr/share/pyshared/twisted/web2/test/test_cgi.py is in python-twisted-web2 8.1.0-3.
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 288 289 290 291 292 293 294 295 296 297 298 299 300 | import sys, os
from twisted.trial import unittest
from twisted.internet import reactor, interfaces, defer
from twisted.python import util
from twisted.web2 import twcgi, server, http, iweb
from twisted.web2 import stream
from twisted.web2.test.test_server import SimpleRequest
DUMMY_CGI = '''
print "Header: OK"
print
print "cgi output"
'''
READINPUT_CGI = '''
# this is an example of a correctly-written CGI script which reads a body
# from stdin, which only reads env['CONTENT_LENGTH'] bytes.
import os, sys
body_length = int(os.environ.get('CONTENT_LENGTH',0))
indata = sys.stdin.read(body_length)
print "Header: OK"
print
print "readinput ok"
'''
READALLINPUT_CGI = '''
# this is an example of the typical (incorrect) CGI script which expects
# the server to close stdin when the body of the request is complete.
# A correct CGI should only read env['CONTENT_LENGTH'] bytes.
import sys
indata = sys.stdin.read()
print "Header: OK"
print
print "readallinput ok"
'''
def readStreamToString(s):
"""
Read all data from a stream into a string.
@param s: a L{twisted.web2.stream.IByteStream} to read from.
@return: a L{Deferred} results in a str
"""
allData = []
def gotData(data):
allData.append(data)
d = stream.readStream(s, gotData)
d.addCallback(lambda ign: ''.join(allData))
return d
class PythonScript(twcgi.FilteredScript):
"""
A specialized FilteredScript that just runs its file in a python
interpreter.
"""
filters = (sys.executable,) # web2's version
class CGITestBase(unittest.TestCase):
"""
Base class for CGI using tests
"""
def setUpResource(self, cgi):
"""
Set up the cgi resource to be tested.
@param cgi: A string containing a Python CGI script.
@return: A L{PythonScript} instance
"""
cgiFilename = os.path.abspath(self.mktemp())
cgiFile = file(cgiFilename, 'wt')
cgiFile.write(cgi)
cgiFile.close()
return PythonScript(cgiFilename)
def getPage(self, request, resource):
"""
Return the body of the given resource for the given request
@param request: A L{SimpleRequest} instance to act on the resource
@param resource: A L{IResource} to be rendered
@return: A L{Deferred} that fires with the response body returned by
resource for the request
"""
d = defer.maybeDeferred(resource.renderHTTP, request)
d.addCallback(lambda resp: readStreamToString(resp.stream))
return d
class CGI(CGITestBase):
"""
Test cases for basic twcgi.FilteredScript functionality
"""
def test_CGI(self):
"""
Test that the given DUMMY_CGI is executed and the expected output
returned
"""
request = SimpleRequest(None, 'GET', '/cgi')
resource = self.setUpResource(DUMMY_CGI)
d = self.getPage(request, resource)
d.addCallback(self._testCGI_1)
return d
def _testCGI_1(self, res):
self.failUnlessEqual(res, "cgi output%s" % os.linesep)
def testReadEmptyInput(self):
"""
Test that the CGI can successfully read from an empty input stream
"""
request = SimpleRequest(None, 'GET', '/cgi')
resource = self.setUpResource(READINPUT_CGI)
d = self.getPage(request, resource)
d.addCallback(self._testReadEmptyInput_1)
return d
def _testReadEmptyInput_1(self, res):
self.failUnlessEqual(res, "readinput ok%s" % os.linesep)
def test_readInput(self):
"""
Test that we can successfully read an input stream with data
"""
request = SimpleRequest(None, "POST", "/cgi",
content="Here is your stdin")
resource = self.setUpResource(READINPUT_CGI)
d = self.getPage(request, resource)
d.addCallback(self._testReadInput_1)
return d
def _testReadInput_1(self, res):
self.failUnlessEqual(res, "readinput ok%s" % os.linesep)
def test_readAllInput(self):
"""
Test that we can all input can be read regardless of CONTENT_LENGTH
"""
request = SimpleRequest(None, "POST", "/cgi",
content="Here is your stdin")
resource = self.setUpResource(READALLINPUT_CGI)
d = self.getPage(request, resource)
d.addCallback(self._testReadAllInput_1)
return d
def _testReadAllInput_1(self, res):
self.failUnlessEqual(res, "readallinput ok%s" % os.linesep)
if not interfaces.IReactorProcess.providedBy(reactor):
CGI.skip = "CGI tests require a functional reactor.spawnProcess()"
class CGIDirectoryTest(CGITestBase):
"""
Test cases for twisted.web2.twcgi.CGIDirectory
"""
def setUp(self):
temp = self.mktemp()
os.mkdir(temp)
cgiFile = open(os.path.join(temp, 'dummy'), 'wt')
cgiFile.write(DUMMY_CGI)
cgiFile.close()
os.mkdir(os.path.join(temp, 'directory'))
self.root = twcgi.CGIDirectory(temp)
def test_notFound(self):
"""
Correctly handle non-existant children by returning a 404
"""
self.assertRaises(http.HTTPError,
self.root.locateChild, None, ('notHere',))
def test_cantRender(self):
"""
We do not support directory listing of CGIDirectories
So our render method should always return a 403
"""
response = self.root.render(None)
self.failUnless(iweb.IResponse.providedBy(response))
self.assertEquals(response.code, 403)
def test_foundScript(self):
"""
We should get twcgi.CGISCript instances when we locate
a CGI
"""
resource, segments = self.root.locateChild(None, ('dummy',))
self.assertEquals(segments, ())
self.failUnless(isinstance(resource, (twcgi.CGIScript,)))
def test_subDirectory(self):
"""
When a subdirectory is request we should get another CGIDirectory
"""
resource, segments = self.root.locateChild(None, ('directory',
'paths',
'that',
'dont',
'matter'))
self.failUnless(isinstance(resource, twcgi.CGIDirectory))
def createScript(self, filename):
"""
Write a dummy cgi script
@param filename: a str destination for the cgi
"""
cgiFile = open(filename, 'wt')
cgiFile.write("#!%s\n\n%s" % (sys.executable,
DUMMY_CGI))
cgiFile.close()
os.chmod(filename, 0700)
def test_scriptsExecute(self):
"""
Verify that CGI scripts within a CGIDirectory can actually be executed
"""
cgiBinDir = os.path.abspath(self.mktemp())
os.mkdir(cgiBinDir)
root = twcgi.CGIDirectory(cgiBinDir)
self.createScript(os.path.join(cgiBinDir, 'dummy'))
cgiSubDir = os.path.join(cgiBinDir, 'sub')
os.mkdir(cgiSubDir)
self.createScript(os.path.join(cgiSubDir, 'dummy'))
site = server.Site(root)
request = SimpleRequest(site, "GET", "/dummy")
d = request.locateResource('/dummy')
def _firstResponse(res):
self.failUnlessEqual(res, "cgi output%s" % os.linesep)
def _firstRequest(resource):
d1 = self.getPage(request, resource)
d1.addCallback(_firstResponse)
return d1
d.addCallback(_firstRequest)
def _secondResponse(res):
self.failUnlessEqual(res, "cgi output%s" % os.linesep)
def _secondRequest(ign):
request = SimpleRequest(site, "GET", '/sub/dummy')
d2 = request.locateResource('/sub/dummy')
d2.addCallback(lambda resource: self.getPage(request, resource))
d2.addCallback(_secondResponse)
return d2
d.addCallback(_secondRequest)
return d
|