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
#![allow(unused_imports)]
use log::{info, error, debug};
use serde::{Deserialize};
use serde::{Serialize, de::DeserializeOwned};
use crate::dio::*;
use crate::crypto::*;
use crate::prelude::*;
#[cfg(test)]
#[derive(Serialize, Deserialize, Debug, Clone)]
pub enum TestEnumDao
{
None,
Blah1,
Blah2(u32),
Blah3(String),
Blah4,
Blah5,
}
#[cfg(test)]
impl Default
for TestEnumDao
{
fn default() -> TestEnumDao {
TestEnumDao::None
}
}
#[cfg(test)]
#[derive(Serialize, Deserialize, Debug, Clone, Default)]
pub struct TestStructDao
{
val: u32,
hidden: String,
inner: DaoVec<TestEnumDao>,
}
#[tokio::main]
#[test]
async fn test_dio()
{
crate::utils::bootstrap_env();
debug!("generating crypto keys");
let write_key = PrivateSignKey::generate(crate::crypto::KeySize::Bit192);
let write_key2 = PrivateSignKey::generate(KeySize::Bit256);
let read_key = EncryptKey::generate(crate::crypto::KeySize::Bit192);
let root_public_key = write_key.as_public_key();
debug!("building the session");
let cfg = ConfAte::default();
let mut session = AteSession::new(&cfg);
session.properties.push(AteSessionProperty::WriteKey(write_key.clone()));
session.properties.push(AteSessionProperty::WriteKey(write_key2.clone()));
session.properties.push(AteSessionProperty::ReadKey(read_key.clone()));
session.properties.push(AteSessionProperty::Identity("author@here.com".to_string()));
debug!("{}", session);
let key1;
let key2;
let key3;
let chain_name = format!("test_dio_{}", PrimaryKey::generate().to_string());
{
debug!("creating the chain-of-trust");
let mut mock_cfg = crate::conf::mock_test_config();
let (chain, _builder) = crate::trust::create_test_chain(&mut mock_cfg, chain_name.clone(), false, false, Some(root_public_key.clone())).await;
{
let mut dio = chain.dio(&session).await;
{
debug!("storing data object 1");
let mut mock_dao = TestStructDao::default();
mock_dao.val = 1;
mock_dao.hidden = "This text should be hidden".to_string();
let mut dao1 = dio.store(mock_dao).unwrap();
let mut dao3 = dao1.push(&mut dio, dao1.inner, TestEnumDao::Blah1).unwrap();
key1 = dao1.key().clone();
debug!("key1: {}", key1.as_hex_string());
key3 = dao3.key().clone();
debug!("key3: {}", key3.as_hex_string());
debug!("loading data object 1");
debug!("setting read and write crypto keys");
dao1.auth_mut().read = ReadOption::Specific(read_key.hash());
dao1.auth_mut().write = WriteOption::Specific(write_key2.hash());
dao1.commit(&mut dio).unwrap();
dao3.commit(&mut dio).unwrap();
}
dio.commit().await.unwrap();
}
{
debug!("new DIO context");
let mut dio = chain.dio(&session).await;
{
debug!("loading data object 1");
let mut dao1 = dio.load::<TestStructDao>(&key1).await.unwrap();
debug!("updating data object");
dao1.val = 2;
dao1.commit(&mut dio).unwrap();
debug!("load the object again");
let test: Dao<TestStructDao> = dio.load(&key1).await.expect("The dirty data object should have been read after it was flushed");
assert_eq!(test.val, 2 as u32);
}
{
debug!("loading data object 1 in new scope");
let mut dao1 = dio.load::<TestStructDao>(&key1).await.unwrap();
debug!("modifying data object 1");
dao1.val = 3;
dao1.commit(&mut dio).unwrap();
}
{
debug!("storing data object 2");
let mut dao2 = dio.store(TestEnumDao::Blah4).unwrap();
debug!("adding a write crypto key");
dao2.auth_mut().write = WriteOption::Specific(write_key2.as_public_key().hash());
dao2.commit(&mut dio).unwrap();
key2 = dao2.key().clone();
debug!("key2: {}", key2.as_hex_string());
}
dio.commit().await.expect("The DIO should commit");
}
{
debug!("new DIO context");
let mut dio = chain.dio(&session).await;
debug!("loading data object 1");
let test = dio.load::<TestStructDao>(&key1).await.expect("The dirty data object should have been read after it was flushed");
assert_eq!(test.val, 3);
debug!("loading children");
let test3 = test.iter(&mut dio, test.inner).await.unwrap().next().expect("Three should be a data object in this collection");
assert_eq!(test3.key(), &key3);
}
{
debug!("new DIO context");
let mut dio = chain.dio(&session).await;
debug!("loading data object 1");
let mut dao1: Dao<TestStructDao> = dio.load(&key1).await.expect("The data object should have been read");
assert_eq!(dao1.val, 3);
dao1.val = 4;
dao1.commit(&mut dio).unwrap();
debug!("loading data object 2");
let dao2 = dio.load::<TestEnumDao>(&key2).await.expect("The record should load before we delete it in this session");
debug!("deleting data object 2");
dao2.delete(&mut dio).unwrap();
debug!("negative test on loading data object 2");
dio.load::<TestEnumDao>(&key2).await.expect_err("This load should fail as we deleted the record");
dio.commit().await.expect("The DIO should commit");
}
}
{
debug!("reloading the chain of trust");
let mut mock_cfg = crate::conf::mock_test_config();
let (chain, _builder) = crate::trust::create_test_chain(&mut mock_cfg, chain_name.clone(), false, false, Some(root_public_key.clone())).await;
{
let mut dio = chain.dio(&session).await;
debug!("loading data object 1");
let dao1: Dao<TestStructDao> = dio.load(&key1).await.expect("The data object should have been read");
assert_eq!(dao1.val, 4);
debug!("loading data object 2");
dio.load::<TestEnumDao>(&key2).await.expect_err("This load should fail as we deleted the record");
}
debug!("destroying the chain of trust");
chain.single().await.destroy().await.unwrap();
}
}