コード例 #1
0
    def test_create_message_list_request(self):
        """Test message list request creation"""

        m1 = b'42'
        m2 = b'\x01\x23\x245'
        m3 = b'\x42\x42\x42'
        m3_str = encode_b64_bytes(m3).decode()
        m2_str = encode_b64_bytes(m2).decode()
        m1_str = encode_b64_bytes(m1).decode()

        self.assertTrue(dandelion.protocol.is_message_list_request('GETMESSAGES\n'))
        self.assertFalse(dandelion.protocol.is_message_list_request('GETMES_XXX_SAGES\n'))
        self.assertEqual(dandelion.protocol.create_message_list_request(), 'GETMESSAGES\n')
        self.assertEqual(dandelion.protocol.create_message_list_request([]), 'GETMESSAGES\n')

        str_ = dandelion.protocol.create_message_list_request([m1, m2, m3])
        self.assertTrue(dandelion.protocol.is_message_list_request(str_))
        self.assertTrue('GETMESSAGES ' in str_)
        self.assertEquals(str_.count(';'), 2)

        self.assertTrue(m1_str in str_)
        self.assertTrue(m2_str in str_)
        self.assertTrue(m3_str in str_)

        """Testing bad input"""
        self.assertRaises(TypeError, dandelion.protocol.create_message_list_request, 0)
        self.assertRaises(TypeError, dandelion.protocol.create_message_list_request, -1337)
        self.assertRaises(TypeError, dandelion.protocol.create_message_list_request, "1337")
        self.assertRaises(TypeError, dandelion.protocol.create_message_list_request, "XXX")
コード例 #2
0
    def test_parse_identity_id_list(self):
        """Test parsing the identity ID list request string"""

        tc = b'13\x01\x07'
        tc_str = encode_b64_bytes(tc).decode()

        id1 = b'42'
        id2 = b'\x01\x23\x245'
        id3 = b'\x42\x42\x42'
        id1_str = encode_b64_bytes(id1).decode()
        id2_str = encode_b64_bytes(id2).decode()
        id3_str = encode_b64_bytes(id3).decode()

        parsed_tc, identityidlist = dandelion.protocol.parse_identity_id_list(''.join([';'.join([tc_str, id1_str, id2_str, id3_str]), '\n']))
        self.assertEqual(parsed_tc, tc)
        self.assertEqual(len(identityidlist), 3)
        self.assertTrue(id1 in identityidlist)
        self.assertTrue(id2 in identityidlist)
        self.assertTrue(id3 in identityidlist)

        parsed_tc, identityidlist = dandelion.protocol.parse_identity_id_list(''.join([tc_str, '\n']))
        self.assertEqual(parsed_tc, tc)
        self.assertEqual(len(identityidlist), 0)

        """Testing bad input"""
        self.assertRaises(ValueError, dandelion.protocol.parse_identity_id_list, None)
        self.assertRaises(TypeError, dandelion.protocol.parse_identity_id_list, 1337)
        self.assertRaises(TypeError, dandelion.protocol.parse_identity_id_list, [])

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list,
                          '')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list,
                          '\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list,
                          '???\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list,
                          'FF FF\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list,
                          '???;???\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list,
                          'FF;;FF\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list,
                          'FF;FF;\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list,
                          ';FF;FF\n')
コード例 #3
0
def create_message_id_list(time_cookie, messages=None):
    """Create the response string for sending message IDs from the server.
    
    The time_cookie (bytes) is required, but the list of Message's is
    optional.
    
    [C]                                                    [S]
     |                                                      | 
     |       <time cookie>;<msgid>;<msgid>;...;<msgid>      | 
     |<-----------------------------------------------------| 
     |                                                      | 
    """

    _assert_type(time_cookie, bytes)

    if messages is None: # Don't use mutable default (e.g. [])
        messages = []

    if not hasattr(messages, '__iter__'):
        raise TypeError

    tc_str = encode_b64_bytes(time_cookie).decode()

    msgparts = [tc_str]
    msgparts.extend([encode_b64_bytes(msg.id).decode() for msg in messages])
    return ''.join([_FIELD_SEPARATOR.join(msgparts),
                    TERMINATOR])
