Redis-Fast

 view release on metacpan or  search on metacpan

deps/hiredis/test.c  view on Meta::CPAN

        redisPollTick(c, 0.1);
    status = redisAsyncCommand(c, commandCallback, NULL, "PING");
    assert(status == REDIS_OK);
    while(astest.ac)
        redisPollTick(c, 0.1);
    test_cond(astest.pongs == 1);

    /* Test a ping/pong after connection that didn't time out.
     * see https://github.com/redis/hiredis/issues/945
     */
    if (config.type == CONN_TCP || config.type == CONN_SSL) {
        test("Async PING/PONG after connect timeout: ");
        config.connect_timeout.tv_usec = 10000; /* 10ms  */
        c = do_aconnect(config, ASTEST_PINGPONG_TIMEOUT);
        while(astest.connected == 0)
            redisPollTick(c, 0.1);
        /* sleep 0.1 s, allowing old timeout to arrive */
        millisleep(10);
        status = redisAsyncCommand(c, commandCallback, NULL, "PING");
        assert(status == REDIS_OK);
        while(astest.ac)
            redisPollTick(c, 0.1);
        test_cond(astest.pongs == 2);
        config = defaultconfig;
    }

    /* Test disconnect from an on_connect callback
     * see https://github.com/redis/hiredis/issues/931
     */
    test("Disconnect from onConnected callback (Issue #931): ");
    c = do_aconnect(config, ASTEST_ISSUE_931);
    while(astest.disconnects == 0)
        redisPollTick(c, 0.1);
    assert(astest.connected == 0);
    assert(astest.connects == 1);
    test_cond(astest.disconnects == 1);

    /* Test ping/pong from an on_connect callback
     * see https://github.com/redis/hiredis/issues/931
     */
    test("Ping/Pong from onConnected callback (Issue #931): ");
    c = do_aconnect(config, ASTEST_ISSUE_931_PING);
    /* connect callback issues ping, response callback destroys context */
    while(astest.ac)
        redisPollTick(c, 0.1);
    assert(astest.connected == 0);
    assert(astest.connects == 1);
    assert(astest.disconnects == 1);
    test_cond(astest.pongs == 1);
}
/* End of Async polling_adapter driven tests */

#ifdef HIREDIS_TEST_SSL
/* Connect to the SSL port with a context bound to an expected peer name.
 * Returns the (possibly errored) context; the caller inspects c->err. */
static redisContext *do_ssl_connect_with_verify_name(struct config config, const char *verify_name) {
    redisSSLContextError ssl_error = REDIS_SSL_CTX_NONE;
    redisSSLOptions options = {
        .cacert_filename = config.ssl.ca_cert,
        .cert_filename = config.ssl.cert,
        .private_key_filename = config.ssl.key,
        .verify_mode = REDIS_SSL_VERIFY_PEER,
        .verify_name = verify_name,
    };

    redisSSLContext *ssl_ctx = redisCreateSSLContextWithOptions(&options, &ssl_error);
    assert(ssl_ctx != NULL && ssl_error == REDIS_SSL_CTX_NONE);

    redisContext *c = redisConnect(config.ssl.host, config.ssl.port);
    assert(c != NULL && c->err == 0);
    redisInitiateSSLWithContext(c, ssl_ctx);

    redisFreeSSLContext(ssl_ctx);
    return c;
}

/* Peer-name verification (redisSSLOptions.verify_name) tests. The server
 * certificate generated by test.sh carries subjectAltName entries for
 * config.ssl.verify_name (a DNS name) and config.ssl.host (an IP address), so
 * a context bound to either must connect, while a context bound to a name the
 * certificate does not carry must fail its handshake even though the
 * certificate chains to the trusted CA. The mismatch cases are the
 * impersonation scenario this option exists to close: without verify_name, ANY
 * certificate from the trusted CA is accepted regardless of the identity it
 * carries.
 *
 * Both polarities are covered for each kind of name, because DNS names and IP
 * addresses are matched by different OpenSSL machinery (X509_check_host()
 * against DNS SANs versus X509_check_ip() against IP SANs). The positive IP
 * case proves the IP path is taken at all -- an IP handed to
 * X509_VERIFY_PARAM_set1_host() would be compared against DNS entries and
 * fail -- while the negative IP case proves that path actually compares the
 * address rather than merely accepting any certificate that happens to carry
 * an IP SAN. */
