1 /* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 // vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
3 #ident "$Id$"
4 /*======
5 This file is part of PerconaFT.
6 
7 
8 Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved.
9 
10     PerconaFT is free software: you can redistribute it and/or modify
11     it under the terms of the GNU General Public License, version 2,
12     as published by the Free Software Foundation.
13 
14     PerconaFT is distributed in the hope that it will be useful,
15     but WITHOUT ANY WARRANTY; without even the implied warranty of
16     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17     GNU General Public License for more details.
18 
19     You should have received a copy of the GNU General Public License
20     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
21 
22 ----------------------------------------
23 
24     PerconaFT is free software: you can redistribute it and/or modify
25     it under the terms of the GNU Affero General Public License, version 3,
26     as published by the Free Software Foundation.
27 
28     PerconaFT is distributed in the hope that it will be useful,
29     but WITHOUT ANY WARRANTY; without even the implied warranty of
30     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
31     GNU Affero General Public License for more details.
32 
33     You should have received a copy of the GNU Affero General Public License
34     along with PerconaFT.  If not, see <http://www.gnu.org/licenses/>.
35 ======= */
36 
37 #ident "Copyright (c) 2006, 2015, Percona and/or its affiliates. All rights reserved."
38 
39 #include "test.h"
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <unistd.h>
43 
44 #include <errno.h>
45 #include <sys/stat.h>
46 #include <db.h>
47 #include <memory.h>
48 
49 const char *dbfile = "test.db";
50 const char *dbname = 0;
51 
52 static int
db_put(DB * db,int k,int v)53 db_put (DB *db, int k, int v) {
54     DBT key, val;
55     int r = db->put(db, 0, dbt_init(&key, &k, sizeof k), dbt_init(&val, &v, sizeof v), 0);
56     return r;
57 }
58 
59 static int
db_get(DB * db,int k,int expectv,int val_flags)60 db_get (DB *db, int k, int expectv, int val_flags) {
61     int v;
62     DBT key, val;
63     memset(&val, 0, sizeof val); val.flags = val_flags;
64     if (val.flags == DB_DBT_USERMEM) {
65         val.ulen = sizeof v; val.data = &v;
66     }
67     int r = db->get(db, 0, dbt_init(&key, &k, sizeof k), &val, 0);
68     if (r == 0) {
69         assert(val.size == sizeof v);
70         if ((val.flags & DB_DBT_USERMEM) == 0) memcpy(&v, val.data, val.size);
71         assert(v == expectv);
72     } else {
73         if (verbose) printf("%s:%d:%d:%s\n", __FILE__, __LINE__, r, db_strerror(r));
74     }
75     if (val.flags & (DB_DBT_MALLOC|DB_DBT_REALLOC))
76         toku_free(val.data);
77     return r;
78 }
79 
80 static void
test_db_create(void)81 test_db_create (void) {
82     int r;
83 
84     unlink(dbfile);
85 
86     DB_ENV *env;
87     r = db_env_create(&env, 0); assert(r == 0);
88     r = env->open(env, TOKU_TEST_FILENAME, DB_CREATE+DB_PRIVATE+DB_INIT_MPOOL, 0); assert(r == 0);
89 
90     DB *db;
91     r = db_create(&db, env, 0); assert(r == 0);
92     db->set_errfile(db,0); // Turn off those annoying errors
93     r = db->open(db, 0, dbfile, dbname, DB_BTREE, DB_CREATE, S_IRWXU+S_IRWXG+S_IRWXO); assert(r == 0);
94     r = db_put(db, htonl(1), 1); assert(r == 0);
95     r = db_get(db, htonl(1), 1, 0); assert(r == 0);
96     r = db_get(db, htonl(1), 1, DB_DBT_USERMEM); assert(r == 0);
97     r = db->close(db, 0); assert(r == 0);
98     r = env->close(env, 0); assert(r == 0);
99 }
100 
101 static void
test_db_thread(void)102 test_db_thread (void) {
103     int r;
104 
105     unlink(dbfile);
106 
107     DB_ENV *env;
108     r = db_env_create(&env, 0); assert(r == 0);
109     r = env->open(env, TOKU_TEST_FILENAME, DB_CREATE+DB_PRIVATE+DB_INIT_MPOOL+DB_THREAD, 0); assert(r == 0);
110 
111     DB *db;
112     r = db_create(&db, env, 0); assert(r == 0);
113     db->set_errfile(db,0); // Turn off those annoying errors
114     r = db->open(db, 0, dbfile, dbname, DB_BTREE, DB_CREATE + DB_THREAD, S_IRWXU+S_IRWXG+S_IRWXO); assert(r == 0);
115     r = db_put(db, htonl(1), 1); assert(r == 0);
116     r = db_get(db, htonl(1), 1, 0); assert(r == EINVAL);
117     r = db_get(db, htonl(1), 1, DB_DBT_MALLOC); assert(r == 0);
118     r = db_get(db, htonl(1), 1, DB_DBT_REALLOC); assert(r == 0);
119     r = db_get(db, htonl(1), 1, DB_DBT_USERMEM); assert(r == 0);
120     r = db->close(db, 0); assert(r == 0);
121     r = env->close(env, 0); assert(r == 0);
122 }
123 
124 int
test_main(int argc,char * const argv[])125 test_main(int argc, char *const argv[]) {
126     parse_args(argc, argv);
127     toku_os_recursive_delete(TOKU_TEST_FILENAME);
128     toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU+S_IRWXG+S_IRWXO);
129     test_db_create();
130     test_db_thread();
131     return 0;
132 }
133