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 // The purpose of this test is to find memory leaks in the ft_loader_open function.  Right now, it finds leaks in some very simple
40 // cases.
41 
42 #define DONT_DEPRECATE_MALLOC
43 #include "test.h"
44 #include "loader/loader.h"
45 #include "loader/loader-internal.h"
46 #include "memory.h"
47 #include <portability/toku_path.h>
48 
49 
50 static int my_malloc_count = 0;
51 static int my_malloc_trigger = 0;
52 
set_my_malloc_trigger(int n)53 static void set_my_malloc_trigger(int n) {
54     my_malloc_count = 0;
55     my_malloc_trigger = n;
56 }
57 
my_malloc(size_t n)58 static void *my_malloc(size_t n) {
59     my_malloc_count++;
60     if (my_malloc_count == my_malloc_trigger) {
61         errno = ENOSPC;
62         return NULL;
63     } else
64         return os_malloc(n);
65 }
66 
my_compare(DB * UU (desc),const DBT * UU (akey),const DBT * UU (bkey))67 static int my_compare(DB *UU(desc), const DBT *UU(akey), const DBT *UU(bkey)) {
68     return EINVAL;
69 }
70 
test_loader_open(int ndbs)71 static void test_loader_open(int ndbs) {
72     int r;
73     FTLOADER loader;
74 
75     // open the ft_loader. this runs the extractor.
76     FT_HANDLE fts[ndbs];
77     DB* dbs[ndbs];
78     const char *fnames[ndbs];
79     ft_compare_func compares[ndbs];
80     for (int i = 0; i < ndbs; i++) {
81         fts[i] = NULL;
82         dbs[i] = NULL;
83         fnames[i] = "";
84         compares[i] = my_compare;
85     }
86 
87     toku_set_func_malloc(my_malloc);
88 
89     int i;
90     for (i = 0; ; i++) {
91         set_my_malloc_trigger(i+1);
92 
93         r = toku_ft_loader_open(&loader, NULL, NULL, NULL, ndbs, fts, dbs, fnames, compares, "", ZERO_LSN, nullptr, true, 0, false, true);
94         if (r == 0)
95             break;
96     }
97 
98     if (verbose) printf("i=%d\n", i);
99 
100     r = toku_ft_loader_abort(loader, true);
101     assert(r == 0);
102 }
103 
test_main(int argc,const char * argv[])104 int test_main (int argc, const char *argv[]) {
105     const char *progname=argv[0];
106     argc--; argv++;
107     while (argc>0) {
108 	if (strcmp(argv[0],"-v")==0) {
109 	    verbose=1;
110 	} else if (strcmp(argv[0],"-q")==0) {
111 	    verbose=0;
112 	} else if (argc!=1) {
113 	    fprintf(stderr, "Usage:\n %s [-v] [-q]\n", progname);
114 	    exit(1);
115 	}
116         else {
117             break;
118         }
119 	argc--; argv++;
120     }
121 
122     test_loader_open(0);
123     test_loader_open(1);
124 
125     return 0;
126 }
127 
128