Debian Codegraph @

src/lib/object_store/test/DBObjectStoreTests.cpp

raw
1 /*
2 * Copyright (c) 2013 SURFnet bv
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 *
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
15 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
16 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
18 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
20 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
21 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
22 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
23 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
24 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
25 */
26
27 /*****************************************************************************
28 DBObjectStoreTests.cpp
29
30 Contains test cases to test the object store implementation
31 *****************************************************************************/
32
33 #include <stdlib.h>
34 #include <string.h>
35 #include <cppunit/extensions/HelperMacros.h>
36 #include "DBObjectStoreTests.h"
37
38 #include <cstdio>
39
40 #ifndef HAVE_SQLITE3_H
41 #error expected sqlite3 to be available
42 #endif
43
44 CPPUNIT_TEST_SUITE_REGISTRATION(test_a_newly_created_object_store);
45
46 void test_a_newly_created_object_store::setUp()
47 {
48 CPPUNIT_ASSERT(!system("mkdir testdir"));
49
50 ObjectStoreToken::selectBackend("db");
51
52 store = new ObjectStore("testdir");
53 nulltoken = NULL;
54 }
55
56 void test_a_newly_created_object_store::tearDown()
57 {
58 delete store;
59
60 ObjectStoreToken::selectBackend("file");
61
62 #ifndef _WIN32
63 CPPUNIT_ASSERT(!system("rm -rf testdir"));
64 #else
65 CPPUNIT_ASSERT(!system("rmdir /s /q testdir 2> nul"));
66 #endif
67 }
68
69
70 void test_a_newly_created_object_store::contains_no_items()
71 {
72 CPPUNIT_ASSERT_EQUAL(store->getTokenCount(), (size_t)0);
73 }
74
75 void test_a_newly_created_object_store::can_create_a_new_token()
76 {
77 ByteString label1 = "DEADC0FFEE";
78
79 ObjectStoreToken *token1 = store->newToken(label1);
80 CPPUNIT_ASSERT(token1 != nulltoken);
81 CPPUNIT_ASSERT_EQUAL(store->getTokenCount(), (size_t)1);
82 }
83
84 CPPUNIT_TEST_SUITE_REGISTRATION(test_a_newly_created_object_store_containing_two_tokens);
85
86
87 void test_a_newly_created_object_store_containing_two_tokens::setUp()
88 {
89 test_a_newly_created_object_store::setUp();
90
91 ByteString label1 = "DEADC0FFEE";
92 ByteString label2 = "DEADBEEF";
93
94 ObjectStoreToken* token1 = store->newToken(label1);
95 CPPUNIT_ASSERT(token1 != nulltoken);
96 CPPUNIT_ASSERT_EQUAL(store->getTokenCount(), (size_t)1);
97
98 ObjectStoreToken* token2 = store->newToken(label2);
99 CPPUNIT_ASSERT(token2 != nulltoken);
100 CPPUNIT_ASSERT_EQUAL(store->getTokenCount(), (size_t)2);
101 }
102
103 void test_a_newly_created_object_store_containing_two_tokens::tearDown()
104 {
105 ObjectStoreToken* token1 = store->getToken(0);
106 ObjectStoreToken* token2 = store->getToken(1);
107 CPPUNIT_ASSERT(store->destroyToken(token1));
108 CPPUNIT_ASSERT(store->destroyToken(token2));
109
110 test_a_newly_created_object_store::tearDown();
111 }
112
113 void test_a_newly_created_object_store_containing_two_tokens::has_two_tokens()
114 {
115 CPPUNIT_ASSERT_EQUAL(store->getTokenCount(), (size_t)2);
116 }
117
118 void test_a_newly_created_object_store_containing_two_tokens::can_access_both_tokens()
119 {
120 // Retrieve both tokens and check that both are present
121 ObjectStoreToken* token1 = store->getToken(0);
122 ObjectStoreToken* token2 = store->getToken(1);
123
124 CPPUNIT_ASSERT(token1 != nulltoken);
125 CPPUNIT_ASSERT(token2 != nulltoken);
126 }
127
128 void test_a_newly_created_object_store_containing_two_tokens::assigned_labels_correctly_to_tokens()
129 {
130 ByteString label1 = "DEADC0FFEE";
131 ByteString label2 = "DEADBEEF";
132
133 // Retrieve both tokens and check that both are present
134 ObjectStoreToken* token1 = store->getToken(0);
135 ObjectStoreToken* token2 = store->getToken(1);
136
137 ByteString retrieveLabel1, retrieveLabel2;
138
139 CPPUNIT_ASSERT(token1->getTokenLabel(retrieveLabel1));
140 CPPUNIT_ASSERT(token2->getTokenLabel(retrieveLabel2));
141
142 CPPUNIT_ASSERT(label1 == retrieveLabel1 || label1 == retrieveLabel2);
143 CPPUNIT_ASSERT(label2 == retrieveLabel1 || label2 == retrieveLabel2);
144 CPPUNIT_ASSERT(label1 != label2);
145 }
146
147 void test_a_newly_created_object_store_containing_two_tokens::assigned_a_unique_serial_number_to_each_token()
148 {
149 // Retrieve both tokens and check that both are present
150 ObjectStoreToken* token1 = store->getToken(0);
151 ObjectStoreToken* token2 = store->getToken(1);
152
153 ByteString retrieveSerial1, retrieveSerial2;
154
155 CPPUNIT_ASSERT(token1->getTokenSerial(retrieveSerial1));
156 CPPUNIT_ASSERT(token2->getTokenSerial(retrieveSerial2));
157
158 CPPUNIT_ASSERT(retrieveSerial1 != retrieveSerial2);
159 }