1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
mod compatibility;
use std::time::Duration;
use bonsaidb_core::connection::{AccessPolicy, Connection};
use bonsaidb_core::permissions::{Permissions, Statement};
#[cfg(feature = "encryption")]
use bonsaidb_core::test_util::EncryptedBasic;
use bonsaidb_core::test_util::{
Basic, BasicByBrokenParentId, BasicByParentId, BasicCollectionWithNoViews,
BasicCollectionWithOnlyBrokenParentId, BasicSchema, HarnessTest, TestDirectory,
};
use crate::config::{Builder, StorageConfiguration};
use crate::{Database, Storage};
macro_rules! define_local_suite {
($name:ident) => {
mod $name {
use super::*;
#[cfg(feature = "async")]
mod r#async {
use bonsaidb_core::connection::AsyncStorageConnection;
use super::*;
use crate::{AsyncDatabase, AsyncStorage};
struct AsyncTestHarness {
_directory: TestDirectory,
db: AsyncDatabase,
storage: AsyncStorage,
}
impl AsyncTestHarness {
async fn new(test: HarnessTest) -> anyhow::Result<Self> {
let directory =
TestDirectory::new(format!("async-{}-{}", stringify!($name), test));
let mut config =
StorageConfiguration::new(&directory).with_schema::<BasicSchema>()?;
if stringify!($name) == "memory" {
config = config.memory_only()
}
#[cfg(feature = "compression")]
{
config = config.default_compression(crate::config::Compression::Lz4);
}
let storage = AsyncStorage::open(config).await?;
let db = storage
.create_database::<BasicSchema>("tests", false)
.await?;
Ok(Self {
_directory: directory,
storage,
db,
})
}
const fn server_name() -> &'static str {
stringify!($name)
}
fn server(&self) -> &'_ AsyncStorage {
&self.storage
}
#[allow(dead_code)]
async fn connect_with_permissions(
&self,
permissions: Vec<Statement>,
_label: &str,
) -> anyhow::Result<AsyncDatabase> {
Ok(self
.db
.with_effective_permissions(Permissions::from(permissions))
.unwrap())
}
async fn connect(&self) -> anyhow::Result<AsyncDatabase> {
Ok(self.db.clone())
}
pub async fn shutdown(&self) -> anyhow::Result<()> {
Ok(())
}
}
bonsaidb_core::define_async_connection_test_suite!(AsyncTestHarness);
bonsaidb_core::define_async_pubsub_test_suite!(AsyncTestHarness);
bonsaidb_core::define_async_kv_test_suite!(AsyncTestHarness);
}
mod blocking {
use bonsaidb_core::connection::StorageConnection;
use super::*;
struct BlockingTestHarness {
_directory: TestDirectory,
db: Database,
storage: Storage,
}
impl BlockingTestHarness {
fn new(test: HarnessTest) -> anyhow::Result<Self> {
let directory =
TestDirectory::new(format!("blocking-{}-{}", stringify!($name), test));
let mut config =
StorageConfiguration::new(&directory).with_schema::<BasicSchema>()?;
if stringify!($name) == "memory" {
config = config.memory_only()
}
#[cfg(feature = "compression")]
{
config = config.default_compression(crate::config::Compression::Lz4);
}
let storage = Storage::open(config)?;
let db = storage.create_database::<BasicSchema>("tests", false)?;
Ok(Self {
_directory: directory,
storage,
db,
})
}
const fn server_name() -> &'static str {
stringify!($name)
}
fn server(&self) -> &'_ Storage {
&self.storage
}
#[allow(dead_code)]
fn connect_with_permissions(
&self,
permissions: Vec<Statement>,
_label: &str,
) -> anyhow::Result<Database> {
Ok(self
.db
.with_effective_permissions(Permissions::from(permissions))
.unwrap())
}
fn connect(&self) -> anyhow::Result<Database> {
Ok(self.db.clone())
}
pub fn shutdown(&self) -> anyhow::Result<()> {
Ok(())
}
}
bonsaidb_core::define_blocking_connection_test_suite!(BlockingTestHarness);
bonsaidb_core::define_blocking_pubsub_test_suite!(BlockingTestHarness);
bonsaidb_core::define_blocking_kv_test_suite!(BlockingTestHarness);
}
}
};
}
define_local_suite!(persisted);
define_local_suite!(memory);
#[test]
#[cfg_attr(not(feature = "compression"), allow(unused_mut))]
fn integrity_checks() -> anyhow::Result<()> {
let path = TestDirectory::new("integrity-checks");
let mut config = StorageConfiguration::new(&path);
#[cfg(feature = "compression")]
{
config = config.default_compression(crate::config::Compression::Lz4);
}
// To ensure full cleanup between each block, each runs in its own runtime;
// Add a doc with no views installed
{
let db = Database::open::<BasicCollectionWithNoViews>(config.clone())?;
let collection = db.collection::<BasicCollectionWithNoViews>();
collection.push(&Basic::default().with_parent_id(1))?;
}
// Connect with a new view and see the automatic update with a query
{
let db = Database::open::<BasicCollectionWithOnlyBrokenParentId>(config.clone())?;
// Give the integrity scanner time to run if it were to run (it shouldn't in this configuration).
std::thread::sleep(Duration::from_millis(100));
// NoUpdate should return data without the validation checker having run.
assert_eq!(
db.view::<BasicByBrokenParentId>()
.with_access_policy(AccessPolicy::NoUpdate)
.query()?
.len(),
0
);
// Regular query should show the correct data
assert_eq!(db.view::<BasicByBrokenParentId>().query()?.len(), 1);
}
// Connect with a fixed view, and wait for the integrity scanner to work
let db = Database::open::<Basic>(config.check_view_integrity_on_open(true))?;
for _ in 0_u8..100 {
std::thread::sleep(Duration::from_millis(1000));
if db
.view::<BasicByParentId>()
.with_access_policy(AccessPolicy::NoUpdate)
.with_key(&Some(1))
.query()?
.len()
== 1
{
return Ok(());
}
}
unreachable!("Integrity checker didn't run in the allocated time")
}
#[test]
#[cfg(feature = "encryption")]
fn encryption() -> anyhow::Result<()> {
use bonsaidb_core::schema::SerializedCollection;
let path = TestDirectory::new("encryption");
let document_header = {
let db = Database::open::<BasicSchema>(StorageConfiguration::new(&path))?;
let document_header = db
.collection::<EncryptedBasic>()
.push(&EncryptedBasic::new("hello"))?;
// Retrieve the document, showing that it was stored successfully.
let doc = db
.collection::<EncryptedBasic>()
.get(&document_header.id)?
.expect("doc not found");
assert_eq!(&EncryptedBasic::document_contents(&doc)?.value, "hello");
document_header
};
// By resetting the encryption key, we should be able to force an error in
// decryption, which proves that the document was encrypted. To ensure the
// server starts up and generates a new key, we must delete the sealing key.
std::fs::remove_file(path.join("master-keys"))?;
let db = Database::open::<BasicSchema>(StorageConfiguration::new(&path))?;
// Try retrieving the document, but expect an error decrypting.
if let Err(bonsaidb_core::Error::Other { error, .. }) =
db.collection::<EncryptedBasic>().get(&document_header.id)
{
assert!(error.contains("vault"));
} else {
panic!("successfully retrieved encrypted document without keys");
}
Ok(())
}
#[test]
fn expiration_after_close() -> anyhow::Result<()> {
use bonsaidb_core::keyvalue::KeyValue;
use bonsaidb_core::test_util::TimingTest;
loop {
let path = TestDirectory::new("expiration-after-close");
// To ensure full cleanup between each block, each runs in its own runtime;
let timing = TimingTest::new(Duration::from_millis(100));
// Set a key with an expiration, then close it. Then try to validate it
// exists after opening, and then expires at the correct time.
{
let db = Database::open::<()>(StorageConfiguration::new(&path))?;
// TODO This is a workaroun for the key-value expiration task
// taking ownership of an instance of Database. If this async
// task runs too quickly, sometimes things don't get cleaned up
// if that task hasn't completed. This pause ensures the startup
// tasks complete before we continue with the test. This should
// be replaced with a proper shutdown call for the local
// storage/database.
std::thread::sleep(Duration::from_millis(100));
db.set_key("a", &0_u32)
.expire_in(Duration::from_secs(3))
.execute()?;
}
{
let db = Database::open::<()>(StorageConfiguration::new(&path))?;
let key = db.get_key("a").query()?;
// Due to not having a reliable way to shut down the database,
// we can't make many guarantees about what happened after
// setting the key in the above block. If we get None back,
// we'll consider the test needing to retry. Once we have a
// shutdown operation that guarantees that the key-value store
// persists, the key.is_none() check shoud be removed, instead
// asserting `key.is_some()`.
if timing.elapsed() > Duration::from_secs(1) || key.is_none() {
println!("Retrying expiration_after_close because it was too slow");
continue;
}
timing.wait_until(Duration::from_secs(4));
assert!(db.get_key("a").query()?.is_none());
}
break;
}
Ok(())
}