mariadb/ft/tests/logcursor-bad-checksum.cc
Leif Walsh d9a440ee26 closes #5949 merge to main
git-svn-id: file:///svn/toku/tokudb@52748 c7de825b-a66e-492c-adef-691d508d4ae1
2013-04-17 00:01:28 -04:00

85 lines
2.3 KiB
C++

/* -*- mode: C++; c-basic-offset: 4; indent-tabs-mode: nil -*- */
// vim: ft=cpp:expandtab:ts=8:sw=4:softtabstop=4:
#ident "$Id$"
#ident "Copyright (c) 2007, 2008 Tokutek Inc. All rights reserved."
#include "logcursor.h"
#include "test.h"
// log a couple of timestamp entries and verify the log by walking
// a cursor through the log entries
static void corrupt_the_checksum(void) {
// change the LSN in the first log entry of log 0. this will cause an checksum error.
char logname[TOKU_PATH_MAX+1];
int r;
sprintf(logname, "%s/log000000000000.tokulog%d", TOKU_TEST_FILENAME, TOKU_LOG_VERSION);
FILE *f = fopen(logname, "r+b"); assert(f);
r = fseek(f, 025, SEEK_SET); assert(r == 0);
char c = 100;
size_t n = fwrite(&c, sizeof c, 1, f); assert(n == sizeof c);
r = fclose(f); assert(r == 0);
}
int
test_main (int argc, const char *argv[]) {
default_parse_args(argc, argv);
int r;
toku_os_recursive_delete(TOKU_TEST_FILENAME);
r = toku_os_mkdir(TOKU_TEST_FILENAME, S_IRWXU); assert(r==0);
TOKULOGGER logger;
LSN lsn = ZERO_LSN;
// log a couple of timestamp log entries
r = toku_logger_create(&logger);
assert(r == 0);
r = toku_logger_open(TOKU_TEST_FILENAME, logger);
assert(r == 0);
BYTESTRING bs0 = { .len = 5, .data = (char *) "hello" };
toku_log_comment(logger, &lsn, 0, 0, bs0);
r = toku_logger_close(&logger);
assert(r == 0);
// change the LSN and corrupt the checksum
corrupt_the_checksum();
if (!verbose) {
// redirect stderr
int devnul = open(DEV_NULL_FILE, O_WRONLY);
assert(devnul >= 0);
r = toku_dup2(devnul, fileno(stderr)); assert(r == fileno(stderr));
r = close(devnul); assert(r == 0);
}
// walk forwards
TOKULOGCURSOR lc = NULL;
struct log_entry *le;
r = toku_logcursor_create(&lc, TOKU_TEST_FILENAME);
assert(r == 0 && lc != NULL);
r = toku_logcursor_next(lc, &le);
assert(r != 0);
r = toku_logcursor_destroy(&lc);
assert(r == 0 && lc == NULL);
// walk backwards
r = toku_logcursor_create(&lc, TOKU_TEST_FILENAME);
assert(r == 0 && lc != NULL);
r = toku_logcursor_prev(lc, &le);
assert(r != 0);
r = toku_logcursor_destroy(&lc);
assert(r == 0 && lc == NULL);
toku_os_recursive_delete(TOKU_TEST_FILENAME);
return 0;
}