diff --git a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy index 778df57b..0e34a21e 100644 --- a/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy +++ b/core/src/main/groovy/com/muwire/core/trust/TrustService.groovy @@ -29,12 +29,28 @@ class TrustService { } 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) } 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) { diff --git a/core/src/test/groovy/com/muwire/core/trust/TrustServiceTest.groovy b/core/src/test/groovy/com/muwire/core/trust/TrustServiceTest.groovy new file mode 100644 index 00000000..c9bf3718 --- /dev/null +++ b/core/src/test/groovy/com/muwire/core/trust/TrustServiceTest.groovy @@ -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) + + } +}