Exemplo n.º 1
0
void test_ListenerCmd_CheckIncomingMessages_should_handle_redundant_ADD_SOCKET_command(void) {
    l->fds[INCOMING_MSG_PIPE_ID].fd = 5;
    l->fds[INCOMING_MSG_PIPE_ID].revents = POLLIN;
    l->read_buf = malloc(256);

    connection_info *ci = calloc(1, sizeof(*ci));
    *(int *)&ci->fd = 5;    

    listener_msg *msg = &l->msgs[0];
    msg->type = MSG_ADD_SOCKET;
    msg->u.add_socket.info = ci;
    msg->u.add_socket.notify_fd = 18;

    l->tracked_fds = 1;
    l->fds[INCOMING_MSG_PIPE].fd = ci->fd;

    cmd_buf[0] = 0;
    syscall_read_ExpectAndReturn(l->fds[INCOMING_MSG_PIPE_ID].fd, cmd_buf, sizeof(cmd_buf), 1);

    expect_notify_caller(l, 18);
    int res = 1;

    ListenerTask_ReleaseMsg_Expect(l, msg);
    ListenerCmd_CheckIncomingMessages(l, &res);
    
    TEST_ASSERT_EQUAL(0, res);
}
Exemplo n.º 2
0
static void setup_command(listener_msg *pmsg, rx_info_t *info) {
    l->fds[INCOMING_MSG_PIPE_ID].fd = 5;
    l->fds[INCOMING_MSG_PIPE_ID].revents = POLLIN;
    l->read_buf = malloc(256);
    l->fds[INCOMING_MSG_PIPE].fd = 9;

    connection_info *ci = calloc(1, sizeof(*ci));
    *(int *)&ci->fd = 91;

    memcpy(&l->msgs[0], pmsg, sizeof(*pmsg));

    cmd_buf[0] = 0;
    syscall_read_ExpectAndReturn(l->fds[INCOMING_MSG_PIPE_ID].fd, cmd_buf, sizeof(cmd_buf), 1);
    if (pmsg->type == MSG_ADD_SOCKET) {
        ListenerTask_GrowReadBuf_ExpectAndReturn(l, 31, true);
    }
    
    if (info) {
        ListenerHelper_GetFreeRXInfo_ExpectAndReturn(l, info == NULL_INFO ? NULL : info);
    }
}
void test_ListenerIO_AttemptRecv_should_handle_successful_socket_read_and_unpack_message(void) {
    l->fds[0 + INCOMING_MSG_PIPE].fd = 5;
    l->fds[0 + INCOMING_MSG_PIPE].events = POLLIN;
    l->fds[0 + INCOMING_MSG_PIPE].revents = POLLIN;
    struct test_progress_info progress_info = {
        .to_read = 123,
    };
    connection_info ci = {
        .fd = 5,
        .type = BUS_SOCKET_PLAIN,
        .to_read_size = 123,
        .udata = &progress_info,
    };
    l->fd_info[0] = &ci;
    l->tracked_fds = 1;
    l->rx_info_max_used = 1;

    l->read_buf = calloc(256, sizeof(uint8_t));
    l->read_buf_size = 256;

    rx_info_t *info = &l->rx_info[0];
    info->state = RIS_EXPECT;
    box->fd = 5;
    info->u.expect.box = box;

    syscall_read_ExpectAndReturn(ci.fd, l->read_buf, ci.to_read_size, ci.to_read_size);

    rx_info_t unpack_res_info = {
        .state = RIS_EXPECT,
    };
    ListenerHelper_FindInfoBySequenceID_ExpectAndReturn(l, ci.fd, 12345, &unpack_res_info);
    ListenerTask_AttemptDelivery_Expect(l, &unpack_res_info);

    ListenerIO_AttemptRecv(l, 1);

    TEST_ASSERT_EQUAL(RX_ERROR_READY_FOR_DELIVERY, unpack_res_info.u.expect.error);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.has_result);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.result.ok);
    TEST_ASSERT_EQUAL(12345, unpack_res_info.u.expect.result.u.success.seq_id);
    TEST_ASSERT_EQUAL(the_result, unpack_res_info.u.expect.result.u.success.msg);
}