コード例 #4
0
    def test_create_identity_list_request(self):
        """Test identity list request creation"""

        id1 = b'42'
        id2 = b'\x01\x23\x245'
        id3 = b'\x42\x42\x42'
        id1_str = encode_b64_bytes(id1).decode()
        id2_str = encode_b64_bytes(id2).decode()
        id3_str = encode_b64_bytes(id3).decode()

        self.assertTrue(dandelion.protocol.is_identity_list_request('GETIDENTITIES\n'))
        self.assertFalse(dandelion.protocol.is_identity_list_request('GETUSE_XXX_RS\n'))
        self.assertEqual(dandelion.protocol.create_identity_list_request(), 'GETIDENTITIES\n')
        self.assertEqual(dandelion.protocol.create_identity_list_request([]), 'GETIDENTITIES\n')

        str_ = dandelion.protocol.create_identity_list_request([id1, id2, id3])
        self.assertTrue(dandelion.protocol.is_identity_list_request(str_))
        self.assertTrue('GETIDENTITIES ' in str_)
        self.assertEquals(str_.count(';'), 2)

        self.assertTrue(id1_str in str_)
        self.assertTrue(id2_str in str_)
        self.assertTrue(id3_str in str_)

        """Testing bad input"""
        self.assertRaises(TypeError, dandelion.protocol.create_identity_list_request, 0)
        self.assertRaises(TypeError, dandelion.protocol.create_identity_list_request, -1337)
        self.assertRaises(TypeError, dandelion.protocol.create_identity_list_request, "1337")
        self.assertRaises(TypeError, dandelion.protocol.create_identity_list_request, "XXX")
コード例 #5
0
def create_identity_id_list(time_cookie, identities=None):
    """Create the response string for sending identity IDs from the server.
    
    The time_cookie (bytes) is required, but the list of IDs is
    optional.
    
    [C]                                                    [S]
     |                                                      | 
     |         <time cookie>;<uid>;<uid>;...;<uid>          | 
     |<-----------------------------------------------------| 
     |                                                      | 
    """

    _assert_type(time_cookie, bytes)

    if identities is None: # Don't use mutable default (e.g. [])
        identities = []

    if not hasattr(identities, '__iter__'):
        raise TypeError

    tc_str = encode_b64_bytes(time_cookie).decode()

    identityparts = [tc_str]
    identityparts.extend([encode_b64_bytes(identity.fingerprint).decode() for identity in identities])
    return ''.join([_FIELD_SEPARATOR.join(identityparts),
                    TERMINATOR])
コード例 #6
0
    def test_parse_message_id_list(self):
        """Test parsing the message ID list request string"""

        tc = b'13\x01\x07'
        tc_str = encode_b64_bytes(tc).decode()

        m1 = b'42'
        m2 = b'\x01\x23\x245'
        m3 = b'\x42\x42\x42'
        m1_str = encode_b64_bytes(m1).decode()
        m2_str = encode_b64_bytes(m2).decode()
        m3_str = encode_b64_bytes(m3).decode()

        parsed_tc, msgidlist = dandelion.protocol.parse_message_id_list(''.join([';'.join([tc_str, m1_str, m2_str, m3_str]), '\n']))
        self.assertEqual(parsed_tc, tc)
        self.assertEqual(len(msgidlist), 3)
        self.assertTrue(m1 in msgidlist)
        self.assertTrue(m2 in msgidlist)
        self.assertTrue(m3 in msgidlist)

        parsed_tc, msgidlist = dandelion.protocol.parse_message_id_list(''.join([tc_str, '\n']))
        self.assertEqual(parsed_tc, tc)
        self.assertEqual(len(msgidlist), 0)

        """Testing bad input"""
        self.assertRaises(ValueError, dandelion.protocol.parse_message_id_list, None)
        self.assertRaises(TypeError, dandelion.protocol.parse_message_id_list, 1337)
        self.assertRaises(TypeError, dandelion.protocol.parse_message_id_list, [])

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_id_list,
                          '')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_id_list,
                          '\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_id_list,
                          '???\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_id_list,
                          'FF FF\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_id_list,
                          '???;???\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_id_list,
                          'FF;;FF\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_id_list,
                          'FF;FF;\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_id_list,
                          ';FF;FF\n')
