Exemple #1
0
static void test_cmd53_read_write_multiple_bytes(sdmmc_card_t* card, size_t n_bytes)
{
    printf("Write multiple bytes using CMD53\n");
    const size_t scratch_area_reg = SLCHOST_CONF_W0 - DR_REG_SLCHOST_BASE;

    uint8_t* src = heap_caps_malloc(512, MALLOC_CAP_DMA);
    uint32_t* src_32 = (uint32_t*) src;

    for (size_t i = 0; i < (n_bytes + 3) / 4; ++i) {
        src_32[i] = rand();
    }

    TEST_ESP_OK(sdmmc_io_write_bytes(card, 1, scratch_area_reg, src, n_bytes));
    ESP_LOG_BUFFER_HEX(TAG, src, n_bytes);

    printf("Read back using CMD52\n");
    uint8_t* dst = heap_caps_malloc(512, MALLOC_CAP_DMA);
    for (size_t i = 0; i < n_bytes; ++i) {
        TEST_ESP_OK(sdmmc_io_read_byte(card, 1, scratch_area_reg + i, &dst[i]));
    }
    ESP_LOG_BUFFER_HEX(TAG, dst, n_bytes);
    TEST_ASSERT_EQUAL_UINT8_ARRAY(src, dst, n_bytes);

    printf("Read back using CMD53\n");
    TEST_ESP_OK(sdmmc_io_read_bytes(card, 1, scratch_area_reg, dst, n_bytes));
    ESP_LOG_BUFFER_HEX(TAG, dst, n_bytes);
    TEST_ASSERT_EQUAL_UINT8_ARRAY(src, dst, n_bytes);

    free(src);
    free(dst);
}
/** \brief test ciaaLibs_circBufGet and ciaaLibs_circBufPut
 **/
void test_ciaaLibs_circBufPutGet(void) {
   ciaaLibs_CircBufType * cbuf;
   size_t ret;
   char * from = "0hallo123-10HALLO12-20hallo12-30HALLO12-40HALLO12-50hallo12-60-4";
                /*0123456789012345678901234567890123456789012345678901234567890123*/
   char to[100];

   /* use linux malloc, free and memcpy */
   ciaaPOSIX_malloc_StubWithCallback(malloc);
   ciaaPOSIX_memcpy_StubWithCallback(memcpy);
   ciaaPOSIX_free_StubWithCallback(free);

   /* creates a cicular buffer with 64 bytes */
   cbuf = ciaaLibs_circBufNew(64);
   TEST_ASSERT_TRUE(NULL != cbuf);

   /* write 25 */
   ret = ciaaLibs_circBufPut(cbuf, from, 25);
   TEST_ASSERT_TRUE(25 == ret);
   memset((void*)to, 0, 100);

   /* get 20 */
   ret = ciaaLibs_circBufGet(cbuf, to, 20);
   TEST_ASSERT_EQUAL_INT(20, ret);
   TEST_ASSERT_EQUAL_UINT8_ARRAY(to, from, 20);

   /* put 51 */
   ret = ciaaLibs_circBufPut(cbuf, &from[10], 51);
   TEST_ASSERT_EQUAL_INT(51, ret);
   memset((void*)to, 0, 100);

   /* get 50: 5 old and 45 new */
   memset((void*)to, 0, 100);
   ret = ciaaLibs_circBufGet(cbuf, to, 50);
   TEST_ASSERT_EQUAL_INT(50, ret);
   TEST_ASSERT_EQUAL_UINT8_ARRAY(to, &from[20], 5);
   TEST_ASSERT_EQUAL_UINT8_ARRAY(&to[5], &from[10], 45);

   memset((void*)to, 0, 100);
   ret = ciaaLibs_circBufGet(cbuf, to, 50);
   TEST_ASSERT_EQUAL_INT(6, ret);
   TEST_ASSERT_EQUAL_UINT8_ARRAY(to, &from[55], 6);

   /* release buffer */
   ciaaLibs_circBufRel(cbuf);
} /* end test_ciaaLibs_circBufPutGet */
/** \brief test function write single register
 **
 ** this function test modbus master write single register
 ** with callback
 **
 **/
