static void test_fail_read_flush(CuTest *tc) { apr_file_t *f; const char *fname = "data/testflush.dat"; apr_status_t rv; char buf[2]; apr_file_remove(fname, p); apr_assert_success(tc, "open test file", apr_file_open(&f, fname, APR_CREATE|APR_READ|APR_BUFFERED, APR_UREAD|APR_UWRITE, p)); /* this write should be buffered. */ apr_assert_success(tc, "buffered write should succeed", apr_file_puts("hello", f)); /* Now, trying a read should fail since the write must be flushed, * and should fail with something other than EOF since the file is * opened read-only. */ rv = apr_file_read_full(f, buf, 2, NULL); CuAssert(tc, "read should flush buffered write and fail", rv != APR_SUCCESS && rv != APR_EOF); /* Likewise for gets */ rv = apr_file_gets(buf, 2, f); CuAssert(tc, "gets should flush buffered write and fail", rv != APR_SUCCESS && rv != APR_EOF); apr_file_close(f); apr_file_remove(fname, p); }
static void test_seek(CuTest *tc) { apr_status_t rv; apr_off_t offset = 5; apr_size_t nbytes = 256; char *str = apr_pcalloc(p, nbytes + 1); apr_file_t *filetest = NULL; rv = apr_file_open(&filetest, FILENAME, APR_READ, APR_UREAD | APR_UWRITE | APR_GREAD, p); apr_assert_success(tc, "Open test file " FILENAME, rv); rv = apr_file_read(filetest, str, &nbytes); CuAssertIntEquals(tc, APR_SUCCESS, rv); CuAssertIntEquals(tc, strlen(TESTSTR), nbytes); CuAssertStrEquals(tc, TESTSTR, str); memset(str, 0, nbytes + 1); rv = apr_file_seek(filetest, SEEK_SET, &offset); CuAssertIntEquals(tc, APR_SUCCESS, rv); rv = apr_file_read(filetest, str, &nbytes); CuAssertIntEquals(tc, APR_SUCCESS, rv); CuAssertIntEquals(tc, strlen(TESTSTR) - 5, nbytes); CuAssertStrEquals(tc, TESTSTR + 5, str); apr_file_close(filetest); /* Test for regression of sign error bug with SEEK_END and buffered files. */ rv = apr_file_open(&filetest, FILENAME, APR_READ | APR_BUFFERED, APR_UREAD | APR_UWRITE | APR_GREAD, p); apr_assert_success(tc, "Open test file " FILENAME, rv); offset = -5; rv = apr_file_seek(filetest, SEEK_END, &offset); CuAssertIntEquals(tc, APR_SUCCESS, rv); CuAssertIntEquals(tc, strlen(TESTSTR) - 5, offset); memset(str, 0, nbytes + 1); nbytes = 256; rv = apr_file_read(filetest, str, &nbytes); CuAssertIntEquals(tc, APR_SUCCESS, rv); CuAssertIntEquals(tc, 5, nbytes); CuAssertStrEquals(tc, TESTSTR + strlen(TESTSTR) - 5, str); apr_file_close(filetest); }
static void test_partition(abts_case *tc, void *data) { apr_bucket_alloc_t *ba = apr_bucket_alloc_create(p); apr_bucket_brigade *bb = apr_brigade_create(p, ba); apr_bucket *e; e = apr_bucket_immortal_create(hello, strlen(hello), ba); APR_BRIGADE_INSERT_HEAD(bb, e); apr_assert_success(tc, "partition brigade", apr_brigade_partition(bb, 5, &e)); test_bucket_content(tc, APR_BRIGADE_FIRST(bb), "hello", 5); test_bucket_content(tc, APR_BRIGADE_LAST(bb), ", world", 7); ABTS_ASSERT(tc, "partition returns APR_INCOMPLETE", apr_brigade_partition(bb, 8192, &e)); ABTS_ASSERT(tc, "APR_INCOMPLETE partition returned sentinel", e == APR_BRIGADE_SENTINEL(bb)); apr_brigade_destroy(bb); apr_bucket_alloc_destroy(ba); }
static void test_fail_write_flush(CuTest *tc) { apr_file_t *f; const char *fname = "data/testflush.dat"; apr_status_t rv; char buf[APR_BUFFERSIZE]; int n; apr_file_remove(fname, p); apr_assert_success(tc, "open test file", apr_file_open(&f, fname, APR_CREATE|APR_READ|APR_BUFFERED, APR_UREAD|APR_UWRITE, p)); memset(buf, 'A', sizeof buf); /* Try three writes. One of these should fail when it exceeds the * internal buffer and actually tries to write to the file, which * was opened read-only and hence should be unwritable. */ for (n = 0, rv = APR_SUCCESS; n < 4 && rv == APR_SUCCESS; n++) { apr_size_t bytes = sizeof buf; rv = apr_file_write(f, buf, &bytes); } CuAssert(tc, "failed to write to read-only buffered fd", rv != APR_SUCCESS); apr_file_close(f); apr_file_remove(fname, p); }
static void test_delenv(CuTest *tc) { char *value; apr_status_t rv; if (!have_env_set) { CuNotImpl(tc, "apr_env_set (skip test for apr_env_delete)"); return; } rv = apr_env_delete(TEST_ENVVAR_NAME, p); have_env_del = (rv != APR_ENOTIMPL); if (!have_env_del) { CuNotImpl(tc, "apr_env_delete"); return; } apr_assert_success(tc, "delete environment variable", rv); if (!have_env_get) { CuNotImpl(tc, "apr_env_get (skip sanity check for apr_env_delete)"); return; } rv = apr_env_get(&value, TEST_ENVVAR_NAME, p); CuAssertIntEquals(tc, APR_ENOENT, rv); }
static void test_glibc_shapass(abts_case *tc, void *data) { int i = 0; while (glibc_sha_pws[i].password) { apr_assert_success(tc, "check for valid glibc crypt-sha password", apr_password_validate(glibc_sha_pws[i].password, glibc_sha_pws[i].hash)); i++; } }
static void test_crypt(abts_case *tc, void *data) { int i; for (i = 0; i < num_passwords; i++) { apr_assert_success(tc, "check for valid password", apr_password_validate(passwords[i].password, passwords[i].hash)); } }
static void test_shapass(abts_case *tc, void *data) { const char *pass = "******"; char hash[100]; apr_sha1_base64(pass, strlen(pass), hash); apr_assert_success(tc, "SHA1 password validated", apr_password_validate(pass, hash)); }
static void test_md5pass(abts_case *tc, void *data) { const char *pass = "******", *salt = "sardine"; char hash[100]; apr_md5_encode(pass, salt, hash, sizeof hash); apr_assert_success(tc, "MD5 password validated", apr_password_validate(pass, hash)); }
static void test_bcryptpass(abts_case *tc, void *data) { const char *pass = "******"; const char *pass2 = "hellojed2"; unsigned char salt[] = "sardine_sardine"; char hash[100]; const char *hash2 = "$2a$08$qipUJiI9fySUN38hcbz.lucXvAmtgowKOWYtB9y3CXyl6lTknruou"; const char *pass3 = "foobar"; apr_assert_success(tc, "bcrypt encode password", apr_bcrypt_encode(pass, 5, salt, sizeof(salt), hash, sizeof(hash))); apr_assert_success(tc, "bcrypt password validated", apr_password_validate(pass, hash)); APR_ASSERT_FAILURE(tc, "wrong bcrypt password should not validate", apr_password_validate(pass2, hash)); apr_assert_success(tc, "bcrypt password validated", apr_password_validate(pass3, hash2)); }
static void test_setenv(CuTest *tc) { apr_status_t rv; rv = apr_env_set(TEST_ENVVAR_NAME, TEST_ENVVAR_VALUE, p); have_env_set = (rv != APR_ENOTIMPL); if (!have_env_set) { CuNotImpl(tc, "apr_env_set"); return; } apr_assert_success(tc, "set environment variable", rv); }
static void test_md5pass(abts_case *tc, void *data) { const char *pass = "******", *salt = "sardine"; const char *pass2 = "hellojed2"; char hash[100]; apr_md5_encode(pass, salt, hash, sizeof hash); apr_assert_success(tc, "MD5 password validated", apr_password_validate(pass, hash)); APR_ASSERT_FAILURE(tc, "wrong MD5 password should not validate", apr_password_validate(pass2, hash)); }
static void test_bwrite(abts_case *tc, void *data) { apr_bucket_alloc_t *ba = apr_bucket_alloc_create(p); apr_bucket_brigade *bb = apr_brigade_create(p, ba); apr_off_t length; int n; for (n = 0; n < COUNT; n++) { apr_assert_success(tc, "brigade_write", apr_brigade_write(bb, NULL, NULL, THESTR, sizeof THESTR)); } apr_assert_success(tc, "determine brigade length", apr_brigade_length(bb, 1, &length)); ABTS_ASSERT(tc, "brigade has correct length", length == (COUNT * sizeof THESTR)); apr_brigade_destroy(bb); apr_bucket_alloc_destroy(ba); }
static void test_shapass(abts_case *tc, void *data) { const char *pass = "******"; const char *pass2 = "hellojed2"; char hash[100]; apr_sha1_base64(pass, strlen(pass), hash); apr_assert_success(tc, "SHA1 password validated", apr_password_validate(pass, hash)); APR_ASSERT_FAILURE(tc, "wrong SHA1 password should not validate", apr_password_validate(pass2, hash)); }
/* Test that bucket E has content EDATA of length ELEN. */ static void test_bucket_content(abts_case *tc, apr_bucket *e, const char *edata, apr_size_t elen) { const char *adata; apr_size_t alen; apr_assert_success(tc, "read from bucket", apr_bucket_read(e, &adata, &alen, APR_BLOCK_READ)); ABTS_ASSERT(tc, "read expected length", alen == elen); ABTS_STR_NEQUAL(tc, edata, adata, elen); }
static void set_debug(CuTest *tc) { apr_status_t rv1, rv2; apr_int32_t ck; /* On some platforms APR_SO_DEBUG can only be set as root; just test * for get/set consistency of this option. */ rv1 = apr_socket_opt_set(sock, APR_SO_DEBUG, 1); rv2 = apr_socket_opt_get(sock, APR_SO_DEBUG, &ck); apr_assert_success(tc, "get SO_DEBUG option", rv2); if (APR_STATUS_IS_SUCCESS(rv1)) { CuAssertIntEquals(tc, 1, ck); } else { CuAssertIntEquals(tc, 0, ck); } }
/* 0.9.4 and earlier rejected valid dates in 2038 */ static void test_2038(CuTest *tc) { apr_time_exp_t xt; apr_time_t t; /* 2038-01-19T03:14:07.000000Z */ xt.tm_year = 138; xt.tm_mon = 0; xt.tm_mday = 19; xt.tm_hour = 3; xt.tm_min = 14; xt.tm_sec = 7; apr_assert_success(tc, "explode January 19th, 2038", apr_time_exp_get(&t, &xt)); }
static void test_exclusive(CuTest *tc, const char *lockname) { apr_proc_t *child[CHILDREN]; apr_status_t rv; int n; rv = apr_proc_mutex_create(&proc_lock, lockname, APR_LOCK_DEFAULT, p); apr_assert_success(tc, "create the mutex", rv); for (n = 0; n < CHILDREN; n++) make_child(tc, &child[n], p); for (n = 0; n < CHILDREN; n++) await_child(tc, child[n]); CuAssert(tc, "Locks don't appear to work", *x == MAX_COUNTER); }
static void test_filename(CuTest *tc) { const char *str; apr_status_t rv; apr_file_t *filetest = NULL; rv = apr_file_open(&filetest, FILENAME, APR_READ, APR_UREAD | APR_UWRITE | APR_GREAD, p); apr_assert_success(tc, "Opening test file " FILENAME, rv); rv = apr_file_name_get(&str, filetest); CuAssertIntEquals(tc, APR_SUCCESS, rv); CuAssertStrEquals(tc, FILENAME, str); apr_file_close(filetest); }
static void test_fileclose(CuTest *tc) { char str; apr_status_t rv; apr_size_t one = 1; apr_file_t *filetest = NULL; rv = apr_file_open(&filetest, FILENAME, APR_READ, APR_UREAD | APR_UWRITE | APR_GREAD, p); apr_assert_success(tc, "Opening test file " FILENAME, rv); rv = apr_file_close(filetest); CuAssertIntEquals(tc, APR_SUCCESS, rv); /* We just closed the file, so this should fail */ rv = apr_file_read(filetest, &str, &one); CuAssertIntEquals(tc, 1, APR_STATUS_IS_EBADF(rv)); }
/* tests that 'bb' flattens to string 'expect'. */ static void flatten_match(abts_case *tc, const char *ctx, apr_bucket_brigade *bb, const char *expect) { apr_size_t elen = strlen(expect); char *buf = malloc(elen); apr_size_t len = elen; char msg[200]; sprintf(msg, "%s: flatten brigade", ctx); apr_assert_success(tc, msg, apr_brigade_flatten(bb, buf, &len)); sprintf(msg, "%s: length match (%ld not %ld)", ctx, (long)len, (long)elen); ABTS_ASSERT(tc, msg, len == elen); sprintf(msg, "%s: result match", msg); ABTS_STR_NEQUAL(tc, expect, buf, len); free(buf); }
static void test_read(CuTest *tc) { apr_status_t rv; apr_size_t nbytes = 256; char *str = apr_pcalloc(p, nbytes + 1); apr_file_t *filetest = NULL; rv = apr_file_open(&filetest, FILENAME, APR_READ, APR_UREAD | APR_UWRITE | APR_GREAD, p); apr_assert_success(tc, "Opening test file " FILENAME, rv); rv = apr_file_read(filetest, str, &nbytes); CuAssertIntEquals(tc, APR_SUCCESS, rv); CuAssertIntEquals(tc, strlen(TESTSTR), nbytes); CuAssertStrEquals(tc, TESTSTR, str); apr_file_close(filetest); }
static void test_getenv(CuTest *tc) { char *value; apr_status_t rv; if (!have_env_set) { CuNotImpl(tc, "apr_env_set (skip test for apr_env_get)"); return; } rv = apr_env_get(&value, TEST_ENVVAR_NAME, p); have_env_get = (rv != APR_ENOTIMPL); if (!have_env_get) { CuNotImpl(tc, "apr_env_get"); return; } apr_assert_success(tc, "get environment variable", rv); CuAssertStrEquals(tc, TEST_ENVVAR_VALUE, value); }
static void test_splitline(abts_case *tc, void *data) { apr_bucket_alloc_t *ba = apr_bucket_alloc_create(p); apr_bucket_brigade *bin, *bout; bin = make_simple_brigade(ba, "blah blah blah-", "end of line.\nfoo foo foo"); bout = apr_brigade_create(p, ba); apr_assert_success(tc, "split line", apr_brigade_split_line(bout, bin, APR_BLOCK_READ, 100)); flatten_match(tc, "split line", bout, "blah blah blah-end of line.\n"); flatten_match(tc, "remainder", bin, "foo foo foo"); apr_brigade_destroy(bout); apr_brigade_destroy(bin); apr_bucket_alloc_destroy(ba); }
static void proc_mutex(CuTest *tc) { #if APR_HAS_FORK apr_status_t rv; const char *shmname = "tpm.shm"; apr_shm_t *shm; /* Use anonymous shm if available. */ rv = apr_shm_create(&shm, sizeof(int), NULL, p); if (rv == APR_ENOTIMPL) { apr_file_remove(shmname, p); rv = apr_shm_create(&shm, sizeof(int), shmname, p); } apr_assert_success(tc, "create shm segment", rv); x = apr_shm_baseaddr_get(shm); test_exclusive(tc, NULL); #else CuNotImpl(tc, "APR lacks fork() support"); #endif }
/** http://issues.apache.org/bugzilla/show_bug.cgi?id=40764 */ static void test_emptyenv(CuTest *tc) { char *value; apr_status_t rv; if (!(have_env_set && have_env_get)) { CuNotImpl(tc, "apr_env_set (skip test_emptyenv)"); return; } /** Set empty string and test that rv != ENOENT) */ rv = apr_env_set(TEST_ENVVAR_NAME, "", p); apr_assert_success(tc, "set environment variable", rv); rv = apr_env_get(&value, TEST_ENVVAR_NAME, p); apr_assert_success(tc, "get environment variable", rv); CuAssertStrEquals(tc, "", value); if (!have_env_del) { CuNotImpl(tc, "apr_env_del (skip recycle test_emptyenv)"); return; } /** Delete and retest */ rv = apr_env_delete(TEST_ENVVAR_NAME, p); apr_assert_success(tc, "delete environment variable", rv); rv = apr_env_get(&value, TEST_ENVVAR_NAME, p); CuAssertIntEquals(tc, APR_ENOENT, rv); /** Set second variable + test*/ rv = apr_env_set(TEST_ENVVAR2_NAME, TEST_ENVVAR_VALUE, p); apr_assert_success(tc, "set second environment variable", rv); rv = apr_env_get(&value, TEST_ENVVAR2_NAME, p); apr_assert_success(tc, "get second environment variable", rv); CuAssertStrEquals(tc, TEST_ENVVAR_VALUE, value); /** Finally, test ENOENT (first variable) followed by second != ENOENT) */ rv = apr_env_get(&value, TEST_ENVVAR_NAME, p); CuAssertIntEquals(tc, APR_ENOENT, rv); rv = apr_env_get(&value, TEST_ENVVAR2_NAME, p); apr_assert_success(tc, "verify second environment variable", rv); CuAssertStrEquals(tc, TEST_ENVVAR_VALUE, value); /** Cleanup */ apr_env_delete(TEST_ENVVAR2_NAME, p); }
static void test_splits(abts_case *tc, void *ctx) { apr_bucket_alloc_t *ba = apr_bucket_alloc_create(p); apr_bucket_brigade *bb; apr_bucket *e; char *str = "alphabeta"; int n; bb = apr_brigade_create(p, ba); APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_immortal_create(str, 9, ba)); APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_transient_create(str, 9, ba)); APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_heap_create(strdup(str), 9, free, ba)); APR_BRIGADE_INSERT_TAIL(bb, apr_bucket_pool_create(apr_pstrdup(p, str), 9, p, ba)); ABTS_ASSERT(tc, "four buckets inserted", count_buckets(bb) == 4); /* now split each of the buckets after byte 5 */ for (n = 0, e = APR_BRIGADE_FIRST(bb); n < 4; n++) { ABTS_ASSERT(tc, "reached end of brigade", e != APR_BRIGADE_SENTINEL(bb)); ABTS_ASSERT(tc, "split bucket OK", apr_bucket_split(e, 5) == APR_SUCCESS); e = APR_BUCKET_NEXT(e); ABTS_ASSERT(tc, "split OK", e != APR_BRIGADE_SENTINEL(bb)); e = APR_BUCKET_NEXT(e); } ABTS_ASSERT(tc, "four buckets split into eight", count_buckets(bb) == 8); for (n = 0, e = APR_BRIGADE_FIRST(bb); n < 4; n++) { const char *data; apr_size_t len; apr_assert_success(tc, "read alpha from bucket", apr_bucket_read(e, &data, &len, APR_BLOCK_READ)); ABTS_ASSERT(tc, "read 5 bytes", len == 5); ABTS_STR_NEQUAL(tc, "alpha", data, 5); e = APR_BUCKET_NEXT(e); apr_assert_success(tc, "read beta from bucket", apr_bucket_read(e, &data, &len, APR_BLOCK_READ)); ABTS_ASSERT(tc, "read 4 bytes", len == 4); ABTS_STR_NEQUAL(tc, "beta", data, 5); e = APR_BUCKET_NEXT(e); } /* now delete the "alpha" buckets */ for (n = 0, e = APR_BRIGADE_FIRST(bb); n < 4; n++) { apr_bucket *f; ABTS_ASSERT(tc, "reached end of brigade", e != APR_BRIGADE_SENTINEL(bb)); f = APR_BUCKET_NEXT(e); apr_bucket_delete(e); e = APR_BUCKET_NEXT(f); } ABTS_ASSERT(tc, "eight buckets reduced to four", count_buckets(bb) == 4); flatten_match(tc, "flatten beta brigade", bb, "beta" "beta" "beta" "beta"); apr_brigade_destroy(bb); apr_bucket_alloc_destroy(ba); }