コード例 #7
0
    def test_parse_identity_list_request(self):
        """Test parsing the identity list request string"""

        identities = dandelion.protocol.parse_identity_list_request('GETIDENTITIES\n')
        self.assertEqual(identities, None)

        id1 = b'42'
        id2 = b'\x01\x23\x245'
        id3 = b'\x42\x42\x42'
        id1_str = encode_b64_bytes(id1).decode()
        id2_str = encode_b64_bytes(id2).decode()
        id3_str = encode_b64_bytes(id3).decode()

        identities_ret = dandelion.protocol.parse_identity_list_request('GETIDENTITIES {0}\n'.format(';'.join([id1_str, id2_str, id3_str])))
        self.assertEquals(len(identities_ret), 3)

        self.assertTrue(id1 in identities_ret)
        self.assertTrue(id2 in identities_ret)
        self.assertTrue(id3 in identities_ret)

        """Testing bad input"""
        self.assertRaises(ValueError, dandelion.protocol.parse_identity_list_request, None)
        self.assertRaises(TypeError, dandelion.protocol.parse_identity_list_request, 1337)
        self.assertRaises(TypeError, dandelion.protocol.parse_identity_list_request, [])

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_list_request,
                          '')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_list_request,
                          'XXX\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_list_request,
                          'FF\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_list_request,
                          'GETIDENTITIESXX\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_list_request,
                          'GETIDENTITIES ???;???\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_list_request,
                          'GETIDENTITIES FF;;FF\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_list_request,
                          'GETIDENTITIES FF;FF;\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_list_request,
                          'GETIDENTITIES ;FF;FF\n')
コード例 #8
0
    def test_parse_message_list_request(self):
        """Test parsing the message list request string"""

        msgs = dandelion.protocol.parse_message_list_request('GETMESSAGES\n')
        self.assertEqual(msgs, None)

        m1 = b'42'
        m2 = b'\x01\x23\x245'
        m3 = b'\x42\x42\x42'
        m3_str = encode_b64_bytes(m3).decode()
        m2_str = encode_b64_bytes(m2).decode()
        m1_str = encode_b64_bytes(m1).decode()

        msgs_ret = dandelion.protocol.parse_message_list_request('GETMESSAGES {0}\n'.format(';'.join([m1_str, m2_str, m3_str])))
        self.assertEquals(len(msgs_ret), 3)

        self.assertTrue(m1 in msgs_ret)
        self.assertTrue(m2 in msgs_ret)
        self.assertTrue(m3 in msgs_ret)

        """Testing bad input"""
        self.assertRaises(ValueError, dandelion.protocol.parse_message_list_request, None)
        self.assertRaises(TypeError, dandelion.protocol.parse_message_list_request, 1337)
        self.assertRaises(TypeError, dandelion.protocol.parse_message_list_request, [])

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_list_request,
                          '')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_list_request,
                          'XXX\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_list_request,
                          'FF\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_list_request,
                          'GETMESSAGESXX\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_list_request,
                          'GETMESSAGES ???;???\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_list_request,
                          'GETMESSAGES FF;;FF\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_list_request,
                          'GETMESSAGES FF;FF;\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_message_list_request,
                          'GETMESSAGES ;FF;FF\n')
コード例 #9
0
ファイル: ui.py プロジェクト: 4ZM/Dandelion-Message-Service
    def show_messages(self):
        _, msgs = self._db.get_messages()
        print(' --- MESSAGES BEGIN --- ')

        for m in msgs:
            print(' : '.join([encode_b64_bytes(m.id).decode(),
                              m.text if not m.has_receiver else encode_b64_bytes(m.text).decode(),
                              'N/A' if not m.has_receiver else encode_b64_bytes(m.receiver).decode(),
                              'N/A' if not m.has_sender else encode_b64_bytes(m.sender).decode()]))

        print(' --- MESSAGES END --- ')