void test_ciaaModbus_masterCmd0x06WriteSingleRegister_01(void)
{
   int32_t hModbusMaster;
   uint8_t slaveIdRecv;
   uint8_t pduRecv[256];
   uint8_t pdu[256] =
   {
      0X06,
      0x00,
      0x01,
      0X00,
      0x03,
   };

   uint32_t sizeRecv;

   /* open modbus master */
   hModbusMaster = ciaaModbus_masterOpen();

   /* request write single register */
   ciaaModbus_masterCmd0x06WriteSingleRegister(
         hModbusMaster,
         0x0001,
         0x0003,
         SLAVE_ID,
         modbusMaster_cbEndOfComm);

   /* perform task modbus master */
   ciaaModbus_masterTask(hModbusMaster);

   /* receive pdu from master */
   ciaaModbus_masterRecvMsg(
         hModbusMaster,
         &slaveIdRecv,
         pduRecv,
         &sizeRecv);

   /* send message to master */
   ciaaModbus_masterSendMsg(
         hModbusMaster,
         SLAVE_ID,
         pdu,
         5);

   /* verify */
   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, slaveIdRecv);

   TEST_ASSERT_EQUAL_UINT8_ARRAY(pdu, pduRecv, 5);

   TEST_ASSERT_EQUAL_UINT32(5, sizeRecv);

   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, callBackData_slaveId);

   TEST_ASSERT_EQUAL_UINT8(0x06, callBackData_numFunc);

   TEST_ASSERT_EQUAL_UINT8(0x00, callBackData_exceptioncode);
}
void
test_IfTable_get_ifPhysAddress(void) {
  /* TODO */

  netsnmp_variable_list data = {0};
  void *lctx = NULL;
  void *dctx1 = NULL;
  void *dctx2 = NULL;
  void *dctx3 = NULL;
  size_t ret_len;

  char *ret_val;

  const uint8_t value1[] = VALUE_ifPhysAddress_1;
  const uint8_t value2[] = VALUE_ifPhysAddress_2;

  data.type = ASN_INTEGER;

  ifTable_get_first_data_point(&lctx, &dctx1, &data, NULL);
  ifTable_get_next_data_point(&lctx, &dctx2, &data, NULL);
  ifTable_data_free(dctx2, NULL);
  ifTable_get_next_data_point(&lctx, &dctx3, &data, NULL);
  TEST_ASSERT_NULL(dctx3);
  ifTable_loop_free(lctx, NULL);
  ret_val = get_ifPhysAddress(dctx1, &ret_len);
  TEST_ASSERT_NOT_NULL(ret_val);
  TEST_ASSERT_EQUAL_UINT64(sizeof(value1), ret_len);
  TEST_ASSERT_EQUAL_UINT8_ARRAY(value1, (uint8_t *)ret_val, sizeof(value1));
  ifTable_data_free(dctx1, NULL);
  ifTable_get_first_data_point(&lctx, &dctx1, &data, NULL);
  ifTable_data_free(dctx1, NULL);
  ifTable_get_next_data_point(&lctx, &dctx2, &data, NULL);
  ifTable_get_next_data_point(&lctx, &dctx3, &data, NULL);
  TEST_ASSERT_NULL(dctx3);
  ifTable_loop_free(lctx, NULL);
  ret_val = get_ifPhysAddress(dctx2, &ret_len);
  TEST_ASSERT_NOT_NULL(ret_val);
  TEST_ASSERT_EQUAL_UINT64(sizeof(value2), ret_len);
  TEST_ASSERT_EQUAL_UINT8_ARRAY(value2, (uint8_t *)ret_val, sizeof(value2));
  ifTable_data_free(dctx2, NULL);
}
void
test_copy_mac_address(void) {
  lagopus_result_t rc;

  const mac_address_t src_mac_addr = {0x11, 0x22, 0x33, 0xAA, 0xBB, 0xCC};
  mac_address_t actual_mac_addr = {0, 0, 0, 0, 0, 0};
  mac_address_t expected_mac_addr = {0x11, 0x22, 0x33, 0xAA, 0xBB, 0xCC};

  rc = copy_mac_address(src_mac_addr, actual_mac_addr);
  TEST_ASSERT_EQUAL(LAGOPUS_RESULT_OK, rc);
  TEST_ASSERT_EQUAL_UINT8_ARRAY(expected_mac_addr, actual_mac_addr, 6);
}
Exemple #6
0
void test_KineticHMAC_Init_should_initialize_the_HMAC_struct_with_the_specified_algorithm(void)
{
    KineticHMAC actual, expected;
    const Com__Seagate__Kinetic__Proto__Command__Security__ACL__HMACAlgorithm expectedAlgorithm =
        COM__SEAGATE__KINETIC__PROTO__COMMAND__SECURITY__ACL__HMACALGORITHM__HmacSHA1;

    memset(&actual, 0xC5, sizeof(KineticHMAC)); // Initialize with bogus data
    expected.algorithm = expectedAlgorithm;
    memset(expected.data, 0, KINETIC_HMAC_MAX_LEN); // Expect HMAC zeroed out

    KineticHMAC_Init(&actual, expectedAlgorithm);

    TEST_ASSERT_EQUAL(expected.algorithm, actual.algorithm);
    TEST_ASSERT_EQUAL_SIZET(KINETIC_HMAC_MAX_LEN, actual.len);
    TEST_ASSERT_EQUAL_UINT8_ARRAY(expected.data, actual.data, KINETIC_HMAC_MAX_LEN);
}
Exemple #7
0
TEST(ringbuffer, aaa) {

    unsigned int len;
    unsigned char buf1[20];
    unsigned char buf2[20];

    (void)memset(buf2, 0x28, sizeof(buf2));

    RingBuffer_Clear(&forTest);
    TEST_ASSERT_EQUAL(TEST_RINGBUFFER_SIZE, RingBuffer_LeftSpace(&forTest));
    TEST_ASSERT_EQUAL(56, RingBuffer_PutData(&forTest, 0x28, 56));
    TEST_ASSERT_EQUAL(TEST_RINGBUFFER_SIZE - 56, RingBuffer_LeftSpace(&forTest));

    for (len = 0; len < 56; ) {
        int read = 56 - len;
        read = read > sizeof(buf1) ? sizeof(buf1) : read;
        TEST_ASSERT_EQUAL(read, RingBuffer_Read(&forTest, buf1, read));
        TEST_ASSERT_EQUAL_UINT8_ARRAY(buf1, buf2, read);
        //TEST_ASSERT_EQUAL_UINT8_ARRAY(buf1, "Hello world!!!", read);
        len += read;
    }


    (void)strcpy(buf1, "Hello world\n");
    TEST_ASSERT_EQUAL(strlen(buf1) + 1, RingBuffer_Write(&forTest, buf1, strlen(buf1) + 1));
    TEST_ASSERT_EQUAL(strlen(buf1) + 1, RingBuffer_PopString(&forTest, buf2, sizeof(buf2)));
    TEST_ASSERT_EQUAL_STRING(buf1, buf2);



    (void)strcpy(buf1, "Hello world\n");
    len = sizeof(buf2);
    TEST_ASSERT_EQUAL(strlen(buf1), RingBuffer_Write(&forTest, buf1, strlen(buf1)));
    TEST_ASSERT(RingBuffer_PopStringIsStartWith(&forTest, "Hello", buf2, &len));
    TEST_ASSERT_EQUAL(strlen(buf1), len);
    TEST_ASSERT_EQUAL_STRING(buf1, buf2);

    (void)strcpy(buf1, "Hello world\n");
    len = sizeof(buf2);
    TEST_ASSERT_EQUAL(strlen(buf1), RingBuffer_Write(&forTest, buf1, strlen(buf1)));
    TEST_ASSERT_FALSE(RingBuffer_PopStringIsStartWith(&forTest, "world", buf2, &len));
    TEST_ASSERT_EQUAL(strlen(buf1), len);
    TEST_ASSERT_EQUAL_STRING(buf1, buf2);
}
/** \brief test function write multiple register
 **
 ** this function test modbus master write multiple registers
 ** with callback
 **
 **/
