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 "toku_os.h"
41 
42 
43 static void
cachetable_fd_test(void)44 cachetable_fd_test (void) {
45     const int test_limit = 1;
46     int r;
47     CACHETABLE ct;
48     toku_cachetable_create(&ct, test_limit, ZERO_LSN, nullptr);
49     toku_os_recursive_delete(TOKU_TEST_FILENAME);
50     r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU);
51     assert_zero(r);
52     char fname1[TOKU_PATH_MAX+1];
53     unlink(toku_path_join(fname1, 2, TOKU_TEST_FILENAME, "test1.dat"));
54     CACHEFILE cf;
55     r = toku_cachetable_openf(&cf, ct, fname1, O_RDWR|O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO); assert(r == 0);
56 
57     int fd1 = toku_cachefile_get_fd(cf); assert(fd1 >= 0);
58 
59     // test set to good fd succeeds
60     char fname2[TOKU_PATH_MAX+1];
61     unlink(toku_path_join(fname2, 2, TOKU_TEST_FILENAME, "test2.dat"));
62     int fd2 = open(fname2, O_RDWR | O_CREAT, S_IRWXU|S_IRWXG|S_IRWXO);
63     assert(fd2 >= 0 && fd1 != fd2);
64     struct fileid id;
65     r = toku_os_get_unique_file_id(fd2, &id);
66     assert(r == 0);
67     close(fd2);
68 
69     // test set to bogus fd fails
70     int fd3 = open(DEV_NULL_FILE, O_RDWR); assert(fd3 >= 0);
71     r = close(fd3);
72     assert(r == 0);
73     r = toku_os_get_unique_file_id(fd3, &id);
74     assert(r < 0);
75 
76     // test the filenum functions
77     FILENUM fn = toku_cachefile_filenum(cf);
78     CACHEFILE newcf = 0;
79     r = toku_cachefile_of_filenum(ct, fn, &newcf);
80     assert(r == 0 && cf == newcf);
81 
82     // test a bogus filenum
83     fn.fileid++;
84     r = toku_cachefile_of_filenum(ct, fn, &newcf);
85     assert(r == ENOENT);
86 
87     toku_cachefile_close(&cf, false, ZERO_LSN);
88     toku_cachetable_close(&ct);
89 }
90 
91 int
test_main(int argc,const char * argv[])92 test_main(int argc, const char *argv[]) {
93     default_parse_args(argc, argv);
94     toku_os_initialize_settings(verbose);
95 
96     cachetable_fd_test();
97     return 0;
98 }
99