def test_bit_read_message_execute_success(self):
     """ Test bit read request encoding """
     context = MockContext()
     context.validate = lambda a, b, c: True
     requests = [
         ReadCoilsRequest(1, 5),
         ReadDiscreteInputsRequest(1, 5),
     ]
     for request in requests:
         result = request.execute(context)
         self.assertEqual(result.bits, [True] * 5)
 def testBitReadMessageExecuteSuccess(self):
     ''' Test bit read request encoding '''
     context = MockContext()
     context.validate = lambda a,b,c: True
     requests = [
         ReadCoilsRequest(1,5),
         ReadDiscreteInputsRequest(1,5),
     ]
     for request in requests:
         result = request.execute(context)
         self.assertEqual(result.bits, [True] * 5)
    def testWriteSingleRegisterRequest(self):
        context = MockContext()
        request = WriteSingleRegisterRequest(0x00, 0xf0000)
        result = request.execute(context)
        self.assertEqual(result.exception_code, ModbusExceptions.IllegalValue)

        request.value = 0x00ff
        result = request.execute(context)
        self.assertEqual(result.exception_code, ModbusExceptions.IllegalAddress)

        context.valid = True
        result = request.execute(context)
        self.assertEqual(result.function_code, request.function_code)
    def testWriteSingleCoilExecute(self):
        context = MockContext(False, default=True)
        request = WriteSingleCoilRequest(2, True)
        result = request.execute(context)
        self.assertEqual(result.exception_code, ModbusExceptions.IllegalAddress)

        context.valid = True
        result = request.execute(context)
        self.assertEqual(result.encode(), "\x00\x02\xff\x00")

        context = MockContext(True, default=False)
        request = WriteSingleCoilRequest(2, False)
        result = request.execute(context)
        self.assertEqual(result.encode(), "\x00\x02\x00\x00")
    def testReadWriteMultipleRegistersValidate(self):
        context = MockContext()
        context.validate = lambda f,a,c: a == 1
        request = ReadWriteMultipleRegistersRequest(read_address=1,
            read_count=10, write_address=2, write_registers=[0x00])
        response = request.execute(context)
        self.assertEqual(response.exception_code, ModbusExceptions.IllegalAddress)

        context.validate = lambda f,a,c: a == 2
        response = request.execute(context)
        self.assertEqual(response.exception_code, ModbusExceptions.IllegalAddress)

        request.write_byte_count = 0x100
        response = request.execute(context)
        self.assertEqual(response.exception_code, ModbusExceptions.IllegalValue)
    def testWriteMultipleRegisterRequest(self):
        context = MockContext()
        request = WriteMultipleRegistersRequest(0x00, [0x00]*10)
        result = request.execute(context)
        self.assertEqual(result.exception_code, ModbusExceptions.IllegalAddress)

        request.count = 0x05 # bytecode != code * 2
        result = request.execute(context)
        self.assertEqual(result.exception_code, ModbusExceptions.IllegalValue)

        request.count = 0x800 # outside of range
        result = request.execute(context)
        self.assertEqual(result.exception_code, ModbusExceptions.IllegalValue)

        context.valid = True
        request = WriteMultipleRegistersRequest(0x00, [0x00]*10)
        result = request.execute(context)
        self.assertEqual(result.function_code, request.function_code)
    def testWriteMultipleCoilsExecute(self):
        context = MockContext(False)
        # too many values
        request = WriteMultipleCoilsRequest(2, FakeList(0x123456))
        result = request.execute(context)
        self.assertEqual(result.exception_code, ModbusExceptions.IllegalValue)

        # bad byte count
        request = WriteMultipleCoilsRequest(2, [0x00] * 4)
        request.byte_count = 0x00
        result = request.execute(context)
        self.assertEqual(result.exception_code, ModbusExceptions.IllegalValue)

        # does not validate
        context.valid = False
        request = WriteMultipleCoilsRequest(2, [0x00] * 4)
        result = request.execute(context)
        self.assertEqual(result.exception_code, ModbusExceptions.IllegalAddress)

        # validated request
        context.valid = True
        result = request.execute(context)
        self.assertEqual(result.encode(), "\x00\x02\x00\x04")