|
| 1 | +from twisted.test.proto_helpers import StringTransport |
| 2 | + |
| 3 | +import parsley |
| 4 | +import pytest |
| 5 | +import netstrings |
| 6 | + |
| 7 | +netstringGrammar = parsley.makeGrammar(netstrings.grammar, {}) |
| 8 | + |
| 9 | +def stringParserFromRule(rule): |
| 10 | + def parseString(s): |
| 11 | + return getattr(netstringGrammar(s), rule)() |
| 12 | + return parseString |
| 13 | + |
| 14 | +def test_digits_parsing(): |
| 15 | + parse = stringParserFromRule('digits') |
| 16 | + |
| 17 | + assert parse('0') == 0 |
| 18 | + assert parse('1') == 1 |
| 19 | + assert parse('1234567890') == 1234567890 |
| 20 | + with pytest.raises(parsley.ParseError): |
| 21 | + parse('01') |
| 22 | + with pytest.raises(parsley.ParseError): |
| 23 | + parse('0001') |
| 24 | + |
| 25 | +def test_netstring_parsing(): |
| 26 | + parse = stringParserFromRule('netstring') |
| 27 | + |
| 28 | + assert parse('0:,') == '' |
| 29 | + assert parse('1:x,') == 'x' |
| 30 | + assert parse('10:abcdefghij,') == 'abcdefghij' |
| 31 | + |
| 32 | + |
| 33 | +def build_testing_sender(): |
| 34 | + transport = StringTransport() |
| 35 | + sender = netstrings.NetstringSender(transport) |
| 36 | + return sender, transport |
| 37 | + |
| 38 | +def test_sending_empty_netstring(): |
| 39 | + sender, transport = build_testing_sender() |
| 40 | + sender.sendNetstring('') |
| 41 | + assert transport.value() == '0:,' |
| 42 | + |
| 43 | +def test_sending_one_netstring(): |
| 44 | + sender, transport = build_testing_sender() |
| 45 | + sender.sendNetstring('foobar') |
| 46 | + assert transport.value() == '6:foobar,' |
| 47 | + |
| 48 | +def test_sending_two_netstrings(): |
| 49 | + sender, transport = build_testing_sender() |
| 50 | + sender.sendNetstring('spam') |
| 51 | + sender.sendNetstring('egggs') |
| 52 | + assert transport.value() == '4:spam,5:egggs,' |
| 53 | + |
| 54 | + |
| 55 | +class FakeReceiver(object): |
| 56 | + def __init__(self, sender, parser): |
| 57 | + self.sender = sender |
| 58 | + self.parser = parser |
| 59 | + self.netstrings = [] |
| 60 | + self.connected = False |
| 61 | + self.lossReason = None |
| 62 | + |
| 63 | + def netstringReceived(self, s): |
| 64 | + self.netstrings.append(s) |
| 65 | + |
| 66 | + def connectionMade(self): |
| 67 | + self.connected = True |
| 68 | + |
| 69 | + def connectionLost(self, reason): |
| 70 | + self.lossReason = reason |
| 71 | + |
| 72 | +TestingNetstringProtocol = parsley.makeProtocol( |
| 73 | + netstrings.grammar, netstrings.NetstringSender, FakeReceiver) |
| 74 | + |
| 75 | +def build_testing_protocol(): |
| 76 | + protocol = TestingNetstringProtocol() |
| 77 | + transport = StringTransport() |
| 78 | + protocol.makeConnection(transport) |
| 79 | + return protocol, transport |
| 80 | + |
| 81 | +def test_receiving_empty_netstring(): |
| 82 | + protocol, transport = build_testing_protocol() |
| 83 | + protocol.dataReceived('0:,') |
| 84 | + assert protocol.receiver.netstrings == [''] |
| 85 | + |
| 86 | +def test_receiving_one_netstring_by_byte(): |
| 87 | + protocol, transport = build_testing_protocol() |
| 88 | + for c in '4:spam,': |
| 89 | + protocol.dataReceived(c) |
| 90 | + assert protocol.receiver.netstrings == ['spam'] |
| 91 | + |
| 92 | +def test_receiving_two_netstrings_by_byte(): |
| 93 | + protocol, transport = build_testing_protocol() |
| 94 | + for c in '4:spam,4:eggs,': |
| 95 | + protocol.dataReceived(c) |
| 96 | + assert protocol.receiver.netstrings == ['spam', 'eggs'] |
| 97 | + |
| 98 | +def test_receiving_two_netstrings_in_chunks(): |
| 99 | + protocol, transport = build_testing_protocol() |
| 100 | + for c in ['4:', 'spa', 'm,4', ':eg', 'gs,']: |
| 101 | + protocol.dataReceived(c) |
| 102 | + assert protocol.receiver.netstrings == ['spam', 'eggs'] |
| 103 | + |
| 104 | +def test_receiving_two_netstrings_at_once(): |
| 105 | + protocol, transport = build_testing_protocol() |
| 106 | + protocol.dataReceived('4:spam,4:eggs,') |
| 107 | + assert protocol.receiver.netstrings == ['spam', 'eggs'] |
| 108 | + |
| 109 | +def test_establishing_connection(): |
| 110 | + assert not FakeReceiver(None, None).connected |
| 111 | + protocol, transport = build_testing_protocol() |
| 112 | + assert protocol.receiver.connected |
| 113 | + |
| 114 | +def test_losing_connection(): |
| 115 | + protocol, transport = build_testing_protocol() |
| 116 | + reason = object() |
| 117 | + protocol.connectionLost(reason) |
| 118 | + assert protocol.receiver.lossReason == reason |
0 commit comments