void test_ciaaModbus_masterCmd0x10WriteMultipleRegisters_01(void)
{
   int32_t hModbusMaster;
   int16_t hrValue[CIAA_MODBUS_QUANTITY_OF_HOLD_REG] =
   {
      0x1234,
      0x5678,
   };
   uint8_t slaveIdRecv;
   uint8_t pduRecv[256];
   uint8_t pduRecvExp[256] =
   {
      0X10,
      (CIAA_MODBUS_STARTING_ADDRESS >> 8) & 0XFF,
      (CIAA_MODBUS_STARTING_ADDRESS >> 0) & 0XFF,
      (CIAA_MODBUS_QUANTITY_OF_HOLD_REG >> 8) & 0XFF,
      (CIAA_MODBUS_QUANTITY_OF_HOLD_REG >> 0) & 0XFF,
      CIAA_MODBUS_QUANTITY_OF_HOLD_REG * 2,
      0x12,
      0x34,
      0x56,
      0x78,
   };
   uint8_t pduSend[256] =
   {
      0X10,
      (CIAA_MODBUS_STARTING_ADDRESS >> 8) & 0XFF,
      (CIAA_MODBUS_STARTING_ADDRESS >> 0) & 0XFF,
      (CIAA_MODBUS_QUANTITY_OF_HOLD_REG >> 8) & 0XFF,
      (CIAA_MODBUS_QUANTITY_OF_HOLD_REG >> 0) & 0XFF,
   };

   uint32_t sizeRecv;
   uint32_t sizeRecvExp = 10;

   /* open modbus master */
   hModbusMaster = ciaaModbus_masterOpen();

   /* request read holding register */
   ciaaModbus_masterCmd0x10WriteMultipleRegisters(
         hModbusMaster,
         CIAA_MODBUS_STARTING_ADDRESS,
         CIAA_MODBUS_QUANTITY_OF_HOLD_REG,
         hrValue,
         SLAVE_ID,
         modbusMaster_cbEndOfComm);

   /* perform task modbus master */
   ciaaModbus_masterTask(hModbusMaster);

   /* receive pdu from master */
   ciaaModbus_masterRecvMsg(
         hModbusMaster,
         &slaveIdRecv,
         pduRecv,
         &sizeRecv);

   /* send message to master */
   ciaaModbus_masterSendMsg(
         hModbusMaster,
         SLAVE_ID,
         pduSend,
         5);

   /* verify */
   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, slaveIdRecv);

   TEST_ASSERT_EQUAL_UINT8_ARRAY(pduRecvExp, pduRecv, sizeRecvExp);

   TEST_ASSERT_EQUAL_UINT32(sizeRecvExp, sizeRecv);

   TEST_ASSERT_EQUAL_UINT16(0X1234, hrValue[0]);

   TEST_ASSERT_EQUAL_UINT16(0X5678, hrValue[1]);

   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, callBackData_slaveId);

   TEST_ASSERT_EQUAL_UINT8(0x10, callBackData_numFunc);

   TEST_ASSERT_EQUAL_UINT8(0x00, callBackData_exceptioncode);
}

/** \brief test function read write multiple register
 **
 ** this function test modbus master read write multiple registers
 ** with callback
 **
 **/
void test_ciaaModbus_masterCmd0x17ReadWriteMultipleRegisters_01(void)
{
   int32_t hModbusMaster;
   int16_t hrValueR[128];
   int16_t hrValueW[] =
   {
      0x1122,
      0x3344,
      0x5566,
   };
   uint8_t slaveIdRecv;
   uint8_t pduRecv[256];
   uint8_t pduRecvExp[256] =
   {
      0X17,
      0X00,
      0X03,
      0X00,
      0X06,
      0X00,
      0X0E,
      0X00,
      0X03,
      0X06,
      0X11,
      0X22,
      0X33,
      0X44,
      0X55,
      0X66,
   };
   uint8_t pduSend[256] =
   {
      0X17,
      0X0C,
      0XFF,
      0XEE,
      0XDD,
      0XCC,
      0XBB,
      0XAA,
      0X99,
      0X88,
      0X77,
      0X66,
      0X55,
      0X44,
   };

   uint32_t sizeRecv;
   uint32_t sizeRecvExp = 16;

   /* open modbus master */
   hModbusMaster = ciaaModbus_masterOpen();

   /* request read holding register */
   ciaaModbus_masterCmd0x17ReadWriteMultipleRegisters(
         hModbusMaster,
         0X0003,
         0X0006,
         hrValueR,
         0X000E,
         0X0003,
         hrValueW,
         SLAVE_ID,
         modbusMaster_cbEndOfComm);

   /* perform task modbus master */
   ciaaModbus_masterTask(hModbusMaster);

   /* receive pdu from master */
   ciaaModbus_masterRecvMsg(
         hModbusMaster,
         &slaveIdRecv,
         pduRecv,
         &sizeRecv);

   /* send message to master */
   ciaaModbus_masterSendMsg(
         hModbusMaster,
         SLAVE_ID,
         pduSend,
         14);

   /* verify */
   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, slaveIdRecv);

   TEST_ASSERT_EQUAL_UINT8_ARRAY(pduRecvExp, pduRecv, sizeRecvExp);

   TEST_ASSERT_EQUAL_UINT32(sizeRecvExp, sizeRecv);

   TEST_ASSERT_EQUAL_UINT16(0XFFEE, hrValueR[0]);

   TEST_ASSERT_EQUAL_UINT16(0XDDCC, hrValueR[1]);

   TEST_ASSERT_EQUAL_UINT16(0XBBAA, hrValueR[2]);

   TEST_ASSERT_EQUAL_UINT16(0X9988, hrValueR[3]);

   TEST_ASSERT_EQUAL_UINT16(0X7766, hrValueR[4]);

   TEST_ASSERT_EQUAL_UINT16(0X5544, hrValueR[5]);

   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, callBackData_slaveId);

   TEST_ASSERT_EQUAL_UINT8(0x17, callBackData_numFunc);

   TEST_ASSERT_EQUAL_UINT8(0x00, callBackData_exceptioncode);
}

