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 "logger/logcursor.h"
40 #include "test.h"
41 
42 // log a couple of timestamp entries and verify the log by walking
43 // a cursor through the log entries
44 
corrupt_the_checksum(void)45 static void corrupt_the_checksum(void) {
46     // change the LSN in the first log entry of log 0.  this will cause an checksum error.
47     char logname[TOKU_PATH_MAX+1];
48     int r;
49     sprintf(logname,  "%s/log000000000000.tokulog%d", TOKU_TEST_FILENAME, TOKU_LOG_VERSION);
50     FILE *f = fopen(logname, "r+b"); assert(f);
51     r = fseek(f, 025, SEEK_SET); assert(r == 0);
52     char c = 100;
53     size_t n = fwrite(&c, sizeof c, 1, f); assert(n == sizeof c);
54     r = fclose(f); assert(r == 0);
55 }
56 
57 int
test_main(int argc,const char * argv[])58 test_main (int argc, const char *argv[]) {
59     default_parse_args(argc, argv);
60 
61     int r;
62     toku_os_recursive_delete(TOKU_TEST_FILENAME);
63     r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU);    assert(r==0);
64     TOKULOGGER logger;
65     LSN lsn = ZERO_LSN;
66 
67     // log a couple of timestamp log entries
68 
69     r = toku_logger_create(&logger);
70     assert(r == 0);
71 
72     r = toku_logger_open(TOKU_TEST_FILENAME, logger);
73     assert(r == 0);
74 
75     BYTESTRING bs0 = { .len = 5, .data = (char *) "hello" };
76     toku_log_comment(logger, &lsn, 0, 0, bs0);
77 
78     r = toku_logger_close(&logger);
79     assert(r == 0);
80 
81     // change the LSN and corrupt the checksum
82     corrupt_the_checksum();
83 
84     if (!verbose) {
85         // redirect stderr
86         int devnul = open(DEV_NULL_FILE, O_WRONLY);
87         assert(devnul >= 0);
88         r = toku_dup2(devnul, fileno(stderr)); assert(r == fileno(stderr));
89         r = close(devnul); assert(r == 0);
90     }
91 
92     // walk forwards
93     TOKULOGCURSOR lc = NULL;
94     struct log_entry *le;
95 
96     r = toku_logcursor_create(&lc, TOKU_TEST_FILENAME);
97     assert(r == 0 && lc != NULL);
98 
99     r = toku_logcursor_next(lc, &le);
100     assert(r != 0);
101 
102     r = toku_logcursor_destroy(&lc);
103     assert(r == 0 && lc == NULL);
104 
105     // walk backwards
106     r = toku_logcursor_create(&lc, TOKU_TEST_FILENAME);
107     assert(r == 0 && lc != NULL);
108 
109     r = toku_logcursor_prev(lc, &le);
110     assert(r != 0);
111 
112     r = toku_logcursor_destroy(&lc);
113     assert(r == 0 && lc == NULL);
114 
115     toku_os_recursive_delete(TOKU_TEST_FILENAME);
116 
117     return 0;
118 }
119