-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTestSqlite.java
More file actions
411 lines (347 loc) · 17.1 KB
/
TestSqlite.java
File metadata and controls
411 lines (347 loc) · 17.1 KB
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
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package com.cosmian.findex;
import static org.junit.jupiter.api.Assertions.assertEquals;
import java.util.Arrays;
import java.util.Base64;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.stream.Collectors;
import org.junit.jupiter.api.BeforeAll;
import org.junit.jupiter.api.Test;
import com.cosmian.TestUtils;
import com.cosmian.jna.findex.DataFilter;
import com.cosmian.jna.findex.Findex;
import com.cosmian.jna.findex.ffi.SearchResults;
import com.cosmian.jna.findex.ffi.KeywordSet;
import com.cosmian.jna.findex.structs.IndexedValue;
import com.cosmian.jna.findex.structs.Keyword;
import com.cosmian.jna.findex.structs.Location;
import com.cosmian.utils.CloudproofException;
import com.cosmian.utils.Resources;
public class TestSqlite {
@BeforeAll
public static void before_all() {
TestUtils.initLogging();
}
public static HashMap<IndexedValue, Set<Keyword>> mapToIndex(String word, int userId) {
Set<Keyword> keywords = new HashSet<>(Arrays.asList(new Keyword(word)));
HashMap<IndexedValue, Set<Keyword>> indexedValuesAndWords = new HashMap<>();
indexedValuesAndWords.put(new Location(userId).toIndexedValue(), keywords);
return indexedValuesAndWords;
}
public static void printMap(String tableName,
Map<byte[], byte[]> map) {
System.out.println(tableName + " size: " + map.size());
for (Map.Entry<byte[], byte[]> entry : map.entrySet()) {
System.out.println(tableName + ": uid: " + Base64.getEncoder().encodeToString(entry.getKey()) + " value: "
+ Base64.getEncoder().encodeToString(entry.getValue()));
}
}
@Test
public void testMultiFetchEntryValues() throws Exception {
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Multi Fetch Entries");
System.out.println("---------------------------------------");
System.out.println("");
//
// Generate key and label
//
SqliteEntryTable entryTable1 = new SqliteEntryTable("target/entry_table_1");
SqliteEntryTable entryTable2 = new SqliteEntryTable("target/entry_table_2");
SqliteChainTable chainTable = new SqliteChainTable("target/chain_table");
entryTable1.flush();
entryTable2.flush();
chainTable.flush();
MultiSqlite compositeEntryTable = new MultiSqlite(Arrays.asList( entryTable1, entryTable2 ));
byte[] key = IndexUtils.loadKey();
String label = IndexUtils.loadLabel();
Findex findex = new Findex(key, label, 2, compositeEntryTable, chainTable);
compositeEntryTable.selectTable(0);
findex.add(mapToIndex("John", 1));
compositeEntryTable.selectTable(1);
findex.add(mapToIndex("John", 2));
long entryTable1Length = entryTable1.fetchAllUids().size();
long entryTable2Length = entryTable2.fetchAllUids().size();
long chainTableLength = chainTable.fetchAllUids().size();
System.out.println("Entry Table 1 length: " + entryTable1Length);
System.out.println("Entry Table 2 length: " + entryTable2Length);
System.out.println("Chain Table length: " + chainTableLength);
assertEquals(1, entryTable1Length);
assertEquals(1, entryTable2Length);
assertEquals(2, chainTableLength);
//
// Search
//
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Search Sqlite through multi entry tables");
System.out.println("---------------------------------------");
System.out.println("");
System.out.println("Searching with multiple entries values");
Set<Keyword> keywords = new HashSet<>(Arrays.asList(new Keyword("John")));
SearchResults searchResults = findex.search(keywords);
assertEquals(searchResults.getNumbers(), new HashSet<>(Arrays.asList(1L, 2L)));
System.out.println("<== successfully found all original French locations");
}
@Test
public void testUpsertAndSearchSqlite() throws Exception {
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Upsert Sqlite");
System.out.println("---------------------------------------");
System.out.println("");
//
// Generate key and label
//
byte[] key = IndexUtils.loadKey();
assertEquals(16, key.length);
String label = IndexUtils.loadLabel();
//
// Recover test vectors
//
Set<Long> expectedDbLocations = IndexUtils.loadExpectedDBLocations();
//
// Build dataset with DB uids and words
//
UsersDataset[] testFindexDataset = IndexUtils.loadDatasets();
//
// Prepare Sqlite tables and users
//
try (SqliteEntryTable entryTable = new SqliteEntryTable();
SqliteChainTable chainTable = new SqliteChainTable();) {
entryTable.flush();
chainTable.flush();
Findex findex = new Findex(key, label, entryTable, chainTable);
//
// Upsert
//
Map<IndexedValue, Set<Keyword>> indexedValuesAndWords = IndexUtils.index(testFindexDataset);
KeywordSet res = findex.add(indexedValuesAndWords);
int entryTableSize = entryTable.fetchAllUids().size();
int chainTableSize = chainTable.fetchAllUids().size();
assertEquals(583, res.getResults().size(), "wrong number of new upserted keywords");
assertEquals(583, entryTableSize, "invalid entry table items number");
assertEquals(618, chainTableSize, "invalid chain table items number");
System.out.println("Upserted " + res.getResults().size() + " new keywords.");
System.out.println("After insertion: entry_table size: " + entryTableSize);
System.out.println("After insertion: chain_table size: " + chainTableSize);
//
// Upsert a new keyword
//
HashMap<IndexedValue, Set<Keyword>> newIndexedKeyword = new HashMap<>();
Set<Keyword> expectedKeywords = new HashSet<>();
expectedKeywords.add(new Keyword("test"));
newIndexedKeyword.put(new IndexedValue(new Location(1)), expectedKeywords);
// It is returned the first time it is added.
Set<Keyword> newKeywords = findex.add(newIndexedKeyword).getResults();
assertEquals(expectedKeywords, newKeywords, "new keyword is not returned");
// It is *not* returned the second time it is added.
newKeywords = findex.add(newIndexedKeyword).getResults();
assert (newKeywords.isEmpty());
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Search Sqlite");
System.out.println("---------------------------------------");
System.out.println("");
{
SearchResults searchResults = findex.search(new String[] { "France"});
assertEquals(expectedDbLocations, searchResults.getNumbers());
System.out.println("<== successfully found all original French locations");
}
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Compact Sqlite");
System.out.println("---------------------------------------");
System.out.println("");
// This compact should do nothing except changing the label since the users
// table didn't change.
entryTableSize = entryTable.fetchAllUids().size();
chainTableSize = chainTable.fetchAllUids().size();
System.out.println("Before first compact: entry_table size: " + entryTableSize);
System.out.println("Before first compact: chain_table size: " + chainTableSize);
findex.compact(key, "NewLabel");
entryTableSize = entryTable.fetchAllUids().size();
chainTableSize = chainTable.fetchAllUids().size();
System.out.println("After first compact: entry_table size: " + entryTableSize);
System.out.println("After first compact: chain_table size: " + chainTableSize);
assertEquals(584, entryTableSize, "invalid entry table items number");
assertEquals(619, chainTableSize, "invalid chain table items number");
{
SearchResults searchResults = findex.search(new String[] { "France"});
assertEquals(expectedDbLocations, searchResults.getNumbers());
System.out.println("<== successfully found all French locations with the new label");
}
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Findex Re-Compact Sqlite");
System.out.println("---------------------------------------");
System.out.println("");
// Delete the user n°17 to test the compact indexedValuesAndWords
Set<Location> filteredLocations = new HashSet<Location>(Arrays.asList( new Location(17)));
expectedDbLocations.remove(17l);
entryTableSize = entryTable.fetchAllUids().size();
chainTableSize = chainTable.fetchAllUids().size();
System.out.println("Before 2nd compact: entry_table size: " + entryTableSize);
System.out.println("Before 2nd compact: chain_table size: " + chainTableSize);
findex.compact(key, "NewLabel2", new DataFilter() {
@Override
public List<Location> filter(List<Location> locations)
throws CloudproofException {
return locations.stream().filter((Location location) -> !filteredLocations.contains(location))
.collect(Collectors.toList());
}
});
{
// Search should return everyone but n°17
SearchResults searchResults = findex.search(new String[] { "France"});
assertEquals(expectedDbLocations, searchResults.getNumbers());
System.out.println("<== successfully found all French locations after removing one and compacting");
}
}
}
/**
* Check allocation problem during insertions. Allocation problem could occur when fetching entry table /* values
* whose sizes depend on words being indexed: the Entry Table Encrypted value is: `EncSym(𝐾value, (ict_uid𝑥𝑤𝑖,
* 𝐾𝑤𝑖 , 𝑤𝑖))`
*/
@Test
public void testCheckAllocations() throws Exception {
byte[] key = IndexUtils.loadKey();
String label = IndexUtils.loadLabel();
UsersDataset[] datasets = IndexUtils.loadDatasets();
Map<IndexedValue, Set<Keyword>> indexedValuesAndWords = IndexUtils.index(datasets);
try (SqliteUserDb db = new SqliteUserDb();
SqliteEntryTable entryTable = new SqliteEntryTable();
SqliteChainTable chainTable = new SqliteChainTable();) {
Findex findex = new Findex(key, label, entryTable, chainTable);
for (int i = 0; i < 100; i++) {
findex.add(indexedValuesAndWords);
}
}
System.out.println("<== successfully performed 100 upserts");
}
void verify(byte[] key,
String label,
HashMap<IndexedValue, Set<Keyword>> indexedValuesAndWords,
String dbPath,
Set<Long> expectedDbLocations)
throws Exception {
try (SqliteUserDb db = new SqliteUserDb(dbPath);
SqliteEntryTable entryTable = new SqliteEntryTable(dbPath);
SqliteChainTable chainTable = new SqliteChainTable(dbPath);)
{
int initialEntryTableSize = entryTable.fetchAllUids().size();
int initialChainTableSize = chainTable.fetchAllUids().size();
System.out
.println("Before insertion: entry_table size: " + initialEntryTableSize);
System.out
.println("Before insertion: chain_table size: " + initialChainTableSize);
Findex findex = new Findex(key, label, entryTable, chainTable);
//
// Search
//
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Verify: Findex Search Sqlite in " + dbPath);
System.out.println("---------------------------------------");
System.out.println("");
{
SearchResults searchResults = findex.search(new String[] { "France"});
assertEquals(expectedDbLocations, searchResults.getNumbers());
System.out.println("<== successfully found all original French locations");
}
//
// Upsert
//
UsersDataset[] users = UsersDataset.fromJson(Resources.load_resource("findex/single_user.json"));
Map<IndexedValue, Set<Keyword>> singleUserIndexedValuesAndWords = IndexUtils.index(users);
findex.add(singleUserIndexedValuesAndWords);
Set<Long> newExpectedDbLocations = new HashSet<>(expectedDbLocations);
for (UsersDataset user : users) {
newExpectedDbLocations.add(user.id);
}
int currentEntryTableSize = entryTable.fetchAllUids().size();
int currentChainTableSize = chainTable.fetchAllUids().size();
System.out.println("After insertion: entry_table size: " + currentEntryTableSize);
System.out.println("After insertion: chain_table size: " + currentChainTableSize);
assertEquals(initialEntryTableSize + 6, currentEntryTableSize);
assertEquals(initialChainTableSize + 8, currentChainTableSize);
//
// Search
//
System.out.println("");
System.out.println("---------------------------------------");
System.out.println("Verify: Findex Search Sqlite");
System.out.println("---------------------------------------");
System.out.println("");
{
SearchResults searchResults = findex.search(new String[] { "France"});
assertEquals(newExpectedDbLocations, searchResults.getNumbers());
}
}
}
@Test
public void test_non_regression_vectors() throws Exception {
//
// Recover key and label
//
byte[] key = IndexUtils.loadKey();
assertEquals(16, key.length);
String label = IndexUtils.loadLabel();
//
// Recover test vectors
//
Set<Long> expectedDbLocations = IndexUtils.loadExpectedDBLocations();
//
// Build dataset with DB uids and words
//
UsersDataset[] testFindexDataset = IndexUtils.loadDatasets();
HashMap<IndexedValue, Set<Keyword>> indexedValuesAndWords = IndexUtils.index(testFindexDataset);
//
// Browse all sqlite.db and check them
//
String testFolder = "src/test/resources/findex/non_regression";
for (String file : TestUtils.listFiles(testFolder)) {
String fullPath = testFolder + "/" + file;
String newPath = System.getProperty("java.io.tmpdir") + "/" + file;
java.nio.file.Files.copy(
new java.io.File(fullPath).toPath(),
new java.io.File(newPath).toPath(),
java.nio.file.StandardCopyOption.REPLACE_EXISTING,
java.nio.file.StandardCopyOption.COPY_ATTRIBUTES,
java.nio.file.LinkOption.NOFOLLOW_LINKS);
System.out.println("Non-regression test file: " + newPath);
verify(key, label, indexedValuesAndWords, newPath, expectedDbLocations);
System.out.println("... OK: Non-regression test file: " + fullPath);
}
}
@Test
public void test_generate_non_regression_vectors() throws Exception {
new java.io.File("./target/sqlite.db").delete();
//
// Recover key and label
//
byte[] key = IndexUtils.loadKey();
assertEquals(16, key.length);
String label = IndexUtils.loadLabel();
//
// Build dataset with DB uids and words
//
UsersDataset[] testFindexDataset = IndexUtils.loadDatasets();
Map<IndexedValue, Set<Keyword>> indexedValuesAndWords = IndexUtils.index(testFindexDataset);
//
// Generate non regression sqlite
//
//
// Upsert
//
Findex findex = new Findex(key,
label,
new SqliteEntryTable("./target/sqlite.db"),
new SqliteChainTable("./target/sqlite.db"));
findex.add(indexedValuesAndWords);
}
}