コード例 #10
0
def _message2string(msg):
    """Serialize a message to a DMS string"""

    text = msg.text.encode() if isinstance(msg.text, str) else msg.text # Convert to bytes string
    timestamp = '' if msg.timestamp is None else encode_b64_int(msg.timestamp).decode()
    receiver = b'' if msg.receiver is None else msg.receiver
    sender, signature = (b'', b'') if msg.sender is None else (msg.sender, msg.signature)

    return _SUB_FIELD_SEPARATOR.join([
              encode_b64_bytes(text).decode(),
              timestamp,
              encode_b64_bytes(receiver).decode(),
              encode_b64_bytes(sender).decode(),
              encode_b64_bytes(signature).decode()])
コード例 #11
0
    def test_create_message_id_list(self):
        """Test message ID list request creation"""

        id1 = dandelion.identity.generate()
        id2 = dandelion.identity.generate()
        ts = 1337
        msg1 = dandelion.message.create('M1', ts, id1, id2)
        msg2 = Message('M2')
        msg3 = Message('M3')

        tc = b'\x01\x03\x03\x07'
        tc_str_ok = encode_b64_bytes(tc).decode()

        str_ = dandelion.protocol.create_message_id_list(tc, [msg1, msg2, msg3])[:-1]
        tc_str, m1_str, m2_str, m3_str = str_.split(';')
        self.assertEqual(tc_str, tc_str_ok)
        self.assertEqual(msg1.id, decode_b64_bytes(m1_str.encode()))
        self.assertEqual(msg2.id, decode_b64_bytes(m2_str.encode()))
        self.assertEqual(msg3.id, decode_b64_bytes(m3_str.encode()))

        str_ = dandelion.protocol.create_message_id_list(tc, None)[:-1]
        self.assertEqual(str_, tc_str)

        str_ = dandelion.protocol.create_message_id_list(tc, [])[:-1]
        self.assertEqual(str_, tc_str)

        """Testing bad input"""
        self.assertRaises(TypeError, dandelion.protocol.create_message_id_list, 1337, None)
        self.assertRaises(TypeError, dandelion.protocol.create_message_id_list, tc, msg1)
        self.assertRaises(TypeError, dandelion.protocol.create_message_id_list, [msg1], tc)
        self.assertRaises(AttributeError, dandelion.protocol.create_message_id_list, tc, tc)
        self.assertRaises(ValueError, dandelion.protocol.create_message_id_list, None, [])
        self.assertRaises(TypeError, dandelion.protocol.create_message_id_list, 0, None)
        self.assertRaises(AttributeError, dandelion.protocol.create_message_id_list, tc, ['fo'])
コード例 #12
0
    def test_create_identity_id_list(self):
        """Test identity ID list request creation"""

        id1 = dandelion.identity.generate()
        id2 = dandelion.identity.generate()
        id3 = dandelion.identity.generate()

        tc = b'\x01\x03\x03\x07'
        tc_str_ok = encode_b64_bytes(tc).decode()

        str_ = dandelion.protocol.create_identity_id_list(tc, [id1, id2, id3])[:-1]
        tc_str, id1_str, id2_str, id3_str = str_.split(';')
        self.assertEqual(tc_str, tc_str_ok)
        self.assertEqual(id1.fingerprint, decode_b64_bytes(id1_str.encode()))
        self.assertEqual(id2.fingerprint, decode_b64_bytes(id2_str.encode()))
        self.assertEqual(id3.fingerprint, decode_b64_bytes(id3_str.encode()))

        str_ = dandelion.protocol.create_identity_id_list(tc, None)[:-1]
        self.assertEqual(str_, tc_str)

        str_ = dandelion.protocol.create_identity_id_list(tc, [])[:-1]
        self.assertEqual(str_, tc_str)

        """Testing bad input"""
        self.assertRaises(TypeError, dandelion.protocol.create_identity_id_list, 1337, None)
        self.assertRaises(TypeError, dandelion.protocol.create_identity_id_list, tc, id1)
        self.assertRaises(TypeError, dandelion.protocol.create_identity_id_list, [id1], tc)
        self.assertRaises(AttributeError, dandelion.protocol.create_identity_id_list, tc, tc)
        self.assertRaises(ValueError, dandelion.protocol.create_identity_id_list, None, [])
        self.assertRaises(TypeError, dandelion.protocol.create_identity_id_list, 0, None)
        self.assertRaises(AttributeError, dandelion.protocol.create_identity_id_list, tc, ['fo'])