/** \brief test get Response timeout
 **
 ** this function test get Response timeout
 **
 **/
void test_ciaaModbus_masterGetRespTimeout_01(void)
{
   int32_t hModbusMaster;
   uint32_t timeout;

   /* open modbus master */
   hModbusMaster = ciaaModbus_masterOpen();

   timeout = ciaaModbus_masterGetRespTimeout(hModbusMaster);

   /* default response timeout 300 ms */
   TEST_ASSERT_EQUAL_UINT32(300,timeout);
}
/** \brief test function write multiple coils
 **
 ** this function test modbus master write multiple coils
 ** with callback
 **
 **/
void test_ciaaModbus_masterCmd0x0FWriteMultipleCoilss_01(void)
{
   int32_t hModbusMaster;
   uint8_t coilsValue[] =
   {
      0xCD,
      0x01,
   };
   uint8_t slaveIdRecv;
   uint8_t pduRecv[256];
   uint8_t pduRecvExp[256] =
   {
      0x0F,
      0x00,
      0x13,
      0x00,
      0x0A,
      0x02,
      0xCD,
      0x01,
   };
   uint8_t pduSend[256] =
   {
      0x0F,
      0x00,
      0x13,
      0x00,
      0x0A,
   };

   uint32_t sizeRecv;
   uint32_t sizeRecvExp = 8;

   /* open modbus master */
   hModbusMaster = ciaaModbus_masterOpen();

   /* request read holding register */
   ciaaModbus_masterCmd0x0FWriteMultipleCoils(
         hModbusMaster,
         0X0013,
         0X000A,
         coilsValue,
         SLAVE_ID,
         modbusMaster_cbEndOfComm);

   /* perform task modbus master */
   ciaaModbus_masterTask(hModbusMaster);

   /* receive pdu from master */
   ciaaModbus_masterRecvMsg(
         hModbusMaster,
         &slaveIdRecv,
         pduRecv,
         &sizeRecv);

   /* send message to master */
   ciaaModbus_masterSendMsg(
         hModbusMaster,
         SLAVE_ID,
         pduSend,
         5);

   /* verify */
   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, slaveIdRecv);

   TEST_ASSERT_EQUAL_UINT8_ARRAY(pduRecvExp, pduRecv, sizeRecvExp);

   TEST_ASSERT_EQUAL_UINT32(sizeRecvExp, sizeRecv);

   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, callBackData_slaveId);

   TEST_ASSERT_EQUAL_UINT8(0x0F, callBackData_numFunc);

   TEST_ASSERT_EQUAL_UINT8(0x00, callBackData_exceptioncode);
}
/** \brief test function read holding registers
 **
 ** this function test modbus master read holding registers
 ** with callback
 **
 **/
void test_ciaaModbus_masterCmd0x03ReadHoldingRegisters_01(void)
{
   int32_t hModbusMaster;
   int16_t hrValue[CIAA_MODBUS_QUANTITY_OF_HOLD_REG];
   uint8_t slaveIdRecv;
   uint8_t pduRecv[256];
   uint8_t pduRecvExp[256] =
   {
      0X03,
      (CIAA_MODBUS_STARTING_ADDRESS >> 8) & 0XFF,
      (CIAA_MODBUS_STARTING_ADDRESS >> 0) & 0XFF,
      (CIAA_MODBUS_QUANTITY_OF_HOLD_REG >> 8) & 0XFF,
      (CIAA_MODBUS_QUANTITY_OF_HOLD_REG >> 0) & 0XFF,
   };
   uint8_t pduSend[256] =
   {
      0X03,
      CIAA_MODBUS_QUANTITY_OF_HOLD_REG * 2,
      0XAA,
      0X55,
      0X11,
      0X22,
   };

   uint32_t sizeRecv;
   uint32_t sizeRecvExp = 5;

   /* open modbus master */
   hModbusMaster = ciaaModbus_masterOpen();

   /* request read holding register */
   ciaaModbus_masterCmd0x03ReadHoldingRegisters(
         hModbusMaster,
         CIAA_MODBUS_STARTING_ADDRESS,
         CIAA_MODBUS_QUANTITY_OF_HOLD_REG,
         hrValue,
         SLAVE_ID,
         modbusMaster_cbEndOfComm);

   /* perform task modbus master */
   ciaaModbus_masterTask(hModbusMaster);

   /* receive pdu from master */
   ciaaModbus_masterRecvMsg(
         hModbusMaster,
         &slaveIdRecv,
         pduRecv,
         &sizeRecv);

   /* send message to master */
   ciaaModbus_masterSendMsg(
         hModbusMaster,
         SLAVE_ID,
         pduSend,
         6);

   /* verify */
   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, slaveIdRecv);

   TEST_ASSERT_EQUAL_UINT8_ARRAY(pduRecvExp, pduRecv, 5);

   TEST_ASSERT_EQUAL_UINT32(sizeRecvExp, sizeRecv);

   TEST_ASSERT_EQUAL_UINT16(0XAA55, hrValue[0]);

   TEST_ASSERT_EQUAL_UINT16(0X1122, hrValue[1]);

   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, callBackData_slaveId);

   TEST_ASSERT_EQUAL_UINT8(0x03, callBackData_numFunc);

   TEST_ASSERT_EQUAL_UINT8(0x00, callBackData_exceptioncode);
}

