test_error.c 2.01 KB
Newer Older
1
//NOTE: fmemopen does not exist in OSX
2 3 4 5 6 7 8
// I want fmemopen
#define _GNU_SOURCE

#include <assert.h>
#include <sys/stat.h>
#include <errno.h>
#include <stdio.h>
9
#include "test.h"
10

11
char const* expect_errpfx;
12 13 14 15 16 17 18 19
int n_handle_error=0;

void handle_error (const DB_ENV *dbenv, const char *errpfx, const char *msg) {
    assert(errpfx==expect_errpfx);
    n_handle_error++;
}
int main (int argc, const char *argv[]) {
    parse_args(argc, argv);
Yoni Fogel's avatar
Yoni Fogel committed
20 21 22 23

#if defined(OSX)
    if (verbose) printf("Warning: fmemopen does not exist in OSX!\n");
#else
24 25 26 27 28 29 30
    
    system("rm -rf " DIR);
    int r=mkdir(DIR, 0777); assert(r==0);

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

38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78
    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++) {
		DB_ENV *env;
		char buf[10000]="";
		FILE *write_here = fmemopen(buf, sizeof(buf), "w");
		n_handle_error=0;
		r = db_env_create(&env, 0); assert(r==0);
		if (do_errpfx) {
		    expect_errpfx="whoopi";
		    env->set_errpfx(env, expect_errpfx);
		} else {
		    expect_errpfx=0;
		}
		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);
		r = env->open(env, DIR, -1, 0644);
		assert(r==EINVAL);
		r = env->close(env, 0); assert(r==0);
		fclose(write_here);
		if (do_errfile) {
		    printf("buf=%s(end of buf)\n", buf);
		    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);
		}
79 80 81
	    }
	}
    }
Yoni Fogel's avatar
Yoni Fogel committed
82
#endif
83 84
    return 0;
}