def test_recvCell_relay_cell_decrypt_fail(self, mock_logging, mock_decrypt): mock_decrypt.side_effect = Exception self.circuit._sendDestroyCell = mock.Mock() self.circuit._closeCircuit = mock.Mock() self.circuit._processRelayDataCell = mock.Mock() self.circuit._processRelayEndCell = mock.Mock() self.circuit._processRelayResolvedCell = mock.Mock() self.circuit._processRelayTruncatedCell = mock.Mock() self.circuit._processRelayConnectedCell = mock.Mock() self.circuit._processRelaySendMeCell = mock.Mock() self.circuit._processRelayDropCell = mock.Mock() cell = EncryptedCell.make(ID, 'a' * 509) self.circuit._recvRelayCell(cell) self.assertTrue(mock_logging.debug.call_count, 1) self.assertEqual(self.circuit._sendDestroyCell.call_count, 0) self.assertEqual(self.circuit._closeCircuit.call_count, 0) self.assertEqual(self.circuit._processRelayDataCell.call_count, 0) self.assertEqual(self.circuit._processRelayEndCell.call_count, 0) self.assertEqual(self.circuit._processRelayConnectedCell.call_count, 0) self.assertEqual(self.circuit._processRelayResolvedCell.call_count, 0) self.assertEqual(self.circuit._processRelayTruncatedCell.call_count, 0) self.assertEqual(self.circuit._processRelaySendMeCell.call_count, 0) self.assertEqual(self.circuit._processRelayDropCell.call_count, 0)
def test_recvCell_relay_unexpected_cell_fail(self, mock_logging, mock_decrypt): mock_decrypt.return_value = (RelayExtended2Cell('test'), 2) self.circuit._sendDestroyCell = mock.Mock() self.circuit._closeCircuit = mock.Mock() self.circuit._processRelayDataCell = mock.Mock() self.circuit._processRelayEndCell = mock.Mock() self.circuit._processRelayResolvedCell = mock.Mock() self.circuit._processRelayTruncatedCell = mock.Mock() self.circuit._processRelayConnectedCell = mock.Mock() self.circuit._processRelaySendMeCell = mock.Mock() self.circuit._processRelayDropCell = mock.Mock() cell = EncryptedCell.make(ID, 'a' * 509) self.circuit._recvRelayCell(cell) self.assertTrue(mock_logging.debug.call_count, 1) self.assertEqual(self.circuit._sendDestroyCell.call_count, 0) self.assertEqual(self.circuit._closeCircuit.call_count, 0) self.assertEqual(self.circuit._processRelayDataCell.call_count, 0) self.assertEqual(self.circuit._processRelayEndCell.call_count, 0) self.assertEqual(self.circuit._processRelayConnectedCell.call_count, 0) self.assertEqual(self.circuit._processRelayResolvedCell.call_count, 0) self.assertEqual(self.circuit._processRelayTruncatedCell.call_count, 0) self.assertEqual(self.circuit._processRelaySendMeCell.call_count, 0) self.assertEqual(self.circuit._processRelayDropCell.call_count, 0)
def encryptCellToTarget(cell, crypt_path, target=2, early=False): '''Encrypt *cell* to the *target* relay in *crypt_path* and update the appropriate forward digest. :param cell cell: cell to encrypt :param list crypt_path: list of RelayCrypto instances available for encryption :param int target: target node to encrypt to :param bool early: if **True**, use a RELAY_EARLY cmd instead of a RELAY cmd :returns: **oppy.cell.fixedlen.EncryptedCell** ''' assert target >= 0 and target < len(crypt_path) assert cell.rheader.digest == EMPTY_DIGEST # 1) update f_digest with cell payload bytes crypt_path[target].forward_digest.update(cell.getPayload()) # 2) insert first four bytes into new digest position cell.rheader.digest = crypt_path[target].forward_digest.digest()[:4] # 3) encrypt payload payload = cell.getPayload() for node in xrange(target + 1): payload = crypt_path[node].forward_cipher.encrypt(payload) # 4) return encrypted relay cell with new payload return EncryptedCell.make(cell.header.circ_id, payload, early=early)
def test_recvCell_relay_unexpected_cell_fail(self, mock_logging, mock_decrypt): mock_decrypt.return_value = (RelayExtended2Cell('test'), 2) self.circuit._sendDestroyCell = mock.Mock() self.circuit._closeCircuit = mock.Mock() self.circuit._processRelayDataCell = mock.Mock() self.circuit._processRelayEndCell = mock.Mock() self.circuit._processRelayResolvedCell = mock.Mock() self.circuit._processRelayTruncatedCell = mock.Mock() self.circuit._processRelayConnectedCell = mock.Mock() self.circuit._processRelaySendMeCell = mock.Mock() self.circuit._processRelayDropCell = mock.Mock() cell = EncryptedCell.make(ID, 'a'*509) self.circuit._recvRelayCell(cell) self.assertTrue(mock_logging.debug.call_count, 1) self.assertEqual(self.circuit._sendDestroyCell.call_count, 0) self.assertEqual(self.circuit._closeCircuit.call_count, 0) self.assertEqual(self.circuit._processRelayDataCell.call_count, 0) self.assertEqual(self.circuit._processRelayEndCell.call_count, 0) self.assertEqual(self.circuit._processRelayConnectedCell.call_count, 0) self.assertEqual(self.circuit._processRelayResolvedCell.call_count, 0) self.assertEqual(self.circuit._processRelayTruncatedCell.call_count, 0) self.assertEqual(self.circuit._processRelaySendMeCell.call_count, 0) self.assertEqual(self.circuit._processRelayDropCell.call_count, 0)
def test_recvCell_relay_cell_decrypt_fail(self, mock_logging, mock_decrypt): mock_decrypt.side_effect = Exception self.circuit._sendDestroyCell = mock.Mock() self.circuit._closeCircuit = mock.Mock() self.circuit._processRelayDataCell = mock.Mock() self.circuit._processRelayEndCell = mock.Mock() self.circuit._processRelayResolvedCell = mock.Mock() self.circuit._processRelayTruncatedCell = mock.Mock() self.circuit._processRelayConnectedCell = mock.Mock() self.circuit._processRelaySendMeCell = mock.Mock() self.circuit._processRelayDropCell = mock.Mock() cell = EncryptedCell.make(ID, 'a'*509) self.circuit._recvRelayCell(cell) self.assertTrue(mock_logging.debug.call_count, 1) self.assertEqual(self.circuit._sendDestroyCell.call_count, 0) self.assertEqual(self.circuit._closeCircuit.call_count, 0) self.assertEqual(self.circuit._processRelayDataCell.call_count, 0) self.assertEqual(self.circuit._processRelayEndCell.call_count, 0) self.assertEqual(self.circuit._processRelayConnectedCell.call_count, 0) self.assertEqual(self.circuit._processRelayResolvedCell.call_count, 0) self.assertEqual(self.circuit._processRelayTruncatedCell.call_count, 0) self.assertEqual(self.circuit._processRelaySendMeCell.call_count, 0) self.assertEqual(self.circuit._processRelayDropCell.call_count, 0)
def test_recvCell_relay_cell(self): self.circuit._recvRelayCell = mock.Mock() self.circuit._pollReadQueue = mock.Mock() cell = EncryptedCell.make(ID, 'a' * 509) self.circuit._recvCell(cell) self.circuit._recvRelayCell.assert_called_once_with(cell) self.assertEqual(self.circuit._pollReadQueue.call_count, 1)
def test_recvCell_relay_cell(self): self.circuit._recvRelayCell = mock.Mock() self.circuit._pollReadQueue = mock.Mock() cell = EncryptedCell.make(ID, 'a'*509) self.circuit._recvCell(cell) self.circuit._recvRelayCell.assert_called_once_with(cell) self.assertEqual(self.circuit._pollReadQueue.call_count, 1)