/** \brief test function read input registers
 **
 ** this function test modbus master read input registers
 ** with callback
 **
 **/
void test_ciaaModbus_masterCmd0x04ReadInputRegisters_01(void)
{
   int32_t hModbusMaster;
   int16_t irValue[CIAA_MODBUS_QUANTITY_OF_INPUT_REG];
   uint8_t slaveIdRecv;
   uint8_t pduRecv[256];
   uint8_t pduRecvExp[256] =
   {
      0X04,
      (CIAA_MODBUS_STARTING_ADDRESS >> 8) & 0XFF,
      (CIAA_MODBUS_STARTING_ADDRESS >> 0) & 0XFF,
      (CIAA_MODBUS_QUANTITY_OF_INPUT_REG >> 8) & 0XFF,
      (CIAA_MODBUS_QUANTITY_OF_INPUT_REG >> 0) & 0XFF,
   };
   uint8_t pduSend[256] =
   {
      0X04,
      CIAA_MODBUS_QUANTITY_OF_INPUT_REG * 2,
      0XAA,
      0X55,
      0X11,
      0X22,
   };

   uint32_t sizeRecv;
   uint32_t sizeRecvExp = 5;

   /* open modbus master */
   hModbusMaster = ciaaModbus_masterOpen();

   /* request read holding register */
   ciaaModbus_masterCmd0x04ReadInputRegisters(
         hModbusMaster,
         CIAA_MODBUS_STARTING_ADDRESS,
         CIAA_MODBUS_QUANTITY_OF_INPUT_REG,
         irValue,
         SLAVE_ID,
         modbusMaster_cbEndOfComm);

   /* perform task modbus master */
   ciaaModbus_masterTask(hModbusMaster);

   /* receive pdu from master */
   ciaaModbus_masterRecvMsg(
         hModbusMaster,
         &slaveIdRecv,
         pduRecv,
         &sizeRecv);

   /* send message to master */
   ciaaModbus_masterSendMsg(
         hModbusMaster,
         SLAVE_ID,
         pduSend,
         6);

   /* verify */
   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, slaveIdRecv);

   TEST_ASSERT_EQUAL_UINT8_ARRAY(pduRecvExp, pduRecv, 5);

   TEST_ASSERT_EQUAL_UINT32(sizeRecvExp, sizeRecv);

   TEST_ASSERT_EQUAL_UINT16(0XAA55, irValue[0]);

   TEST_ASSERT_EQUAL_UINT16(0X1122, irValue[1]);

   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, callBackData_slaveId);

   TEST_ASSERT_EQUAL_UINT8(0x04, callBackData_numFunc);

   TEST_ASSERT_EQUAL_UINT8(0x00, callBackData_exceptioncode);
}

/** \brief test function write single coil
 **
 ** this function test modbus master write single coil
 ** with callback
 **
 **/
void test_ciaaModbus_masterCmd0x05WriteSingleCoil_01(void)
{
   int32_t hModbusMaster;
   uint8_t slaveIdRecv;
   uint8_t pduRecv[256];
   uint8_t pdu[256] =
   {
      0X05,
      0x00,
      0xAC,
      0XFF,
      0x00,
   };

   uint32_t sizeRecv;

   /* open modbus master */
   hModbusMaster = ciaaModbus_masterOpen();

   /* request read holding register */
   ciaaModbus_masterCmd0x05WriteSingleCoil(
         hModbusMaster,
         0x00AC,
         true,
         SLAVE_ID,
         modbusMaster_cbEndOfComm);

   /* perform task modbus master */
   ciaaModbus_masterTask(hModbusMaster);

   /* receive pdu from master */
   ciaaModbus_masterRecvMsg(
         hModbusMaster,
         &slaveIdRecv,
         pduRecv,
         &sizeRecv);

   /* send message to master */
   ciaaModbus_masterSendMsg(
         hModbusMaster,
         SLAVE_ID,
         pdu,
         5);

   /* verify */
   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, slaveIdRecv);

   TEST_ASSERT_EQUAL_UINT8_ARRAY(pdu, pduRecv, 5);

   TEST_ASSERT_EQUAL_UINT32(5, sizeRecv);

   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, callBackData_slaveId);

   TEST_ASSERT_EQUAL_UINT8(0x05, callBackData_numFunc);

   TEST_ASSERT_EQUAL_UINT8(0x00, callBackData_exceptioncode);
}
/** \brief test function read discrete inputs
 **
 ** this function test modbus master read discrete inputs
 ** with callback
 **
 **/
void test_ciaaModbus_masterCmd0x02ReadDiscreteInputs_01(void)
{
   int32_t hModbusMaster;
   uint8_t discreteInputsValue[256];
   uint8_t slaveIdRecv;
   uint8_t pduRecv[256];
   uint8_t pduRecvExp[256] =
   {
      0X02,
      0x12,
      0x34,
      0x00,
      0x13,
   };
   uint8_t pduSend[256] =
   {
      0X02,
      0x03,
      0XCD,
      0X6B,
      0X05,
   };

   uint32_t sizeRecv;
   uint32_t sizeRecvExp = 5;

   /* open modbus master */
   hModbusMaster = ciaaModbus_masterOpen();

   /* request read holding register */
   ciaaModbus_masterCmd0x02ReadDiscreteInputs(
         hModbusMaster,
         0X1234,
         0X0013,
         discreteInputsValue,
         SLAVE_ID,
         modbusMaster_cbEndOfComm);

   /* perform task modbus master */
   ciaaModbus_masterTask(hModbusMaster);

   /* receive pdu from master */
   ciaaModbus_masterRecvMsg(
         hModbusMaster,
         &slaveIdRecv,
         pduRecv,
         &sizeRecv);

   /* send message to master */
   ciaaModbus_masterSendMsg(
         hModbusMaster,
         SLAVE_ID,
         pduSend,
         5);

   /* verify */
   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, slaveIdRecv);

   TEST_ASSERT_EQUAL_UINT8_ARRAY(pduRecvExp, pduRecv, 5);

   TEST_ASSERT_EQUAL_UINT32(sizeRecvExp, sizeRecv);

   TEST_ASSERT_EQUAL_UINT8(0XCD, discreteInputsValue[0]);

   TEST_ASSERT_EQUAL_UINT16(0X6B, discreteInputsValue[1]);

   TEST_ASSERT_EQUAL_UINT16(0X05, discreteInputsValue[2]);

   TEST_ASSERT_EQUAL_UINT8(SLAVE_ID, callBackData_slaveId);

   TEST_ASSERT_EQUAL_UINT8(0x02, callBackData_numFunc);

   TEST_ASSERT_EQUAL_UINT8(0x00, callBackData_exceptioncode);
}
/****************************************************************************
 * Name: Receive
 *
 * Description:
 *   Non-blocking & instant connect+recv
 *
 * Input Parameters:
 *   dconf - socket daemon configuration
 *
 * Returned Value:
 *   None
 *
 * Assumptions/Limitations:
 *   None
 *
 ****************************************************************************/
