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 
41 
update_fun(DB * UU (db),const DBT * UU (key),const DBT * UU (old_val),const DBT * UU (extra),void (* set_val)(const DBT * new_val,void * set_extra),void * UU (set_extra))42 static int update_fun(DB *UU(db),
43                       const DBT *UU(key),
44                       const DBT *UU(old_val), const DBT *UU(extra),
45                       void (*set_val)(const DBT *new_val,
46                                       void *set_extra),
47                       void *UU(set_extra))
48 {
49     abort();
50     assert(set_val != NULL);
51     return 0;
52 }
53 
generate_row_for_put(DB * UU (dest_db),DB * UU (src_db),DBT_ARRAY * UU (dest_key_arrays),DBT_ARRAY * UU (dest_val_arrays),const DBT * UU (src_key),const DBT * UU (src_val))54 static int generate_row_for_put(
55     DB *UU(dest_db),
56     DB *UU(src_db),
57     DBT_ARRAY *UU(dest_key_arrays),
58     DBT_ARRAY *UU(dest_val_arrays),
59     const DBT *UU(src_key),
60     const DBT *UU(src_val)
61     )
62 {
63     abort();
64     return 0;
65 }
66 
generate_row_for_del(DB * UU (dest_db),DB * UU (src_db),DBT_ARRAY * UU (dest_key_arrays),const DBT * UU (src_key),const DBT * UU (src_val))67 static int generate_row_for_del(
68     DB *UU(dest_db),
69     DB *UU(src_db),
70     DBT_ARRAY *UU(dest_key_arrays),
71     const DBT *UU(src_key),
72     const DBT *UU(src_val)
73     )
74 {
75     abort();
76     return 0;
77 }
78 
test_invalid_ops(uint32_t iso_flags)79 static void test_invalid_ops(uint32_t iso_flags) {
80     int r;
81     DB * db;
82     DB_ENV * env;
83 
84     toku_os_recursive_delete(TOKU_TEST_FILENAME);
85     r = toku_os_mkdir(TOKU_TEST_FILENAME, 0755); { int chk_r = r; CKERR(chk_r); }
86 
87     // set things up
88     r = db_env_create(&env, 0);
89     CKERR(r);
90     r = env->set_generate_row_callback_for_put(env,generate_row_for_put);
91     CKERR(r);
92     r = env->set_generate_row_callback_for_del(env,generate_row_for_del);
93     CKERR(r);
94     env->set_update(env, update_fun);
95     r = env->open(env, TOKU_TEST_FILENAME, DB_INIT_MPOOL|DB_CREATE|DB_THREAD |DB_INIT_LOCK|DB_INIT_LOG|DB_INIT_TXN|DB_PRIVATE, 0755);
96     CKERR(r);
97     r = db_create(&db, env, 0);
98     CKERR(r);
99 
100     DB_TXN* txn = NULL;
101     r = env->txn_begin(env, 0, &txn, iso_flags | DB_TXN_READ_ONLY);
102     CKERR(r);
103 
104     r = db->open(db, txn, "foo.db", NULL, DB_BTREE, DB_CREATE, 0644);
105     CKERR2(r, EINVAL);
106     r = db->open(db, NULL, "foo.db", NULL, DB_BTREE, DB_CREATE, 0644);
107     CKERR(r);
108 
109     int k = 1;
110     int v = 10;
111     DBT key, val;
112     dbt_init(&key, &k, sizeof k);
113     dbt_init(&val, &v, sizeof v);
114 
115     uint32_t db_flags = 0;
116     uint32_t indexer_flags = 0;
117     DB_INDEXER* indexer;
118     r = env->create_indexer(
119         env,
120         txn,
121         &indexer,
122         db,
123         1,
124         &db,
125         &db_flags,
126         indexer_flags
127         );
128     CKERR2(r, EINVAL);
129 
130 
131     // test invalid operations of ydb_db.cc,
132     // db->open tested above
133     DB_LOADER* loader;
134     uint32_t put_flags = 0;
135     uint32_t dbt_flags = 0;
136     r = env->create_loader(env, txn, &loader, NULL, 1, &db, &put_flags, &dbt_flags, 0);
137     CKERR2(r, EINVAL);
138 
139     r = db->change_descriptor(db, txn, &key, 0);
140     CKERR2(r, EINVAL);
141 
142     //
143     // test invalid operations return EINVAL from ydb_write.cc
144     //
145     r = db->put(db, txn, &key, &val,0);
146     CKERR2(r, EINVAL);
147     r = db->del(db, txn, &key, DB_DELETE_ANY);
148     CKERR2(r, EINVAL);
149     r = db->update(db, txn, &key, &val, 0);
150     CKERR2(r, EINVAL);
151     r = db->update_broadcast(db, txn, &val, 0);
152     CKERR2(r, EINVAL);
153 
154     r = env_put_multiple_test_no_array(env, NULL, txn, &key, &val, 1, &db, &key, &val, 0);
155     CKERR2(r, EINVAL);
156     r = env_del_multiple_test_no_array(env, NULL, txn, &key, &val, 1, &db, &key, 0);
157     CKERR2(r, EINVAL);
158     uint32_t flags;
159     r = env_update_multiple_test_no_array(
160         env, NULL, txn,
161         &key, &val,
162         &key, &val,
163         1, &db, &flags,
164         1, &key,
165         1, &val
166         );
167     CKERR2(r, EINVAL);
168 
169     r = db->close(db, 0);
170     CKERR(r);
171 
172     // test invalid operations of ydb.cc, dbrename and dbremove
173     r = env->dbremove(env, txn, "foo.db", NULL, 0);
174     CKERR2(r, EINVAL);
175     // test invalid operations of ydb.cc, dbrename and dbremove
176     r = env->dbrename(env, txn, "foo.db", NULL, "bar.db", 0);
177     CKERR2(r, EINVAL);
178 
179     r = txn->commit(txn, 0);
180     CKERR(r);
181 
182     // clean things up
183     r = env->close(env, 0);
184     CKERR(r);
185 }
186 
187 
test_main(int argc,char * const argv[])188 int test_main(int argc, char * const argv[]) {
189     (void) argc;
190     (void) argv;
191     test_invalid_ops(0);
192     test_invalid_ops(DB_TXN_SNAPSHOT);
193     test_invalid_ops(DB_READ_COMMITTED);
194     test_invalid_ops(DB_READ_UNCOMMITTED);
195     return 0;
196 }
197