static void test_ssl_verify_name(struct config config) {
    redisContext *c;
    redisReply *reply;

    test("SSL verify_name accepts a certificate carrying the expected DNS name: ");
    c = do_ssl_connect_with_verify_name(config, config.ssl.verify_name);
    reply = redisCommand(c, "PING");
    test_cond(c->err == 0 && reply != NULL && reply->type == REDIS_REPLY_STATUS &&
              strcasecmp(reply->str, "pong") == 0);
    freeReplyObject(reply);
    redisFree(c);

    test("SSL verify_name accepts a certificate carrying the expected IP address: ");
    c = do_ssl_connect_with_verify_name(config, config.ssl.host);
    reply = redisCommand(c, "PING");
    test_cond(c->err == 0 && reply != NULL && reply->type == REDIS_REPLY_STATUS &&
              strcasecmp(reply->str, "pong") == 0);
    freeReplyObject(reply);
    redisFree(c);

    test("SSL verify_name rejects a trusted certificate lacking the expected name: ");
    c = do_ssl_connect_with_verify_name(config, "wrong.example.com");
    test_cond(c->err != 0);
    redisFree(c);

    /* A well-formed address that the certificate does not carry: the IP SAN
     * present in the certificate must not satisfy a different expected IP. */
    test("SSL verify_name rejects a trusted certificate lacking the expected IP address: ");
    c = do_ssl_connect_with_verify_name(config, "10.99.99.99");
    test_cond(c->err != 0);
    redisFree(c);

    /* An empty name is not a usable identity, and OpenSSL reads it as "clear
     * the expected hosts" while reporting success. Context creation must reject
     * it rather than hand back a context that silently validates only the CA
     * chain; callers skip verification by passing NULL. */
    test("SSL verify_name rejects an empty expected name: ");
    redisSSLContextError ssl_error = REDIS_SSL_CTX_NONE;
    redisSSLOptions options = {
        .cacert_filename = config.ssl.ca_cert,
        .cert_filename = config.ssl.cert,
        .private_key_filename = config.ssl.key,
        .verify_mode = REDIS_SSL_VERIFY_PEER,
        .verify_name = "",
    };
    redisSSLContext *ssl_ctx = redisCreateSSLContextWithOptions(&options, &ssl_error);
    test_cond(ssl_ctx == NULL && ssl_error == REDIS_SSL_CTX_VERIFY_NAME_FAILED);
    redisFreeSSLContext(ssl_ctx);

    /* A name cannot be enforced while peer verification is disabled: OpenSSL
     * completes the handshake regardless of the verification result, so a
     * wrong-name certificate would be accepted. REDIS_SSL_VERIFY_NONE is 0, so
     * this is also what a zero-initialized redisSSLOptions asks for. */
    test("SSL verify_name is rejected when peer verification is disabled: ");
    ssl_error = REDIS_SSL_CTX_NONE;
    options.verify_mode = REDIS_SSL_VERIFY_NONE;
    options.verify_name = config.ssl.verify_name;
    ssl_ctx = redisCreateSSLContextWithOptions(&options, &ssl_error);
    test_cond(ssl_ctx == NULL && ssl_error == REDIS_SSL_CTX_VERIFY_NAME_FAILED);
    redisFreeSSLContext(ssl_ctx);
}
#endif

int main(int argc, char **argv) {
    struct config cfg = {
        .tcp = {
            .host = "127.0.0.1",
            .port = 6379
        },
        .unix_sock = {
            .path = "/tmp/redis.sock"
        }
    };
    int throughput = 1;
    int test_inherit_fd = 1;
    int skips_as_fails = 0;
    int test_unix_socket;

    /* Parse command line options. */
    argv++; argc--;
    while (argc) {
        if (argc >= 2 && !strcmp(argv[0],"-h")) {
            argv++; argc--;
            cfg.tcp.host = argv[0];
        } else if (argc >= 2 && !strcmp(argv[0],"-p")) {
            argv++; argc--;
            cfg.tcp.port = atoi(argv[0]);
        } else if (argc >= 2 && !strcmp(argv[0],"-s")) {
            argv++; argc--;
            cfg.unix_sock.path = argv[0];
        } else if (argc >= 1 && !strcmp(argv[0],"--skip-throughput")) {
            throughput = 0;
        } else if (argc >= 1 && !strcmp(argv[0],"--skip-inherit-fd")) {
            test_inherit_fd = 0;
        } else if (argc >= 1 && !strcmp(argv[0],"--skips-as-fails")) {
            skips_as_fails = 1;
#ifdef HIREDIS_TEST_SSL
        } else if (argc >= 2 && !strcmp(argv[0],"--ssl-port")) {
            argv++; argc--;
            cfg.ssl.port = atoi(argv[0]);
        } else if (argc >= 2 && !strcmp(argv[0],"--ssl-host")) {
            argv++; argc--;



( run in 1.605 second using v1.01-cache-2.11-cpan-8379b9b8ec2 )