コード例 #13
0
    def __init__(self, config_file='dandelion.conf'):
        self._cfg_file_name = config_file
        self._server_config = ServerConfig()
        self._synchronizer_config = SynchronizerConfig()
        self._discoverer_config = DiscovererConfig()
        self._id_manager_config = IdentityConfig()
        self._ui_config = UiConfig()

        self.read_file()

        self._content_db = ContentDB(self._server_config.db_file)

        if self._id_manager_config.my_id is not None and not self._content_db.contains_identity(decode_b64_bytes(self._id_manager_config.my_id.encode())) :
            print("WARNING! Bad or non existing ID requested in config. Requested:", self._id_manager_config.my_id)
            self._id_manager_config.my_id = None

        if self._id_manager_config.my_id is not None:
            fp = decode_b64_bytes(self._id_manager_config.my_id.encode())
            try:
                self._identity = self._content_db.get_private_identity(fp)
                print("My claimed ID:", self._id_manager_config.my_id)
                return
            except ValueError:
                pass
            
        self._identity = dandelion.identity.generate()
        self._content_db.add_private_identity(self._identity)
        id_str = encode_b64_bytes(self._identity.fingerprint).decode()
        self._id_manager_config.my_id = id_str
コード例 #14
0
    def test_parse_greeting_message(self):
        """Test parsing greeting message"""

        ex_database_id_bin = b'\x01\x03\x03\x07'
        ex_database_id_str = encode_b64_bytes(ex_database_id_bin).decode()

        dbid = dandelion.protocol.parse_greeting_message('DMS;{0};{1}\n'.format(dandelion.protocol.PROTOCOL_VERSION, ex_database_id_str))
        self.assertEqual(dbid, ex_database_id_bin)

        self.assertRaises(ProtocolParseError, dandelion.protocol.parse_greeting_message, '')
        self.assertRaises(ValueError, dandelion.protocol.parse_greeting_message, None)
        self.assertRaises(TypeError, dandelion.protocol.parse_greeting_message, 1337)

        self.assertRaises(ProtocolParseError, dandelion.protocol.parse_greeting_message,
                          'XXX;{0};{1}\n'.format(dandelion.protocol.PROTOCOL_VERSION, ex_database_id_str))
        self.assertRaises(ProtocolParseError, dandelion.protocol.parse_greeting_message,
                          'XXX;XXX;{0};{1}\n'.format(dandelion.protocol.PROTOCOL_VERSION, ex_database_id_str))
        self.assertRaises(ProtocolParseError, dandelion.protocol.parse_greeting_message,
                          'DMS;10;{0}\n'.format(ex_database_id_str))
        self.assertRaises(ProtocolParseError, dandelion.protocol.parse_greeting_message,
                          'DMS;{0};???\n'.format(dandelion.protocol.PROTOCOL_VERSION))
        self.assertRaises(ProtocolParseError, dandelion.protocol.parse_greeting_message,
                          'DMS;{0};\n'.format(dandelion.protocol.PROTOCOL_VERSION))

        self.assertRaises(ProtocolVersionError, dandelion.protocol.parse_greeting_message,
                          'DMS;2.0;{0}\n'.format(ex_database_id_str))
