home *** CD-ROM | disk | FTP | other *** search
/ PC Welt 2006 November (DVD) / PCWELT_11_2006.ISO / casper / filesystem.squashfs / usr / lib / python2.4 / site-packages / BitTorrent / Encrypter.pyc (.txt) < prev    next >
Encoding:
Python Compiled Bytecode  |  2006-08-31  |  21.8 KB  |  843 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyc (Python 2.4)
  3.  
  4. from cStringIO import StringIO
  5. from binascii import b2a_hex
  6. from socket import error as socketerror
  7. protocol_name = 'BitTorrent protocol'
  8.  
  9. def toint(s):
  10.     return long(b2a_hex(s), 16)
  11.  
  12.  
  13. def tobinary(i):
  14.     return chr(i >> 24) + chr(i >> 16 & 255) + chr(i >> 8 & 255) + chr(i & 255)
  15.  
  16.  
  17. class Connection:
  18.     
  19.     def __init__(self, Encoder, connection, id, is_local):
  20.         self.encoder = Encoder
  21.         self.connection = connection
  22.         self.id = id
  23.         self.locally_initiated = is_local
  24.         self.complete = False
  25.         self.closed = False
  26.         self.buffer = StringIO()
  27.         self.next_len = 1
  28.         self.next_func = self.read_header_len
  29.         if self.locally_initiated:
  30.             connection.write(chr(len(protocol_name)) + protocol_name + chr(0) * 8 + self.encoder.download_id)
  31.             if self.id is not None:
  32.                 connection.write(self.encoder.my_id)
  33.             
  34.         
  35.  
  36.     
  37.     def get_ip(self):
  38.         return self.connection.get_ip()
  39.  
  40.     
  41.     def get_id(self):
  42.         return self.id
  43.  
  44.     
  45.     def is_locally_initiated(self):
  46.         return self.locally_initiated
  47.  
  48.     
  49.     def is_flushed(self):
  50.         return self.connection.is_flushed()
  51.  
  52.     
  53.     def read_header_len(self, s):
  54.         if ord(s) != len(protocol_name):
  55.             return None
  56.         
  57.         return (len(protocol_name), self.read_header)
  58.  
  59.     
  60.     def read_header(self, s):
  61.         if s != protocol_name:
  62.             return None
  63.         
  64.         return (8, self.read_reserved)
  65.  
  66.     
  67.     def read_reserved(self, s):
  68.         return (20, self.read_download_id)
  69.  
  70.     
  71.     def read_download_id(self, s):
  72.         if s != self.encoder.download_id:
  73.             return None
  74.         
  75.         if not self.locally_initiated:
  76.             self.connection.write(chr(len(protocol_name)) + protocol_name + chr(0) * 8 + self.encoder.download_id + self.encoder.my_id)
  77.         
  78.         return (20, self.read_peer_id)
  79.  
  80.     
  81.     def read_peer_id(self, s):
  82.         if not self.id:
  83.             if s == self.encoder.my_id:
  84.                 return None
  85.             
  86.             for v in self.encoder.connections.values():
  87.                 if s and v.id == s:
  88.                     return None
  89.                     continue
  90.             
  91.             self.id = s
  92.             if self.locally_initiated:
  93.                 self.connection.write(self.encoder.my_id)
  94.             else:
  95.                 self.encoder.everinc = True
  96.         elif s != self.id:
  97.             return None
  98.         
  99.         self.complete = True
  100.         self.encoder.connecter.connection_made(self)
  101.         return (4, self.read_len)
  102.  
  103.     
  104.     def read_len(self, s):
  105.         l = toint(s)
  106.         if l > self.encoder.max_len:
  107.             return None
  108.         
  109.         return (l, self.read_message)
  110.  
  111.     
  112.     def read_message(self, s):
  113.         if s != '':
  114.             self.encoder.connecter.got_message(self, s)
  115.         
  116.         return (4, self.read_len)
  117.  
  118.     
  119.     def read_dead(self, s):
  120.         pass
  121.  
  122.     
  123.     def close(self):
  124.         if not self.closed:
  125.             self.connection.close()
  126.             self.sever()
  127.         
  128.  
  129.     
  130.     def sever(self):
  131.         self.closed = True
  132.         del self.encoder.connections[self.connection]
  133.         if self.complete:
  134.             self.encoder.connecter.connection_lost(self)
  135.         
  136.  
  137.     
  138.     def send_message(self, message):
  139.         self.connection.write(tobinary(len(message)) + message)
  140.  
  141.     
  142.     def data_came_in(self, s):
  143.         while True:
  144.             if self.closed:
  145.                 return None
  146.             
  147.             i = self.next_len - self.buffer.tell()
  148.             if i > len(s):
  149.                 self.buffer.write(s)
  150.                 return None
  151.             
  152.             self.buffer.write(s[:i])
  153.             s = s[i:]
  154.             m = self.buffer.getvalue()
  155.             self.buffer.reset()
  156.             self.buffer.truncate()
  157.             
  158.             try:
  159.                 x = self.next_func(m)
  160.             except:
  161.                 self.next_len = 1
  162.                 self.next_func = self.read_dead
  163.                 raise 
  164.  
  165.             if x is None:
  166.                 self.close()
  167.                 return None
  168.             
  169.             (self.next_len, self.next_func) = x
  170.  
  171.  
  172.  
  173. class Encoder:
  174.     
  175.     def __init__(self, connecter, raw_server, my_id, max_len, schedulefunc, keepalive_delay, download_id, max_initiate = 40):
  176.         self.raw_server = raw_server
  177.         self.connecter = connecter
  178.         self.my_id = my_id
  179.         self.max_len = max_len
  180.         self.schedulefunc = schedulefunc
  181.         self.keepalive_delay = keepalive_delay
  182.         self.download_id = download_id
  183.         self.max_initiate = max_initiate
  184.         self.everinc = False
  185.         self.connections = { }
  186.         self.spares = []
  187.         schedulefunc(self.send_keepalives, keepalive_delay)
  188.  
  189.     
  190.     def send_keepalives(self):
  191.         self.schedulefunc(self.send_keepalives, self.keepalive_delay)
  192.         for c in self.connections.values():
  193.             if c.complete:
  194.                 c.send_message('')
  195.                 continue
  196.         
  197.  
  198.     
  199.     def start_connection(self, dns, id):
  200.         if id:
  201.             if id == self.my_id:
  202.                 return None
  203.             
  204.             for v in self.connections.values():
  205.                 if v.id == id:
  206.                     return None
  207.                     continue
  208.             
  209.         
  210.         if len(self.connections) >= self.max_initiate:
  211.             if len(self.spares) < self.max_initiate and dns not in self.spares:
  212.                 self.spares.append(dns)
  213.             
  214.             return None
  215.         
  216.         
  217.         try:
  218.             c = self.raw_server.start_connection(dns)
  219.             self.connections[c] = Connection(self, c, id, True)
  220.         except socketerror:
  221.             pass
  222.  
  223.  
  224.     
  225.     def _start_connection(self, dns, id):
  226.         
  227.         def foo(self = self, dns = dns, id = id):
  228.             self.start_connection(dns, id)
  229.  
  230.         self.schedulefunc(foo, 0)
  231.  
  232.     
  233.     def got_id(self, connection):
  234.         for v in self.connections.values():
  235.             if connection is not v and connection.id == v.id:
  236.                 connection.close()
  237.                 return None
  238.                 continue
  239.         
  240.         self.connecter.connection_made(connection)
  241.  
  242.     
  243.     def ever_got_incoming(self):
  244.         return self.everinc
  245.  
  246.     
  247.     def external_connection_made(self, connection):
  248.         self.connections[connection] = Connection(self, connection, None, False)
  249.  
  250.     
  251.     def connection_flushed(self, connection):
  252.         c = self.connections[connection]
  253.         if c.complete:
  254.             self.connecter.connection_flushed(c)
  255.         
  256.  
  257.     
  258.     def connection_lost(self, connection):
  259.         self.connections[connection].sever()
  260.         while len(self.connections) < self.max_initiate and self.spares:
  261.             self.start_connection(self.spares.pop(), None)
  262.  
  263.     
  264.     def data_came_in(self, connection, data):
  265.         self.connections[connection].data_came_in(data)
  266.  
  267.  
  268.  
  269. class DummyConnecter:
  270.     
  271.     def __init__(self):
  272.         self.log = []
  273.         self.close_next = False
  274.  
  275.     
  276.     def connection_made(self, connection):
  277.         self.log.append(('made', connection))
  278.  
  279.     
  280.     def connection_lost(self, connection):
  281.         self.log.append(('lost', connection))
  282.  
  283.     
  284.     def connection_flushed(self, connection):
  285.         self.log.append(('flushed', connection))
  286.  
  287.     
  288.     def got_message(self, connection, message):
  289.         self.log.append(('got', connection, message))
  290.         if self.close_next:
  291.             connection.close()
  292.         
  293.  
  294.  
  295.  
  296. class DummyRawServer:
  297.     
  298.     def __init__(self):
  299.         self.connects = []
  300.  
  301.     
  302.     def start_connection(self, dns):
  303.         c = DummyRawConnection()
  304.         self.connects.append((dns, c))
  305.         return c
  306.  
  307.  
  308.  
  309. class DummyRawConnection:
  310.     
  311.     def __init__(self):
  312.         self.closed = False
  313.         self.data = []
  314.         self.flushed = True
  315.  
  316.     
  317.     def get_ip(self):
  318.         return 'fake.ip'
  319.  
  320.     
  321.     def is_flushed(self):
  322.         return self.flushed
  323.  
  324.     
  325.     def write(self, data):
  326.         if not not (self.closed):
  327.             raise AssertionError
  328.         self.data.append(data)
  329.  
  330.     
  331.     def close(self):
  332.         if not not (self.closed):
  333.             raise AssertionError
  334.         self.closed = True
  335.  
  336.     
  337.     def pop(self):
  338.         r = ''.join(self.data)
  339.         del self.data[:]
  340.         return r
  341.  
  342.  
  343.  
  344. def dummyschedule(a, b):
  345.     pass
  346.  
  347.  
  348. def test_messages_in_and_out():
  349.     c = DummyConnecter()
  350.     rs = DummyRawServer()
  351.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  352.     c1 = DummyRawConnection()
  353.     e.external_connection_made(c1)
  354.     if not c1.pop() == '':
  355.         raise AssertionError
  356.     if not c.log == []:
  357.         raise AssertionError
  358.     if not rs.connects == []:
  359.         raise AssertionError
  360.     if not not (c1.closed):
  361.         raise AssertionError
  362.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20)
  363.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  364.         raise AssertionError
  365.     if not c.log == []:
  366.         raise AssertionError
  367.     if not rs.connects == []:
  368.         raise AssertionError
  369.     if not not (c1.closed):
  370.         raise AssertionError
  371.     e.data_came_in(c1, 'b' * 20)
  372.     if not c1.pop() == '':
  373.         raise AssertionError
  374.     if not len(c.log) == 1:
  375.         raise AssertionError
  376.     if not c.log[0][0] == 'made':
  377.         raise AssertionError
  378.     ch = c.log[0][1]
  379.     del c.log[:]
  380.     if not rs.connects == []:
  381.         raise AssertionError
  382.     if not not (c1.closed):
  383.         raise AssertionError
  384.     if not ch.get_ip() == 'fake.ip':
  385.         raise AssertionError
  386.     ch.send_message('abc')
  387.     if not c1.pop() == chr(0) * 3 + chr(3) + 'abc':
  388.         raise AssertionError
  389.     if not c.log == []:
  390.         raise AssertionError
  391.     if not rs.connects == []:
  392.         raise AssertionError
  393.     if not not (c1.closed):
  394.         raise AssertionError
  395.     e.data_came_in(c1, chr(0) * 3 + chr(3) + 'def')
  396.     if not c1.pop() == '':
  397.         raise AssertionError
  398.     if not c.log == [
  399.         ('got', ch, 'def')]:
  400.         raise AssertionError
  401.     del c.log[:]
  402.     if not rs.connects == []:
  403.         raise AssertionError
  404.     if not not (c1.closed):
  405.         raise AssertionError
  406.  
  407.  
  408. def test_flushed():
  409.     c = DummyConnecter()
  410.     rs = DummyRawServer()
  411.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  412.     c1 = DummyRawConnection()
  413.     e.external_connection_made(c1)
  414.     if not c1.pop() == '':
  415.         raise AssertionError
  416.     if not c.log == []:
  417.         raise AssertionError
  418.     if not rs.connects == []:
  419.         raise AssertionError
  420.     if not not (c1.closed):
  421.         raise AssertionError
  422.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20)
  423.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  424.         raise AssertionError
  425.     if not c1.pop() == '':
  426.         raise AssertionError
  427.     if not c.log == []:
  428.         raise AssertionError
  429.     if not rs.connects == []:
  430.         raise AssertionError
  431.     if not not (c1.closed):
  432.         raise AssertionError
  433.     e.connection_flushed(c1)
  434.     if not c1.pop() == '':
  435.         raise AssertionError
  436.     if not c.log == []:
  437.         raise AssertionError
  438.     if not rs.connects == []:
  439.         raise AssertionError
  440.     if not not (c1.closed):
  441.         raise AssertionError
  442.     e.data_came_in(c1, 'b' * 20)
  443.     if not c1.pop() == '':
  444.         raise AssertionError
  445.     if not len(c.log) == 1:
  446.         raise AssertionError
  447.     if not c.log[0][0] == 'made':
  448.         raise AssertionError
  449.     ch = c.log[0][1]
  450.     del c.log[:]
  451.     if not rs.connects == []:
  452.         raise AssertionError
  453.     if not not (c1.closed):
  454.         raise AssertionError
  455.     if not ch.is_flushed():
  456.         raise AssertionError
  457.     e.connection_flushed(c1)
  458.     if not c1.pop() == '':
  459.         raise AssertionError
  460.     if not c.log == [
  461.         ('flushed', ch)]:
  462.         raise AssertionError
  463.     if not rs.connects == []:
  464.         raise AssertionError
  465.     if not not (c1.closed):
  466.         raise AssertionError
  467.     c1.flushed = False
  468.     if not not ch.is_flushed():
  469.         raise AssertionError
  470.  
  471.  
  472. def test_wrong_header_length():
  473.     c = DummyConnecter()
  474.     rs = DummyRawServer()
  475.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  476.     c1 = DummyRawConnection()
  477.     e.external_connection_made(c1)
  478.     if not c1.pop() == '':
  479.         raise AssertionError
  480.     if not c.log == []:
  481.         raise AssertionError
  482.     if not rs.connects == []:
  483.         raise AssertionError
  484.     if not not (c1.closed):
  485.         raise AssertionError
  486.     e.data_came_in(c1, chr(5) * 30)
  487.     if not c.log == []:
  488.         raise AssertionError
  489.     if not c1.closed:
  490.         raise AssertionError
  491.  
  492.  
  493. def test_wrong_header():
  494.     c = DummyConnecter()
  495.     rs = DummyRawServer()
  496.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  497.     c1 = DummyRawConnection()
  498.     e.external_connection_made(c1)
  499.     if not c1.pop() == '':
  500.         raise AssertionError
  501.     if not c.log == []:
  502.         raise AssertionError
  503.     if not rs.connects == []:
  504.         raise AssertionError
  505.     if not not (c1.closed):
  506.         raise AssertionError
  507.     e.data_came_in(c1, chr(len(protocol_name)) + 'a' * len(protocol_name))
  508.     if not c.log == []:
  509.         raise AssertionError
  510.     if not c1.closed:
  511.         raise AssertionError
  512.  
  513.  
  514. def test_wrong_download_id():
  515.     c = DummyConnecter()
  516.     rs = DummyRawServer()
  517.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  518.     c1 = DummyRawConnection()
  519.     e.external_connection_made(c1)
  520.     if not c1.pop() == '':
  521.         raise AssertionError
  522.     if not c.log == []:
  523.         raise AssertionError
  524.     if not rs.connects == []:
  525.         raise AssertionError
  526.     if not not (c1.closed):
  527.         raise AssertionError
  528.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'e' * 20)
  529.     if not c1.pop() == '':
  530.         raise AssertionError
  531.     if not c.log == []:
  532.         raise AssertionError
  533.     if not c1.closed:
  534.         raise AssertionError
  535.  
  536.  
  537. def test_wrong_other_id():
  538.     c = DummyConnecter()
  539.     rs = DummyRawServer()
  540.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  541.     e.start_connection('dns', 'o' * 20)
  542.     if not c.log == []:
  543.         raise AssertionError
  544.     if not len(rs.connects) == 1:
  545.         raise AssertionError
  546.     if not rs.connects[0][0] == 'dns':
  547.         raise AssertionError
  548.     c1 = rs.connects[0][1]
  549.     del rs.connects[:]
  550.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  551.         raise AssertionError
  552.     if not not (c1.closed):
  553.         raise AssertionError
  554.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'b' * 20)
  555.     if not c.log == []:
  556.         raise AssertionError
  557.     if not c1.closed:
  558.         raise AssertionError
  559.  
  560.  
  561. def test_over_max_len():
  562.     c = DummyConnecter()
  563.     rs = DummyRawServer()
  564.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  565.     c1 = DummyRawConnection()
  566.     e.external_connection_made(c1)
  567.     if not c1.pop() == '':
  568.         raise AssertionError
  569.     if not c.log == []:
  570.         raise AssertionError
  571.     if not rs.connects == []:
  572.         raise AssertionError
  573.     if not not (c1.closed):
  574.         raise AssertionError
  575.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  576.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  577.         raise AssertionError
  578.     if not len(c.log) == 1 or c.log[0][0] == 'made':
  579.         raise AssertionError
  580.     ch = c.log[0][1]
  581.     del c.log[:]
  582.     if not not (c1.closed):
  583.         raise AssertionError
  584.     e.data_came_in(c1, chr(1) + chr(0) * 3)
  585.     if not c.log == [
  586.         ('lost', ch)]:
  587.         raise AssertionError
  588.     if not c1.closed:
  589.         raise AssertionError
  590.  
  591.  
  592. def test_keepalive():
  593.     s = []
  594.     
  595.     def sched(interval, thing, s = s):
  596.         s.append((interval, thing))
  597.  
  598.     c = DummyConnecter()
  599.     rs = DummyRawServer()
  600.     e = Encoder(c, rs, 'a' * 20, 500, sched, 30, 'd' * 20)
  601.     if not len(s) == 1:
  602.         raise AssertionError
  603.     if not s[0][1] == 30:
  604.         raise AssertionError
  605.     kfunc = s[0][0]
  606.     del s[:]
  607.     c1 = DummyRawConnection()
  608.     e.external_connection_made(c1)
  609.     if not c1.pop() == '':
  610.         raise AssertionError
  611.     if not c.log == []:
  612.         raise AssertionError
  613.     if not not (c1.closed):
  614.         raise AssertionError
  615.     kfunc()
  616.     if not c1.pop() == '':
  617.         raise AssertionError
  618.     if not c.log == []:
  619.         raise AssertionError
  620.     if not not (c1.closed):
  621.         raise AssertionError
  622.     if not s == [
  623.         (kfunc, 30)]:
  624.         raise AssertionError
  625.     del s[:]
  626.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  627.     if not len(c.log) == 1 or c.log[0][0] == 'made':
  628.         raise AssertionError
  629.     del c.log[:]
  630.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  631.         raise AssertionError
  632.     if not not (c1.closed):
  633.         raise AssertionError
  634.     kfunc()
  635.     if not c1.pop() == chr(0) * 4:
  636.         raise AssertionError
  637.     if not c.log == []:
  638.         raise AssertionError
  639.     if not not (c1.closed):
  640.         raise AssertionError
  641.  
  642.  
  643. def test_swallow_keepalive():
  644.     c = DummyConnecter()
  645.     rs = DummyRawServer()
  646.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  647.     c1 = DummyRawConnection()
  648.     e.external_connection_made(c1)
  649.     if not c1.pop() == '':
  650.         raise AssertionError
  651.     if not c.log == []:
  652.         raise AssertionError
  653.     if not rs.connects == []:
  654.         raise AssertionError
  655.     if not not (c1.closed):
  656.         raise AssertionError
  657.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  658.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  659.         raise AssertionError
  660.     if not len(c.log) == 1 or c.log[0][0] == 'made':
  661.         raise AssertionError
  662.     del c.log[:]
  663.     if not not (c1.closed):
  664.         raise AssertionError
  665.     e.data_came_in(c1, chr(0) * 4)
  666.     if not c.log == []:
  667.         raise AssertionError
  668.     if not not (c1.closed):
  669.         raise AssertionError
  670.  
  671.  
  672. def test_local_close():
  673.     c = DummyConnecter()
  674.     rs = DummyRawServer()
  675.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  676.     c1 = DummyRawConnection()
  677.     e.external_connection_made(c1)
  678.     if not c1.pop() == '':
  679.         raise AssertionError
  680.     if not c.log == []:
  681.         raise AssertionError
  682.     if not rs.connects == []:
  683.         raise AssertionError
  684.     if not not (c1.closed):
  685.         raise AssertionError
  686.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  687.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  688.         raise AssertionError
  689.     if not len(c.log) == 1 or c.log[0][0] == 'made':
  690.         raise AssertionError
  691.     ch = c.log[0][1]
  692.     del c.log[:]
  693.     if not not (c1.closed):
  694.         raise AssertionError
  695.     ch.close()
  696.     if not c.log == [
  697.         ('lost', ch)]:
  698.         raise AssertionError
  699.     del c.log[:]
  700.     if not c1.closed:
  701.         raise AssertionError
  702.  
  703.  
  704. def test_local_close_in_message_receive():
  705.     c = DummyConnecter()
  706.     rs = DummyRawServer()
  707.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  708.     c1 = DummyRawConnection()
  709.     e.external_connection_made(c1)
  710.     if not c1.pop() == '':
  711.         raise AssertionError
  712.     if not c.log == []:
  713.         raise AssertionError
  714.     if not rs.connects == []:
  715.         raise AssertionError
  716.     if not not (c1.closed):
  717.         raise AssertionError
  718.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  719.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  720.         raise AssertionError
  721.     if not len(c.log) == 1 or c.log[0][0] == 'made':
  722.         raise AssertionError
  723.     ch = c.log[0][1]
  724.     del c.log[:]
  725.     if not not (c1.closed):
  726.         raise AssertionError
  727.     c.close_next = True
  728.     e.data_came_in(c1, chr(0) * 3 + chr(4) + 'abcd')
  729.     if not c.log == [
  730.         ('got', ch, 'abcd'),
  731.         ('lost', ch)]:
  732.         raise AssertionError
  733.     if not c1.closed:
  734.         raise AssertionError
  735.  
  736.  
  737. def test_remote_close():
  738.     c = DummyConnecter()
  739.     rs = DummyRawServer()
  740.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  741.     c1 = DummyRawConnection()
  742.     e.external_connection_made(c1)
  743.     if not c1.pop() == '':
  744.         raise AssertionError
  745.     if not c.log == []:
  746.         raise AssertionError
  747.     if not rs.connects == []:
  748.         raise AssertionError
  749.     if not not (c1.closed):
  750.         raise AssertionError
  751.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  752.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  753.         raise AssertionError
  754.     if not len(c.log) == 1 or c.log[0][0] == 'made':
  755.         raise AssertionError
  756.     ch = c.log[0][1]
  757.     del c.log[:]
  758.     if not not (c1.closed):
  759.         raise AssertionError
  760.     e.connection_lost(c1)
  761.     if not c.log == [
  762.         ('lost', ch)]:
  763.         raise AssertionError
  764.     if not not (c1.closed):
  765.         raise AssertionError
  766.  
  767.  
  768. def test_partial_data_in():
  769.     c = DummyConnecter()
  770.     rs = DummyRawServer()
  771.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  772.     c1 = DummyRawConnection()
  773.     e.external_connection_made(c1)
  774.     if not c1.pop() == '':
  775.         raise AssertionError
  776.     if not c.log == []:
  777.         raise AssertionError
  778.     if not rs.connects == []:
  779.         raise AssertionError
  780.     if not not (c1.closed):
  781.         raise AssertionError
  782.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 4)
  783.     e.data_came_in(c1, chr(0) * 4 + 'd' * 20 + 'c' * 10)
  784.     e.data_came_in(c1, 'c' * 10)
  785.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  786.         raise AssertionError
  787.     if not len(c.log) == 1 or c.log[0][0] == 'made':
  788.         raise AssertionError
  789.     del c.log[:]
  790.     if not not (c1.closed):
  791.         raise AssertionError
  792.  
  793.  
  794. def test_ignore_connect_of_extant():
  795.     c = DummyConnecter()
  796.     rs = DummyRawServer()
  797.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  798.     c1 = DummyRawConnection()
  799.     e.external_connection_made(c1)
  800.     if not c1.pop() == '':
  801.         raise AssertionError
  802.     if not c.log == []:
  803.         raise AssertionError
  804.     if not rs.connects == []:
  805.         raise AssertionError
  806.     if not not (c1.closed):
  807.         raise AssertionError
  808.     e.data_came_in(c1, chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'o' * 20)
  809.     if not c1.pop() == chr(len(protocol_name)) + protocol_name + chr(0) * 8 + 'd' * 20 + 'a' * 20:
  810.         raise AssertionError
  811.     if not len(c.log) == 1 or c.log[0][0] == 'made':
  812.         raise AssertionError
  813.     del c.log[:]
  814.     if not not (c1.closed):
  815.         raise AssertionError
  816.     e.start_connection('dns', 'o' * 20)
  817.     if not c.log == []:
  818.         raise AssertionError
  819.     if not rs.connects == []:
  820.         raise AssertionError
  821.     if not not (c1.closed):
  822.         raise AssertionError
  823.  
  824.  
  825. def test_ignore_connect_to_self():
  826.     c = DummyConnecter()
  827.     rs = DummyRawServer()
  828.     e = Encoder(c, rs, 'a' * 20, 500, dummyschedule, 30, 'd' * 20)
  829.     c1 = DummyRawConnection()
  830.     e.start_connection('dns', 'a' * 20)
  831.     if not c.log == []:
  832.         raise AssertionError
  833.     if not rs.connects == []:
  834.         raise AssertionError
  835.     if not not (c1.closed):
  836.         raise AssertionError
  837.  
  838.  
  839. def test_conversion():
  840.     if not toint(tobinary(50000)) == 50000:
  841.         raise AssertionError
  842.  
  843.