def test_remote_cookies(self):
        """Test the remote time cookie interface"""
        db = ContentDB(tempfile.NamedTemporaryFile().name)

        remotefp_1 = b"1337"
        remotefp_2 = b"2342"
        remotetc_1 = b"1"
        remotetc_2 = b"2"

        # No time cookie for the db yet       
        self.assertIsNone(db.get_last_time_cookie(remotefp_1))

        # Initial tc
        db.update_last_time_cookie(remotefp_1, remotetc_1)
        self.assertEqual(db.get_last_time_cookie(remotefp_1), remotetc_1)

        # Same tc again
        db.update_last_time_cookie(remotefp_1, remotetc_1)
        self.assertEqual(db.get_last_time_cookie(remotefp_1), remotetc_1)

        # Second tc
        db.update_last_time_cookie(remotefp_1, remotetc_2)
        self.assertEqual(db.get_last_time_cookie(remotefp_1), remotetc_2)

        # Second db
        db.update_last_time_cookie(remotefp_2, remotetc_1)
        self.assertEqual(db.get_last_time_cookie(remotefp_1), remotetc_2)
        self.assertEqual(db.get_last_time_cookie(remotefp_2), remotetc_1)
    def test_time_cookies(self):
        """Test the data base time cookies (revision) functionality."""

        db = ContentDB(tempfile.NamedTemporaryFile().name)

        # Adding a message        
        first_msg = Message('A Single Message')
        first_cookie = db.add_messages([first_msg])
        self.assertNotEqual(first_cookie, None)
        self.assertTrue(isinstance(first_cookie, bytes))
        self.assertTrue((db.get_last_time_cookie(None) is None) or (db.get_last_time_cookie(None) == first_cookie))

        # Same message again
        self.assertEqual(first_cookie, db.add_messages([first_msg]))

        # New message, new cookie
        id1 = dandelion.identity.generate()
        id2 = dandelion.identity.generate()
        second_msg = dandelion.message.create('Another Single Message', sender=id1, receiver=id2)
        second_cookie = db.add_messages([second_msg])
        self.assertNotEqual(second_cookie, None)
        self.assertNotEqual(second_cookie, first_cookie)
        self.assertTrue((db.get_last_time_cookie(None) is None) or (db.get_last_time_cookie(None) == second_cookie))

        # Since first should only be second
        tc, some_messages = db.get_messages(time_cookie=first_cookie)
        self.assertNotEqual(some_messages, None)
        self.assertEqual(tc, second_cookie)

        self.assertEqual(len(some_messages), 1)
        self.assertEqual(some_messages[0], second_msg)

        # Nothing new since last message was added
        tc, last_messages = db.get_messages(time_cookie=second_cookie)
        self.assertNotEqual(last_messages, None)
        self.assertEqual(len(last_messages), 0)
        self.assertEqual(tc, second_cookie)

        # Same id gives same tc
        self.assertEqual(second_cookie, db.add_messages([first_msg]))

        # New identity, new cookie
        identity = dandelion.identity.generate()
        third_cookie = db.add_identities([identity])
        self.assertNotEqual(third_cookie, None)
        self.assertNotEqual(third_cookie, second_cookie)
        self.assertTrue(db.get_last_time_cookie() == third_cookie)

        # Trying some bad input
        self.assertRaises(TypeError, db.get_messages, [], 0)
        self.assertRaises(TypeError, db.get_messages, [], '')
        self.assertRaises(TypeError, db.get_messages, [], 'fubar')
        self.assertRaises(ValueError, db.get_messages, [], b'')
        self.assertRaises(ValueError, db.get_messages, [], b'1337')