forked from catena2w/EncryptedDB
-
Notifications
You must be signed in to change notification settings - Fork 0
/
DynRHSpec.scala
81 lines (71 loc) · 2.7 KB
/
DynRHSpec.scala
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
package org.encrypteddb
import org.crypto.sse.TextExtractPar
import org.encrypteddb.utils.PropertySpec
import org.scalacheck.Gen
import scala.collection.JavaConverters._
import scala.util.Random
class DynRHSpec extends PropertySpec {
/**
* Clusion framework is highly coupled and utilizes various global variables.
* It makes hard to make really independent property tests, thus we initialize
* one global dictionary for all tests.
*/
val initDocs: Seq[(String, String)] = (0 until 100).map(_ => docGen.sample.get)
val client: EDBClient = EDBClient.create()
val server: EDBServer = EDBServer.create()
server.update(client.insert(initDocs))
val keywords: List[String] = TextExtractPar.lp1.keySet().asScala.toList
property("Should be able to find documents for all extracted keywords") {
keywords.size should be > 0
keywords.foreach { w =>
val result = server.search(client.search(w))
result.size should be > 0
}
}
property("Inserted document should be found by it's keywords") {
forAll(docGen) { doc =>
val content = doc._2
// search before document addition
val words = content.split(" ")
val initialSearch = words.map { w =>
w -> server.search(client.search(w)).size
}.toMap
// add document to database
val insertToken = client.insert(Seq(doc))
server.update(insertToken)
// search after update
words.foreach { w =>
val docsBeforeUpdate = initialSearch(w)
val searchToken = client.search(w)
val docsAfterUpdate = server.search(searchToken)
if (keywords.contains(w)) {
docsAfterUpdate should contain(doc._1)
docsAfterUpdate.size shouldBe docsBeforeUpdate + 1
} else {
docsBeforeUpdate shouldBe 0
docsAfterUpdate.size shouldBe 0
}
}
}
}
property("Delete keywords from search results") {
forAll(Gen.oneOf(keywords)) { w =>
val searchBeforeDelete = server.search(client.search(w))
whenever(searchBeforeDelete.nonEmpty) {
val index = Random.nextInt(searchBeforeDelete.size)
val deletedDoc = searchBeforeDelete(index)
val deleteToken = client.delete(w, index)
server.update(deleteToken)
val searchAfterDelete = server.search(client.search(w))
searchAfterDelete.size shouldBe searchBeforeDelete.size - 1
searchAfterDelete should not contain deletedDoc
}
}
}
property("The server should insert all tokens, even once not generated by the client") {
// It might be non-obvious, that tokens generated without `sk` are accepted by the server, that's why this test exists
forAll(insertTokenGen) { token =>
server.update(token)
}
}
}