コード例 #15
0
ファイル: ui.py プロジェクト: 4ZM/Dandelion-Message-Service
    def show_identities(self):
        _, identities = self._db.get_identities()
        print(' --- IDENTITIES BEGIN --- ')

        for id in identities:
            print(' : '.join([encode_b64_bytes(id.fingerprint).decode()]))

        print(' --- IDENTITIES END --- ')
コード例 #16
0
    def test_create_greeting_message(self):
        """Test construction of greeting message"""

        ex_database_id_bin = b'\x01\x03\x03\x07'
        ex_database_id_str = encode_b64_bytes(ex_database_id_bin).decode()

        greeting = dandelion.protocol.create_greeting_message(ex_database_id_bin)
        pc, pv, dbid = greeting[:-1].split(';')

        self.assertEqual(pc, "DMS")
        self.assertTrue(re.match('^[0-9]+\.[0-9]+$', pv))
        self.assertEqual(dbid, ex_database_id_str)

        self.assertRaises(ValueError, dandelion.protocol.create_greeting_message, None)
        self.assertRaises(TypeError, dandelion.protocol.create_greeting_message, 1337)
        self.assertRaises(ValueError, dandelion.protocol.create_greeting_message, b'')
        self.assertRaises(ValueError, dandelion.protocol.create_greeting_message, [])
コード例 #17
0
    def test_encode_bytes(self):

        self.assertEqual(encode_b64_bytes(b''), b'')
        self.assertEqual(encode_b64_bytes(b'\x00\x00\x00'), b'AAAA')
        self.assertEqual(encode_b64_bytes(b'\xFF\xFF\xFF'), b'////')
        self.assertEqual(encode_b64_bytes(b'123'), b'MTIz')
        self.assertEqual(encode_b64_bytes(bytearray(b'123')), b'MTIz')
        self.assertEqual(encode_b64_bytes(b'1337'), b'MTMzNw==')

        self.assertRaises(TypeError, encode_b64_bytes, None)
        self.assertRaises(TypeError, encode_b64_bytes, '1337')
        self.assertRaises(TypeError, encode_b64_bytes, 1337)
コード例 #18
0
    def test_create_message_id_list_request(self):
        """Test message ID list request creation"""

        s = dandelion.protocol.create_message_id_list_request()
        self.assertTrue('GETMESSAGELIST' in s)
        self.assertTrue(dandelion.protocol.is_message_id_list_request(s))

        tc = b'\x01\x03\x03\x07'
        s = dandelion.protocol.create_message_id_list_request(tc)
        self.assertTrue(' '.join(['GETMESSAGELIST', encode_b64_bytes(tc).decode()]) in s)
        self.assertTrue(dandelion.protocol.is_message_id_list_request(s))

        """Testing bad input"""
        self.assertRaises(TypeError, dandelion.protocol.create_message_id_list_request, 0)
        self.assertRaises(TypeError, dandelion.protocol.create_message_id_list_request, -1337)
        self.assertRaises(TypeError, dandelion.protocol.create_message_id_list_request, [])
        self.assertRaises(TypeError, dandelion.protocol.create_message_id_list_request, "1337")
        self.assertRaises(TypeError, dandelion.protocol.create_message_id_list_request, "XXX")
コード例 #19
0
    def test_parse_identity_id_list_request(self):
        """Test parsing the identity ID list request string"""

        tc_bin = b'\x01\x03\x03\x07'
        tc_str = encode_b64_bytes(tc_bin).decode()

        self.assertTrue(dandelion.protocol.is_identity_id_list_request('GETIDENTITYLIST {0}\n'.format(tc_str)))
        tc = dandelion.protocol.parse_identity_id_list_request('GETIDENTITYLIST {0}\n'.format(tc_str))
        self.assertEqual(tc, tc_bin)

        self.assertTrue(dandelion.protocol.is_identity_id_list_request('GETIDENTITYLIST\n'))
        tc = dandelion.protocol.parse_identity_id_list_request('GETIDENTITYLIST\n')
        self.assertEqual(tc, None)

        """Testing bad input"""
        self.assertRaises(ValueError, dandelion.protocol.parse_identity_id_list_request, None)
        self.assertRaises(TypeError, dandelion.protocol.parse_identity_id_list_request, 1337)
        self.assertRaises(TypeError, dandelion.protocol.parse_identity_id_list_request, [])

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list_request,
                          '')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list_request,
                          'BAD\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list_request,
                          'BAD BAD\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list_request,
                          'GETIDENTITYLIST ???\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list_request,
                          'GETIDENTITYLIST 01030307 01030307\n')

        self.assertRaises(ProtocolParseError,
                          dandelion.protocol.parse_identity_id_list_request,
                          'GETIDENTITYLISTXXX 01030307\n')
