mirror of https://github.com/zlatinb/muwire
tests for trust service
parent
a0c9252a3f
commit
04cb23bbef
|
@ -29,12 +29,28 @@ class TrustService {
|
||||||
}
|
}
|
||||||
|
|
||||||
private void load() {
|
private void load() {
|
||||||
// TODO: load good and bad
|
if (persistGood.exists()) {
|
||||||
|
persistGood.eachLine {
|
||||||
|
good.add(new Destination(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if (persistBad.exists()) {
|
||||||
|
persistBad.eachLine {
|
||||||
|
bad.add(new Destination(it))
|
||||||
|
}
|
||||||
|
}
|
||||||
timer.schedule({persist()} as TimerTask, persistInterval, persistInterval)
|
timer.schedule({persist()} as TimerTask, persistInterval, persistInterval)
|
||||||
}
|
}
|
||||||
|
|
||||||
private void persist() {
|
private void persist() {
|
||||||
// TODO: persist good and bad
|
persistGood.delete()
|
||||||
|
good.each {
|
||||||
|
persistGood.append("${it.toBase64()}\n")
|
||||||
|
}
|
||||||
|
persistBad.delete()
|
||||||
|
bad.each {
|
||||||
|
persistBad.append("${it.toBase64()}\n")
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
TrustLevel getLevel(Destination dest) {
|
TrustLevel getLevel(Destination dest) {
|
||||||
|
|
|
@ -0,0 +1,83 @@
|
||||||
|
package com.muwire.core.trust
|
||||||
|
|
||||||
|
import org.junit.After
|
||||||
|
import org.junit.Before
|
||||||
|
import org.junit.Test
|
||||||
|
|
||||||
|
import com.muwire.core.Destinations
|
||||||
|
|
||||||
|
import net.i2p.data.Destination
|
||||||
|
|
||||||
|
class TrustServiceTest {
|
||||||
|
|
||||||
|
TrustService service
|
||||||
|
File persistGood, persistBad
|
||||||
|
Destinations dests = new Destinations()
|
||||||
|
|
||||||
|
@Before
|
||||||
|
void before() {
|
||||||
|
persistGood = new File("good.trust")
|
||||||
|
persistBad = new File("bad.trust")
|
||||||
|
persistGood.delete()
|
||||||
|
persistBad.delete()
|
||||||
|
persistGood.deleteOnExit()
|
||||||
|
persistBad.deleteOnExit()
|
||||||
|
service = new TrustService(persistGood, persistBad, 100)
|
||||||
|
service.start()
|
||||||
|
}
|
||||||
|
|
||||||
|
@After
|
||||||
|
void after() {
|
||||||
|
service.stop()
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testEmpty() {
|
||||||
|
assert TrustLevel.NEUTRAL == service.getLevel(dests.dest1)
|
||||||
|
assert TrustLevel.NEUTRAL == service.getLevel(dests.dest2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testOnEvent() {
|
||||||
|
service.onTrustEvent new TrustEvent(level: TrustLevel.TRUSTED, destination: dests.dest1)
|
||||||
|
service.onTrustEvent new TrustEvent(level: TrustLevel.DISTRUSTED, destination: dests.dest2)
|
||||||
|
|
||||||
|
assert TrustLevel.TRUSTED == service.getLevel(dests.dest1)
|
||||||
|
assert TrustLevel.DISTRUSTED == service.getLevel(dests.dest2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testPersist() {
|
||||||
|
service.onTrustEvent new TrustEvent(level: TrustLevel.TRUSTED, destination: dests.dest1)
|
||||||
|
service.onTrustEvent new TrustEvent(level: TrustLevel.DISTRUSTED, destination: dests.dest2)
|
||||||
|
|
||||||
|
Thread.sleep(250)
|
||||||
|
def trusted = new HashSet<>()
|
||||||
|
persistGood.eachLine {
|
||||||
|
trusted.add(new Destination(it))
|
||||||
|
}
|
||||||
|
def distrusted = new HashSet<>()
|
||||||
|
persistBad.eachLine {
|
||||||
|
distrusted.add(new Destination(it))
|
||||||
|
}
|
||||||
|
|
||||||
|
assert trusted.size() == 1
|
||||||
|
assert trusted.contains(dests.dest1)
|
||||||
|
assert distrusted.size() == 1
|
||||||
|
assert distrusted.contains(dests.dest2)
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
void testLoad() {
|
||||||
|
service.stop()
|
||||||
|
persistGood.append("${dests.dest1.toBase64()}\n")
|
||||||
|
persistBad.append("${dests.dest2.toBase64()}\n")
|
||||||
|
service = new TrustService(persistGood, persistBad, 100)
|
||||||
|
service.start()
|
||||||
|
Thread.sleep(10)
|
||||||
|
|
||||||
|
assert TrustLevel.TRUSTED == service.getLevel(dests.dest1)
|
||||||
|
assert TrustLevel.DISTRUSTED == service.getLevel(dests.dest2)
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue