def test_offset(self):
        """
        Test that, if the server's BEGIN_CONTENT message specifies an offset,
        the file to be uploaded is seek'ed to the right position.
        """
        offset = 23
        mocker = Mocker()
        transport = mocker.mock()
        transport.registerProducer(ANY, streaming=True)
        transport.write(ANY)
        mocker.count(1, None)  # I don't really care how many times
        fd = mocker.mock()
        fd.seek(offset)  # this is really all I care about
        fd.read(ANY)
        mocker.result('')
        mocker.replay()

        protocol = StorageClient()
        protocol.transport = transport
        pc = PutContent(protocol, 'share', 'node', '', '', 0, 0, 0, fd)
        message = protocol_pb2.Message()
        message.type = protocol_pb2.Message.BEGIN_CONTENT
        message.begin_content.offset = offset
        pc.start()
        pc.processMessage(message)
 def test_magic_hash_none(self):
     """Don't send magic hash in the PutContent."""
     pc = PutContent(self.protocol, 'share', 'node', '', '', 0, 0, 0,
                     StringIO(''))
     pc.start()
     pc_msg = protocol_pb2.Message()
     data = self.protocol.transport.value()
     pc_msg.ParseFromString(data[request.SIZE_FMT_SIZE:])
     self.assertEqual(pc_msg.put_content.magic_hash, '')
 def test_client_upload_id(self):
     """Test that, we send the upload id to the server."""
     pc = PutContent(self.protocol, 'share', 'node', '', '',
                     0, 0, 0, StringIO(''), upload_id='foo')
     pc.start()
     pc_msg = protocol_pb2.Message()
     data = self.protocol.transport.value()
     pc_msg.ParseFromString(data[request.SIZE_FMT_SIZE:])
     self.assertEqual(pc_msg.put_content.upload_id, 'foo')
 def test_server_upload_id_none(self):
     """Test that if there is no upload_id we ignore it."""
     called = []
     pc = PutContent(self.protocol, 'share', 'node', '', '',
                     0, 0, 0, StringIO(''), upload_id_cb=called.append)
     message = protocol_pb2.Message()
     message.type = protocol_pb2.Message.BEGIN_CONTENT
     pc.start()
     pc.processMessage(message)
     self.assertEqual(len(called), 0)
 def test_server_upload_id(self):
     """Test that, if the server specify an upload_id, we save it."""
     upload_id = "foo"
     called = []
     pc = PutContent(self.protocol, 'share', 'node', '', '', 0, 0, 0,
                     StringIO(''), upload_id_cb=lambda *a: called.append(a))
     message = protocol_pb2.Message()
     message.type = protocol_pb2.Message.BEGIN_CONTENT
     message.begin_content.upload_id = upload_id
     message.begin_content.offset = 0
     pc.start()
     pc.processMessage(message)
     self.assertEqual(len(called), 1)
     self.assertEqual(called[0], ('foo', 0))
 def test_callback(self):
     """Test that, if the server specify an offset, we call back with it."""
     upload_id = 'foo'
     offset = 123456
     called = []
     pc = PutContent(self.protocol, 'share', 'node', '', '', 0, 0, 0,
                     StringIO(''), upload_id_cb=lambda *a: called.append(a))
     message = protocol_pb2.Message()
     message.type = protocol_pb2.Message.BEGIN_CONTENT
     message.begin_content.upload_id = upload_id
     message.begin_content.offset = offset
     pc.start()
     pc.processMessage(message)
     self.assertEqual(len(called), 1)
     self.assertEqual(called[0], (upload_id, offset))
    def test_server_upload_id_no_cb(self):
        """Test that, if the server specify an upload_id, we save it.

        Only if we have the upload_id_cb defined.
        """
        offset = 23
        # just to check something
        mocker = Mocker()
        fd = mocker.mock()
        fd.seek(offset)  # this is really all I care about
        fd.read(ANY)
        mocker.result('')
        mocker.replay()

        upload_id = "foo"
        pc = PutContent(self.protocol, 'share', 'node', '', '', 0, 0, 0, fd)
        message = protocol_pb2.Message()
        message.type = protocol_pb2.Message.BEGIN_CONTENT
        message.begin_content.upload_id = upload_id
        message.begin_content.offset = offset
        pc.start()
        pc.processMessage(message)