void test_ListenerIO_AttemptRecv_should_handle_successful_socket_read_and_unpack_message_in_hold_state(void) {
    l->fds[0 + INCOMING_MSG_PIPE].fd = 5;
    l->fds[0 + INCOMING_MSG_PIPE].events = POLLIN;
    l->fds[0 + INCOMING_MSG_PIPE].revents = POLLIN;
    struct test_progress_info progress_info = {
        .to_read = 123,
    };
    connection_info ci = {
        .fd = 5,
        .type = BUS_SOCKET_PLAIN,
        .to_read_size = 123,
        .udata = &progress_info,
    };
    l->fd_info[0] = &ci;
    l->tracked_fds = 1;
    l->rx_info_max_used = 1;

    l->read_buf = calloc(256, sizeof(uint8_t));
    l->read_buf_size = 256;

    rx_info_t *info = &l->rx_info[0];
    info->state = RIS_HOLD;
    box->fd = 5;

    syscall_read_ExpectAndReturn(ci.fd, l->read_buf, ci.to_read_size, ci.to_read_size);

    rx_info_t unpack_res_info = {
        .state = RIS_HOLD,
    };
    ListenerHelper_FindInfoBySequenceID_ExpectAndReturn(l, ci.fd, 12345, &unpack_res_info);

    ListenerIO_AttemptRecv(l, 1);

    TEST_ASSERT_EQUAL(true, unpack_res_info.u.hold.has_result);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.hold.result.ok);
    TEST_ASSERT_EQUAL(12345, unpack_res_info.u.hold.result.u.success.seq_id);
    TEST_ASSERT_EQUAL(the_result, unpack_res_info.u.hold.result.u.success.msg);
}

void test_ListenerIO_AttemptRecv_should_handle_successful_socket_read_and_unpack_message_in_multiple_pieces(void) {
    l->fds[0 + INCOMING_MSG_PIPE].fd = 5;
    l->fds[0 + INCOMING_MSG_PIPE].events = POLLIN;
    l->fds[0 + INCOMING_MSG_PIPE].revents = POLLIN;
    struct test_progress_info progress_info = {
        .to_read = 123,
    };
    connection_info ci = {
        .fd = 5,
        .type = BUS_SOCKET_PLAIN,
        .to_read_size = 123,
        .udata = &progress_info,
    };
    l->fd_info[0] = &ci;
    l->tracked_fds = 1;
    l->rx_info_max_used = 1;

    l->read_buf = calloc(256, sizeof(uint8_t));
    l->read_buf_size = 256;

    rx_info_t *info = &l->rx_info[0];
    info->state = RIS_EXPECT;
    box->fd = 5;
    info->u.expect.box = box;

    syscall_read_ExpectAndReturn(ci.fd, l->read_buf, ci.to_read_size, ci.to_read_size - 1);
    syscall_read_ExpectAndReturn(ci.fd, l->read_buf, 1, 1);

    rx_info_t unpack_res_info = {
        .state = RIS_EXPECT,
    };
    ListenerHelper_FindInfoBySequenceID_ExpectAndReturn(l, ci.fd, 12345, &unpack_res_info);
    ListenerTask_AttemptDelivery_Expect(l, &unpack_res_info);

    ListenerIO_AttemptRecv(l, 1);

    TEST_ASSERT_EQUAL(RX_ERROR_READY_FOR_DELIVERY, unpack_res_info.u.expect.error);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.has_result);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.result.ok);
    TEST_ASSERT_EQUAL(12345, unpack_res_info.u.expect.result.u.success.seq_id);
    TEST_ASSERT_EQUAL(the_result, unpack_res_info.u.expect.result.u.success.msg);
}

