1bc68bb02SChiyoung Seo/* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2bc68bb02SChiyoung Seo/*
3bc68bb02SChiyoung Seo *     Copyright 2010 Couchbase, Inc
4bc68bb02SChiyoung Seo *
5bc68bb02SChiyoung Seo *   Licensed under the Apache License, Version 2.0 (the "License");
6bc68bb02SChiyoung Seo *   you may not use this file except in compliance with the License.
7bc68bb02SChiyoung Seo *   You may obtain a copy of the License at
8bc68bb02SChiyoung Seo *
9bc68bb02SChiyoung Seo *       http://www.apache.org/licenses/LICENSE-2.0
10bc68bb02SChiyoung Seo *
11bc68bb02SChiyoung Seo *   Unless required by applicable law or agreed to in writing, software
12bc68bb02SChiyoung Seo *   distributed under the License is distributed on an "AS IS" BASIS,
13bc68bb02SChiyoung Seo *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14bc68bb02SChiyoung Seo *   See the License for the specific language governing permissions and
15bc68bb02SChiyoung Seo *   limitations under the License.
16bc68bb02SChiyoung Seo */
17bc68bb02SChiyoung Seo
187c0433f5SJung-Sang Ahn#include <stdio.h>
197c0433f5SJung-Sang Ahn#include <stdlib.h>
207c0433f5SJung-Sang Ahn#include <string.h>
217c0433f5SJung-Sang Ahn
227c0433f5SJung-Sang Ahn#include "filemgr.h"
23f693a021SSundar Sridharan#include "filemgr_ops.h"
247c0433f5SJung-Sang Ahn#include "test.h"
257c0433f5SJung-Sang Ahn
264690a73fSJens Alfkevoid basic_test(fdb_encryption_algorithm_t encryption)
277c0433f5SJung-Sang Ahn{
282889254eSJung-Sang Ahn    TEST_INIT();
297c0433f5SJung-Sang Ahn
302889254eSJung-Sang Ahn    struct filemgr *file;
312889254eSJung-Sang Ahn    struct filemgr_config config;
328e1b9ec2SJung-Sang Ahn    const char *dbheader = "dbheader";
338e1b9ec2SJung-Sang Ahn    const char *dbheader2 = "dbheader2222222222";
342889254eSJung-Sang Ahn    char buf[256];
357c0433f5SJung-Sang Ahn
362534ac38SJung-Sang Ahn    memset(&config, 0, sizeof(config));
372889254eSJung-Sang Ahn    config.blocksize = 4096;
382889254eSJung-Sang Ahn    config.ncacheblock = 1024;
39a765ad59SChiyoung Seo    config.options = FILEMGR_CREATE;
40b84b312aSChiyoung Seo    config.num_wal_shards = 8;
419ceccb2cSJung-Sang Ahn
424690a73fSJens Alfke    config.encryption_key.algorithm = encryption;
434690a73fSJens Alfke    memset(&config.encryption_key.bytes, 0x55, sizeof(config.encryption_key.bytes));
444690a73fSJens Alfke
45225450a4SSundar Sridharan    filemgr_open_result result = filemgr_open((char *) "./filemgr_testfile",
46a765ad59SChiyoung Seo                                              get_filemgr_ops(), &config, NULL);
47225450a4SSundar Sridharan    result = filemgr_open((char *) "./filemgr_testfile", get_filemgr_ops(), &config, NULL);
48a765ad59SChiyoung Seo    file = result.file;
497c0433f5SJung-Sang Ahn
50f0b1bf77SJung-Sang Ahn    filemgr_update_header(file, (void*)dbheader, strlen(dbheader)+1, true);
517c0433f5SJung-Sang Ahn
52fef67a48SChiyoung Seo    filemgr_close(file, true, NULL, NULL);
53225450a4SSundar Sridharan    result = filemgr_open((char *) "./filemgr_testfile", get_filemgr_ops(), &config, NULL);
54a765ad59SChiyoung Seo    file = result.file;
557c0433f5SJung-Sang Ahn
562889254eSJung-Sang Ahn    memcpy(buf, file->header.data, file->header.size);
572889254eSJung-Sang Ahn    printf("%s\n", buf);
587c0433f5SJung-Sang Ahn
59f0b1bf77SJung-Sang Ahn    filemgr_update_header(file, (void*)dbheader2, strlen(dbheader2) + 1, true);
607c0433f5SJung-Sang Ahn
61fef67a48SChiyoung Seo    filemgr_close(file, true, NULL, NULL);
627c0433f5SJung-Sang Ahn
634690a73fSJens Alfke    sprintf(buf, "basic test, encryption=%d", (int)encryption);
644690a73fSJens Alfke    TEST_RESULT(buf);
657c0433f5SJung-Sang Ahn}
667c0433f5SJung-Sang Ahn
677c0433f5SJung-Sang Ahnvoid mt_init_test()
687c0433f5SJung-Sang Ahn{
692889254eSJung-Sang Ahn    TEST_INIT();
707c0433f5SJung-Sang Ahn
712889254eSJung-Sang Ahn    TEST_RESULT("multi threaded initialization test");
727c0433f5SJung-Sang Ahn}
737c0433f5SJung-Sang Ahn
747c0433f5SJung-Sang Ahnint main()
757c0433f5SJung-Sang Ahn{
76225450a4SSundar Sridharan    int r = system(SHELL_DEL" filemgr_testfile");
7745647b9eSSundar Sridharan    (void)r;
787c0433f5SJung-Sang Ahn
794690a73fSJens Alfke    basic_test(FDB_ENCRYPTION_NONE);
804690a73fSJens Alfke    basic_test(FDB_ENCRYPTION_BOGUS);
812889254eSJung-Sang Ahn    mt_init_test();
827c0433f5SJung-Sang Ahn
832889254eSJung-Sang Ahn    return 0;
847c0433f5SJung-Sang Ahn}