Esempio n. 1
0
int mcs_write_command_and_free(MCSPacket *pkt, int fd)
{
    int ret = mcs_write_command(pkt, fd);
    mcs_free(pkt);
    return ret;
}
Esempio n. 2
0
void run_test(enum MCSType type)
{
    const MCSCommandOptionsCommon *cmd;
    MCSPacket *pkt_wr, *pkt_rd;
    unsigned char *args;
    unsigned char data[] = "Hello World!";
    int i, j;
    bool error = false;

    for(i = 0; i < mcs_command_list_size[type]; ++i) {
        start_test();

        if(type == MCS_TYPE_MESSAGE) {
            cmd = &mcs_command_message_list[i].cmd;
        } else if(type == MCS_TYPE_STATE) {
            cmd = &mcs_command_state_list[i].cmd;
        } else if(type == MCS_TYPE_PAYLOAD) {
            cmd = &mcs_command_payload_list[i].cmd;
        } else {
            cmd = NULL;
            abs_test_printf("mcs_command_list_size is incorrect for type %d\n",
                                                                        type);
            abs_test_fail_and_exit("MCS Type");
        }

        abs_test_printf("Testing command %s\n", cmd->name);

        if(cmd->nargs == 0) {
            args = NULL;
        } else {
            args = malloc(cmd->nargs);
            for(j = 0; j < cmd->nargs; ++j) {
                args[j] = j;
            }
        }

        if(type == MCS_TYPE_MESSAGE &&
                    mcs_command_message_list[i].destination != NULL &&
                    mcs_command_message_list[i].destination[0] == '@') {
            if(cmd->raw_data) {
                pkt_wr = mcs_create_packet_with_dest(
                        (MCSCommand)((int)(type << 16) | i), "test_dest",
                        cmd->nargs, args, strlen((char *)data), data);
            } else {
                pkt_wr = mcs_create_packet_with_dest(
                        (MCSCommand)((int)(type << 16) | i), "test_dest",
                        cmd->nargs, args, 0, NULL);
            }
        } else {
            if(cmd->raw_data) {
                pkt_wr = mcs_create_packet(
                                (MCSCommand)((int)(type << 16) | i),
                                cmd->nargs, args, strlen((char *)data), data);
            } else {
                pkt_wr = mcs_create_packet(
                                (MCSCommand)((int)(type << 16) | i),
                                cmd->nargs, args, 0, NULL);
            }
        }

        if(cmd->nargs != 0) {
            free(args);
        }

        if(pkt_wr != NULL) {
            if(mcs_write_command(pkt_wr, pipe_fd[1]) < 0) {
                abs_test_printf("mcs_write_command returned a wrong value\n");
                error = true;
            } else {
                pkt_rd = mcs_read_command(pipe_fd[0], pipe_fd[1]);
                error = !mcs_cmp(pkt_wr, pkt_rd);
                mcs_free(pkt_rd);
            }
        } else {
            error = true;
        }

        mcs_free(pkt_wr);
        end_test();

        if(error) {
            abs_test_add_result(FAIL, cmd->name);
        } else {
            abs_test_add_result(PASS, cmd->name);
        }
    }

}