def __init__(self): bindings = grammar.bindings if bindings is None: bindings = {} ParserProtocol.__init__(self, grammar=self.source, senderFactory=SOCKS4Sender, receiverFactory=SOCKS4Receiver, bindings=bindings)
def __init__(self): source = OMeta(grammar.grammarSource).parseGrammar('Grammar') bindings = grammar.bindings if bindings is None: bindings = {} ParserProtocol.__init__(self, grammar=source, senderFactory=SOCKS4Sender, receiverFactory=SOCKS4Receiver, bindings=bindings)
def __init__(self): source = OMeta(grammar.grammarSource).parseGrammar('Grammar') bindings = grammar.bindings if bindings is None: bindings = {} ParserProtocol.__init__(self, grammar=source, senderFactory=SOCKS5Sender, receiverFactory=stack(SOCKS5AuthDispatcher, SOCKS5Receiver), bindings=bindings)
def dataReceived(self, data): if self.receiver.currentRule == 'State_readData': # Shortcut for efficiency self.receiver.dataReceived(data) else: ParserProtocol.dataReceived(self, data)
def __init__(self, *args): ParserProtocol.__init__(self, *args)
def setUp(self): self.protocol = ParserProtocol( testGrammar, SenderFactory, ReceiverFactory, {})
class ParserProtocolTestCase(unittest.TestCase): skip = skip def setUp(self): self.protocol = ParserProtocol( testGrammar, SenderFactory, ReceiverFactory, {}) def test_transportPassed(self): """The sender is passed the transport recieved by the protocol.""" transport = object() self.protocol.makeConnection(transport) self.assertEqual(transport, self.protocol.sender.transport) def test_senderPassed(self): """The sender is passed to the receiver.""" self.protocol.makeConnection(None) self.assertEqual(self.protocol.sender, self.protocol.receiver.sender) def test_parserPassed(self): """The parser is passed in the prepareParsing method.""" self.protocol.makeConnection(None) self.assertEqual(self.protocol, self.protocol.receiver.parser) def test_connectionEstablishes(self): """prepareParsing is called on the receiver after connection establishment.""" self.protocol.makeConnection(None) self.assert_(self.protocol.receiver.connected) def test_basicParsing(self): """Rules can be parsed multiple times for the same effect.""" self.protocol.makeConnection(None) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a', 'a']) def test_parsingChunks(self): """Any number of rules can be called from one dataRecived.""" self.protocol.makeConnection(None) self.protocol.dataReceived('a') self.assertEqual(self.protocol.receiver.calls, []) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('aaa') self.assertEqual(self.protocol.receiver.calls, ['a', 'a', 'a']) def test_ruleSwitching(self): """The rule being parsed can specify the next rule to be parsed.""" self.protocol.makeConnection(None) self.protocol.receiver.returnMap.update(dict(a='someB', b='someA')) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('bb') self.assertEqual(self.protocol.receiver.calls, ['a', 'b']) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a', 'b', 'a']) def test_ruleSwitchingWithChunks(self): """Any number of rules can be called even during rule switching.""" self.protocol.makeConnection(None) self.protocol.receiver.returnMap.update(dict(a='someB', b='someA')) self.protocol.dataReceived('a') self.assertEqual(self.protocol.receiver.calls, []) self.protocol.dataReceived('ab') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('baa') self.assertEqual(self.protocol.receiver.calls, ['a', 'b', 'a']) def test_rulesCannotBeSwitchedDuringParsing(self): """ One can set a new rule during parsing, but it won't change the rule currently being parsed. """ self.protocol.makeConnection(None) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('a') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.receiver.currentRule = 'someC' self.protocol.dataReceived('acc') self.assertEqual(self.protocol.receiver.calls, ['a', 'a', 'c']) def test_connectionLoss(self): """The reason for connection loss is forwarded to the receiver.""" self.protocol.makeConnection(None) reason = object() self.protocol.connectionLost(reason) self.assertEqual(self.protocol.receiver.lossReason, reason) def test_parseFailure(self): """ Parse failures cause connection abortion with the parse error as the reason. """ transport = FakeTransport() self.protocol.makeConnection(transport) self.protocol.dataReceived('b') self.failIfEqual(self.protocol.receiver.lossReason, None) self.failUnlessIsInstance(self.protocol.receiver.lossReason.value, ParseError) self.assert_(transport.aborted) def test_exceptionsRaisedFromReceiver(self): """ Raising an exception from receiver methods called from the grammar propagate to finishParsing. """ transport = FakeTransport() self.protocol.makeConnection(transport) self.protocol.dataReceived('e') self.failIfEqual(self.protocol.receiver.lossReason, None) self.failUnlessIsInstance(self.protocol.receiver.lossReason.value, SomeException) self.assert_(transport.aborted) def test_dataIgnoredAfterDisconnection(self): """After connectionLost is called, all incoming data is ignored.""" transport = FakeTransport() self.protocol.makeConnection(transport) reason = object() self.protocol.connectionLost(reason) self.protocol.dataReceived('d') self.assertEqual(self.protocol.receiver.lossReason, reason) self.assert_(not transport.aborted)
def setUp(self): self.protocol = ParserProtocol(testGrammar, SenderFactory, ReceiverFactory, {})
class ParserProtocolTestCase(unittest.TestCase): skip = skip def setUp(self): self.protocol = ParserProtocol(testGrammar, SenderFactory, ReceiverFactory, {}) def test_transportPassed(self): """The sender is passed the transport recieved by the protocol.""" transport = object() self.protocol.makeConnection(transport) self.assertEqual(transport, self.protocol.sender.transport) def test_senderPassed(self): """The sender is passed to the receiver.""" self.protocol.makeConnection(None) self.assertEqual(self.protocol.sender, self.protocol.receiver.sender) def test_parserPassed(self): """The parser is passed in the prepareParsing method.""" self.protocol.makeConnection(None) self.assertEqual(self.protocol, self.protocol.receiver.parser) def test_connectionEstablishes(self): """prepareParsing is called on the receiver after connection establishment.""" self.protocol.makeConnection(None) self.assert_(self.protocol.receiver.connected) def test_basicParsing(self): """Rules can be parsed multiple times for the same effect.""" self.protocol.makeConnection(None) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a', 'a']) def test_parsingChunks(self): """Any number of rules can be called from one dataRecived.""" self.protocol.makeConnection(None) self.protocol.dataReceived('a') self.assertEqual(self.protocol.receiver.calls, []) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('aaa') self.assertEqual(self.protocol.receiver.calls, ['a', 'a', 'a']) def test_ruleSwitching(self): """The rule being parsed can specify the next rule to be parsed.""" self.protocol.makeConnection(None) self.protocol.receiver.returnMap.update(dict(a='someB', b='someA')) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('bb') self.assertEqual(self.protocol.receiver.calls, ['a', 'b']) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a', 'b', 'a']) def test_ruleSwitchingWithChunks(self): """Any number of rules can be called even during rule switching.""" self.protocol.makeConnection(None) self.protocol.receiver.returnMap.update(dict(a='someB', b='someA')) self.protocol.dataReceived('a') self.assertEqual(self.protocol.receiver.calls, []) self.protocol.dataReceived('ab') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('baa') self.assertEqual(self.protocol.receiver.calls, ['a', 'b', 'a']) def test_rulesCannotBeSwitchedDuringParsing(self): """ One can set a new rule during parsing, but it won't change the rule currently being parsed. """ self.protocol.makeConnection(None) self.protocol.dataReceived('aa') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.dataReceived('a') self.assertEqual(self.protocol.receiver.calls, ['a']) self.protocol.receiver.currentRule = 'someC' self.protocol.dataReceived('acc') self.assertEqual(self.protocol.receiver.calls, ['a', 'a', 'c']) def test_connectionLoss(self): """The reason for connection loss is forwarded to the receiver.""" self.protocol.makeConnection(None) reason = object() self.protocol.connectionLost(reason) self.assertEqual(self.protocol.receiver.lossReason, reason) def test_parseFailure(self): """ Parse failures cause connection abortion with the parse error as the reason. """ transport = FakeTransport() self.protocol.makeConnection(transport) self.protocol.dataReceived('b') self.failIfEqual(self.protocol.receiver.lossReason, None) self.failUnlessIsInstance(self.protocol.receiver.lossReason.value, ParseError) self.assert_(transport.aborted) def test_exceptionsRaisedFromReceiver(self): """ Raising an exception from receiver methods called from the grammar propagate to finishParsing. """ transport = FakeTransport() self.protocol.makeConnection(transport) self.protocol.dataReceived('e') self.failIfEqual(self.protocol.receiver.lossReason, None) self.failUnlessIsInstance(self.protocol.receiver.lossReason.value, SomeException) self.assert_(transport.aborted) def test_dataIgnoredAfterDisconnection(self): """After connectionLost is called, all incoming data is ignored.""" transport = FakeTransport() self.protocol.makeConnection(transport) reason = object() self.protocol.connectionLost(reason) self.protocol.dataReceived('d') self.assertEqual(self.protocol.receiver.lossReason, reason) self.assert_(not transport.aborted)
def dataReceived(self, data): data = to_string(data) return ParserProtocol.dataReceived(self, data)