'Simulate corruption of the http payload.'

def testCorruption(self):

    self.corruptor_field = None
    def Corruptor(url='', data=None, **kwargs):
        'Futz    with    some    of    the    fields.'
        comm_cls = rdf_flows.ClientCommunication
        if (data is not None):
            self.client_communication = comm_cls.FromSerializedString(data)
        else:
            self.client_communication = comm_cls(None)
        if (self.corruptor_field and ('server.pem' not in url)):
            orig_str_repr = self.client_communication.SerializeToString()
            field_data = getattr(self.client_communication, self.corruptor_field)
            if hasattr(field_data, 'SerializeToString'):
                field_data = field_data.SerializeToString()
            modified_data = array.array('c', field_data)
            offset = (len(field_data) / 2)
            modified_data[offset] = chr(((ord(field_data[offset]) % 250) + 1))
            setattr(self.client_communication, self.corruptor_field, modified_data.tostring())
            self.assertNotEqual(field_data, modified_data)
            mod_str_repr = self.client_communication.SerializeToString()
            self.assertEqual(len(orig_str_repr), len(mod_str_repr))
            differences = [True for (x, y) in zip(orig_str_repr, mod_str_repr) if (x != y)]
            self.assertEqual(len(differences), 1)
        data = self.client_communication.SerializeToString()
        return self.UrlMock(url=url, data=data, **kwargs)
    with utils.Stubber(requests, 'request', Corruptor):
        self.SendToServer()
        status = self.client_communicator.RunOnce()
        self.assertEqual(status.code, 200)
        for field in ['packet_iv', 'encrypted']:
            self.corruptor_field = field
            self.SendToServer()
            status = self.client_communicator.RunOnce()
            self.assertEqual(status.code, 500)
            self.assertTrue(('HMAC    verification    failed' in str(self.last_urlmock_error)))
        for field in ['encrypted_cipher', 'encrypted_cipher_metadata']:
            self.corruptor_field = field
            self.SendToServer()
            status = self.client_communicator.RunOnce()
            self.assertEqual(status.code, 500)
