test_amp.py 108 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331
  1. # Copyright (c) 2005 Divmod, Inc.
  2. # Copyright (c) Twisted Matrix Laboratories.
  3. # See LICENSE for details.
  4. """
  5. Tests for L{twisted.protocols.amp}.
  6. """
  7. from __future__ import absolute_import, division
  8. import datetime
  9. import decimal
  10. from zope.interface import implementer
  11. from zope.interface.verify import verifyClass, verifyObject
  12. from twisted.python import filepath
  13. from twisted.python.compat import intToBytes
  14. from twisted.python.failure import Failure
  15. from twisted.protocols import amp
  16. from twisted.trial import unittest
  17. from twisted.internet import (
  18. address, protocol, defer, error, reactor, interfaces)
  19. from twisted.test import iosim
  20. from twisted.test.proto_helpers import StringTransport
  21. ssl = None
  22. try:
  23. from twisted.internet import ssl
  24. except ImportError:
  25. pass
  26. if ssl and not ssl.supported:
  27. ssl = None
  28. if ssl is None:
  29. skipSSL = "SSL not available"
  30. else:
  31. skipSSL = None
  32. tz = amp._FixedOffsetTZInfo.fromSignHoursMinutes
  33. class TestProto(protocol.Protocol):
  34. """
  35. A trivial protocol for use in testing where a L{Protocol} is expected.
  36. @ivar instanceId: the id of this instance
  37. @ivar onConnLost: deferred that will fired when the connection is lost
  38. @ivar dataToSend: data to send on the protocol
  39. """
  40. instanceCount = 0
  41. def __init__(self, onConnLost, dataToSend):
  42. assert isinstance(dataToSend, bytes), repr(dataToSend)
  43. self.onConnLost = onConnLost
  44. self.dataToSend = dataToSend
  45. self.instanceId = TestProto.instanceCount
  46. TestProto.instanceCount = TestProto.instanceCount + 1
  47. def connectionMade(self):
  48. self.data = []
  49. self.transport.write(self.dataToSend)
  50. def dataReceived(self, bytes):
  51. self.data.append(bytes)
  52. def connectionLost(self, reason):
  53. self.onConnLost.callback(self.data)
  54. def __repr__(self):
  55. """
  56. Custom repr for testing to avoid coupling amp tests with repr from
  57. L{Protocol}
  58. Returns a string which contains a unique identifier that can be looked
  59. up using the instanceId property::
  60. <TestProto #3>
  61. """
  62. return "<TestProto #%d>" % (self.instanceId,)
  63. class SimpleSymmetricProtocol(amp.AMP):
  64. def sendHello(self, text):
  65. return self.callRemoteString(
  66. b"hello",
  67. hello=text)
  68. def amp_HELLO(self, box):
  69. return amp.Box(hello=box[b'hello'])
  70. class UnfriendlyGreeting(Exception):
  71. """Greeting was insufficiently kind.
  72. """
  73. class DeathThreat(Exception):
  74. """Greeting was insufficiently kind.
  75. """
  76. class UnknownProtocol(Exception):
  77. """Asked to switch to the wrong protocol.
  78. """
  79. class TransportPeer(amp.Argument):
  80. # this serves as some informal documentation for how to get variables from
  81. # the protocol or your environment and pass them to methods as arguments.
  82. def retrieve(self, d, name, proto):
  83. return b''
  84. def fromStringProto(self, notAString, proto):
  85. return proto.transport.getPeer()
  86. def toBox(self, name, strings, objects, proto):
  87. return
  88. class Hello(amp.Command):
  89. commandName = b'hello'
  90. arguments = [(b'hello', amp.String()),
  91. (b'optional', amp.Boolean(optional=True)),
  92. (b'print', amp.Unicode(optional=True)),
  93. (b'from', TransportPeer(optional=True)),
  94. (b'mixedCase', amp.String(optional=True)),
  95. (b'dash-arg', amp.String(optional=True)),
  96. (b'underscore_arg', amp.String(optional=True))]
  97. response = [(b'hello', amp.String()),
  98. (b'print', amp.Unicode(optional=True))]
  99. errors = {UnfriendlyGreeting: b'UNFRIENDLY'}
  100. fatalErrors = {DeathThreat: b'DEAD'}
  101. class NoAnswerHello(Hello):
  102. commandName = Hello.commandName
  103. requiresAnswer = False
  104. class FutureHello(amp.Command):
  105. commandName = b'hello'
  106. arguments = [(b'hello', amp.String()),
  107. (b'optional', amp.Boolean(optional=True)),
  108. (b'print', amp.Unicode(optional=True)),
  109. (b'from', TransportPeer(optional=True)),
  110. (b'bonus', amp.String(optional=True)), # addt'l arguments
  111. # should generally be
  112. # added at the end, and
  113. # be optional...
  114. ]
  115. response = [(b'hello', amp.String()),
  116. (b'print', amp.Unicode(optional=True))]
  117. errors = {UnfriendlyGreeting: b'UNFRIENDLY'}
  118. class WTF(amp.Command):
  119. """
  120. An example of an invalid command.
  121. """
  122. class BrokenReturn(amp.Command):
  123. """ An example of a perfectly good command, but the handler is going to return
  124. None...
  125. """
  126. commandName = b'broken_return'
  127. class Goodbye(amp.Command):
  128. # commandName left blank on purpose: this tests implicit command names.
  129. response = [(b'goodbye', amp.String())]
  130. responseType = amp.QuitBox
  131. class WaitForever(amp.Command):
  132. commandName = b'wait_forever'
  133. class GetList(amp.Command):
  134. commandName = b'getlist'
  135. arguments = [(b'length', amp.Integer())]
  136. response = [(b'body', amp.AmpList([(b'x', amp.Integer())]))]
  137. class DontRejectMe(amp.Command):
  138. commandName = b'dontrejectme'
  139. arguments = [
  140. (b'magicWord', amp.Unicode()),
  141. (b'list', amp.AmpList([(b'name', amp.Unicode())], optional=True)),
  142. ]
  143. response = [(b'response', amp.Unicode())]
  144. class SecuredPing(amp.Command):
  145. # XXX TODO: actually make this refuse to send over an insecure connection
  146. response = [(b'pinged', amp.Boolean())]
  147. class TestSwitchProto(amp.ProtocolSwitchCommand):
  148. commandName = b'Switch-Proto'
  149. arguments = [
  150. (b'name', amp.String()),
  151. ]
  152. errors = {UnknownProtocol: b'UNKNOWN'}
  153. class SingleUseFactory(protocol.ClientFactory):
  154. def __init__(self, proto):
  155. self.proto = proto
  156. self.proto.factory = self
  157. def buildProtocol(self, addr):
  158. p, self.proto = self.proto, None
  159. return p
  160. reasonFailed = None
  161. def clientConnectionFailed(self, connector, reason):
  162. self.reasonFailed = reason
  163. return
  164. THING_I_DONT_UNDERSTAND = b'gwebol nargo'
  165. class ThingIDontUnderstandError(Exception):
  166. pass
  167. class FactoryNotifier(amp.AMP):
  168. factory = None
  169. def connectionMade(self):
  170. if self.factory is not None:
  171. self.factory.theProto = self
  172. if hasattr(self.factory, 'onMade'):
  173. self.factory.onMade.callback(None)
  174. def emitpong(self):
  175. from twisted.internet.interfaces import ISSLTransport
  176. if not ISSLTransport.providedBy(self.transport):
  177. raise DeathThreat("only send secure pings over secure channels")
  178. return {'pinged': True}
  179. SecuredPing.responder(emitpong)
  180. class SimpleSymmetricCommandProtocol(FactoryNotifier):
  181. maybeLater = None
  182. def __init__(self, onConnLost=None):
  183. amp.AMP.__init__(self)
  184. self.onConnLost = onConnLost
  185. def sendHello(self, text):
  186. return self.callRemote(Hello, hello=text)
  187. def sendUnicodeHello(self, text, translation):
  188. return self.callRemote(Hello, hello=text, Print=translation)
  189. greeted = False
  190. def cmdHello(self, hello, From, optional=None, Print=None,
  191. mixedCase=None, dash_arg=None, underscore_arg=None):
  192. assert From == self.transport.getPeer()
  193. if hello == THING_I_DONT_UNDERSTAND:
  194. raise ThingIDontUnderstandError()
  195. if hello.startswith(b'fuck'):
  196. raise UnfriendlyGreeting("Don't be a dick.")
  197. if hello == b'die':
  198. raise DeathThreat("aieeeeeeeee")
  199. result = dict(hello=hello)
  200. if Print is not None:
  201. result.update(dict(Print=Print))
  202. self.greeted = True
  203. return result
  204. Hello.responder(cmdHello)
  205. def cmdGetlist(self, length):
  206. return {'body': [dict(x=1)] * length}
  207. GetList.responder(cmdGetlist)
  208. def okiwont(self, magicWord, list=None):
  209. if list is None:
  210. response = u'list omitted'
  211. else:
  212. response = u'%s accepted' % (list[0]['name'])
  213. return dict(response=response)
  214. DontRejectMe.responder(okiwont)
  215. def waitforit(self):
  216. self.waiting = defer.Deferred()
  217. return self.waiting
  218. WaitForever.responder(waitforit)
  219. def saybye(self):
  220. return dict(goodbye=b"everyone")
  221. Goodbye.responder(saybye)
  222. def switchToTestProtocol(self, fail=False):
  223. if fail:
  224. name = b'no-proto'
  225. else:
  226. name = b'test-proto'
  227. p = TestProto(self.onConnLost, SWITCH_CLIENT_DATA)
  228. return self.callRemote(
  229. TestSwitchProto,
  230. SingleUseFactory(p), name=name).addCallback(lambda ign: p)
  231. def switchit(self, name):
  232. if name == b'test-proto':
  233. return TestProto(self.onConnLost, SWITCH_SERVER_DATA)
  234. raise UnknownProtocol(name)
  235. TestSwitchProto.responder(switchit)
  236. def donothing(self):
  237. return None
  238. BrokenReturn.responder(donothing)
  239. class DeferredSymmetricCommandProtocol(SimpleSymmetricCommandProtocol):
  240. def switchit(self, name):
  241. if name == b'test-proto':
  242. self.maybeLaterProto = TestProto(self.onConnLost, SWITCH_SERVER_DATA)
  243. self.maybeLater = defer.Deferred()
  244. return self.maybeLater
  245. TestSwitchProto.responder(switchit)
  246. class BadNoAnswerCommandProtocol(SimpleSymmetricCommandProtocol):
  247. def badResponder(self, hello, From, optional=None, Print=None,
  248. mixedCase=None, dash_arg=None, underscore_arg=None):
  249. """
  250. This responder does nothing and forgets to return a dictionary.
  251. """
  252. NoAnswerHello.responder(badResponder)
  253. class NoAnswerCommandProtocol(SimpleSymmetricCommandProtocol):
  254. def goodNoAnswerResponder(self, hello, From, optional=None, Print=None,
  255. mixedCase=None, dash_arg=None, underscore_arg=None):
  256. return dict(hello=hello+b"-noanswer")
  257. NoAnswerHello.responder(goodNoAnswerResponder)
  258. def connectedServerAndClient(ServerClass=SimpleSymmetricProtocol,
  259. ClientClass=SimpleSymmetricProtocol,
  260. *a, **kw):
  261. """Returns a 3-tuple: (client, server, pump)
  262. """
  263. return iosim.connectedServerAndClient(
  264. ServerClass, ClientClass,
  265. *a, **kw)
  266. class TotallyDumbProtocol(protocol.Protocol):
  267. buf = b''
  268. def dataReceived(self, data):
  269. self.buf += data
  270. class LiteralAmp(amp.AMP):
  271. def __init__(self):
  272. self.boxes = []
  273. def ampBoxReceived(self, box):
  274. self.boxes.append(box)
  275. return
  276. class AmpBoxTests(unittest.TestCase):
  277. """
  278. Test a few essential properties of AMP boxes, mostly with respect to
  279. serialization correctness.
  280. """
  281. def test_serializeStr(self):
  282. """
  283. Make sure that strs serialize to strs.
  284. """
  285. a = amp.AmpBox(key=b'value')
  286. self.assertEqual(type(a.serialize()), bytes)
  287. def test_serializeUnicodeKeyRaises(self):
  288. """
  289. Verify that TypeError is raised when trying to serialize Unicode keys.
  290. """
  291. a = amp.AmpBox(**{u'key': 'value'})
  292. self.assertRaises(TypeError, a.serialize)
  293. def test_serializeUnicodeValueRaises(self):
  294. """
  295. Verify that TypeError is raised when trying to serialize Unicode
  296. values.
  297. """
  298. a = amp.AmpBox(key=u'value')
  299. self.assertRaises(TypeError, a.serialize)
  300. class ParsingTests(unittest.TestCase):
  301. def test_booleanValues(self):
  302. """
  303. Verify that the Boolean parser parses 'True' and 'False', but nothing
  304. else.
  305. """
  306. b = amp.Boolean()
  307. self.assertTrue(b.fromString(b"True"))
  308. self.assertFalse(b.fromString(b"False"))
  309. self.assertRaises(TypeError, b.fromString, b"ninja")
  310. self.assertRaises(TypeError, b.fromString, b"true")
  311. self.assertRaises(TypeError, b.fromString, b"TRUE")
  312. self.assertEqual(b.toString(True), b'True')
  313. self.assertEqual(b.toString(False), b'False')
  314. def test_pathValueRoundTrip(self):
  315. """
  316. Verify the 'Path' argument can parse and emit a file path.
  317. """
  318. fp = filepath.FilePath(self.mktemp())
  319. p = amp.Path()
  320. s = p.toString(fp)
  321. v = p.fromString(s)
  322. self.assertIsNot(fp, v) # sanity check
  323. self.assertEqual(fp, v)
  324. def test_sillyEmptyThing(self):
  325. """
  326. Test that empty boxes raise an error; they aren't supposed to be sent
  327. on purpose.
  328. """
  329. a = amp.AMP()
  330. return self.assertRaises(amp.NoEmptyBoxes, a.ampBoxReceived, amp.Box())
  331. def test_ParsingRoundTrip(self):
  332. """
  333. Verify that various kinds of data make it through the encode/parse
  334. round-trip unharmed.
  335. """
  336. c, s, p = connectedServerAndClient(ClientClass=LiteralAmp,
  337. ServerClass=LiteralAmp)
  338. SIMPLE = (b'simple', b'test')
  339. CE = (b'ceq', b': ')
  340. CR = (b'crtest', b'test\r')
  341. LF = (b'lftest', b'hello\n')
  342. NEWLINE = (b'newline', b'test\r\none\r\ntwo')
  343. NEWLINE2 = (b'newline2', b'test\r\none\r\n two')
  344. BODYTEST = (b'body', b'blah\r\n\r\ntesttest')
  345. testData = [
  346. [SIMPLE],
  347. [SIMPLE, BODYTEST],
  348. [SIMPLE, CE],
  349. [SIMPLE, CR],
  350. [SIMPLE, CE, CR, LF],
  351. [CE, CR, LF],
  352. [SIMPLE, NEWLINE, CE, NEWLINE2],
  353. [BODYTEST, SIMPLE, NEWLINE]
  354. ]
  355. for test in testData:
  356. jb = amp.Box()
  357. jb.update(dict(test))
  358. jb._sendTo(c)
  359. p.flush()
  360. self.assertEqual(s.boxes[-1], jb)
  361. class FakeLocator(object):
  362. """
  363. This is a fake implementation of the interface implied by
  364. L{CommandLocator}.
  365. """
  366. def __init__(self):
  367. """
  368. Remember the given keyword arguments as a set of responders.
  369. """
  370. self.commands = {}
  371. def locateResponder(self, commandName):
  372. """
  373. Look up and return a function passed as a keyword argument of the given
  374. name to the constructor.
  375. """
  376. return self.commands[commandName]
  377. class FakeSender:
  378. """
  379. This is a fake implementation of the 'box sender' interface implied by
  380. L{AMP}.
  381. """
  382. def __init__(self):
  383. """
  384. Create a fake sender and initialize the list of received boxes and
  385. unhandled errors.
  386. """
  387. self.sentBoxes = []
  388. self.unhandledErrors = []
  389. self.expectedErrors = 0
  390. def expectError(self):
  391. """
  392. Expect one error, so that the test doesn't fail.
  393. """
  394. self.expectedErrors += 1
  395. def sendBox(self, box):
  396. """
  397. Accept a box, but don't do anything.
  398. """
  399. self.sentBoxes.append(box)
  400. def unhandledError(self, failure):
  401. """
  402. Deal with failures by instantly re-raising them for easier debugging.
  403. """
  404. self.expectedErrors -= 1
  405. if self.expectedErrors < 0:
  406. failure.raiseException()
  407. else:
  408. self.unhandledErrors.append(failure)
  409. class CommandDispatchTests(unittest.TestCase):
  410. """
  411. The AMP CommandDispatcher class dispatches converts AMP boxes into commands
  412. and responses using Command.responder decorator.
  413. Note: Originally, AMP's factoring was such that many tests for this
  414. functionality are now implemented as full round-trip tests in L{AMPTests}.
  415. Future tests should be written at this level instead, to ensure API
  416. compatibility and to provide more granular, readable units of test
  417. coverage.
  418. """
  419. def setUp(self):
  420. """
  421. Create a dispatcher to use.
  422. """
  423. self.locator = FakeLocator()
  424. self.sender = FakeSender()
  425. self.dispatcher = amp.BoxDispatcher(self.locator)
  426. self.dispatcher.startReceivingBoxes(self.sender)
  427. def test_receivedAsk(self):
  428. """
  429. L{CommandDispatcher.ampBoxReceived} should locate the appropriate
  430. command in its responder lookup, based on the '_ask' key.
  431. """
  432. received = []
  433. def thunk(box):
  434. received.append(box)
  435. return amp.Box({"hello": "goodbye"})
  436. input = amp.Box(_command="hello",
  437. _ask="test-command-id",
  438. hello="world")
  439. self.locator.commands['hello'] = thunk
  440. self.dispatcher.ampBoxReceived(input)
  441. self.assertEqual(received, [input])
  442. def test_sendUnhandledError(self):
  443. """
  444. L{CommandDispatcher} should relay its unhandled errors in responding to
  445. boxes to its boxSender.
  446. """
  447. err = RuntimeError("something went wrong, oh no")
  448. self.sender.expectError()
  449. self.dispatcher.unhandledError(Failure(err))
  450. self.assertEqual(len(self.sender.unhandledErrors), 1)
  451. self.assertEqual(self.sender.unhandledErrors[0].value, err)
  452. def test_unhandledSerializationError(self):
  453. """
  454. Errors during serialization ought to be relayed to the sender's
  455. unhandledError method.
  456. """
  457. err = RuntimeError("something undefined went wrong")
  458. def thunk(result):
  459. class BrokenBox(amp.Box):
  460. def _sendTo(self, proto):
  461. raise err
  462. return BrokenBox()
  463. self.locator.commands['hello'] = thunk
  464. input = amp.Box(_command="hello",
  465. _ask="test-command-id",
  466. hello="world")
  467. self.sender.expectError()
  468. self.dispatcher.ampBoxReceived(input)
  469. self.assertEqual(len(self.sender.unhandledErrors), 1)
  470. self.assertEqual(self.sender.unhandledErrors[0].value, err)
  471. def test_callRemote(self):
  472. """
  473. L{CommandDispatcher.callRemote} should emit a properly formatted '_ask'
  474. box to its boxSender and record an outstanding L{Deferred}. When a
  475. corresponding '_answer' packet is received, the L{Deferred} should be
  476. fired, and the results translated via the given L{Command}'s response
  477. de-serialization.
  478. """
  479. D = self.dispatcher.callRemote(Hello, hello=b'world')
  480. self.assertEqual(self.sender.sentBoxes,
  481. [amp.AmpBox(_command=b"hello",
  482. _ask=b"1",
  483. hello=b"world")])
  484. answers = []
  485. D.addCallback(answers.append)
  486. self.assertEqual(answers, [])
  487. self.dispatcher.ampBoxReceived(amp.AmpBox({b'hello': b"yay",
  488. b'print': b"ignored",
  489. b'_answer': b"1"}))
  490. self.assertEqual(answers, [dict(hello=b"yay",
  491. Print=u"ignored")])
  492. def _localCallbackErrorLoggingTest(self, callResult):
  493. """
  494. Verify that C{callResult} completes with a L{None} result and that an
  495. unhandled error has been logged.
  496. """
  497. finalResult = []
  498. callResult.addBoth(finalResult.append)
  499. self.assertEqual(1, len(self.sender.unhandledErrors))
  500. self.assertIsInstance(
  501. self.sender.unhandledErrors[0].value, ZeroDivisionError)
  502. self.assertEqual([None], finalResult)
  503. def test_callRemoteSuccessLocalCallbackErrorLogging(self):
  504. """
  505. If the last callback on the L{Deferred} returned by C{callRemote} (added
  506. by application code calling C{callRemote}) fails, the failure is passed
  507. to the sender's C{unhandledError} method.
  508. """
  509. self.sender.expectError()
  510. callResult = self.dispatcher.callRemote(Hello, hello=b'world')
  511. callResult.addCallback(lambda result: 1 // 0)
  512. self.dispatcher.ampBoxReceived(amp.AmpBox({
  513. b'hello': b"yay", b'print': b"ignored", b'_answer': b"1"}))
  514. self._localCallbackErrorLoggingTest(callResult)
  515. def test_callRemoteErrorLocalCallbackErrorLogging(self):
  516. """
  517. Like L{test_callRemoteSuccessLocalCallbackErrorLogging}, but for the
  518. case where the L{Deferred} returned by C{callRemote} fails.
  519. """
  520. self.sender.expectError()
  521. callResult = self.dispatcher.callRemote(Hello, hello=b'world')
  522. callResult.addErrback(lambda result: 1 // 0)
  523. self.dispatcher.ampBoxReceived(amp.AmpBox({
  524. b'_error': b'1', b'_error_code': b'bugs',
  525. b'_error_description': b'stuff'}))
  526. self._localCallbackErrorLoggingTest(callResult)
  527. class SimpleGreeting(amp.Command):
  528. """
  529. A very simple greeting command that uses a few basic argument types.
  530. """
  531. commandName = b'simple'
  532. arguments = [(b'greeting', amp.Unicode()),
  533. (b'cookie', amp.Integer())]
  534. response = [(b'cookieplus', amp.Integer())]
  535. class TestLocator(amp.CommandLocator):
  536. """
  537. A locator which implements a responder to the 'simple' command.
  538. """
  539. def __init__(self):
  540. self.greetings = []
  541. def greetingResponder(self, greeting, cookie):
  542. self.greetings.append((greeting, cookie))
  543. return dict(cookieplus=cookie + 3)
  544. greetingResponder = SimpleGreeting.responder(greetingResponder)
  545. class OverridingLocator(TestLocator):
  546. """
  547. A locator which overrides the responder to the 'simple' command.
  548. """
  549. def greetingResponder(self, greeting, cookie):
  550. """
  551. Return a different cookieplus than L{TestLocator.greetingResponder}.
  552. """
  553. self.greetings.append((greeting, cookie))
  554. return dict(cookieplus=cookie + 4)
  555. greetingResponder = SimpleGreeting.responder(greetingResponder)
  556. class InheritingLocator(OverridingLocator):
  557. """
  558. This locator should inherit the responder from L{OverridingLocator}.
  559. """
  560. class OverrideLocatorAMP(amp.AMP):
  561. def __init__(self):
  562. amp.AMP.__init__(self)
  563. self.customResponder = object()
  564. self.expectations = {b"custom": self.customResponder}
  565. self.greetings = []
  566. def lookupFunction(self, name):
  567. """
  568. Override the deprecated lookupFunction function.
  569. """
  570. if name in self.expectations:
  571. result = self.expectations[name]
  572. return result
  573. else:
  574. return super(OverrideLocatorAMP, self).lookupFunction(name)
  575. def greetingResponder(self, greeting, cookie):
  576. self.greetings.append((greeting, cookie))
  577. return dict(cookieplus=cookie + 3)
  578. greetingResponder = SimpleGreeting.responder(greetingResponder)
  579. class CommandLocatorTests(unittest.TestCase):
  580. """
  581. The CommandLocator should enable users to specify responders to commands as
  582. functions that take structured objects, annotated with metadata.
  583. """
  584. def _checkSimpleGreeting(self, locatorClass, expected):
  585. """
  586. Check that a locator of type C{locatorClass} finds a responder
  587. for command named I{simple} and that the found responder answers
  588. with the C{expected} result to a C{SimpleGreeting<"ni hao", 5>}
  589. command.
  590. """
  591. locator = locatorClass()
  592. responderCallable = locator.locateResponder(b"simple")
  593. result = responderCallable(amp.Box(greeting=b"ni hao", cookie=b"5"))
  594. def done(values):
  595. self.assertEqual(values, amp.AmpBox(cookieplus=intToBytes(expected)))
  596. return result.addCallback(done)
  597. def test_responderDecorator(self):
  598. """
  599. A method on a L{CommandLocator} subclass decorated with a L{Command}
  600. subclass's L{responder} decorator should be returned from
  601. locateResponder, wrapped in logic to serialize and deserialize its
  602. arguments.
  603. """
  604. return self._checkSimpleGreeting(TestLocator, 8)
  605. def test_responderOverriding(self):
  606. """
  607. L{CommandLocator} subclasses can override a responder inherited from
  608. a base class by using the L{Command.responder} decorator to register
  609. a new responder method.
  610. """
  611. return self._checkSimpleGreeting(OverridingLocator, 9)
  612. def test_responderInheritance(self):
  613. """
  614. Responder lookup follows the same rules as normal method lookup
  615. rules, particularly with respect to inheritance.
  616. """
  617. return self._checkSimpleGreeting(InheritingLocator, 9)
  618. def test_lookupFunctionDeprecatedOverride(self):
  619. """
  620. Subclasses which override locateResponder under its old name,
  621. lookupFunction, should have the override invoked instead. (This tests
  622. an AMP subclass, because in the version of the code that could invoke
  623. this deprecated code path, there was no L{CommandLocator}.)
  624. """
  625. locator = OverrideLocatorAMP()
  626. customResponderObject = self.assertWarns(
  627. PendingDeprecationWarning,
  628. "Override locateResponder, not lookupFunction.",
  629. __file__, lambda : locator.locateResponder(b"custom"))
  630. self.assertEqual(locator.customResponder, customResponderObject)
  631. # Make sure upcalling works too
  632. normalResponderObject = self.assertWarns(
  633. PendingDeprecationWarning,
  634. "Override locateResponder, not lookupFunction.",
  635. __file__, lambda : locator.locateResponder(b"simple"))
  636. result = normalResponderObject(amp.Box(greeting=b"ni hao", cookie=b"5"))
  637. def done(values):
  638. self.assertEqual(values, amp.AmpBox(cookieplus=b'8'))
  639. return result.addCallback(done)
  640. def test_lookupFunctionDeprecatedInvoke(self):
  641. """
  642. Invoking locateResponder under its old name, lookupFunction, should
  643. emit a deprecation warning, but do the same thing.
  644. """
  645. locator = TestLocator()
  646. responderCallable = self.assertWarns(
  647. PendingDeprecationWarning,
  648. "Call locateResponder, not lookupFunction.", __file__,
  649. lambda : locator.lookupFunction(b"simple"))
  650. result = responderCallable(amp.Box(greeting=b"ni hao", cookie=b"5"))
  651. def done(values):
  652. self.assertEqual(values, amp.AmpBox(cookieplus=b'8'))
  653. return result.addCallback(done)
  654. SWITCH_CLIENT_DATA = b'Success!'
  655. SWITCH_SERVER_DATA = b'No, really. Success.'
  656. class BinaryProtocolTests(unittest.TestCase):
  657. """
  658. Tests for L{amp.BinaryBoxProtocol}.
  659. @ivar _boxSender: After C{startReceivingBoxes} is called, the L{IBoxSender}
  660. which was passed to it.
  661. """
  662. def setUp(self):
  663. """
  664. Keep track of all boxes received by this test in its capacity as an
  665. L{IBoxReceiver} implementor.
  666. """
  667. self.boxes = []
  668. self.data = []
  669. def startReceivingBoxes(self, sender):
  670. """
  671. Implement L{IBoxReceiver.startReceivingBoxes} to just remember the
  672. value passed in.
  673. """
  674. self._boxSender = sender
  675. def ampBoxReceived(self, box):
  676. """
  677. A box was received by the protocol.
  678. """
  679. self.boxes.append(box)
  680. stopReason = None
  681. def stopReceivingBoxes(self, reason):
  682. """
  683. Record the reason that we stopped receiving boxes.
  684. """
  685. self.stopReason = reason
  686. # fake ITransport
  687. def getPeer(self):
  688. return 'no peer'
  689. def getHost(self):
  690. return 'no host'
  691. def write(self, data):
  692. self.assertIsInstance(data, bytes)
  693. self.data.append(data)
  694. def test_startReceivingBoxes(self):
  695. """
  696. When L{amp.BinaryBoxProtocol} is connected to a transport, it calls
  697. C{startReceivingBoxes} on its L{IBoxReceiver} with itself as the
  698. L{IBoxSender} parameter.
  699. """
  700. protocol = amp.BinaryBoxProtocol(self)
  701. protocol.makeConnection(None)
  702. self.assertIs(self._boxSender, protocol)
  703. def test_sendBoxInStartReceivingBoxes(self):
  704. """
  705. The L{IBoxReceiver} which is started when L{amp.BinaryBoxProtocol} is
  706. connected to a transport can call C{sendBox} on the L{IBoxSender}
  707. passed to it before C{startReceivingBoxes} returns and have that box
  708. sent.
  709. """
  710. class SynchronouslySendingReceiver:
  711. def startReceivingBoxes(self, sender):
  712. sender.sendBox(amp.Box({b'foo': b'bar'}))
  713. transport = StringTransport()
  714. protocol = amp.BinaryBoxProtocol(SynchronouslySendingReceiver())
  715. protocol.makeConnection(transport)
  716. self.assertEqual(
  717. transport.value(),
  718. b'\x00\x03foo\x00\x03bar\x00\x00')
  719. def test_receiveBoxStateMachine(self):
  720. """
  721. When a binary box protocol receives:
  722. * a key
  723. * a value
  724. * an empty string
  725. it should emit a box and send it to its boxReceiver.
  726. """
  727. a = amp.BinaryBoxProtocol(self)
  728. a.stringReceived(b"hello")
  729. a.stringReceived(b"world")
  730. a.stringReceived(b"")
  731. self.assertEqual(self.boxes, [amp.AmpBox(hello=b"world")])
  732. def test_firstBoxFirstKeyExcessiveLength(self):
  733. """
  734. L{amp.BinaryBoxProtocol} drops its connection if the length prefix for
  735. the first a key it receives is larger than 255.
  736. """
  737. transport = StringTransport()
  738. protocol = amp.BinaryBoxProtocol(self)
  739. protocol.makeConnection(transport)
  740. protocol.dataReceived(b'\x01\x00')
  741. self.assertTrue(transport.disconnecting)
  742. def test_firstBoxSubsequentKeyExcessiveLength(self):
  743. """
  744. L{amp.BinaryBoxProtocol} drops its connection if the length prefix for
  745. a subsequent key in the first box it receives is larger than 255.
  746. """
  747. transport = StringTransport()
  748. protocol = amp.BinaryBoxProtocol(self)
  749. protocol.makeConnection(transport)
  750. protocol.dataReceived(b'\x00\x01k\x00\x01v')
  751. self.assertFalse(transport.disconnecting)
  752. protocol.dataReceived(b'\x01\x00')
  753. self.assertTrue(transport.disconnecting)
  754. def test_subsequentBoxFirstKeyExcessiveLength(self):
  755. """
  756. L{amp.BinaryBoxProtocol} drops its connection if the length prefix for
  757. the first key in a subsequent box it receives is larger than 255.
  758. """
  759. transport = StringTransport()
  760. protocol = amp.BinaryBoxProtocol(self)
  761. protocol.makeConnection(transport)
  762. protocol.dataReceived(b'\x00\x01k\x00\x01v\x00\x00')
  763. self.assertFalse(transport.disconnecting)
  764. protocol.dataReceived(b'\x01\x00')
  765. self.assertTrue(transport.disconnecting)
  766. def test_excessiveKeyFailure(self):
  767. """
  768. If L{amp.BinaryBoxProtocol} disconnects because it received a key
  769. length prefix which was too large, the L{IBoxReceiver}'s
  770. C{stopReceivingBoxes} method is called with a L{TooLong} failure.
  771. """
  772. protocol = amp.BinaryBoxProtocol(self)
  773. protocol.makeConnection(StringTransport())
  774. protocol.dataReceived(b'\x01\x00')
  775. protocol.connectionLost(
  776. Failure(error.ConnectionDone("simulated connection done")))
  777. self.stopReason.trap(amp.TooLong)
  778. self.assertTrue(self.stopReason.value.isKey)
  779. self.assertFalse(self.stopReason.value.isLocal)
  780. self.assertIsNone(self.stopReason.value.value)
  781. self.assertIsNone(self.stopReason.value.keyName)
  782. def test_unhandledErrorWithTransport(self):
  783. """
  784. L{amp.BinaryBoxProtocol.unhandledError} logs the failure passed to it
  785. and disconnects its transport.
  786. """
  787. transport = StringTransport()
  788. protocol = amp.BinaryBoxProtocol(self)
  789. protocol.makeConnection(transport)
  790. protocol.unhandledError(Failure(RuntimeError("Fake error")))
  791. self.assertEqual(1, len(self.flushLoggedErrors(RuntimeError)))
  792. self.assertTrue(transport.disconnecting)
  793. def test_unhandledErrorWithoutTransport(self):
  794. """
  795. L{amp.BinaryBoxProtocol.unhandledError} completes without error when
  796. there is no associated transport.
  797. """
  798. protocol = amp.BinaryBoxProtocol(self)
  799. protocol.makeConnection(StringTransport())
  800. protocol.connectionLost(Failure(Exception("Simulated")))
  801. protocol.unhandledError(Failure(RuntimeError("Fake error")))
  802. self.assertEqual(1, len(self.flushLoggedErrors(RuntimeError)))
  803. def test_receiveBoxData(self):
  804. """
  805. When a binary box protocol receives the serialized form of an AMP box,
  806. it should emit a similar box to its boxReceiver.
  807. """
  808. a = amp.BinaryBoxProtocol(self)
  809. a.dataReceived(amp.Box({b"testKey": b"valueTest",
  810. b"anotherKey": b"anotherValue"}).serialize())
  811. self.assertEqual(self.boxes,
  812. [amp.Box({b"testKey": b"valueTest",
  813. b"anotherKey": b"anotherValue"})])
  814. def test_receiveLongerBoxData(self):
  815. """
  816. An L{amp.BinaryBoxProtocol} can receive serialized AMP boxes with
  817. values of up to (2 ** 16 - 1) bytes.
  818. """
  819. length = (2 ** 16 - 1)
  820. value = b'x' * length
  821. transport = StringTransport()
  822. protocol = amp.BinaryBoxProtocol(self)
  823. protocol.makeConnection(transport)
  824. protocol.dataReceived(amp.Box({'k': value}).serialize())
  825. self.assertEqual(self.boxes, [amp.Box({'k': value})])
  826. self.assertFalse(transport.disconnecting)
  827. def test_sendBox(self):
  828. """
  829. When a binary box protocol sends a box, it should emit the serialized
  830. bytes of that box to its transport.
  831. """
  832. a = amp.BinaryBoxProtocol(self)
  833. a.makeConnection(self)
  834. aBox = amp.Box({b"testKey": b"valueTest",
  835. b"someData": b"hello"})
  836. a.makeConnection(self)
  837. a.sendBox(aBox)
  838. self.assertEqual(b''.join(self.data), aBox.serialize())
  839. def test_connectionLostStopSendingBoxes(self):
  840. """
  841. When a binary box protocol loses its connection, it should notify its
  842. box receiver that it has stopped receiving boxes.
  843. """
  844. a = amp.BinaryBoxProtocol(self)
  845. a.makeConnection(self)
  846. connectionFailure = Failure(RuntimeError())
  847. a.connectionLost(connectionFailure)
  848. self.assertIs(self.stopReason, connectionFailure)
  849. def test_protocolSwitch(self):
  850. """
  851. L{BinaryBoxProtocol} has the capacity to switch to a different protocol
  852. on a box boundary. When a protocol is in the process of switching, it
  853. cannot receive traffic.
  854. """
  855. otherProto = TestProto(None, b"outgoing data")
  856. test = self
  857. class SwitchyReceiver:
  858. switched = False
  859. def startReceivingBoxes(self, sender):
  860. pass
  861. def ampBoxReceived(self, box):
  862. test.assertFalse(self.switched,
  863. "Should only receive one box!")
  864. self.switched = True
  865. a._lockForSwitch()
  866. a._switchTo(otherProto)
  867. a = amp.BinaryBoxProtocol(SwitchyReceiver())
  868. anyOldBox = amp.Box({b"include": b"lots",
  869. b"of": b"data"})
  870. a.makeConnection(self)
  871. # Include a 0-length box at the beginning of the next protocol's data,
  872. # to make sure that AMP doesn't eat the data or try to deliver extra
  873. # boxes either...
  874. moreThanOneBox = anyOldBox.serialize() + b"\x00\x00Hello, world!"
  875. a.dataReceived(moreThanOneBox)
  876. self.assertIs(otherProto.transport, self)
  877. self.assertEqual(b"".join(otherProto.data), b"\x00\x00Hello, world!")
  878. self.assertEqual(self.data, [b"outgoing data"])
  879. a.dataReceived(b"more data")
  880. self.assertEqual(b"".join(otherProto.data),
  881. b"\x00\x00Hello, world!more data")
  882. self.assertRaises(amp.ProtocolSwitched, a.sendBox, anyOldBox)
  883. def test_protocolSwitchEmptyBuffer(self):
  884. """
  885. After switching to a different protocol, if no extra bytes beyond
  886. the switch box were delivered, an empty string is not passed to the
  887. switched protocol's C{dataReceived} method.
  888. """
  889. a = amp.BinaryBoxProtocol(self)
  890. a.makeConnection(self)
  891. otherProto = TestProto(None, b"")
  892. a._switchTo(otherProto)
  893. self.assertEqual(otherProto.data, [])
  894. def test_protocolSwitchInvalidStates(self):
  895. """
  896. In order to make sure the protocol never gets any invalid data sent
  897. into the middle of a box, it must be locked for switching before it is
  898. switched. It can only be unlocked if the switch failed, and attempting
  899. to send a box while it is locked should raise an exception.
  900. """
  901. a = amp.BinaryBoxProtocol(self)
  902. a.makeConnection(self)
  903. sampleBox = amp.Box({b"some": b"data"})
  904. a._lockForSwitch()
  905. self.assertRaises(amp.ProtocolSwitched, a.sendBox, sampleBox)
  906. a._unlockFromSwitch()
  907. a.sendBox(sampleBox)
  908. self.assertEqual(b''.join(self.data), sampleBox.serialize())
  909. a._lockForSwitch()
  910. otherProto = TestProto(None, b"outgoing data")
  911. a._switchTo(otherProto)
  912. self.assertRaises(amp.ProtocolSwitched, a._unlockFromSwitch)
  913. def test_protocolSwitchLoseConnection(self):
  914. """
  915. When the protocol is switched, it should notify its nested protocol of
  916. disconnection.
  917. """
  918. class Loser(protocol.Protocol):
  919. reason = None
  920. def connectionLost(self, reason):
  921. self.reason = reason
  922. connectionLoser = Loser()
  923. a = amp.BinaryBoxProtocol(self)
  924. a.makeConnection(self)
  925. a._lockForSwitch()
  926. a._switchTo(connectionLoser)
  927. connectionFailure = Failure(RuntimeError())
  928. a.connectionLost(connectionFailure)
  929. self.assertEqual(connectionLoser.reason, connectionFailure)
  930. def test_protocolSwitchLoseClientConnection(self):
  931. """
  932. When the protocol is switched, it should notify its nested client
  933. protocol factory of disconnection.
  934. """
  935. class ClientLoser:
  936. reason = None
  937. def clientConnectionLost(self, connector, reason):
  938. self.reason = reason
  939. a = amp.BinaryBoxProtocol(self)
  940. connectionLoser = protocol.Protocol()
  941. clientLoser = ClientLoser()
  942. a.makeConnection(self)
  943. a._lockForSwitch()
  944. a._switchTo(connectionLoser, clientLoser)
  945. connectionFailure = Failure(RuntimeError())
  946. a.connectionLost(connectionFailure)
  947. self.assertEqual(clientLoser.reason, connectionFailure)
  948. class AMPTests(unittest.TestCase):
  949. def test_interfaceDeclarations(self):
  950. """
  951. The classes in the amp module ought to implement the interfaces that
  952. are declared for their benefit.
  953. """
  954. for interface, implementation in [(amp.IBoxSender, amp.BinaryBoxProtocol),
  955. (amp.IBoxReceiver, amp.BoxDispatcher),
  956. (amp.IResponderLocator, amp.CommandLocator),
  957. (amp.IResponderLocator, amp.SimpleStringLocator),
  958. (amp.IBoxSender, amp.AMP),
  959. (amp.IBoxReceiver, amp.AMP),
  960. (amp.IResponderLocator, amp.AMP)]:
  961. self.assertTrue(interface.implementedBy(implementation),
  962. "%s does not implements(%s)" % (implementation, interface))
  963. def test_helloWorld(self):
  964. """
  965. Verify that a simple command can be sent and its response received with
  966. the simple low-level string-based API.
  967. """
  968. c, s, p = connectedServerAndClient()
  969. L = []
  970. HELLO = b'world'
  971. c.sendHello(HELLO).addCallback(L.append)
  972. p.flush()
  973. self.assertEqual(L[0][b'hello'], HELLO)
  974. def test_wireFormatRoundTrip(self):
  975. """
  976. Verify that mixed-case, underscored and dashed arguments are mapped to
  977. their python names properly.
  978. """
  979. c, s, p = connectedServerAndClient()
  980. L = []
  981. HELLO = b'world'
  982. c.sendHello(HELLO).addCallback(L.append)
  983. p.flush()
  984. self.assertEqual(L[0][b'hello'], HELLO)
  985. def test_helloWorldUnicode(self):
  986. """
  987. Verify that unicode arguments can be encoded and decoded.
  988. """
  989. c, s, p = connectedServerAndClient(
  990. ServerClass=SimpleSymmetricCommandProtocol,
  991. ClientClass=SimpleSymmetricCommandProtocol)
  992. L = []
  993. HELLO = b'world'
  994. HELLO_UNICODE = u'wor\u1234ld'
  995. c.sendUnicodeHello(HELLO, HELLO_UNICODE).addCallback(L.append)
  996. p.flush()
  997. self.assertEqual(L[0]['hello'], HELLO)
  998. self.assertEqual(L[0]['Print'], HELLO_UNICODE)
  999. def test_callRemoteStringRequiresAnswerFalse(self):
  1000. """
  1001. L{BoxDispatcher.callRemoteString} returns L{None} if C{requiresAnswer}
  1002. is C{False}.
  1003. """
  1004. c, s, p = connectedServerAndClient()
  1005. ret = c.callRemoteString(b"WTF", requiresAnswer=False)
  1006. self.assertIsNone(ret)
  1007. def test_unknownCommandLow(self):
  1008. """
  1009. Verify that unknown commands using low-level APIs will be rejected with an
  1010. error, but will NOT terminate the connection.
  1011. """
  1012. c, s, p = connectedServerAndClient()
  1013. L = []
  1014. def clearAndAdd(e):
  1015. """
  1016. You can't propagate the error...
  1017. """
  1018. e.trap(amp.UnhandledCommand)
  1019. return "OK"
  1020. c.callRemoteString(b"WTF").addErrback(clearAndAdd).addCallback(L.append)
  1021. p.flush()
  1022. self.assertEqual(L.pop(), "OK")
  1023. HELLO = b'world'
  1024. c.sendHello(HELLO).addCallback(L.append)
  1025. p.flush()
  1026. self.assertEqual(L[0][b'hello'], HELLO)
  1027. def test_unknownCommandHigh(self):
  1028. """
  1029. Verify that unknown commands using high-level APIs will be rejected with an
  1030. error, but will NOT terminate the connection.
  1031. """
  1032. c, s, p = connectedServerAndClient()
  1033. L = []
  1034. def clearAndAdd(e):
  1035. """
  1036. You can't propagate the error...
  1037. """
  1038. e.trap(amp.UnhandledCommand)
  1039. return "OK"
  1040. c.callRemote(WTF).addErrback(clearAndAdd).addCallback(L.append)
  1041. p.flush()
  1042. self.assertEqual(L.pop(), "OK")
  1043. HELLO = b'world'
  1044. c.sendHello(HELLO).addCallback(L.append)
  1045. p.flush()
  1046. self.assertEqual(L[0][b'hello'], HELLO)
  1047. def test_brokenReturnValue(self):
  1048. """
  1049. It can be very confusing if you write some code which responds to a
  1050. command, but gets the return value wrong. Most commonly you end up
  1051. returning None instead of a dictionary.
  1052. Verify that if that happens, the framework logs a useful error.
  1053. """
  1054. L = []
  1055. SimpleSymmetricCommandProtocol().dispatchCommand(
  1056. amp.AmpBox(_command=BrokenReturn.commandName)).addErrback(L.append)
  1057. L[0].trap(amp.BadLocalReturn)
  1058. self.failUnlessIn('None', repr(L[0].value))
  1059. def test_unknownArgument(self):
  1060. """
  1061. Verify that unknown arguments are ignored, and not passed to a Python
  1062. function which can't accept them.
  1063. """
  1064. c, s, p = connectedServerAndClient(
  1065. ServerClass=SimpleSymmetricCommandProtocol,
  1066. ClientClass=SimpleSymmetricCommandProtocol)
  1067. L = []
  1068. HELLO = b'world'
  1069. # c.sendHello(HELLO).addCallback(L.append)
  1070. c.callRemote(FutureHello,
  1071. hello=HELLO,
  1072. bonus=b"I'm not in the book!").addCallback(
  1073. L.append)
  1074. p.flush()
  1075. self.assertEqual(L[0]['hello'], HELLO)
  1076. def test_simpleReprs(self):
  1077. """
  1078. Verify that the various Box objects repr properly, for debugging.
  1079. """
  1080. self.assertEqual(type(repr(amp._SwitchBox('a'))), str)
  1081. self.assertEqual(type(repr(amp.QuitBox())), str)
  1082. self.assertEqual(type(repr(amp.AmpBox())), str)
  1083. self.assertIn("AmpBox", repr(amp.AmpBox()))
  1084. def test_innerProtocolInRepr(self):
  1085. """
  1086. Verify that L{AMP} objects output their innerProtocol when set.
  1087. """
  1088. otherProto = TestProto(None, b"outgoing data")
  1089. a = amp.AMP()
  1090. a.innerProtocol = otherProto
  1091. self.assertEqual(
  1092. repr(a), "<AMP inner <TestProto #%d> at 0x%x>" % (
  1093. otherProto.instanceId, id(a)))
  1094. def test_innerProtocolNotInRepr(self):
  1095. """
  1096. Verify that L{AMP} objects do not output 'inner' when no innerProtocol
  1097. is set.
  1098. """
  1099. a = amp.AMP()
  1100. self.assertEqual(repr(a), "<AMP at 0x%x>" % (id(a),))
  1101. def test_simpleSSLRepr(self):
  1102. """
  1103. L{amp._TLSBox.__repr__} returns a string.
  1104. """
  1105. self.assertEqual(type(repr(amp._TLSBox())), str)
  1106. test_simpleSSLRepr.skip = skipSSL
  1107. def test_keyTooLong(self):
  1108. """
  1109. Verify that a key that is too long will immediately raise a synchronous
  1110. exception.
  1111. """
  1112. c, s, p = connectedServerAndClient()
  1113. x = "H" * (0xff+1)
  1114. tl = self.assertRaises(amp.TooLong,
  1115. c.callRemoteString, b"Hello",
  1116. **{x: b"hi"})
  1117. self.assertTrue(tl.isKey)
  1118. self.assertTrue(tl.isLocal)
  1119. self.assertIsNone(tl.keyName)
  1120. self.assertEqual(tl.value, x.encode("ascii"))
  1121. self.assertIn(str(len(x)), repr(tl))
  1122. self.assertIn("key", repr(tl))
  1123. def test_valueTooLong(self):
  1124. """
  1125. Verify that attempting to send value longer than 64k will immediately
  1126. raise an exception.
  1127. """
  1128. c, s, p = connectedServerAndClient()
  1129. x = b"H" * (0xffff+1)
  1130. tl = self.assertRaises(amp.TooLong, c.sendHello, x)
  1131. p.flush()
  1132. self.assertFalse(tl.isKey)
  1133. self.assertTrue(tl.isLocal)
  1134. self.assertEqual(tl.keyName, b'hello')
  1135. self.failUnlessIdentical(tl.value, x)
  1136. self.assertIn(str(len(x)), repr(tl))
  1137. self.assertIn("value", repr(tl))
  1138. self.assertIn('hello', repr(tl))
  1139. def test_helloWorldCommand(self):
  1140. """
  1141. Verify that a simple command can be sent and its response received with
  1142. the high-level value parsing API.
  1143. """
  1144. c, s, p = connectedServerAndClient(
  1145. ServerClass=SimpleSymmetricCommandProtocol,
  1146. ClientClass=SimpleSymmetricCommandProtocol)
  1147. L = []
  1148. HELLO = b'world'
  1149. c.sendHello(HELLO).addCallback(L.append)
  1150. p.flush()
  1151. self.assertEqual(L[0]['hello'], HELLO)
  1152. def test_helloErrorHandling(self):
  1153. """
  1154. Verify that if a known error type is raised and handled, it will be
  1155. properly relayed to the other end of the connection and translated into
  1156. an exception, and no error will be logged.
  1157. """
  1158. L=[]
  1159. c, s, p = connectedServerAndClient(
  1160. ServerClass=SimpleSymmetricCommandProtocol,
  1161. ClientClass=SimpleSymmetricCommandProtocol)
  1162. HELLO = b'fuck you'
  1163. c.sendHello(HELLO).addErrback(L.append)
  1164. p.flush()
  1165. L[0].trap(UnfriendlyGreeting)
  1166. self.assertEqual(str(L[0].value), "Don't be a dick.")
  1167. def test_helloFatalErrorHandling(self):
  1168. """
  1169. Verify that if a known, fatal error type is raised and handled, it will
  1170. be properly relayed to the other end of the connection and translated
  1171. into an exception, no error will be logged, and the connection will be
  1172. terminated.
  1173. """
  1174. L=[]
  1175. c, s, p = connectedServerAndClient(
  1176. ServerClass=SimpleSymmetricCommandProtocol,
  1177. ClientClass=SimpleSymmetricCommandProtocol)
  1178. HELLO = b'die'
  1179. c.sendHello(HELLO).addErrback(L.append)
  1180. p.flush()
  1181. L.pop().trap(DeathThreat)
  1182. c.sendHello(HELLO).addErrback(L.append)
  1183. p.flush()
  1184. L.pop().trap(error.ConnectionDone)
  1185. def test_helloNoErrorHandling(self):
  1186. """
  1187. Verify that if an unknown error type is raised, it will be relayed to
  1188. the other end of the connection and translated into an exception, it
  1189. will be logged, and then the connection will be dropped.
  1190. """
  1191. L=[]
  1192. c, s, p = connectedServerAndClient(
  1193. ServerClass=SimpleSymmetricCommandProtocol,
  1194. ClientClass=SimpleSymmetricCommandProtocol)
  1195. HELLO = THING_I_DONT_UNDERSTAND
  1196. c.sendHello(HELLO).addErrback(L.append)
  1197. p.flush()
  1198. ure = L.pop()
  1199. ure.trap(amp.UnknownRemoteError)
  1200. c.sendHello(HELLO).addErrback(L.append)
  1201. cl = L.pop()
  1202. cl.trap(error.ConnectionDone)
  1203. # The exception should have been logged.
  1204. self.assertTrue(self.flushLoggedErrors(ThingIDontUnderstandError))
  1205. def test_lateAnswer(self):
  1206. """
  1207. Verify that a command that does not get answered until after the
  1208. connection terminates will not cause any errors.
  1209. """
  1210. c, s, p = connectedServerAndClient(
  1211. ServerClass=SimpleSymmetricCommandProtocol,
  1212. ClientClass=SimpleSymmetricCommandProtocol)
  1213. L = []
  1214. c.callRemote(WaitForever).addErrback(L.append)
  1215. p.flush()
  1216. self.assertEqual(L, [])
  1217. s.transport.loseConnection()
  1218. p.flush()
  1219. L.pop().trap(error.ConnectionDone)
  1220. # Just make sure that it doesn't error...
  1221. s.waiting.callback({})
  1222. return s.waiting
  1223. def test_requiresNoAnswer(self):
  1224. """
  1225. Verify that a command that requires no answer is run.
  1226. """
  1227. c, s, p = connectedServerAndClient(
  1228. ServerClass=SimpleSymmetricCommandProtocol,
  1229. ClientClass=SimpleSymmetricCommandProtocol)
  1230. HELLO = b'world'
  1231. c.callRemote(NoAnswerHello, hello=HELLO)
  1232. p.flush()
  1233. self.assertTrue(s.greeted)
  1234. def test_requiresNoAnswerFail(self):
  1235. """
  1236. Verify that commands sent after a failed no-answer request do not complete.
  1237. """
  1238. L=[]
  1239. c, s, p = connectedServerAndClient(
  1240. ServerClass=SimpleSymmetricCommandProtocol,
  1241. ClientClass=SimpleSymmetricCommandProtocol)
  1242. HELLO = b'fuck you'
  1243. c.callRemote(NoAnswerHello, hello=HELLO)
  1244. p.flush()
  1245. # This should be logged locally.
  1246. self.assertTrue(self.flushLoggedErrors(amp.RemoteAmpError))
  1247. HELLO = b'world'
  1248. c.callRemote(Hello, hello=HELLO).addErrback(L.append)
  1249. p.flush()
  1250. L.pop().trap(error.ConnectionDone)
  1251. self.assertFalse(s.greeted)
  1252. def test_noAnswerResponderBadAnswer(self):
  1253. """
  1254. Verify that responders of requiresAnswer=False commands have to return
  1255. a dictionary anyway.
  1256. (requiresAnswer is a hint from the _client_ - the server may be called
  1257. upon to answer commands in any case, if the client wants to know when
  1258. they complete.)
  1259. """
  1260. c, s, p = connectedServerAndClient(
  1261. ServerClass=BadNoAnswerCommandProtocol,
  1262. ClientClass=SimpleSymmetricCommandProtocol)
  1263. c.callRemote(NoAnswerHello, hello=b"hello")
  1264. p.flush()
  1265. le = self.flushLoggedErrors(amp.BadLocalReturn)
  1266. self.assertEqual(len(le), 1)
  1267. def test_noAnswerResponderAskedForAnswer(self):
  1268. """
  1269. Verify that responders with requiresAnswer=False will actually respond
  1270. if the client sets requiresAnswer=True. In other words, verify that
  1271. requiresAnswer is a hint honored only by the client.
  1272. """
  1273. c, s, p = connectedServerAndClient(
  1274. ServerClass=NoAnswerCommandProtocol,
  1275. ClientClass=SimpleSymmetricCommandProtocol)
  1276. L = []
  1277. c.callRemote(Hello, hello=b"Hello!").addCallback(L.append)
  1278. p.flush()
  1279. self.assertEqual(len(L), 1)
  1280. self.assertEqual(L, [dict(hello=b"Hello!-noanswer",
  1281. Print=None)]) # Optional response argument
  1282. def test_ampListCommand(self):
  1283. """
  1284. Test encoding of an argument that uses the AmpList encoding.
  1285. """
  1286. c, s, p = connectedServerAndClient(
  1287. ServerClass=SimpleSymmetricCommandProtocol,
  1288. ClientClass=SimpleSymmetricCommandProtocol)
  1289. L = []
  1290. c.callRemote(GetList, length=10).addCallback(L.append)
  1291. p.flush()
  1292. values = L.pop().get('body')
  1293. self.assertEqual(values, [{'x': 1}] * 10)
  1294. def test_optionalAmpListOmitted(self):
  1295. """
  1296. Sending a command with an omitted AmpList argument that is
  1297. designated as optional does not raise an InvalidSignature error.
  1298. """
  1299. c, s, p = connectedServerAndClient(
  1300. ServerClass=SimpleSymmetricCommandProtocol,
  1301. ClientClass=SimpleSymmetricCommandProtocol)
  1302. L = []
  1303. c.callRemote(DontRejectMe, magicWord=u'please').addCallback(L.append)
  1304. p.flush()
  1305. response = L.pop().get('response')
  1306. self.assertEqual(response, 'list omitted')
  1307. def test_optionalAmpListPresent(self):
  1308. """
  1309. Sanity check that optional AmpList arguments are processed normally.
  1310. """
  1311. c, s, p = connectedServerAndClient(
  1312. ServerClass=SimpleSymmetricCommandProtocol,
  1313. ClientClass=SimpleSymmetricCommandProtocol)
  1314. L = []
  1315. c.callRemote(DontRejectMe, magicWord=u'please',
  1316. list=[{'name': u'foo'}]).addCallback(L.append)
  1317. p.flush()
  1318. response = L.pop().get('response')
  1319. self.assertEqual(response, 'foo accepted')
  1320. def test_failEarlyOnArgSending(self):
  1321. """
  1322. Verify that if we pass an invalid argument list (omitting an argument),
  1323. an exception will be raised.
  1324. """
  1325. self.assertRaises(amp.InvalidSignature, Hello)
  1326. def test_doubleProtocolSwitch(self):
  1327. """
  1328. As a debugging aid, a protocol system should raise a
  1329. L{ProtocolSwitched} exception when asked to switch a protocol that is
  1330. already switched.
  1331. """
  1332. serverDeferred = defer.Deferred()
  1333. serverProto = SimpleSymmetricCommandProtocol(serverDeferred)
  1334. clientDeferred = defer.Deferred()
  1335. clientProto = SimpleSymmetricCommandProtocol(clientDeferred)
  1336. c, s, p = connectedServerAndClient(ServerClass=lambda: serverProto,
  1337. ClientClass=lambda: clientProto)
  1338. def switched(result):
  1339. self.assertRaises(amp.ProtocolSwitched, c.switchToTestProtocol)
  1340. self.testSucceeded = True
  1341. c.switchToTestProtocol().addCallback(switched)
  1342. p.flush()
  1343. self.assertTrue(self.testSucceeded)
  1344. def test_protocolSwitch(self, switcher=SimpleSymmetricCommandProtocol,
  1345. spuriousTraffic=False,
  1346. spuriousError=False):
  1347. """
  1348. Verify that it is possible to switch to another protocol mid-connection and
  1349. send data to it successfully.
  1350. """
  1351. self.testSucceeded = False
  1352. serverDeferred = defer.Deferred()
  1353. serverProto = switcher(serverDeferred)
  1354. clientDeferred = defer.Deferred()
  1355. clientProto = switcher(clientDeferred)
  1356. c, s, p = connectedServerAndClient(ServerClass=lambda: serverProto,
  1357. ClientClass=lambda: clientProto)
  1358. if spuriousTraffic:
  1359. wfdr = [] # remote
  1360. c.callRemote(WaitForever).addErrback(wfdr.append)
  1361. switchDeferred = c.switchToTestProtocol()
  1362. if spuriousTraffic:
  1363. self.assertRaises(amp.ProtocolSwitched, c.sendHello, b'world')
  1364. def cbConnsLost(info):
  1365. ((serverSuccess, serverData), (clientSuccess, clientData)) = info
  1366. self.assertTrue(serverSuccess)
  1367. self.assertTrue(clientSuccess)
  1368. self.assertEqual(b''.join(serverData), SWITCH_CLIENT_DATA)
  1369. self.assertEqual(b''.join(clientData), SWITCH_SERVER_DATA)
  1370. self.testSucceeded = True
  1371. def cbSwitch(proto):
  1372. return defer.DeferredList(
  1373. [serverDeferred, clientDeferred]).addCallback(cbConnsLost)
  1374. switchDeferred.addCallback(cbSwitch)
  1375. p.flush()
  1376. if serverProto.maybeLater is not None:
  1377. serverProto.maybeLater.callback(serverProto.maybeLaterProto)
  1378. p.flush()
  1379. if spuriousTraffic:
  1380. # switch is done here; do this here to make sure that if we're
  1381. # going to corrupt the connection, we do it before it's closed.
  1382. if spuriousError:
  1383. s.waiting.errback(amp.RemoteAmpError(
  1384. b"SPURIOUS",
  1385. "Here's some traffic in the form of an error."))
  1386. else:
  1387. s.waiting.callback({})
  1388. p.flush()
  1389. c.transport.loseConnection() # close it
  1390. p.flush()
  1391. self.assertTrue(self.testSucceeded)
  1392. def test_protocolSwitchDeferred(self):
  1393. """
  1394. Verify that protocol-switching even works if the value returned from
  1395. the command that does the switch is deferred.
  1396. """
  1397. return self.test_protocolSwitch(switcher=DeferredSymmetricCommandProtocol)
  1398. def test_protocolSwitchFail(self, switcher=SimpleSymmetricCommandProtocol):
  1399. """
  1400. Verify that if we try to switch protocols and it fails, the connection
  1401. stays up and we can go back to speaking AMP.
  1402. """
  1403. self.testSucceeded = False
  1404. serverDeferred = defer.Deferred()
  1405. serverProto = switcher(serverDeferred)
  1406. clientDeferred = defer.Deferred()
  1407. clientProto = switcher(clientDeferred)
  1408. c, s, p = connectedServerAndClient(ServerClass=lambda: serverProto,
  1409. ClientClass=lambda: clientProto)
  1410. L = []
  1411. c.switchToTestProtocol(fail=True).addErrback(L.append)
  1412. p.flush()
  1413. L.pop().trap(UnknownProtocol)
  1414. self.assertFalse(self.testSucceeded)
  1415. # It's a known error, so let's send a "hello" on the same connection;
  1416. # it should work.
  1417. c.sendHello(b'world').addCallback(L.append)
  1418. p.flush()
  1419. self.assertEqual(L.pop()['hello'], b'world')
  1420. def test_trafficAfterSwitch(self):
  1421. """
  1422. Verify that attempts to send traffic after a switch will not corrupt
  1423. the nested protocol.
  1424. """
  1425. return self.test_protocolSwitch(spuriousTraffic=True)
  1426. def test_errorAfterSwitch(self):
  1427. """
  1428. Returning an error after a protocol switch should record the underlying
  1429. error.
  1430. """
  1431. return self.test_protocolSwitch(spuriousTraffic=True,
  1432. spuriousError=True)
  1433. def test_quitBoxQuits(self):
  1434. """
  1435. Verify that commands with a responseType of QuitBox will in fact
  1436. terminate the connection.
  1437. """
  1438. c, s, p = connectedServerAndClient(
  1439. ServerClass=SimpleSymmetricCommandProtocol,
  1440. ClientClass=SimpleSymmetricCommandProtocol)
  1441. L = []
  1442. HELLO = b'world'
  1443. GOODBYE = b'everyone'
  1444. c.sendHello(HELLO).addCallback(L.append)
  1445. p.flush()
  1446. self.assertEqual(L.pop()['hello'], HELLO)
  1447. c.callRemote(Goodbye).addCallback(L.append)
  1448. p.flush()
  1449. self.assertEqual(L.pop()['goodbye'], GOODBYE)
  1450. c.sendHello(HELLO).addErrback(L.append)
  1451. L.pop().trap(error.ConnectionDone)
  1452. def test_basicLiteralEmit(self):
  1453. """
  1454. Verify that the command dictionaries for a callRemoteN look correct
  1455. after being serialized and parsed.
  1456. """
  1457. c, s, p = connectedServerAndClient()
  1458. L = []
  1459. s.ampBoxReceived = L.append
  1460. c.callRemote(Hello, hello=b'hello test', mixedCase=b'mixed case arg test',
  1461. dash_arg=b'x', underscore_arg=b'y')
  1462. p.flush()
  1463. self.assertEqual(len(L), 1)
  1464. for k, v in [(b'_command', Hello.commandName),
  1465. (b'hello', b'hello test'),
  1466. (b'mixedCase', b'mixed case arg test'),
  1467. (b'dash-arg', b'x'),
  1468. (b'underscore_arg', b'y')]:
  1469. self.assertEqual(L[-1].pop(k), v)
  1470. L[-1].pop(b'_ask')
  1471. self.assertEqual(L[-1], {})
  1472. def test_basicStructuredEmit(self):
  1473. """
  1474. Verify that a call similar to basicLiteralEmit's is handled properly with
  1475. high-level quoting and passing to Python methods, and that argument
  1476. names are correctly handled.
  1477. """
  1478. L = []
  1479. class StructuredHello(amp.AMP):
  1480. def h(self, *a, **k):
  1481. L.append((a, k))
  1482. return dict(hello=b'aaa')
  1483. Hello.responder(h)
  1484. c, s, p = connectedServerAndClient(ServerClass=StructuredHello)
  1485. c.callRemote(Hello, hello=b'hello test', mixedCase=b'mixed case arg test',
  1486. dash_arg=b'x', underscore_arg=b'y').addCallback(L.append)
  1487. p.flush()
  1488. self.assertEqual(len(L), 2)
  1489. self.assertEqual(L[0],
  1490. ((), dict(
  1491. hello=b'hello test',
  1492. mixedCase=b'mixed case arg test',
  1493. dash_arg=b'x',
  1494. underscore_arg=b'y',
  1495. From=s.transport.getPeer(),
  1496. # XXX - should optional arguments just not be passed?
  1497. # passing None seems a little odd, looking at the way it
  1498. # turns out here... -glyph
  1499. Print=None,
  1500. optional=None,
  1501. )))
  1502. self.assertEqual(L[1], dict(Print=None, hello=b'aaa'))
  1503. class PretendRemoteCertificateAuthority:
  1504. def checkIsPretendRemote(self):
  1505. return True
  1506. class IOSimCert:
  1507. verifyCount = 0
  1508. def options(self, *ign):
  1509. return self
  1510. def iosimVerify(self, otherCert):
  1511. """
  1512. This isn't a real certificate, and wouldn't work on a real socket, but
  1513. iosim specifies a different API so that we don't have to do any crypto
  1514. math to demonstrate that the right functions get called in the right
  1515. places.
  1516. """
  1517. assert otherCert is self
  1518. self.verifyCount += 1
  1519. return True
  1520. class OKCert(IOSimCert):
  1521. def options(self, x):
  1522. assert x.checkIsPretendRemote()
  1523. return self
  1524. class GrumpyCert(IOSimCert):
  1525. def iosimVerify(self, otherCert):
  1526. self.verifyCount += 1
  1527. return False
  1528. class DroppyCert(IOSimCert):
  1529. def __init__(self, toDrop):
  1530. self.toDrop = toDrop
  1531. def iosimVerify(self, otherCert):
  1532. self.verifyCount += 1
  1533. self.toDrop.loseConnection()
  1534. return True
  1535. class SecurableProto(FactoryNotifier):
  1536. factory = None
  1537. def verifyFactory(self):
  1538. return [PretendRemoteCertificateAuthority()]
  1539. def getTLSVars(self):
  1540. cert = self.certFactory()
  1541. verify = self.verifyFactory()
  1542. return dict(
  1543. tls_localCertificate=cert,
  1544. tls_verifyAuthorities=verify)
  1545. amp.StartTLS.responder(getTLSVars)
  1546. class TLSTests(unittest.TestCase):
  1547. def test_startingTLS(self):
  1548. """
  1549. Verify that starting TLS and succeeding at handshaking sends all the
  1550. notifications to all the right places.
  1551. """
  1552. cli, svr, p = connectedServerAndClient(
  1553. ServerClass=SecurableProto,
  1554. ClientClass=SecurableProto)
  1555. okc = OKCert()
  1556. svr.certFactory = lambda : okc
  1557. cli.callRemote(
  1558. amp.StartTLS, tls_localCertificate=okc,
  1559. tls_verifyAuthorities=[PretendRemoteCertificateAuthority()])
  1560. # let's buffer something to be delivered securely
  1561. L = []
  1562. cli.callRemote(SecuredPing).addCallback(L.append)
  1563. p.flush()
  1564. # once for client once for server
  1565. self.assertEqual(okc.verifyCount, 2)
  1566. L = []
  1567. cli.callRemote(SecuredPing).addCallback(L.append)
  1568. p.flush()
  1569. self.assertEqual(L[0], {'pinged': True})
  1570. def test_startTooManyTimes(self):
  1571. """
  1572. Verify that the protocol will complain if we attempt to renegotiate TLS,
  1573. which we don't support.
  1574. """
  1575. cli, svr, p = connectedServerAndClient(
  1576. ServerClass=SecurableProto,
  1577. ClientClass=SecurableProto)
  1578. okc = OKCert()
  1579. svr.certFactory = lambda : okc
  1580. cli.callRemote(amp.StartTLS,
  1581. tls_localCertificate=okc,
  1582. tls_verifyAuthorities=[PretendRemoteCertificateAuthority()])
  1583. p.flush()
  1584. cli.noPeerCertificate = True # this is totally fake
  1585. self.assertRaises(
  1586. amp.OnlyOneTLS,
  1587. cli.callRemote,
  1588. amp.StartTLS,
  1589. tls_localCertificate=okc,
  1590. tls_verifyAuthorities=[PretendRemoteCertificateAuthority()])
  1591. def test_negotiationFailed(self):
  1592. """
  1593. Verify that starting TLS and failing on both sides at handshaking sends
  1594. notifications to all the right places and terminates the connection.
  1595. """
  1596. badCert = GrumpyCert()
  1597. cli, svr, p = connectedServerAndClient(
  1598. ServerClass=SecurableProto,
  1599. ClientClass=SecurableProto)
  1600. svr.certFactory = lambda : badCert
  1601. cli.callRemote(amp.StartTLS,
  1602. tls_localCertificate=badCert)
  1603. p.flush()
  1604. # once for client once for server - but both fail
  1605. self.assertEqual(badCert.verifyCount, 2)
  1606. d = cli.callRemote(SecuredPing)
  1607. p.flush()
  1608. self.assertFailure(d, iosim.NativeOpenSSLError)
  1609. def test_negotiationFailedByClosing(self):
  1610. """
  1611. Verify that starting TLS and failing by way of a lost connection
  1612. notices that it is probably an SSL problem.
  1613. """
  1614. cli, svr, p = connectedServerAndClient(
  1615. ServerClass=SecurableProto,
  1616. ClientClass=SecurableProto)
  1617. droppyCert = DroppyCert(svr.transport)
  1618. svr.certFactory = lambda : droppyCert
  1619. cli.callRemote(amp.StartTLS, tls_localCertificate=droppyCert)
  1620. p.flush()
  1621. self.assertEqual(droppyCert.verifyCount, 2)
  1622. d = cli.callRemote(SecuredPing)
  1623. p.flush()
  1624. # it might be a good idea to move this exception somewhere more
  1625. # reasonable.
  1626. self.assertFailure(d, error.PeerVerifyError)
  1627. skip = skipSSL
  1628. class TLSNotAvailableTests(unittest.TestCase):
  1629. """
  1630. Tests what happened when ssl is not available in current installation.
  1631. """
  1632. def setUp(self):
  1633. """
  1634. Disable ssl in amp.
  1635. """
  1636. self.ssl = amp.ssl
  1637. amp.ssl = None
  1638. def tearDown(self):
  1639. """
  1640. Restore ssl module.
  1641. """
  1642. amp.ssl = self.ssl
  1643. def test_callRemoteError(self):
  1644. """
  1645. Check that callRemote raises an exception when called with a
  1646. L{amp.StartTLS}.
  1647. """
  1648. cli, svr, p = connectedServerAndClient(
  1649. ServerClass=SecurableProto,
  1650. ClientClass=SecurableProto)
  1651. okc = OKCert()
  1652. svr.certFactory = lambda : okc
  1653. return self.assertFailure(cli.callRemote(
  1654. amp.StartTLS, tls_localCertificate=okc,
  1655. tls_verifyAuthorities=[PretendRemoteCertificateAuthority()]),
  1656. RuntimeError)
  1657. def test_messageReceivedError(self):
  1658. """
  1659. When a client with SSL enabled talks to a server without SSL, it
  1660. should return a meaningful error.
  1661. """
  1662. svr = SecurableProto()
  1663. okc = OKCert()
  1664. svr.certFactory = lambda : okc
  1665. box = amp.Box()
  1666. box[b'_command'] = b'StartTLS'
  1667. box[b'_ask'] = b'1'
  1668. boxes = []
  1669. svr.sendBox = boxes.append
  1670. svr.makeConnection(StringTransport())
  1671. svr.ampBoxReceived(box)
  1672. self.assertEqual(boxes,
  1673. [{b'_error_code': b'TLS_ERROR',
  1674. b'_error': b'1',
  1675. b'_error_description': b'TLS not available'}])
  1676. class InheritedError(Exception):
  1677. """
  1678. This error is used to check inheritance.
  1679. """
  1680. class OtherInheritedError(Exception):
  1681. """
  1682. This is a distinct error for checking inheritance.
  1683. """
  1684. class BaseCommand(amp.Command):
  1685. """
  1686. This provides a command that will be subclassed.
  1687. """
  1688. errors = {InheritedError: b'INHERITED_ERROR'}
  1689. class InheritedCommand(BaseCommand):
  1690. """
  1691. This is a command which subclasses another command but does not override
  1692. anything.
  1693. """
  1694. class AddErrorsCommand(BaseCommand):
  1695. """
  1696. This is a command which subclasses another command but adds errors to the
  1697. list.
  1698. """
  1699. arguments = [(b'other', amp.Boolean())]
  1700. errors = {OtherInheritedError: b'OTHER_INHERITED_ERROR'}
  1701. class NormalCommandProtocol(amp.AMP):
  1702. """
  1703. This is a protocol which responds to L{BaseCommand}, and is used to test
  1704. that inheritance does not interfere with the normal handling of errors.
  1705. """
  1706. def resp(self):
  1707. raise InheritedError()
  1708. BaseCommand.responder(resp)
  1709. class InheritedCommandProtocol(amp.AMP):
  1710. """
  1711. This is a protocol which responds to L{InheritedCommand}, and is used to
  1712. test that inherited commands inherit their bases' errors if they do not
  1713. respond to any of their own.
  1714. """
  1715. def resp(self):
  1716. raise InheritedError()
  1717. InheritedCommand.responder(resp)
  1718. class AddedCommandProtocol(amp.AMP):
  1719. """
  1720. This is a protocol which responds to L{AddErrorsCommand}, and is used to
  1721. test that inherited commands can add their own new types of errors, but
  1722. still respond in the same way to their parents types of errors.
  1723. """
  1724. def resp(self, other):
  1725. if other:
  1726. raise OtherInheritedError()
  1727. else:
  1728. raise InheritedError()
  1729. AddErrorsCommand.responder(resp)
  1730. class CommandInheritanceTests(unittest.TestCase):
  1731. """
  1732. These tests verify that commands inherit error conditions properly.
  1733. """
  1734. def errorCheck(self, err, proto, cmd, **kw):
  1735. """
  1736. Check that the appropriate kind of error is raised when a given command
  1737. is sent to a given protocol.
  1738. """
  1739. c, s, p = connectedServerAndClient(ServerClass=proto,
  1740. ClientClass=proto)
  1741. d = c.callRemote(cmd, **kw)
  1742. d2 = self.failUnlessFailure(d, err)
  1743. p.flush()
  1744. return d2
  1745. def test_basicErrorPropagation(self):
  1746. """
  1747. Verify that errors specified in a superclass are respected normally
  1748. even if it has subclasses.
  1749. """
  1750. return self.errorCheck(
  1751. InheritedError, NormalCommandProtocol, BaseCommand)
  1752. def test_inheritedErrorPropagation(self):
  1753. """
  1754. Verify that errors specified in a superclass command are propagated to
  1755. its subclasses.
  1756. """
  1757. return self.errorCheck(
  1758. InheritedError, InheritedCommandProtocol, InheritedCommand)
  1759. def test_inheritedErrorAddition(self):
  1760. """
  1761. Verify that new errors specified in a subclass of an existing command
  1762. are honored even if the superclass defines some errors.
  1763. """
  1764. return self.errorCheck(
  1765. OtherInheritedError, AddedCommandProtocol, AddErrorsCommand, other=True)
  1766. def test_additionWithOriginalError(self):
  1767. """
  1768. Verify that errors specified in a command's superclass are respected
  1769. even if that command defines new errors itself.
  1770. """
  1771. return self.errorCheck(
  1772. InheritedError, AddedCommandProtocol, AddErrorsCommand, other=False)
  1773. def _loseAndPass(err, proto):
  1774. # be specific, pass on the error to the client.
  1775. err.trap(error.ConnectionLost, error.ConnectionDone)
  1776. del proto.connectionLost
  1777. proto.connectionLost(err)
  1778. class LiveFireBase:
  1779. """
  1780. Utility for connected reactor-using tests.
  1781. """
  1782. def setUp(self):
  1783. """
  1784. Create an amp server and connect a client to it.
  1785. """
  1786. from twisted.internet import reactor
  1787. self.serverFactory = protocol.ServerFactory()
  1788. self.serverFactory.protocol = self.serverProto
  1789. self.clientFactory = protocol.ClientFactory()
  1790. self.clientFactory.protocol = self.clientProto
  1791. self.clientFactory.onMade = defer.Deferred()
  1792. self.serverFactory.onMade = defer.Deferred()
  1793. self.serverPort = reactor.listenTCP(0, self.serverFactory)
  1794. self.addCleanup(self.serverPort.stopListening)
  1795. self.clientConn = reactor.connectTCP(
  1796. '127.0.0.1', self.serverPort.getHost().port,
  1797. self.clientFactory)
  1798. self.addCleanup(self.clientConn.disconnect)
  1799. def getProtos(rlst):
  1800. self.cli = self.clientFactory.theProto
  1801. self.svr = self.serverFactory.theProto
  1802. dl = defer.DeferredList([self.clientFactory.onMade,
  1803. self.serverFactory.onMade])
  1804. return dl.addCallback(getProtos)
  1805. def tearDown(self):
  1806. """
  1807. Cleanup client and server connections, and check the error got at
  1808. C{connectionLost}.
  1809. """
  1810. L = []
  1811. for conn in self.cli, self.svr:
  1812. if conn.transport is not None:
  1813. # depend on amp's function connection-dropping behavior
  1814. d = defer.Deferred().addErrback(_loseAndPass, conn)
  1815. conn.connectionLost = d.errback
  1816. conn.transport.loseConnection()
  1817. L.append(d)
  1818. return defer.gatherResults(L
  1819. ).addErrback(lambda first: first.value.subFailure)
  1820. def show(x):
  1821. import sys
  1822. sys.stdout.write(x+'\n')
  1823. sys.stdout.flush()
  1824. def tempSelfSigned():
  1825. from twisted.internet import ssl
  1826. sharedDN = ssl.DN(CN='shared')
  1827. key = ssl.KeyPair.generate()
  1828. cr = key.certificateRequest(sharedDN)
  1829. sscrd = key.signCertificateRequest(
  1830. sharedDN, cr, lambda dn: True, 1234567)
  1831. cert = key.newCertificate(sscrd)
  1832. return cert
  1833. if ssl is not None:
  1834. tempcert = tempSelfSigned()
  1835. class LiveFireTLSTests(LiveFireBase, unittest.TestCase):
  1836. clientProto = SecurableProto
  1837. serverProto = SecurableProto
  1838. def test_liveFireCustomTLS(self):
  1839. """
  1840. Using real, live TLS, actually negotiate a connection.
  1841. This also looks at the 'peerCertificate' attribute's correctness, since
  1842. that's actually loaded using OpenSSL calls, but the main purpose is to
  1843. make sure that we didn't miss anything obvious in iosim about TLS
  1844. negotiations.
  1845. """
  1846. cert = tempcert
  1847. self.svr.verifyFactory = lambda : [cert]
  1848. self.svr.certFactory = lambda : cert
  1849. # only needed on the server, we specify the client below.
  1850. def secured(rslt):
  1851. x = cert.digest()
  1852. def pinged(rslt2):
  1853. # Interesting. OpenSSL won't even _tell_ us about the peer
  1854. # cert until we negotiate. we should be able to do this in
  1855. # 'secured' instead, but it looks like we can't. I think this
  1856. # is a bug somewhere far deeper than here.
  1857. self.assertEqual(x, self.cli.hostCertificate.digest())
  1858. self.assertEqual(x, self.cli.peerCertificate.digest())
  1859. self.assertEqual(x, self.svr.hostCertificate.digest())
  1860. self.assertEqual(x, self.svr.peerCertificate.digest())
  1861. return self.cli.callRemote(SecuredPing).addCallback(pinged)
  1862. return self.cli.callRemote(amp.StartTLS,
  1863. tls_localCertificate=cert,
  1864. tls_verifyAuthorities=[cert]).addCallback(secured)
  1865. skip = skipSSL
  1866. class SlightlySmartTLS(SimpleSymmetricCommandProtocol):
  1867. """
  1868. Specific implementation of server side protocol with different
  1869. management of TLS.
  1870. """
  1871. def getTLSVars(self):
  1872. """
  1873. @return: the global C{tempcert} certificate as local certificate.
  1874. """
  1875. return dict(tls_localCertificate=tempcert)
  1876. amp.StartTLS.responder(getTLSVars)
  1877. class PlainVanillaLiveFireTests(LiveFireBase, unittest.TestCase):
  1878. clientProto = SimpleSymmetricCommandProtocol
  1879. serverProto = SimpleSymmetricCommandProtocol
  1880. def test_liveFireDefaultTLS(self):
  1881. """
  1882. Verify that out of the box, we can start TLS to at least encrypt the
  1883. connection, even if we don't have any certificates to use.
  1884. """
  1885. def secured(result):
  1886. return self.cli.callRemote(SecuredPing)
  1887. return self.cli.callRemote(amp.StartTLS).addCallback(secured)
  1888. skip = skipSSL
  1889. class WithServerTLSVerificationTests(LiveFireBase, unittest.TestCase):
  1890. clientProto = SimpleSymmetricCommandProtocol
  1891. serverProto = SlightlySmartTLS
  1892. def test_anonymousVerifyingClient(self):
  1893. """
  1894. Verify that anonymous clients can verify server certificates.
  1895. """
  1896. def secured(result):
  1897. return self.cli.callRemote(SecuredPing)
  1898. return self.cli.callRemote(amp.StartTLS,
  1899. tls_verifyAuthorities=[tempcert]
  1900. ).addCallback(secured)
  1901. skip = skipSSL
  1902. class ProtocolIncludingArgument(amp.Argument):
  1903. """
  1904. An L{amp.Argument} which encodes its parser and serializer
  1905. arguments *including the protocol* into its parsed and serialized
  1906. forms.
  1907. """
  1908. def fromStringProto(self, string, protocol):
  1909. """
  1910. Don't decode anything; just return all possible information.
  1911. @return: A two-tuple of the input string and the protocol.
  1912. """
  1913. return (string, protocol)
  1914. def toStringProto(self, obj, protocol):
  1915. """
  1916. Encode identifying information about L{object} and protocol
  1917. into a string for later verification.
  1918. @type obj: L{object}
  1919. @type protocol: L{amp.AMP}
  1920. """
  1921. ident = u"%d:%d" % (id(obj), id(protocol))
  1922. return ident.encode("ascii")
  1923. class ProtocolIncludingCommand(amp.Command):
  1924. """
  1925. A command that has argument and response schemas which use
  1926. L{ProtocolIncludingArgument}.
  1927. """
  1928. arguments = [(b'weird', ProtocolIncludingArgument())]
  1929. response = [(b'weird', ProtocolIncludingArgument())]
  1930. class MagicSchemaCommand(amp.Command):
  1931. """
  1932. A command which overrides L{parseResponse}, L{parseArguments}, and
  1933. L{makeResponse}.
  1934. """
  1935. def parseResponse(self, strings, protocol):
  1936. """
  1937. Don't do any parsing, just jam the input strings and protocol
  1938. onto the C{protocol.parseResponseArguments} attribute as a
  1939. two-tuple. Return the original strings.
  1940. """
  1941. protocol.parseResponseArguments = (strings, protocol)
  1942. return strings
  1943. parseResponse = classmethod(parseResponse)
  1944. def parseArguments(cls, strings, protocol):
  1945. """
  1946. Don't do any parsing, just jam the input strings and protocol
  1947. onto the C{protocol.parseArgumentsArguments} attribute as a
  1948. two-tuple. Return the original strings.
  1949. """
  1950. protocol.parseArgumentsArguments = (strings, protocol)
  1951. return strings
  1952. parseArguments = classmethod(parseArguments)
  1953. def makeArguments(cls, objects, protocol):
  1954. """
  1955. Don't do any serializing, just jam the input strings and protocol
  1956. onto the C{protocol.makeArgumentsArguments} attribute as a
  1957. two-tuple. Return the original strings.
  1958. """
  1959. protocol.makeArgumentsArguments = (objects, protocol)
  1960. return objects
  1961. makeArguments = classmethod(makeArguments)
  1962. class NoNetworkProtocol(amp.AMP):
  1963. """
  1964. An L{amp.AMP} subclass which overrides private methods to avoid
  1965. testing the network. It also provides a responder for
  1966. L{MagicSchemaCommand} that does nothing, so that tests can test
  1967. aspects of the interaction of L{amp.Command}s and L{amp.AMP}.
  1968. @ivar parseArgumentsArguments: Arguments that have been passed to any
  1969. L{MagicSchemaCommand}, if L{MagicSchemaCommand} has been handled by
  1970. this protocol.
  1971. @ivar parseResponseArguments: Responses that have been returned from a
  1972. L{MagicSchemaCommand}, if L{MagicSchemaCommand} has been handled by
  1973. this protocol.
  1974. @ivar makeArgumentsArguments: Arguments that have been serialized by any
  1975. L{MagicSchemaCommand}, if L{MagicSchemaCommand} has been handled by
  1976. this protocol.
  1977. """
  1978. def _sendBoxCommand(self, commandName, strings, requiresAnswer):
  1979. """
  1980. Return a Deferred which fires with the original strings.
  1981. """
  1982. return defer.succeed(strings)
  1983. MagicSchemaCommand.responder(lambda s, weird: {})
  1984. class MyBox(dict):
  1985. """
  1986. A unique dict subclass.
  1987. """
  1988. class ProtocolIncludingCommandWithDifferentCommandType(
  1989. ProtocolIncludingCommand):
  1990. """
  1991. A L{ProtocolIncludingCommand} subclass whose commandType is L{MyBox}
  1992. """
  1993. commandType = MyBox
  1994. class CommandTests(unittest.TestCase):
  1995. """
  1996. Tests for L{amp.Argument} and L{amp.Command}.
  1997. """
  1998. def test_argumentInterface(self):
  1999. """
  2000. L{Argument} instances provide L{amp.IArgumentType}.
  2001. """
  2002. self.assertTrue(verifyObject(amp.IArgumentType, amp.Argument()))
  2003. def test_parseResponse(self):
  2004. """
  2005. There should be a class method of Command which accepts a
  2006. mapping of argument names to serialized forms and returns a
  2007. similar mapping whose values have been parsed via the
  2008. Command's response schema.
  2009. """
  2010. protocol = object()
  2011. result = b'whatever'
  2012. strings = {b'weird': result}
  2013. self.assertEqual(
  2014. ProtocolIncludingCommand.parseResponse(strings, protocol),
  2015. {'weird': (result, protocol)})
  2016. def test_callRemoteCallsParseResponse(self):
  2017. """
  2018. Making a remote call on a L{amp.Command} subclass which
  2019. overrides the C{parseResponse} method should call that
  2020. C{parseResponse} method to get the response.
  2021. """
  2022. client = NoNetworkProtocol()
  2023. thingy = b"weeoo"
  2024. response = client.callRemote(MagicSchemaCommand, weird=thingy)
  2025. def gotResponse(ign):
  2026. self.assertEqual(client.parseResponseArguments,
  2027. ({"weird": thingy}, client))
  2028. response.addCallback(gotResponse)
  2029. return response
  2030. def test_parseArguments(self):
  2031. """
  2032. There should be a class method of L{amp.Command} which accepts
  2033. a mapping of argument names to serialized forms and returns a
  2034. similar mapping whose values have been parsed via the
  2035. command's argument schema.
  2036. """
  2037. protocol = object()
  2038. result = b'whatever'
  2039. strings = {b'weird': result}
  2040. self.assertEqual(
  2041. ProtocolIncludingCommand.parseArguments(strings, protocol),
  2042. {'weird': (result, protocol)})
  2043. def test_responderCallsParseArguments(self):
  2044. """
  2045. Making a remote call on a L{amp.Command} subclass which
  2046. overrides the C{parseArguments} method should call that
  2047. C{parseArguments} method to get the arguments.
  2048. """
  2049. protocol = NoNetworkProtocol()
  2050. responder = protocol.locateResponder(MagicSchemaCommand.commandName)
  2051. argument = object()
  2052. response = responder(dict(weird=argument))
  2053. response.addCallback(
  2054. lambda ign: self.assertEqual(protocol.parseArgumentsArguments,
  2055. ({"weird": argument}, protocol)))
  2056. return response
  2057. def test_makeArguments(self):
  2058. """
  2059. There should be a class method of L{amp.Command} which accepts
  2060. a mapping of argument names to objects and returns a similar
  2061. mapping whose values have been serialized via the command's
  2062. argument schema.
  2063. """
  2064. protocol = object()
  2065. argument = object()
  2066. objects = {'weird': argument}
  2067. ident = u"%d:%d" % (id(argument), id(protocol))
  2068. self.assertEqual(
  2069. ProtocolIncludingCommand.makeArguments(objects, protocol),
  2070. {b'weird': ident.encode("ascii")})
  2071. def test_makeArgumentsUsesCommandType(self):
  2072. """
  2073. L{amp.Command.makeArguments}'s return type should be the type
  2074. of the result of L{amp.Command.commandType}.
  2075. """
  2076. protocol = object()
  2077. objects = {"weird": b"whatever"}
  2078. result = ProtocolIncludingCommandWithDifferentCommandType.makeArguments(
  2079. objects, protocol)
  2080. self.assertIs(type(result), MyBox)
  2081. def test_callRemoteCallsMakeArguments(self):
  2082. """
  2083. Making a remote call on a L{amp.Command} subclass which
  2084. overrides the C{makeArguments} method should call that
  2085. C{makeArguments} method to get the response.
  2086. """
  2087. client = NoNetworkProtocol()
  2088. argument = object()
  2089. response = client.callRemote(MagicSchemaCommand, weird=argument)
  2090. def gotResponse(ign):
  2091. self.assertEqual(client.makeArgumentsArguments,
  2092. ({"weird": argument}, client))
  2093. response.addCallback(gotResponse)
  2094. return response
  2095. def test_extraArgumentsDisallowed(self):
  2096. """
  2097. L{Command.makeArguments} raises L{amp.InvalidSignature} if the objects
  2098. dictionary passed to it includes a key which does not correspond to the
  2099. Python identifier for a defined argument.
  2100. """
  2101. self.assertRaises(
  2102. amp.InvalidSignature,
  2103. Hello.makeArguments,
  2104. dict(hello="hello", bogusArgument=object()), None)
  2105. def test_wireSpellingDisallowed(self):
  2106. """
  2107. If a command argument conflicts with a Python keyword, the
  2108. untransformed argument name is not allowed as a key in the dictionary
  2109. passed to L{Command.makeArguments}. If it is supplied,
  2110. L{amp.InvalidSignature} is raised.
  2111. This may be a pointless implementation restriction which may be lifted.
  2112. The current behavior is tested to verify that such arguments are not
  2113. silently dropped on the floor (the previous behavior).
  2114. """
  2115. self.assertRaises(
  2116. amp.InvalidSignature,
  2117. Hello.makeArguments,
  2118. dict(hello="required", **{"print": "print value"}),
  2119. None)
  2120. def test_commandNameDefaultsToClassNameAsByteString(self):
  2121. """
  2122. A L{Command} subclass without a defined C{commandName} that's
  2123. not a byte string.
  2124. """
  2125. class NewCommand(amp.Command):
  2126. """
  2127. A new command.
  2128. """
  2129. self.assertEqual(b"NewCommand", NewCommand.commandName)
  2130. def test_commandNameMustBeAByteString(self):
  2131. """
  2132. A L{Command} subclass cannot be defined with a C{commandName} that's
  2133. not a byte string.
  2134. """
  2135. error = self.assertRaises(
  2136. TypeError, type, "NewCommand", (amp.Command, ),
  2137. {"commandName": u"FOO"})
  2138. self.assertRegex(
  2139. str(error), "^Command names must be byte strings, got: u?'FOO'$")
  2140. def test_commandArgumentsMustBeNamedWithByteStrings(self):
  2141. """
  2142. A L{Command} subclass's C{arguments} must have byte string names.
  2143. """
  2144. error = self.assertRaises(
  2145. TypeError, type, "NewCommand", (amp.Command, ),
  2146. {"arguments": [(u"foo", None)]})
  2147. self.assertRegex(
  2148. str(error), "^Argument names must be byte strings, got: u?'foo'$")
  2149. def test_commandResponseMustBeNamedWithByteStrings(self):
  2150. """
  2151. A L{Command} subclass's C{response} must have byte string names.
  2152. """
  2153. error = self.assertRaises(
  2154. TypeError, type, "NewCommand", (amp.Command, ),
  2155. {"response": [(u"foo", None)]})
  2156. self.assertRegex(
  2157. str(error), "^Response names must be byte strings, got: u?'foo'$")
  2158. def test_commandErrorsIsConvertedToDict(self):
  2159. """
  2160. A L{Command} subclass's C{errors} is coerced into a C{dict}.
  2161. """
  2162. class NewCommand(amp.Command):
  2163. errors = [(ZeroDivisionError, b"ZDE")]
  2164. self.assertEqual(
  2165. {ZeroDivisionError: b"ZDE"},
  2166. NewCommand.errors)
  2167. def test_commandErrorsMustUseBytesForOnWireRepresentation(self):
  2168. """
  2169. A L{Command} subclass's C{errors} must map exceptions to byte strings.
  2170. """
  2171. error = self.assertRaises(
  2172. TypeError, type, "NewCommand", (amp.Command, ),
  2173. {"errors": [(ZeroDivisionError, u"foo")]})
  2174. self.assertRegex(
  2175. str(error), "^Error names must be byte strings, got: u?'foo'$")
  2176. def test_commandFatalErrorsIsConvertedToDict(self):
  2177. """
  2178. A L{Command} subclass's C{fatalErrors} is coerced into a C{dict}.
  2179. """
  2180. class NewCommand(amp.Command):
  2181. fatalErrors = [(ZeroDivisionError, b"ZDE")]
  2182. self.assertEqual(
  2183. {ZeroDivisionError: b"ZDE"},
  2184. NewCommand.fatalErrors)
  2185. def test_commandFatalErrorsMustUseBytesForOnWireRepresentation(self):
  2186. """
  2187. A L{Command} subclass's C{fatalErrors} must map exceptions to byte
  2188. strings.
  2189. """
  2190. error = self.assertRaises(
  2191. TypeError, type, "NewCommand", (amp.Command, ),
  2192. {"fatalErrors": [(ZeroDivisionError, u"foo")]})
  2193. self.assertRegex(
  2194. str(error), "^Fatal error names must be byte strings, "
  2195. "got: u?'foo'$")
  2196. class ListOfTestsMixin:
  2197. """
  2198. Base class for testing L{ListOf}, a parameterized zero-or-more argument
  2199. type.
  2200. @ivar elementType: Subclasses should set this to an L{Argument}
  2201. instance. The tests will make a L{ListOf} using this.
  2202. @ivar strings: Subclasses should set this to a dictionary mapping some
  2203. number of keys -- as BYTE strings -- to the correct serialized form
  2204. for some example values. These should agree with what L{elementType}
  2205. produces/accepts.
  2206. @ivar objects: Subclasses should set this to a dictionary with the same
  2207. keys as C{strings} -- as NATIVE strings -- and with values which are
  2208. the lists which should serialize to the values in the C{strings}
  2209. dictionary.
  2210. """
  2211. def test_toBox(self):
  2212. """
  2213. L{ListOf.toBox} extracts the list of objects from the C{objects}
  2214. dictionary passed to it, using the C{name} key also passed to it,
  2215. serializes each of the elements in that list using the L{Argument}
  2216. instance previously passed to its initializer, combines the serialized
  2217. results, and inserts the result into the C{strings} dictionary using
  2218. the same C{name} key.
  2219. """
  2220. stringList = amp.ListOf(self.elementType)
  2221. strings = amp.AmpBox()
  2222. for key in self.objects:
  2223. stringList.toBox(
  2224. key.encode("ascii"), strings, self.objects.copy(), None)
  2225. self.assertEqual(strings, self.strings)
  2226. def test_fromBox(self):
  2227. """
  2228. L{ListOf.fromBox} reverses the operation performed by L{ListOf.toBox}.
  2229. """
  2230. stringList = amp.ListOf(self.elementType)
  2231. objects = {}
  2232. for key in self.strings:
  2233. stringList.fromBox(key, self.strings.copy(), objects, None)
  2234. self.assertEqual(objects, self.objects)
  2235. class ListOfStringsTests(unittest.TestCase, ListOfTestsMixin):
  2236. """
  2237. Tests for L{ListOf} combined with L{amp.String}.
  2238. """
  2239. elementType = amp.String()
  2240. strings = {
  2241. b"empty": b"",
  2242. b"single": b"\x00\x03foo",
  2243. b"multiple": b"\x00\x03bar\x00\x03baz\x00\x04quux"}
  2244. objects = {
  2245. "empty": [],
  2246. "single": [b"foo"],
  2247. "multiple": [b"bar", b"baz", b"quux"]}
  2248. class ListOfIntegersTests(unittest.TestCase, ListOfTestsMixin):
  2249. """
  2250. Tests for L{ListOf} combined with L{amp.Integer}.
  2251. """
  2252. elementType = amp.Integer()
  2253. huge = (
  2254. 9999999999999999999999999999999999999999999999999999999999 *
  2255. 9999999999999999999999999999999999999999999999999999999999)
  2256. strings = {
  2257. b"empty": b"",
  2258. b"single": b"\x00\x0210",
  2259. b"multiple": b"\x00\x011\x00\x0220\x00\x03500",
  2260. b"huge": b"\x00\x74" + intToBytes(huge),
  2261. b"negative": b"\x00\x02-1"}
  2262. objects = {
  2263. "empty": [],
  2264. "single": [10],
  2265. "multiple": [1, 20, 500],
  2266. "huge": [huge],
  2267. "negative": [-1]}
  2268. class ListOfUnicodeTests(unittest.TestCase, ListOfTestsMixin):
  2269. """
  2270. Tests for L{ListOf} combined with L{amp.Unicode}.
  2271. """
  2272. elementType = amp.Unicode()
  2273. strings = {
  2274. b"empty": b"",
  2275. b"single": b"\x00\x03foo",
  2276. b"multiple": b"\x00\x03\xe2\x98\x83\x00\x05Hello\x00\x05world"}
  2277. objects = {
  2278. "empty": [],
  2279. "single": [u"foo"],
  2280. "multiple": [u"\N{SNOWMAN}", u"Hello", u"world"]}
  2281. class ListOfDecimalTests(unittest.TestCase, ListOfTestsMixin):
  2282. """
  2283. Tests for L{ListOf} combined with L{amp.Decimal}.
  2284. """
  2285. elementType = amp.Decimal()
  2286. strings = {
  2287. b"empty": b"",
  2288. b"single": b"\x00\x031.1",
  2289. b"extreme": b"\x00\x08Infinity\x00\x09-Infinity",
  2290. b"scientist": b"\x00\x083.141E+5\x00\x0a0.00003141\x00\x083.141E-7"
  2291. b"\x00\x09-3.141E+5\x00\x0b-0.00003141\x00\x09-3.141E-7",
  2292. b"engineer": (
  2293. b"\x00\x04" +
  2294. decimal.Decimal("0e6").to_eng_string().encode("ascii") +
  2295. b"\x00\x06" +
  2296. decimal.Decimal("1.5E-9").to_eng_string().encode("ascii")),
  2297. }
  2298. objects = {
  2299. "empty": [],
  2300. "single": [decimal.Decimal("1.1")],
  2301. "extreme": [
  2302. decimal.Decimal("Infinity"),
  2303. decimal.Decimal("-Infinity"),
  2304. ],
  2305. # exarkun objected to AMP supporting engineering notation because
  2306. # it was redundant, until we realised that 1E6 has less precision
  2307. # than 1000000 and is represented differently. But they compare
  2308. # and even hash equally. There were tears.
  2309. "scientist": [
  2310. decimal.Decimal("3.141E5"),
  2311. decimal.Decimal("3.141e-5"),
  2312. decimal.Decimal("3.141E-7"),
  2313. decimal.Decimal("-3.141e5"),
  2314. decimal.Decimal("-3.141E-5"),
  2315. decimal.Decimal("-3.141e-7"),
  2316. ],
  2317. "engineer": [
  2318. decimal.Decimal("0e6"),
  2319. decimal.Decimal("1.5E-9"),
  2320. ],
  2321. }
  2322. class ListOfDecimalNanTests(unittest.TestCase, ListOfTestsMixin):
  2323. """
  2324. Tests for L{ListOf} combined with L{amp.Decimal} for not-a-number values.
  2325. """
  2326. elementType = amp.Decimal()
  2327. strings = {
  2328. b"nan": b"\x00\x03NaN\x00\x04-NaN\x00\x04sNaN\x00\x05-sNaN",
  2329. }
  2330. objects = {
  2331. "nan": [
  2332. decimal.Decimal("NaN"),
  2333. decimal.Decimal("-NaN"),
  2334. decimal.Decimal("sNaN"),
  2335. decimal.Decimal("-sNaN"),
  2336. ]
  2337. }
  2338. def test_fromBox(self):
  2339. """
  2340. L{ListOf.fromBox} reverses the operation performed by L{ListOf.toBox}.
  2341. """
  2342. # Helpers. Decimal.is_{qnan,snan,signed}() are new in 2.6 (or 2.5.2,
  2343. # but who's counting).
  2344. def is_qnan(decimal):
  2345. return 'NaN' in str(decimal) and 'sNaN' not in str(decimal)
  2346. def is_snan(decimal):
  2347. return 'sNaN' in str(decimal)
  2348. def is_signed(decimal):
  2349. return '-' in str(decimal)
  2350. # NaN values have unusual equality semantics, so this method is
  2351. # overridden to compare the resulting objects in a way which works with
  2352. # NaNs.
  2353. stringList = amp.ListOf(self.elementType)
  2354. objects = {}
  2355. for key in self.strings:
  2356. stringList.fromBox(key, self.strings.copy(), objects, None)
  2357. n = objects["nan"]
  2358. self.assertTrue(is_qnan(n[0]) and not is_signed(n[0]))
  2359. self.assertTrue(is_qnan(n[1]) and is_signed(n[1]))
  2360. self.assertTrue(is_snan(n[2]) and not is_signed(n[2]))
  2361. self.assertTrue(is_snan(n[3]) and is_signed(n[3]))
  2362. class DecimalTests(unittest.TestCase):
  2363. """
  2364. Tests for L{amp.Decimal}.
  2365. """
  2366. def test_nonDecimal(self):
  2367. """
  2368. L{amp.Decimal.toString} raises L{ValueError} if passed an object which
  2369. is not an instance of C{decimal.Decimal}.
  2370. """
  2371. argument = amp.Decimal()
  2372. self.assertRaises(ValueError, argument.toString, "1.234")
  2373. self.assertRaises(ValueError, argument.toString, 1.234)
  2374. self.assertRaises(ValueError, argument.toString, 1234)
  2375. class FloatTests(unittest.TestCase):
  2376. """
  2377. Tests for L{amp.Float}.
  2378. """
  2379. def test_nonFloat(self):
  2380. """
  2381. L{amp.Float.toString} raises L{ValueError} if passed an object which
  2382. is not a L{float}.
  2383. """
  2384. argument = amp.Float()
  2385. self.assertRaises(ValueError, argument.toString, u"1.234")
  2386. self.assertRaises(ValueError, argument.toString, b"1.234")
  2387. self.assertRaises(ValueError, argument.toString, 1234)
  2388. def test_float(self):
  2389. """
  2390. L{amp.Float.toString} returns a bytestring when it is given a L{float}.
  2391. """
  2392. argument = amp.Float()
  2393. self.assertEqual(argument.toString(1.234), b"1.234")
  2394. class ListOfDateTimeTests(unittest.TestCase, ListOfTestsMixin):
  2395. """
  2396. Tests for L{ListOf} combined with L{amp.DateTime}.
  2397. """
  2398. elementType = amp.DateTime()
  2399. strings = {
  2400. b"christmas": b"\x00\x202010-12-25T00:00:00.000000-00:00"
  2401. b"\x00\x202010-12-25T00:00:00.000000-00:00",
  2402. b"christmas in eu": b"\x00\x202010-12-25T00:00:00.000000+01:00",
  2403. b"christmas in iran": b"\x00\x202010-12-25T00:00:00.000000+03:30",
  2404. b"christmas in nyc": b"\x00\x202010-12-25T00:00:00.000000-05:00",
  2405. b"previous tests": b"\x00\x202010-12-25T00:00:00.000000+03:19"
  2406. b"\x00\x202010-12-25T00:00:00.000000-06:59",
  2407. }
  2408. objects = {
  2409. "christmas": [
  2410. datetime.datetime(2010, 12, 25, 0, 0, 0, tzinfo=amp.utc),
  2411. datetime.datetime(2010, 12, 25, 0, 0, 0, tzinfo=tz('+', 0, 0)),
  2412. ],
  2413. "christmas in eu": [
  2414. datetime.datetime(2010, 12, 25, 0, 0, 0, tzinfo=tz('+', 1, 0)),
  2415. ],
  2416. "christmas in iran": [
  2417. datetime.datetime(2010, 12, 25, 0, 0, 0, tzinfo=tz('+', 3, 30)),
  2418. ],
  2419. "christmas in nyc": [
  2420. datetime.datetime(2010, 12, 25, 0, 0, 0, tzinfo=tz('-', 5, 0)),
  2421. ],
  2422. "previous tests": [
  2423. datetime.datetime(2010, 12, 25, 0, 0, 0, tzinfo=tz('+', 3, 19)),
  2424. datetime.datetime(2010, 12, 25, 0, 0, 0, tzinfo=tz('-', 6, 59)),
  2425. ],
  2426. }
  2427. class ListOfOptionalTests(unittest.TestCase):
  2428. """
  2429. Tests to ensure L{ListOf} AMP arguments can be omitted from AMP commands
  2430. via the 'optional' flag.
  2431. """
  2432. def test_requiredArgumentWithNoneValueRaisesTypeError(self):
  2433. """
  2434. L{ListOf.toBox} raises C{TypeError} when passed a value of L{None}
  2435. for the argument.
  2436. """
  2437. stringList = amp.ListOf(amp.Integer())
  2438. self.assertRaises(
  2439. TypeError, stringList.toBox, b'omitted', amp.AmpBox(),
  2440. {'omitted': None}, None)
  2441. def test_optionalArgumentWithNoneValueOmitted(self):
  2442. """
  2443. L{ListOf.toBox} silently omits serializing any argument with a
  2444. value of L{None} that is designated as optional for the protocol.
  2445. """
  2446. stringList = amp.ListOf(amp.Integer(), optional=True)
  2447. strings = amp.AmpBox()
  2448. stringList.toBox(b'omitted', strings, {b'omitted': None}, None)
  2449. self.assertEqual(strings, {})
  2450. def test_requiredArgumentWithKeyMissingRaisesKeyError(self):
  2451. """
  2452. L{ListOf.toBox} raises C{KeyError} if the argument's key is not
  2453. present in the objects dictionary.
  2454. """
  2455. stringList = amp.ListOf(amp.Integer())
  2456. self.assertRaises(
  2457. KeyError, stringList.toBox, b'ommited', amp.AmpBox(),
  2458. {'someOtherKey': 0}, None)
  2459. def test_optionalArgumentWithKeyMissingOmitted(self):
  2460. """
  2461. L{ListOf.toBox} silently omits serializing any argument designated
  2462. as optional whose key is not present in the objects dictionary.
  2463. """
  2464. stringList = amp.ListOf(amp.Integer(), optional=True)
  2465. stringList.toBox(b'ommited', amp.AmpBox(), {b'someOtherKey': 0}, None)
  2466. def test_omittedOptionalArgumentDeserializesAsNone(self):
  2467. """
  2468. L{ListOf.fromBox} correctly reverses the operation performed by
  2469. L{ListOf.toBox} for optional arguments.
  2470. """
  2471. stringList = amp.ListOf(amp.Integer(), optional=True)
  2472. objects = {}
  2473. stringList.fromBox(b'omitted', {}, objects, None)
  2474. self.assertEqual(objects, {'omitted': None})
  2475. @implementer(interfaces.IUNIXTransport)
  2476. class UNIXStringTransport(object):
  2477. """
  2478. An in-memory implementation of L{interfaces.IUNIXTransport} which collects
  2479. all data given to it for later inspection.
  2480. @ivar _queue: A C{list} of the data which has been given to this transport,
  2481. eg via C{write} or C{sendFileDescriptor}. Elements are two-tuples of a
  2482. string (identifying the destination of the data) and the data itself.
  2483. """
  2484. def __init__(self, descriptorFuzz):
  2485. """
  2486. @param descriptorFuzz: An offset to apply to descriptors.
  2487. @type descriptorFuzz: C{int}
  2488. """
  2489. self._fuzz = descriptorFuzz
  2490. self._queue = []
  2491. def sendFileDescriptor(self, descriptor):
  2492. self._queue.append((
  2493. 'fileDescriptorReceived', descriptor + self._fuzz))
  2494. def write(self, data):
  2495. self._queue.append(('dataReceived', data))
  2496. def writeSequence(self, seq):
  2497. for data in seq:
  2498. self.write(data)
  2499. def loseConnection(self):
  2500. self._queue.append(('connectionLost', Failure(error.ConnectionLost())))
  2501. def getHost(self):
  2502. return address.UNIXAddress('/tmp/some-path')
  2503. def getPeer(self):
  2504. return address.UNIXAddress('/tmp/another-path')
  2505. # Minimal evidence that we got the signatures right
  2506. verifyClass(interfaces.ITransport, UNIXStringTransport)
  2507. verifyClass(interfaces.IUNIXTransport, UNIXStringTransport)
  2508. class DescriptorTests(unittest.TestCase):
  2509. """
  2510. Tests for L{amp.Descriptor}, an argument type for passing a file descriptor
  2511. over an AMP connection over a UNIX domain socket.
  2512. """
  2513. def setUp(self):
  2514. self.fuzz = 3
  2515. self.transport = UNIXStringTransport(descriptorFuzz=self.fuzz)
  2516. self.protocol = amp.BinaryBoxProtocol(
  2517. amp.BoxDispatcher(amp.CommandLocator()))
  2518. self.protocol.makeConnection(self.transport)
  2519. def test_fromStringProto(self):
  2520. """
  2521. L{Descriptor.fromStringProto} constructs a file descriptor value by
  2522. extracting a previously received file descriptor corresponding to the
  2523. wire value of the argument from the L{_DescriptorExchanger} state of the
  2524. protocol passed to it.
  2525. This is a whitebox test which involves direct L{_DescriptorExchanger}
  2526. state inspection.
  2527. """
  2528. argument = amp.Descriptor()
  2529. self.protocol.fileDescriptorReceived(5)
  2530. self.protocol.fileDescriptorReceived(3)
  2531. self.protocol.fileDescriptorReceived(1)
  2532. self.assertEqual(
  2533. 5, argument.fromStringProto("0", self.protocol))
  2534. self.assertEqual(
  2535. 3, argument.fromStringProto("1", self.protocol))
  2536. self.assertEqual(
  2537. 1, argument.fromStringProto("2", self.protocol))
  2538. self.assertEqual({}, self.protocol._descriptors)
  2539. def test_toStringProto(self):
  2540. """
  2541. To send a file descriptor, L{Descriptor.toStringProto} uses the
  2542. L{IUNIXTransport.sendFileDescriptor} implementation of the transport of
  2543. the protocol passed to it to copy the file descriptor. Each subsequent
  2544. descriptor sent over a particular AMP connection is assigned the next
  2545. integer value, starting from 0. The base ten string representation of
  2546. this value is the byte encoding of the argument.
  2547. This is a whitebox test which involves direct L{_DescriptorExchanger}
  2548. state inspection and mutation.
  2549. """
  2550. argument = amp.Descriptor()
  2551. self.assertEqual(b"0", argument.toStringProto(2, self.protocol))
  2552. self.assertEqual(
  2553. ("fileDescriptorReceived", 2 + self.fuzz), self.transport._queue.pop(0))
  2554. self.assertEqual(b"1", argument.toStringProto(4, self.protocol))
  2555. self.assertEqual(
  2556. ("fileDescriptorReceived", 4 + self.fuzz), self.transport._queue.pop(0))
  2557. self.assertEqual(b"2", argument.toStringProto(6, self.protocol))
  2558. self.assertEqual(
  2559. ("fileDescriptorReceived", 6 + self.fuzz), self.transport._queue.pop(0))
  2560. self.assertEqual({}, self.protocol._descriptors)
  2561. def test_roundTrip(self):
  2562. """
  2563. L{amp.Descriptor.fromBox} can interpret an L{amp.AmpBox} constructed by
  2564. L{amp.Descriptor.toBox} to reconstruct a file descriptor value.
  2565. """
  2566. name = "alpha"
  2567. nameAsBytes = name.encode("ascii")
  2568. strings = {}
  2569. descriptor = 17
  2570. sendObjects = {name: descriptor}
  2571. argument = amp.Descriptor()
  2572. argument.toBox(nameAsBytes, strings, sendObjects.copy(), self.protocol)
  2573. receiver = amp.BinaryBoxProtocol(
  2574. amp.BoxDispatcher(amp.CommandLocator()))
  2575. for event in self.transport._queue:
  2576. getattr(receiver, event[0])(*event[1:])
  2577. receiveObjects = {}
  2578. argument.fromBox(
  2579. nameAsBytes, strings.copy(), receiveObjects, receiver)
  2580. # Make sure we got the descriptor. Adjust by fuzz to be more convincing
  2581. # of having gone through L{IUNIXTransport.sendFileDescriptor}, not just
  2582. # converted to a string and then parsed back into an integer.
  2583. self.assertEqual(descriptor + self.fuzz, receiveObjects[name])
  2584. class DateTimeTests(unittest.TestCase):
  2585. """
  2586. Tests for L{amp.DateTime}, L{amp._FixedOffsetTZInfo}, and L{amp.utc}.
  2587. """
  2588. string = b'9876-01-23T12:34:56.054321-01:23'
  2589. tzinfo = tz('-', 1, 23)
  2590. object = datetime.datetime(9876, 1, 23, 12, 34, 56, 54321, tzinfo)
  2591. def test_invalidString(self):
  2592. """
  2593. L{amp.DateTime.fromString} raises L{ValueError} when passed a string
  2594. which does not represent a timestamp in the proper format.
  2595. """
  2596. d = amp.DateTime()
  2597. self.assertRaises(ValueError, d.fromString, 'abc')
  2598. def test_invalidDatetime(self):
  2599. """
  2600. L{amp.DateTime.toString} raises L{ValueError} when passed a naive
  2601. datetime (a datetime with no timezone information).
  2602. """
  2603. d = amp.DateTime()
  2604. self.assertRaises(ValueError, d.toString,
  2605. datetime.datetime(2010, 12, 25, 0, 0, 0))
  2606. def test_fromString(self):
  2607. """
  2608. L{amp.DateTime.fromString} returns a C{datetime.datetime} with all of
  2609. its fields populated from the string passed to it.
  2610. """
  2611. argument = amp.DateTime()
  2612. value = argument.fromString(self.string)
  2613. self.assertEqual(value, self.object)
  2614. def test_toString(self):
  2615. """
  2616. L{amp.DateTime.toString} returns a C{str} in the wire format including
  2617. all of the information from the C{datetime.datetime} passed into it,
  2618. including the timezone offset.
  2619. """
  2620. argument = amp.DateTime()
  2621. value = argument.toString(self.object)
  2622. self.assertEqual(value, self.string)
  2623. class UTCTests(unittest.TestCase):
  2624. """
  2625. Tests for L{amp.utc}.
  2626. """
  2627. def test_tzname(self):
  2628. """
  2629. L{amp.utc.tzname} returns C{"+00:00"}.
  2630. """
  2631. self.assertEqual(amp.utc.tzname(None), '+00:00')
  2632. def test_dst(self):
  2633. """
  2634. L{amp.utc.dst} returns a zero timedelta.
  2635. """
  2636. self.assertEqual(amp.utc.dst(None), datetime.timedelta(0))
  2637. def test_utcoffset(self):
  2638. """
  2639. L{amp.utc.utcoffset} returns a zero timedelta.
  2640. """
  2641. self.assertEqual(amp.utc.utcoffset(None), datetime.timedelta(0))
  2642. def test_badSign(self):
  2643. """
  2644. L{amp._FixedOffsetTZInfo.fromSignHoursMinutes} raises L{ValueError} if
  2645. passed an offset sign other than C{'+'} or C{'-'}.
  2646. """
  2647. self.assertRaises(ValueError, tz, '?', 0, 0)
  2648. class RemoteAmpErrorTests(unittest.TestCase):
  2649. """
  2650. Tests for L{amp.RemoteAmpError}.
  2651. """
  2652. def test_stringMessage(self):
  2653. """
  2654. L{amp.RemoteAmpError} renders the given C{errorCode} (C{bytes}) and
  2655. C{description} into a native string.
  2656. """
  2657. error = amp.RemoteAmpError(b"BROKEN", "Something has broken")
  2658. self.assertEqual("Code<BROKEN>: Something has broken", str(error))
  2659. def test_stringMessageReplacesNonAsciiText(self):
  2660. """
  2661. When C{errorCode} contains non-ASCII characters, L{amp.RemoteAmpError}
  2662. renders then as backslash-escape sequences.
  2663. """
  2664. error = amp.RemoteAmpError(b"BROKEN-\xff", "Something has broken")
  2665. self.assertEqual("Code<BROKEN-\\xff>: Something has broken", str(error))
  2666. def test_stringMessageWithLocalFailure(self):
  2667. """
  2668. L{amp.RemoteAmpError} renders local errors with a "(local)" marker and
  2669. a brief traceback.
  2670. """
  2671. failure = Failure(Exception("Something came loose"))
  2672. error = amp.RemoteAmpError(
  2673. b"BROKEN", "Something has broken", local=failure)
  2674. self.assertRegex(
  2675. str(error), (
  2676. "^Code<BROKEN> [(]local[)]: Something has broken\n"
  2677. "Traceback [(]failure with no frames[)]: "
  2678. "<.+Exception.>: Something came loose\n"
  2679. ))
  2680. if not interfaces.IReactorSSL.providedBy(reactor):
  2681. skipMsg = 'This test case requires SSL support in the reactor'
  2682. TLSTests.skip = skipMsg
  2683. LiveFireTLSTests.skip = skipMsg
  2684. PlainVanillaLiveFireTests.skip = skipMsg
  2685. WithServerTLSVerificationTests.skip = skipMsg