Exemplo n.º 1
0
class TestEncryption(DCSATestCase):
    def test_group_creator_should_be_able_to_enable_encryption_on_group_creation(self):
        self.c = Chat((Chat.DEFAULT_LISTEN_IP, getUnusedListenPort()))
        self.c.start()
        self._registerStartedChatSession(self.c)
        self.c.createGroup()
        self.c.turnOnEncryption()
       
        
    def test_should_create_cert_and_key_files(self):
        self.test_group_creator_should_be_able_to_enable_encryption_on_group_creation()
               
       
    def test_should_be_able_to_send_commands_to_other_peers_by_encryption_if_encryption_flag_enabled(self):
        self.test_group_creator_should_be_able_to_enable_encryption_on_group_creation()
        
        self.c2 = Chat((Chat.DEFAULT_LISTEN_IP, getUnusedListenPort()))
        self.c2.start()
        self.c2.turnOnEncryption()
        self._registerStartedChatSession(self.c2)
        self.c.addUser(self.c2.getListenAddress())
        time.sleep(0.2)
        self.c2_total_msgs = len(self.c2.getChannel().getMessages())
        self.c.sendMessage('hi')
       
        
    def test_should_be_able_to_receive_and_decode_commands_from_other_peers_by_encryption_if_encryption_flag_enabled(self):
        self.test_should_be_able_to_send_commands_to_other_peers_by_encryption_if_encryption_flag_enabled()
        time.sleep(0.1)
        self.assertEqual(self.c2_total_msgs + 1, len(self.c2.getChannel().getMessages()))
Exemplo n.º 2
0
class DCSATestCase(unittest.TestCase):
    def setUp(self):
        self._clearLogDir()
        self.c = Chat((Chat.DEFAULT_LISTEN_IP, getUnusedListenPort())) # default chat session
        self._chat_sessions = []
        
        
    def tearDown(self):
        # Make sure all initialized chat sessions are closed
        for c in self._chat_sessions:
            try:
                timeout(c.stop, timeout_duration=1)
                # slnc: Without this tests hang, I still don't know the reason. 
            except:
                pass
        
        #self._clearLogDir()        
        
        
    def _clearLogDir(self):
        for infile in glob.glob(os.path.join('log', 'log*')):
            os.unlink(infile)


    def assertLogEntryContains(self, message, filename):
        try:
            logging.shutdown()
        except IOError:
            pass
        
        #print file(filename).readlines()
        time.sleep(0.2)
        self.assertNotEqual(-1, file(filename).read().find(message))

    def assertLogEntryDoesNotContain(self, message, filename):
        try:
            logging.shutdown()
        except IOError:
            pass
        
        #print file(filename).readlines()
        time.sleep(0.2)
        #print file(filename).read()
        self.assertEqual(-1, file(filename).read().find(message))
        
    def assertLastLogEntryContains(self, message, filename):
        try:
            logging.shutdown()
        except IOError:
            pass
        #print "Searching for\n%s\n" % message
        #print file(filename).readlines()
        time.sleep(0.2)
        self.assertNotEqual(-1, file(filename).readlines()[-1].find(message), )


    def _startTwoAgents(self):
        self.c = Chat((Chat.DEFAULT_LISTEN_IP, getUnusedListenPort()))
        self.c.start()
        self.c.createGroup()
        self._registerStartedChatSession(self.c)
        
        self.c2 = Chat((Chat.DEFAULT_LISTEN_IP, getUnusedListenPort()))
        self.c2.start()
        self._registerStartedChatSession(self.c2)
        self.c.addUser(self.c2.getListenAddress())
        
        time.sleep(0.2)
        
        self.assertEqual(self.c.groupView.getGroup(), self.c2.groupView.getGroup())
        self.assertEqual(Chat.ONLINE, self.c.userStatus(self.c2.getListenAddress()))
        self.assertTrue(self.c2.groupView.isInGroup())


    def _startThreeAgents(self):
        self.c = Chat((Chat.DEFAULT_LISTEN_IP, getUnusedListenPort()))
        self.c.start()
        self.c.createGroup()
        self._registerStartedChatSession(self.c)
        
        self.c2 = Chat((Chat.DEFAULT_LISTEN_IP, getUnusedListenPort()))
        self.c2.start()
        self._registerStartedChatSession(self.c2)
        self.c.addUser(self.c2.getListenAddress())
        
        time.sleep(0.1)
        
        self.assertTrue(self.c2.groupView.isInGroup())
        self.c3 = Chat((Chat.DEFAULT_LISTEN_IP, getUnusedListenPort()))
        self.c3.start()
        self._registerStartedChatSession(self.c3)
        self.c.addUser(self.c3.getListenAddress())
        
        time.sleep(0.3)

        assert(self.c.groupView.isInGroup())
        assert(self.c2.groupView.isInGroup())
        assert(self.c3.groupView.isInGroup())
        self.assertEqual(self.c.groupView.getGroup(), self.c2.groupView.getGroup())
        self.assertEqual(self.c.groupView.getGroup(), self.c3.groupView.getGroup())
        self.assertEqual(Chat.ONLINE, self.c2.userStatus(self.c3.getListenAddress()))
        self.assertEqual(Chat.ONLINE, self.c2.userStatus(self.c.getListenAddress()))
        self.assertEqual(Chat.ONLINE, self.c.userStatus(self.c2.getListenAddress()))
        self.assertEqual(Chat.ONLINE, self.c.userStatus(self.c3.getListenAddress()))
        self.assertEqual(Chat.ONLINE, self.c3.userStatus(self.c.getListenAddress()))
        self.assertEqual(Chat.ONLINE, self.c3.userStatus(self.c2.getListenAddress()))
        
        

    def _try_to_connect(self, address):
        '''Tries to connect to tcp socket identified by address, a tuple of the form: ('0.0.0.0', 9090)'''
        sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
        sock.connect(address)
        sock.close()
        
    def _registerStartedChatSession(self, c):
        self._chat_sessions.append(c)