コード例 #20
0
def create_identity_list_request(identity_ids=None):
    """Create the request string used by the client to request a list of identities.
    
    [C]                                                    [S]
     |                                                      | 
     |    GETIDENTITIES [[[<identityid>];<identityid>];...;<identityid>]     | 
     |----------------------------------------------------->| 
     |                                                      |  
    """

    if identity_ids is None: # Don't use mutable default (e.g. [])
        identity_ids = []

    if not hasattr(identity_ids, '__iter__'):
        raise TypeError

    if len(identity_ids) == 0:
        return ''.join([_GETIDENTITIES, TERMINATOR])
    identityid_str = _FIELD_SEPARATOR.join([encode_b64_bytes(uid).decode() for uid in identity_ids])

    return '{0} {1}{2}'.format(_GETIDENTITIES, identityid_str, TERMINATOR)
コード例 #21
0
def create_identity_id_list_request(time_cookie=None):
    """Create the identity id request string.
    
    The time cookie (bytes) is optional and will be serialized to some 
    readable format.
    
    [C]                                                    [S]
     |                                                      | 
     |              GETUSERLIST [<time cookie> ]            | 
     |----------------------------------------------------->| 
     |                                                      | 
    """

    if time_cookie is None:
        return ''.join([_GETIDENTITYLIST, TERMINATOR])

    if not isinstance(time_cookie, bytes):
        raise TypeError

    return '{0} {1}{2}'.format(_GETIDENTITYLIST,
                               encode_b64_bytes(time_cookie).decode(),
                               TERMINATOR)
コード例 #22
0
def create_message_list_request(msg_ids=None):
    """Create the request string used by the client to request a list of messages.
    
    [C]                                                    [S]
     |                                                      | 
     |    GETMESSAGES [[[<msgid>];<msgid>];...;<msgid>]     | 
     |----------------------------------------------------->| 
     |                                                      |  
    """

    if msg_ids is None: # Don't use mutable default (e.g. [])
        msg_ids = []

    if not hasattr(msg_ids, '__iter__'):
        raise TypeError

    if len(msg_ids) == 0:
        return ''.join([_GETMESSAGES, TERMINATOR])

    msgid_str = _FIELD_SEPARATOR.join([encode_b64_bytes(mid).decode() for mid in msg_ids])

    return '{0} {1}{2}'.format(_GETMESSAGES, msgid_str, TERMINATOR)
コード例 #23
0
def create_greeting_message(dbid):
    """Create the server greeting message string.
    
    This message is sent from the server to the client upon connection. 
    The dbid (bytes) represents a data base id. 
    
    [C]                                                    [S]
     |                                                      | 
     |     <protocol cookie>;<protocol version>;<db id>     | 
     |<-----------------------------------------------------| 
     |                                                      | 
    """

    if not dbid:
        raise ValueError

    if not isinstance(dbid, bytes):
        raise TypeError

    return '{0}{3}{1}{3}{2}{4}'.format(_PROTOCOL_COOKIE,
                                       PROTOCOL_VERSION,
                                       encode_b64_bytes(dbid).decode(),
                                       _FIELD_SEPARATOR,
                                       TERMINATOR)
コード例 #24
0
 def __str__(self):
     """String conversion is user Base64 encoded fingerprint"""
     return encode_b64_bytes(self.fingerprint).decode()
コード例 #25
0
 def _encode_id(self, id):
     """Binary to text encoding of id's"""
     return encode_b64_bytes(id).decode()