1 /* -*- Mode: C++; tab-width: 4; c-basic-offset: 4; indent-tabs-mode: nil -*- */
2 /*
3  *     Copyright 2010 Couchbase, Inc
4  *
5  *   Licensed under the Apache License, Version 2.0 (the "License");
6  *   you may not use this file except in compliance with the License.
7  *   You may obtain a copy of the License at
8  *
9  *       http://www.apache.org/licenses/LICENSE-2.0
10  *
11  *   Unless required by applicable law or agreed to in writing, software
12  *   distributed under the License is distributed on an "AS IS" BASIS,
13  *   WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  *   See the License for the specific language governing permissions and
15  *   limitations under the License.
16  */
17 
18 #include <stdio.h>
19 #include <stdlib.h>
20 #include <string.h>
21 
22 #include "filemgr.h"
23 #include "filemgr_ops.h"
24 #include "test.h"
25 
basic_test(fdb_encryption_algorithm_t encryption)26 void basic_test(fdb_encryption_algorithm_t encryption)
27 {
28     TEST_INIT();
29 
30     struct filemgr *file;
31     struct filemgr_config config;
32     const char *dbheader = "dbheader";
33     const char *dbheader2 = "dbheader2222222222";
34     char buf[256];
35 
36     memset(&config, 0, sizeof(config));
37     config.blocksize = 4096;
38     config.ncacheblock = 1024;
39     config.options = FILEMGR_CREATE;
40     config.num_wal_shards = 8;
41 
42     config.encryption_key.algorithm = encryption;
43     memset(&config.encryption_key.bytes, 0x55, sizeof(config.encryption_key.bytes));
44 
45     filemgr_open_result result = filemgr_open((char *) "./filemgr_testfile",
46                                               get_filemgr_ops(), &config, NULL);
47     result = filemgr_open((char *) "./filemgr_testfile", get_filemgr_ops(), &config, NULL);
48     file = result.file;
49 
50     filemgr_update_header(file, (void*)dbheader, strlen(dbheader)+1, true);
51 
52     filemgr_close(file, true, NULL, NULL);
53     result = filemgr_open((char *) "./filemgr_testfile", get_filemgr_ops(), &config, NULL);
54     file = result.file;
55 
56     memcpy(buf, file->header.data, file->header.size);
57     printf("%s\n", buf);
58 
59     filemgr_update_header(file, (void*)dbheader2, strlen(dbheader2) + 1, true);
60 
61     filemgr_close(file, true, NULL, NULL);
62 
63     sprintf(buf, "basic test, encryption=%d", (int)encryption);
64     TEST_RESULT(buf);
65 }
66 
mt_init_test()67 void mt_init_test()
68 {
69     TEST_INIT();
70 
71     TEST_RESULT("multi threaded initialization test");
72 }
73 
main()74 int main()
75 {
76     int r = system(SHELL_DEL" filemgr_testfile");
77     (void)r;
78 
79     basic_test(FDB_ENCRYPTION_NONE);
80     basic_test(FDB_ENCRYPTION_BOGUS);
81     mt_init_test();
82 
83     return 0;
84 }
85