void test_ListenerIO_AttemptRecv_should_handle_successful_socket_read_and_unpack_message_on_EINTR_followed_by_successful_read(void) {
    l->fds[0 + INCOMING_MSG_PIPE].fd = 5;
    l->fds[0 + INCOMING_MSG_PIPE].events = POLLIN;
    l->fds[0 + INCOMING_MSG_PIPE].revents = POLLIN;
    struct test_progress_info progress_info = {
        .to_read = 123,
    };
    connection_info ci = {
        .fd = 5,
        .type = BUS_SOCKET_PLAIN,
        .to_read_size = 123,
        .udata = &progress_info,
    };
    l->fd_info[0] = &ci;
    l->tracked_fds = 1;
    l->rx_info_max_used = 1;

    l->read_buf = calloc(256, sizeof(uint8_t));
    l->read_buf_size = 256;

    rx_info_t *info = &l->rx_info[0];
    info->state = RIS_EXPECT;
    box->fd = 5;
    info->u.expect.box = box;

    errno = EINTR;
    syscall_read_ExpectAndReturn(ci.fd, l->read_buf, ci.to_read_size, -1);
    Util_IsResumableIOError_ExpectAndReturn(EINTR, true);

    syscall_read_ExpectAndReturn(ci.fd, l->read_buf, ci.to_read_size, ci.to_read_size);

    rx_info_t unpack_res_info = {
        .state = RIS_EXPECT,
    };
    ListenerHelper_FindInfoBySequenceID_ExpectAndReturn(l, ci.fd, 12345, &unpack_res_info);
    ListenerTask_AttemptDelivery_Expect(l, &unpack_res_info);

    ListenerIO_AttemptRecv(l, 1);

    TEST_ASSERT_EQUAL(RX_ERROR_READY_FOR_DELIVERY, unpack_res_info.u.expect.error);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.has_result);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.result.ok);
    TEST_ASSERT_EQUAL(12345, unpack_res_info.u.expect.result.u.success.seq_id);
    TEST_ASSERT_EQUAL(the_result, unpack_res_info.u.expect.result.u.success.msg);
}

void test_ListenerIO_AttemptRecv_should_handle_socket_hangup_during_read(void) {
    l->fds[0 + INCOMING_MSG_PIPE].fd = 5;
    l->fds[0 + INCOMING_MSG_PIPE].events = POLLIN;
    l->fds[0 + INCOMING_MSG_PIPE].revents = POLLIN;
    struct test_progress_info progress_info = {
        .to_read = 123,
    };
    connection_info ci = {
        .fd = 5,
        .type = BUS_SOCKET_PLAIN,
        .to_read_size = 123,
        .udata = &progress_info,
    };
    l->fd_info[0] = &ci;
    l->tracked_fds = 1;
    l->inactive_fds = 0;
    l->rx_info_max_used = 1;

    l->read_buf = calloc(256, sizeof(uint8_t));
    l->read_buf_size = 256;

    rx_info_t *info = &l->rx_info[0];
    info->state = RIS_EXPECT;
    box->fd = 5;
    info->u.expect.box = box;

    syscall_read_ExpectAndReturn(ci.fd, l->read_buf, ci.to_read_size, -1);
    errno = ECONNRESET;
    Util_IsResumableIOError_ExpectAndReturn(errno, false);
    ListenerIO_AttemptRecv(l, 1);

    TEST_ASSERT_EQUAL(0, l->fds[0 + INCOMING_MSG_PIPE].events & POLLIN);
    TEST_ASSERT_EQUAL(1, l->inactive_fds);
    TEST_ASSERT_EQUAL(RX_ERROR_READ_FAILURE, ci.error);
}

