test_error.c 2.33 KB
Newer Older
1 2 3 4
#include <assert.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
5
#include <unistd.h>
6
#include "test.h"
7

8
char const* expect_errpfx;
9 10
int n_handle_error=0;

11 12
static void
handle_error (const DB_ENV *UU(dbenv), const char *errpfx, const char *UU(msg)) {
13 14 15
    assert(errpfx==expect_errpfx);
    n_handle_error++;
}
Yoni Fogel's avatar
Yoni Fogel committed
16 17
int
test_main (int argc, const char *argv[]) {
18
    parse_args(argc, argv);
Yoni Fogel's avatar
Yoni Fogel committed
19 20 21 22

#if defined(OSX)
    if (verbose) printf("Warning: fmemopen does not exist in OSX!\n");
#else
23
    
24
    system("rm -rf " ENVDIR);
25
    int r=toku_os_mkdir(ENVDIR, S_IRWXU+S_IRWXG+S_IRWXO); assert(r==0);
26 27 28 29

    {
	DB_ENV *env;
	r = db_env_create(&env, 0); assert(r==0);
30
	env->set_errfile(env,0); // Turn off those annoying errors
31
	r = env->open(env, ENVDIR, -1, 0644);
32 33
	assert(r==EINVAL);
	assert(n_handle_error==0);
34 35 36
	r = env->close(env, 0); assert(r==0);
    }

37 38 39 40
    int do_errfile, do_errcall,do_errpfx;
    for (do_errpfx=0; do_errpfx<2; do_errpfx++) {
	for (do_errfile=0; do_errfile<2; do_errfile++) {
	    for (do_errcall=0; do_errcall<2; do_errcall++) {
41 42 43 44 45 46 47 48
		char errfname[] = __FILE__ ".errs";
		unlink(errfname);
		{
		    DB_ENV *env;
		    FILE *write_here = fopen(errfname, "w");
		    assert(write_here);
		    n_handle_error=0;
		    r = db_env_create(&env, 0); assert(r==0);
49
		    if (do_errpfx) {
50 51
			expect_errpfx="whoopi";
			env->set_errpfx(env, expect_errpfx);
52
		    } else {
53
			expect_errpfx=0;
54
		    }
55 56 57 58 59
		    env->set_errfile(env,0); // Turn off those annoying errors
		    if (do_errfile)
			env->set_errfile(env, write_here);
		    if (do_errcall) 
			env->set_errcall(env, handle_error);
60
		    r = env->open(env, ENVDIR, -1, 0644);
61 62 63
		    assert(r==EINVAL);
		    r = env->close(env, 0); assert(r==0);
		    fclose(write_here);
64
		}
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87
		{
		    FILE *read_here = fopen(errfname, "r");
		    assert(read_here);
		    char buf[10000];
		    int buflen = fread(buf, 1, sizeof(buf)-1, read_here);
		    assert(buflen>=0);
		    buf[buflen]=0;
		    if (do_errfile) {
			if (do_errpfx) {
			    assert(strncmp(buf,"whoopi:",6)==0);
			} else {
			    assert(buf[0]!=0); 
			    assert(buf[0]!=':');
			}
			assert(buf[strlen(buf)-1]=='\n');
		    } else {
			assert(buf[0]==0);
		    }
		    if (do_errcall) {
			assert(n_handle_error==1);
		    } else {
			assert(n_handle_error==0);
		    }
88
		    fclose(read_here);
89
		}
90
		unlink(errfname);
91 92 93
	    }
	}
    }
Yoni Fogel's avatar
Yoni Fogel committed
94
#endif
95 96
    return 0;
}