示例#1
0
void test_two_failed_tests() {
    suite_t* suite = create_suite();
    silence_suite(suite);

    add_test_to_suite(suite, fails);
    add_test_to_suite(suite, also_fails);

    run_suite(suite);

    expect(get_failure_count(suite) == 2);

    destroy_suite(suite);
}
示例#2
0
void test_expect_unexpected_fails() {
    suite_t* suite = create_suite();
    silence_suite(suite);

    add_test_to_suite(suite, expect_unexpected);

    run_suite(suite);

    expect(get_failure_count(suite) == 1);

    destroy_suite(suite);
}
示例#3
0
/**
 * Initialize the tests for the client.
 * @param ctl Client connection
 * @param options Test options
 * @param buff The string to hold the tests
 * @param buff_strlen The length of buff
 * @return integer the test codes OR'd together on success
 *          0 or less is an error.
 *          Error codes:
 *                      -1 message reading in error
 *			-2 Invalid test request
 *			-3 Invalid test suite request
 *			-4 client timed out
 */
int initialize_tests(Connection *ctl, TestOptions *options, char *buff,
                     size_t buff_strlen) {
    char msgValue[CS_VERSION_LENGTH_MAX + 1] = {'\0'};
    unsigned char useropt = 0;
    int msgType;
    int msgLen = CS_VERSION_LENGTH_MAX + 1;
    int first = 1;
    char *invalid_test_suite = "Invalid test suite request.";
    char *invalid_test = "Invalid test request.";
    char *invalid_login_msg = "Invalid login message.";
    char *jsonMsgValue;

    // char remhostarr[256], protologlocalarr[256];
    // char *remhost_ptr = get_remotehost();

    assert(ctl->socket != -1);
    assert(options);

    memset(options->client_version, 0, sizeof(options->client_version));

    if (ctl->ssl != NULL) {
        options->connection_flags |= TLS_SUPPORT;
    }
    // read the test suite request, if we get what looks like a websocket HTTP
    // request then set up the websocket, and then read the test suite request.
    if (recv_msg_plus_websocket(ctl, options, &msgType, msgValue, &msgLen)) {
        send_msg_any(ctl, MSG_ERROR, invalid_test_suite,
                     strlen(invalid_test_suite));
        log_println(2, "recv_msg_plus_websocket failed");
        return (-1);
    }
    if (msgLen == -1) {
        snprintf(buff, buff_strlen, "Client timeout");
        log_println(2, "Client timed out");
        return (-4);
    }

    if (!(options->connection_flags & WEBSOCKET_SUPPORT)) {
        if (kick_off_old_clients(ctl) != KICK_SUCCESS) return -1;
    }
    /*
     * Expecting a MSG_LOGIN or MSG_EXTENDED_LOGIN with payload byte indicating
     * tests to be run and potentially a US-ASCII string indicating the version
     * number.
     * Three cases:
     * 1: MSG_LOGIN: Check that msgLen is 1
     * 2: MSG_EXTENDED_LOGIN: Check that msgLen is >= 1 and <= the maximum
     *    length of the client/server version string (plus 1 to account for the
     *    initial useropt and then copy the client version into client_version.
     * 3: Neither
     *
     * In case (1) or (2) we copy the 0th byte from the msgValue into useropt so
     * we'll do that in the fallthrough.  In cases (1), (2), and (4) we should
     * make sure to send the code which kicks off old clients.  Old clients that
     * tickle case (3) will get kicked off when they fail the websocket
     * handshake.
     */
    if (msgType == MSG_LOGIN) { /* Case 1 */
        options->connection_flags &= ~JSON_SUPPORT;
        if (msgLen != 1) {
            send_msg_any(ctl, MSG_ERROR, invalid_test, strlen(invalid_test));
            return (-2);
        }
        useropt = msgValue[0];
    } else if (msgType == MSG_EXTENDED_LOGIN) { /* Case 2 */
        msgValue[msgLen] = '\0';  // Null-terminate the received string
        options->connection_flags |= JSON_SUPPORT;
        jsonMsgValue = json_read_map_value(msgValue, "tests");
        if (jsonMsgValue == NULL) {
            send_json_message_any(ctl, MSG_ERROR, invalid_test, strlen(invalid_test),
                                  options->connection_flags, JSON_SINGLE_VALUE);
            return (-2);
        }
        useropt = atoi(jsonMsgValue);
        free(jsonMsgValue);
        jsonMsgValue = json_read_map_value(msgValue, DEFAULT_KEY);
        if (jsonMsgValue == NULL) {
            send_json_message_any(ctl, MSG_ERROR, invalid_test, strlen(invalid_test),
                                  options->connection_flags, JSON_SINGLE_VALUE);
            return (-2);
        }
        strlcpy(msgValue, jsonMsgValue, sizeof(msgValue));
        msgLen = strlen(jsonMsgValue);
        free(jsonMsgValue);
        if (msgLen >= 1 && msgLen <= (CS_VERSION_LENGTH_MAX + 1)) {
            memcpy(options->client_version, msgValue + 1, msgLen - 1);
            log_println(1, "Client version: %s-\n", options->client_version);
        } else {
            send_json_message_any(ctl, MSG_ERROR, invalid_test, strlen(invalid_test),
                                  options->connection_flags, JSON_SINGLE_VALUE);
            return (-2);
        }
    } else { /* Case 3 */
        send_msg_any(ctl, MSG_ERROR, invalid_login_msg, strlen(invalid_login_msg));
        return (-2);
    }

    // client connect received correctly. Logging activity
    // log that client connected, and create log file
    log_println(1,
                "Client connect received from :IP %s to some server on socket %d",
                get_remotehostaddress(), ctl->socket);

    // set_protologfile(get_remotehost(), protologlocalarr);

    if (!(useropt
            & (TEST_MID | TEST_C2S | TEST_S2C | TEST_SFW | TEST_STATUS | TEST_META | TEST_C2S_EXT | TEST_S2C_EXT))) {
        // message received does not indicate a valid test!
        send_json_message_any(ctl, MSG_ERROR, invalid_test_suite,
                              strlen(invalid_test_suite), options->connection_flags,
                              JSON_SINGLE_VALUE);
        return (-3);
    }
    // construct test suite request based on user options received
    // Can't connect from server to client using browser websockets, so we can't
    // support both websockets and TEST_MID and TEST_SFW
    if (useropt & TEST_MID && !(options->connection_flags & WEBSOCKET_SUPPORT)) {
        add_test_to_suite(&first, buff, buff_strlen, TEST_MID);
    }
    if (useropt & TEST_SFW && !(options->connection_flags & WEBSOCKET_SUPPORT)) {
        add_test_to_suite(&first, buff, buff_strlen, TEST_SFW);
    }
    if (useropt & TEST_C2S_EXT) {
        add_test_to_suite(&first, buff, buff_strlen, TEST_C2S_EXT);
    } else if (useropt & TEST_C2S) {
        add_test_to_suite(&first, buff, buff_strlen, TEST_C2S);
    }
    if (useropt & TEST_S2C_EXT) {
        add_test_to_suite(&first, buff, buff_strlen, TEST_S2C_EXT);
    } else if (useropt & TEST_S2C) {
        add_test_to_suite(&first, buff, buff_strlen, TEST_S2C);
    }
    if (useropt & TEST_META) {
        add_test_to_suite(&first, buff, buff_strlen, TEST_META);
    }
    // Meta data that should be unconditionally saved is set here.
    addAdditionalMetaBoolEntry("websockets", (options->connection_flags & WEBSOCKET_SUPPORT));
    addAdditionalMetaBoolEntry("tls", (options->connection_flags & TLS_SUPPORT));
    return useropt;
}