void test_ListenerIO_AttemptRecv_should_handle_successful_socket_read_and_unpack_message_over_SSL(void) {
    l->fds[0 + INCOMING_MSG_PIPE].fd = 5;
    l->fds[0 + INCOMING_MSG_PIPE].events = POLLIN;
    l->fds[0 + INCOMING_MSG_PIPE].revents = POLLIN;
    struct test_progress_info progress_info = {
        .to_read = 123,
    };
    SSL fake_ssl;
    connection_info ci = {
        .fd = 5,
        .type = BUS_SOCKET_SSL,
        .to_read_size = 123,
        .udata = &progress_info,
        .ssl = &fake_ssl,
    };
    l->fd_info[0] = &ci;
    l->tracked_fds = 1;
    l->rx_info_max_used = 1;

    l->read_buf = calloc(256, sizeof(uint8_t));
    l->read_buf_size = 256;

    rx_info_t *info = &l->rx_info[0];
    info->state = RIS_EXPECT;
    box->fd = 5;
    info->u.expect.box = box;

    syscall_SSL_read_ExpectAndReturn(ci.ssl, l->read_buf, ci.to_read_size, ci.to_read_size);

    rx_info_t unpack_res_info = {
        .state = RIS_EXPECT,
    };
    ListenerHelper_FindInfoBySequenceID_ExpectAndReturn(l, ci.fd, 12345, &unpack_res_info);
    ListenerTask_AttemptDelivery_Expect(l, &unpack_res_info);

    ListenerIO_AttemptRecv(l, 1);

    TEST_ASSERT_EQUAL(RX_ERROR_READY_FOR_DELIVERY, unpack_res_info.u.expect.error);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.has_result);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.result.ok);
    TEST_ASSERT_EQUAL(12345, unpack_res_info.u.expect.result.u.success.seq_id);
    TEST_ASSERT_EQUAL(the_result, unpack_res_info.u.expect.result.u.success.msg);
}

void test_ListenerIO_AttemptRecv_should_handle_successful_socket_read_and_unpack_message_in_multiple_pieces_over_SSL(void) {
    l->fds[0 + INCOMING_MSG_PIPE].fd = 5;
    l->fds[0 + INCOMING_MSG_PIPE].events = POLLIN;
    l->fds[0 + INCOMING_MSG_PIPE].revents = POLLIN;
    struct test_progress_info progress_info = {
        .to_read = 123,
    };
    SSL fake_ssl;
    connection_info ci = {
        .fd = 5,
        .type = BUS_SOCKET_SSL,
        .to_read_size = 123,
        .udata = &progress_info,
        .ssl = &fake_ssl,
    };
    l->fd_info[0] = &ci;
    l->tracked_fds = 1;
    l->rx_info_max_used = 1;

    l->read_buf = calloc(256, sizeof(uint8_t));
    l->read_buf_size = 256;

    rx_info_t *info = &l->rx_info[0];
    info->state = RIS_EXPECT;
    box->fd = 5;
    info->u.expect.box = box;

    syscall_SSL_read_ExpectAndReturn(ci.ssl, l->read_buf, ci.to_read_size, ci.to_read_size - 1);

    syscall_SSL_read_ExpectAndReturn(ci.ssl, l->read_buf, 1, 1);

    rx_info_t unpack_res_info = {
        .state = RIS_EXPECT,
    };
    ListenerHelper_FindInfoBySequenceID_ExpectAndReturn(l, ci.fd, 12345, &unpack_res_info);
    ListenerTask_AttemptDelivery_Expect(l, &unpack_res_info);

    ListenerIO_AttemptRecv(l, 1);

    TEST_ASSERT_EQUAL(RX_ERROR_READY_FOR_DELIVERY, unpack_res_info.u.expect.error);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.has_result);
    TEST_ASSERT_EQUAL(true, unpack_res_info.u.expect.result.ok);
    TEST_ASSERT_EQUAL(12345, unpack_res_info.u.expect.result.u.success.seq_id);
    TEST_ASSERT_EQUAL(the_result, unpack_res_info.u.expect.result.u.success.msg);
}