static void Receive(struct usrsocktest_daemon_conf_s *dconf)
{
  int flags;
  int count;
  ssize_t ret;
  size_t datalen;
  void *data;
  struct sockaddr_in addr;
  char databuf[4];
  struct sockaddr_in remoteaddr;
  socklen_t addrlen;

  /* Start test daemon. */

  dconf->endpoint_addr = "127.0.0.1";
  dconf->endpoint_port = 255;
  dconf->endpoint_recv_avail_from_start = true;
  dconf->endpoint_recv_avail = 7;
  TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_start(dconf));
  started = true;
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets());

  /* Open socket */

  sd = socket(AF_INET, SOCK_STREAM, 0);
  TEST_ASSERT_TRUE(sd >= 0);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_waiting_connect_sockets());

  /* Make socket non-blocking */

  flags = fcntl(sd, F_GETFL, 0);
  TEST_ASSERT_TRUE(flags >= 0);
  TEST_ASSERT_EQUAL(O_RDWR, flags & O_RDWR);
  TEST_ASSERT_EQUAL(0, flags & O_NONBLOCK);
  ret = fcntl(sd, F_SETFL, flags | O_NONBLOCK);
  TEST_ASSERT_EQUAL(0, ret);
  flags = fcntl(sd, F_GETFL, 0);
  TEST_ASSERT_TRUE(flags >= 0);
  TEST_ASSERT_EQUAL(O_RDWR, flags & O_RDWR);
  TEST_ASSERT_EQUAL(O_NONBLOCK, flags & O_NONBLOCK);

  /* Do connect, should succeed instantly. */

  inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(255);
  ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr));
  if (!dconf->delay_all_responses)
    {
      TEST_ASSERT_EQUAL(0, ret);
      TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
      TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
      TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_waiting_connect_sockets());
      TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_recv_empty_sockets());
    }
  else
    {
      TEST_ASSERT_EQUAL(-1, ret);
      TEST_ASSERT_EQUAL(EINPROGRESS, errno);
      TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
      TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets());
      TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_waiting_connect_sockets());

      for (count = 0; usrsocktest_daemon_get_num_connected_sockets() != 1; count++)
        {
          TEST_ASSERT_TRUE(count <= 3);
          usleep(25 * 1000);
        }

      ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr));
      TEST_ASSERT_EQUAL(-1, ret);
      TEST_ASSERT_EQUAL(EISCONN, errno);
      TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
      TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
      TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_recv_empty_sockets());
    }

  /* Receive data from remote, daemon returns 4 bytes. */

  data = databuf;
  datalen = sizeof(databuf);
  ret = recvfrom(sd, data, datalen, 0, NULL, NULL);
  TEST_ASSERT_EQUAL(datalen, ret);
  TEST_ASSERT_EQUAL(4, datalen);
  TEST_ASSERT_EQUAL_UINT8_ARRAY("abcd", data, 4);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(4, usrsocktest_daemon_get_recv_bytes());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Receive data from remote with address, daemon returns 3 bytes. */

  addrlen = sizeof(remoteaddr);
  ret = recvfrom(sd, data, datalen, 0, (FAR struct sockaddr *)&remoteaddr, &addrlen);
  TEST_ASSERT_EQUAL(3, ret);
  TEST_ASSERT_EQUAL_UINT8_ARRAY("abc", data, 3);
  TEST_ASSERT_EQUAL(addrlen, sizeof(remoteaddr));
  TEST_ASSERT_EQUAL_UINT8_ARRAY(&remoteaddr, &addr, addrlen);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(datalen + ret, usrsocktest_daemon_get_recv_bytes());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Receive data from remote, daemon has 0 bytes buffered => -EAGAIN */

  data = databuf;
  datalen = sizeof(databuf);
  ret = recvfrom(sd, data, datalen, 0, NULL, NULL);
  TEST_ASSERT_EQUAL(-1, ret);
  TEST_ASSERT_EQUAL(EAGAIN, errno);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(7, usrsocktest_daemon_get_recv_bytes());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Receive data from remote, daemon has 0 bytes buffered => -EAGAIN */

  data = databuf;
  datalen = sizeof(databuf);
  ret = read(sd, data, datalen);
  TEST_ASSERT_EQUAL(-1, ret);
  TEST_ASSERT_EQUAL(EAGAIN, errno);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(7, usrsocktest_daemon_get_recv_bytes());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Reset recv buffer for open sockets */

  TEST_ASSERT_TRUE(usrsocktest_send_delayed_command('r', 0));
  for (count = 0; usrsocktest_daemon_get_num_recv_empty_sockets() > 0; count++)
    {
      TEST_ASSERT_TRUE(count <= 5);
      usleep(5 * 1000);
    }
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Receive data from remote, daemon returns 4 bytes. */

  data = databuf;
  datalen = sizeof(databuf);
  ret = recvfrom(sd, data, datalen, 0, NULL, NULL);
  TEST_ASSERT_EQUAL(datalen, ret);
  TEST_ASSERT_EQUAL(4, datalen);
  TEST_ASSERT_EQUAL_UINT8_ARRAY("abcd", data, 4);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(7 + 4, usrsocktest_daemon_get_recv_bytes());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Close socket */

  TEST_ASSERT_TRUE(close(sd) >= 0);
  sd = -1;
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(7 + 4, usrsocktest_daemon_get_recv_bytes());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Stopping daemon should succeed. */

  TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_stop());
  started = false;
  TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_endp_malloc_cnt);
  TEST_ASSERT_EQUAL(0, usrsocktest_dcmd_malloc_cnt);
}
/****************************************************************************
 * Name: DelayedConnect
 *
 * Description:
 *   Non-blocking & delayed connect
 *
 * Input Parameters:
 *   dconf - socket daemon configuration
 *
 * Returned Value:
 *   None
 *
 * Assumptions/Limitations:
 *   None
 *
 ****************************************************************************/
