def _handle_initial_write_packet(self, packet): """ Check if there is an application action to receive this message. If so, then send an acknowledgement and move the state to WRITING. Otherwise, send back an error packet and move the state to COMPLETED. Args: packet: An unpacked WriteRequestPacket. Returns: An Acknowledgement packet if the request's filename matches any possible write rule. Otherwise, an ErrorPacket with an access violation code and message. """ assert isinstance(packet, packets.WriteRequestPacket) self.filename = packet.filename self.mode = packet.mode self.current_block_num = 0 self.write_action = self.response_router.initialize_write( self.filename, self.client_host, self.client_port) if self.write_action: self.state = WRITING self.write_buffer = WriteBuffer() return packets.AcknowledgementPacket(0) else: self.state = COMPLETED self.log("WRITEREQUEST", "Access Violation") return packets.ErrorPacket( 2, "Access Violation. Host: %s, Port: %s" % (self.client_host, self.client_port))
def test_finish_writing(self): packet = packets.DataPacket(3, "O" * 511) conversation = TFTPConversation(self.client_host, self.client_port, StubResponseRouterTwo()) conversation.state = tftp_conversation.WRITING conversation.write_buffer = WriteBuffer() conversation.write_buffer.data = "X" * 512 conversation.filename = "stub_filename" conversation.current_block_num = 2 write_action_wrapper = StubWriteActionWrapper() conversation.write_action = write_action_wrapper.stub_action response_packet = conversation.handle_packet(packet) self.assertEqual(conversation.state, tftp_conversation.COMPLETED) self.assertEqual(conversation.current_block_num, 3) self.assertEqual(conversation.cached_packet, response_packet) self.assertEqual(response_packet.__class__, packets.AcknowledgementPacket) self.assertEqual(response_packet.block_num, 3) # action should get invoked, saving this state in the wrapper class self.assertEqual(write_action_wrapper.received_state, ("10.26.0.3", 12345, "stub_filename", "X" * 512 + "O" * 511))
def initialize_write(self, urn, client_host, client_port): return WriteBuffer()