/usr/share/pyshared/pyrad/tests/testServer.py is in python-pyrad 2.0-2.
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 301 302 303 304 305 306 307 308 | import select
import socket
import unittest
from pyrad.packet import PacketError
from pyrad.server import RemoteHost
from pyrad.server import Server
from pyrad.server import ServerPacketError
from pyrad.tests.mock import MockFinished
from pyrad.tests.mock import MockFd
from pyrad.tests.mock import MockPoll
from pyrad.tests.mock import MockSocket
from pyrad.tests.mock import MockClassMethod
from pyrad.tests.mock import UnmockClassMethods
from pyrad.packet import AccessRequest
from pyrad.packet import AccountingRequest
class TrivialObject:
"""dummy objec"""
class RemoteHostTests(unittest.TestCase):
def testSimpleConstruction(self):
host = RemoteHost('address', 'secret', 'name', 'authport', 'acctport')
self.assertEqual(host.address, 'address')
self.assertEqual(host.secret, 'secret')
self.assertEqual(host.name, 'name')
self.assertEqual(host.authport, 'authport')
self.assertEqual(host.acctport, 'acctport')
def testNamedConstruction(self):
host = RemoteHost(address='address', secret='secret', name='name',
authport='authport', acctport='acctport')
self.assertEqual(host.address, 'address')
self.assertEqual(host.secret, 'secret')
self.assertEqual(host.name, 'name')
self.assertEqual(host.authport, 'authport')
self.assertEqual(host.acctport, 'acctport')
class ServerConstructiontests(unittest.TestCase):
def testSimpleConstruction(self):
server = Server()
self.assertEqual(server.authfds, [])
self.assertEqual(server.acctfds, [])
self.assertEqual(server.authport, 1812)
self.assertEqual(server.acctport, 1813)
self.assertEqual(server.hosts, {})
def testParameterOrder(self):
server = Server([], 'authport', 'acctport', 'hosts', 'dict')
self.assertEqual(server.authfds, [])
self.assertEqual(server.acctfds, [])
self.assertEqual(server.authport, 'authport')
self.assertEqual(server.acctport, 'acctport')
self.assertEqual(server.dict, 'dict')
def testBindDuringConstruction(self):
def BindToAddress(self, addr):
self.bound.append(addr)
bta = Server.BindToAddress
Server.BindToAddress = BindToAddress
Server.bound = []
server = Server(['one', 'two', 'three'])
self.assertEqual(server.bound, ['one', 'two', 'three'])
del Server.bound
Server.BindToAddress = bta
class SocketTests(unittest.TestCase):
def setUp(self):
self.orgsocket = socket.socket
socket.socket = MockSocket
self.server = Server()
def tearDown(self):
socket.socket = self.orgsocket
def testBind(self):
self.server.BindToAddress('192.168.13.13')
self.assertEqual(len(self.server.authfds), 1)
self.assertEqual(self.server.authfds[0].address,
('192.168.13.13', 1812))
self.assertEqual(len(self.server.acctfds), 1)
self.assertEqual(self.server.acctfds[0].address,
('192.168.13.13', 1813))
def testGrabPacket(self):
def gen(data):
res = TrivialObject()
res.data = data
return res
fd = MockFd()
fd.source = object()
pkt = self.server._GrabPacket(gen, fd)
self.failUnless(isinstance(pkt, TrivialObject))
self.failUnless(pkt.fd is fd)
self.failUnless(pkt.source is fd.source)
self.failUnless(pkt.data is fd.data)
def testPrepareSocketNoFds(self):
self.server._poll = MockPoll()
self.server._PrepareSockets()
self.assertEqual(self.server._poll.registry, [])
self.assertEqual(self.server._realauthfds, [])
self.assertEqual(self.server._realacctfds, [])
def testPrepareSocketAuthFds(self):
self.server._poll = MockPoll()
self.server._fdmap = {}
self.server.authfds = [MockFd(12), MockFd(14)]
self.server._PrepareSockets()
self.assertEqual(list(self.server._fdmap.keys()), [12, 14])
self.assertEqual(self.server._poll.registry,
[(12, select.POLLIN | select.POLLPRI | select.POLLERR),
(14, select.POLLIN | select.POLLPRI | select.POLLERR)])
def testPrepareSocketAcctFds(self):
self.server._poll = MockPoll()
self.server._fdmap = {}
self.server.acctfds = [MockFd(12), MockFd(14)]
self.server._PrepareSockets()
self.assertEqual(list(self.server._fdmap.keys()), [12, 14])
self.assertEqual(self.server._poll.registry,
[(12, select.POLLIN | select.POLLPRI | select.POLLERR),
(14, select.POLLIN | select.POLLPRI | select.POLLERR)])
class AuthPacketHandlingTests(unittest.TestCase):
def setUp(self):
self.server = Server()
self.server.hosts['host'] = TrivialObject()
self.server.hosts['host'].secret = 'supersecret'
self.packet = TrivialObject()
self.packet.code = AccessRequest
self.packet.source = ('host', 'port')
def testHandleAuthPacketUnknownHost(self):
self.packet.source = ('stranger', 'port')
try:
self.server._HandleAuthPacket(self.packet)
except ServerPacketError as e:
self.failUnless('unknown host' in str(e))
else:
self.fail()
def testHandleAuthPacketWrongPort(self):
self.packet.code = AccountingRequest
try:
self.server._HandleAuthPacket(self.packet)
except ServerPacketError as e:
self.failUnless('port' in str(e))
else:
self.fail()
def testHandleAuthPacket(self):
def HandleAuthPacket(self, pkt):
self.handled = pkt
hap = Server.HandleAuthPacket
Server.HandleAuthPacket = HandleAuthPacket
self.server._HandleAuthPacket(self.packet)
self.failUnless(self.server.handled is self.packet)
Server.HandleAuthPacket = hap
class AcctPacketHandlingTests(unittest.TestCase):
def setUp(self):
self.server = Server()
self.server.hosts['host'] = TrivialObject()
self.server.hosts['host'].secret = 'supersecret'
self.packet = TrivialObject()
self.packet.code = AccountingRequest
self.packet.source = ('host', 'port')
def testHandleAcctPacketUnknownHost(self):
self.packet.source = ('stranger', 'port')
try:
self.server._HandleAcctPacket(self.packet)
except ServerPacketError as e:
self.failUnless('unknown host' in str(e))
else:
self.fail()
def testHandleAcctPacketWrongPort(self):
self.packet.code = AccessRequest
try:
self.server._HandleAcctPacket(self.packet)
except ServerPacketError as e:
self.failUnless('port' in str(e))
else:
self.fail()
def testHandleAcctPacket(self):
def HandleAcctPacket(self, pkt):
self.handled = pkt
hap = Server.HandleAcctPacket
Server.HandleAcctPacket = HandleAcctPacket
self.server._HandleAcctPacket(self.packet)
self.failUnless(self.server.handled is self.packet)
Server.HandleAcctPacket = hap
class OtherTests(unittest.TestCase):
def setUp(self):
self.server = Server()
def tearDown(self):
UnmockClassMethods(Server)
def testCreateReplyPacket(self):
class TrivialPacket:
source = object()
def CreateReply(self, **kw):
reply = TrivialObject()
reply.kw = kw
return reply
reply = self.server.CreateReplyPacket(TrivialPacket(),
one='one', two='two')
self.failUnless(isinstance(reply, TrivialObject))
self.failUnless(reply.source is TrivialPacket.source)
self.assertEqual(reply.kw, dict(one='one', two='two'))
def testAuthProcessInput(self):
fd = MockFd(1)
self.server._realauthfds = [1]
MockClassMethod(Server, '_GrabPacket')
MockClassMethod(Server, '_HandleAuthPacket')
self.server._ProcessInput(fd)
self.assertEqual([x[0] for x in self.server.called],
['_GrabPacket', '_HandleAuthPacket'])
self.assertEqual(self.server.called[0][1][1], fd)
def testAcctProcessInput(self):
fd = MockFd(1)
self.server._realauthfds = []
self.server._realacctfds = [1]
MockClassMethod(Server, '_GrabPacket')
MockClassMethod(Server, '_HandleAcctPacket')
self.server._ProcessInput(fd)
self.assertEqual([x[0] for x in self.server.called],
['_GrabPacket', '_HandleAcctPacket'])
self.assertEqual(self.server.called[0][1][1], fd)
class ServerRunTests(unittest.TestCase):
def setUp(self):
self.server = Server()
self.origpoll = select.poll
select.poll = MockPoll
def tearDown(self):
MockPoll.results = []
select.poll = self.origpoll
UnmockClassMethods(Server)
def testRunInitializes(self):
MockClassMethod(Server, '_PrepareSockets')
self.assertRaises(MockFinished, self.server.Run)
self.assertEqual(self.server.called, [('_PrepareSockets', (), {})])
self.failUnless(isinstance(self.server._fdmap, dict))
self.failUnless(isinstance(self.server._poll, MockPoll))
def testRunIgnoresPollErrors(self):
self.server.authfds = [MockFd()]
MockPoll.results = [(0, select.POLLERR)]
self.assertRaises(MockFinished, self.server.Run)
def testRunIgnoresServerPacketErrors(self):
def RaisePacketError(self, fd):
raise ServerPacketError
MockClassMethod(Server, '_ProcessInput', RaisePacketError)
self.server.authfds = [MockFd()]
MockPoll.results = [(0, select.POLLIN)]
self.assertRaises(MockFinished, self.server.Run)
def testRunIgnoresPacketErrors(self):
def RaisePacketError(self, fd):
raise PacketError
MockClassMethod(Server, '_ProcessInput', RaisePacketError)
self.server.authfds = [MockFd()]
MockPoll.results = [(0, select.POLLIN)]
self.assertRaises(MockFinished, self.server.Run)
def testRunRunsProcessInput(self):
MockClassMethod(Server, '_ProcessInput')
self.server.authfds = fd = [MockFd()]
MockPoll.results = [(0, select.POLLIN)]
self.assertRaises(MockFinished, self.server.Run)
self.assertEqual(self.server.called, [('_ProcessInput', (fd[0],), {})])
if not hasattr(select, 'poll'):
del SocketTests
del ServerRunTests
|