static void DelayedConnect(struct usrsocktest_daemon_conf_s *dconf)
{
  int flags;
  int count;
  ssize_t ret;
  size_t datalen;
  void *data;
  struct sockaddr_in addr;
  char databuf[4];
  struct sockaddr_in remoteaddr;
  socklen_t addrlen;

  /* Start test daemon. */

  dconf->endpoint_block_connect = true;
  dconf->endpoint_recv_avail_from_start = false;
  dconf->endpoint_recv_avail = 4;
  dconf->endpoint_addr = "127.0.0.1";
  dconf->endpoint_port = 255;
  TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_start(dconf));
  started = true;
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets());

  /* Open socket */

  sd = socket(AF_INET, SOCK_STREAM, 0);
  TEST_ASSERT_TRUE(sd >= 0);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_waiting_connect_sockets());

  /* Make socket non-blocking */

  flags = fcntl(sd, F_GETFL, 0);
  TEST_ASSERT_TRUE(flags >= 0);
  TEST_ASSERT_EQUAL(O_RDWR, flags & O_RDWR);
  TEST_ASSERT_EQUAL(0, flags & O_NONBLOCK);
  ret = fcntl(sd, F_SETFL, flags | O_NONBLOCK);
  TEST_ASSERT_EQUAL(0, ret);
  flags = fcntl(sd, F_GETFL, 0);
  TEST_ASSERT_TRUE(flags >= 0);
  TEST_ASSERT_EQUAL(O_RDWR, flags & O_RDWR);
  TEST_ASSERT_EQUAL(O_NONBLOCK, flags & O_NONBLOCK);

  /* Launch connect attempt, daemon delays actual connection until triggered. */

  inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(255);
  ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr));
  TEST_ASSERT_EQUAL(-1, ret);
  TEST_ASSERT_EQUAL(EINPROGRESS, errno);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_waiting_connect_sockets());

  /* Try receive data, not connected yet. */

  data = databuf;
  datalen = sizeof(databuf);
  ret = read(sd, data, datalen);
  TEST_ASSERT_EQUAL(-1, ret);
  TEST_ASSERT_EQUAL(EAGAIN, errno);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_waiting_connect_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_send_bytes());

  /* Release delayed connect. */

  TEST_ASSERT_TRUE(usrsocktest_daemon_establish_waiting_connections());
  for (count = 0; usrsocktest_daemon_get_num_waiting_connect_sockets() > 0; count++)
    {
      TEST_ASSERT_TRUE(count <= 5);
      usleep(10 * 1000);
    }
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_waiting_connect_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Try receive data, not received data yet. */

  data = databuf;
  datalen = sizeof(databuf);
  addrlen = sizeof(remoteaddr);
  ret = recvfrom(sd, data, datalen, 0, (FAR struct sockaddr *)&remoteaddr, &addrlen);
  TEST_ASSERT_EQUAL(-1, ret);
  TEST_ASSERT_EQUAL(EAGAIN, errno);
  TEST_ASSERT_EQUAL(0, addrlen);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_send_bytes());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Reset recv buffer for open sockets */

  TEST_ASSERT_TRUE(usrsocktest_send_delayed_command('r', 0));
  for (count = 0; usrsocktest_daemon_get_num_recv_empty_sockets() > 0; count++)
    {
      TEST_ASSERT_TRUE(count <= 5);
      usleep(5 * 1000);
    }

  /* Receive data from remote, daemon returns 4 bytes. */

  data = databuf;
  datalen = sizeof(databuf);
  ret = recvfrom(sd, data, datalen, 0, NULL, NULL);
  TEST_ASSERT_EQUAL(datalen, ret);
  TEST_ASSERT_EQUAL(4, datalen);
  TEST_ASSERT_EQUAL_UINT8_ARRAY("abcd", data, 4);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(4, usrsocktest_daemon_get_recv_bytes());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Close socket. */

  TEST_ASSERT_TRUE(close(sd) >= 0);
  sd = -1;
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_waiting_connect_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Stopping daemon should succeed. */

  TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_stop());
  started = false;
  TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_waiting_connect_sockets());
  TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_recv_empty_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_endp_malloc_cnt);
  TEST_ASSERT_EQUAL(0, usrsocktest_dcmd_malloc_cnt);
}
Exemple #14
0
TEST(rle, encode_decode)
{
	const uint8_t decode_data_0[] = {
		0x01, 0x32, 0x32, 0x32, 0x32, 0x32, 0x55, 0x37, 0x11, 0xAC, 0x8E, 0x97,
		0xC9, 0xC9, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0xA0, 0x88
	};
	const uint8_t encode_data_0[] = {
		0x01, 0x01, 0x05, 0x32, 0x00, 0x06, 0x55, 0x37, 0x11, 0xAC, 0x8E, 0x97,
		0x02, 0xC9, 0x08, 0xA0, 0x01, 0x88
	};

	const uint8_t decode_data_1[] = {
		0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3, 0xB3
	};
	const uint8_t encode_data_1[] = {
		0x0C, 0xB3
	};

	const uint8_t decode_data_2[] = {
		0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF
	};
	const uint8_t encode_data_2[] = {
		0x00, 0x08, 0x12, 0x34, 0x56, 0x78, 0x90, 0xAB, 0xCD, 0xEF
	};

	const uint8_t decode_data_3[] = {
		0x12, 0x12, 0x12, 0x78, 0x90, 0xAB, 0xAB, 0xAB, 0xAB
	};
	const uint8_t encode_data_3[] = {
		0x03, 0x12, 0x00, 0x02, 0x78, 0x90, 0x04, 0xAB
	};

	struct rle_test_data_s {
		const uint8_t *decode_data;
		const uint8_t *encode_data;
		size_t numof_decode_data;
		size_t numof_encode_data;
	} rtd[] = {
		{ decode_data_0, encode_data_0, sizeof(decode_data_0), sizeof(encode_data_0) },
		{ decode_data_1, encode_data_1, sizeof(decode_data_1), sizeof(encode_data_1) },
		{ decode_data_2, encode_data_2, sizeof(decode_data_2), sizeof(encode_data_2) },
		{ decode_data_3, encode_data_3, sizeof(decode_data_3), sizeof(encode_data_3) }
	};
	size_t numof_rle_data = sizeof(rtd)/sizeof(rtd[0]);
	int32_t i;

	for (i=0; i<(int32_t)numof_rle_data; i++) {
		struct rle_test_data_s *p = &rtd[i];
		uint8_t work[32];
		memset(work, 0, sizeof(work));

		size_t act_len = rle_encode(p->decode_data, p->numof_decode_data, work, sizeof(work));

		/* printf("<encode>\n"); */
		dump(p->decode_data, p->numof_decode_data);
		dump(work, act_len);
		/* printf("\n"); */

		TEST_ASSERT_EQUAL(p->numof_encode_data, act_len);
		TEST_ASSERT_EQUAL_UINT8_ARRAY(p->encode_data, work, act_len);

		memset(work, 0, sizeof(work));
		act_len = rle_decode(p->encode_data, p->numof_encode_data, work, sizeof(work));

		/* printf("<decode>\n"); */
		dump(p->encode_data, p->numof_encode_data);
		dump(work, act_len);
		/* printf("\n"); */

		TEST_ASSERT_EQUAL(p->numof_decode_data, act_len);
		TEST_ASSERT_EQUAL_UINT8_ARRAY(p->decode_data, work, act_len);
	}

}
Exemple #15
0
/****************************************************************************
 * Name: ConnectReceive
 *
 * Description:
 *   Blocking connect and receive
 *
 * Input Parameters:
 *   dconf - socket daemon configuration
 *
 * Returned Value:
 *   None
 *
 * Assumptions/Limitations:
 *   None
 *
 ****************************************************************************/
static void ConnectReceive(struct usrsocktest_daemon_conf_s *dconf)
{
  ssize_t ret;
  size_t datalen;
  void *data;
  struct sockaddr_in addr;
  char databuf[5];

  /* Start test daemon. */

  dconf->endpoint_addr = "127.0.0.1";
  dconf->endpoint_port = 255;
  dconf->endpoint_block_connect = true;
  dconf->endpoint_block_send = true;
  dconf->endpoint_recv_avail_from_start = false;
  dconf->endpoint_recv_avail = 7;
  TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_start(dconf));
  started = true;
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets());

  /* Open socket */

  sd = socket(AF_INET, SOCK_STREAM, 0);
  TEST_ASSERT_TRUE(sd >= 0);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_waiting_connect_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Do connect, should succeed (after connect block released). */

  inet_pton(AF_INET, "127.0.0.1", &addr.sin_addr.s_addr);
  addr.sin_family = AF_INET;
  addr.sin_port = htons(255);
  TEST_ASSERT_TRUE(usrsocktest_send_delayed_command('E', 100));
  ret = connect(sd, (FAR const struct sockaddr *)&addr, sizeof(addr));
  TEST_ASSERT_EQUAL(0, ret);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_waiting_connect_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Receive data from remote */

  data = databuf;
  datalen = sizeof(databuf);
  TEST_ASSERT_TRUE(usrsocktest_send_delayed_command('r', 100));
  ret = recvfrom(sd, data, datalen, 0, NULL, 0);
  TEST_ASSERT_EQUAL(datalen, ret);
  TEST_ASSERT_EQUAL(5, ret);
  TEST_ASSERT_EQUAL_UINT8_ARRAY("abcde", data, 5);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(5, usrsocktest_daemon_get_recv_bytes());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Receive data from remote */

  data = databuf;
  datalen = sizeof(databuf);
  ret = recvfrom(sd, data, datalen, 0, NULL, 0);
  TEST_ASSERT_EQUAL(2, ret);
  TEST_ASSERT_EQUAL_UINT8_ARRAY("ab", data, 2);
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(7, usrsocktest_daemon_get_recv_bytes());
  TEST_ASSERT_EQUAL(1, usrsocktest_daemon_get_num_recv_empty_sockets());

  /* Close socket */

  TEST_ASSERT_TRUE(close(sd) >= 0);
  sd = -1;
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_daemon_get_num_connected_sockets());

  /* Stopping daemon should succeed. */

  TEST_ASSERT_EQUAL(OK, usrsocktest_daemon_stop());
  started = false;
  TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_active_sockets());
  TEST_ASSERT_EQUAL(-ENODEV, usrsocktest_daemon_get_num_connected_sockets());
  TEST_ASSERT_EQUAL(0, usrsocktest_endp_malloc_cnt);
  TEST_ASSERT_EQUAL(0, usrsocktest_dcmd_malloc_cnt);
}