pss: add testing for forwarding cache clearing.

This commit is contained in:
Jared Wasinger 2018-04-06 07:11:21 +00:00
parent 83355516c3
commit 04367fc5ea

View file

@ -144,9 +144,11 @@ func TestCache(t *testing.T) {
t.Fatal(err) t.Fatal(err)
} }
ps := newTestPss(privkey, nil, nil) ps := newTestPss(privkey, nil, nil)
pp := NewPssParams(privkey) pp := NewPssParams(privkey)
data := []byte("foo") data := []byte("foo")
datatwo := []byte("bar") datatwo := []byte("bar")
datathree := []byte("baz")
wparams := &whisper.MessageParams{ wparams := &whisper.MessageParams{
TTL: defaultWhisperTTL, TTL: defaultWhisperTTL,
Src: privkey, Src: privkey,
@ -169,6 +171,13 @@ func TestCache(t *testing.T) {
Payload: envtwo, Payload: envtwo,
To: to, To: to,
} }
wparams.Payload = datathree
woutmsg, err = whisper.NewSentMessage(wparams)
envthree, err := woutmsg.Wrap(wparams)
msgthree := &PssMsg{
Payload: envthree,
To: to,
}
digest := ps.digest(msg) digest := ps.digest(msg)
if err != nil { if err != nil {
@ -178,6 +187,11 @@ func TestCache(t *testing.T) {
if err != nil { if err != nil {
t.Fatalf("could not store cache msgtwo: %v", err) t.Fatalf("could not store cache msgtwo: %v", err)
} }
digestthree := ps.digest(msgthree)
if err != nil {
t.Fatalf("could not store cache msgthree: %v", err)
}
if digest == digesttwo { if digest == digesttwo {
t.Fatalf("different msgs return same hash: %d", digesttwo) t.Fatalf("different msgs return same hash: %d", digesttwo)
@ -197,10 +211,23 @@ func TestCache(t *testing.T) {
t.Fatalf("message %v should NOT have EXPIRE record in cache but checkCache returned true", msgtwo) t.Fatalf("message %v should NOT have EXPIRE record in cache but checkCache returned true", msgtwo)
} }
time.Sleep(pp.CacheTTL) time.Sleep(pp.CacheTTL + 1*time.Second)
err = ps.addFwdCache(msgthree)
if err != nil {
t.Fatalf("write to pss expire cache failed: %v", err)
}
if ps.checkFwdCache(msg) { if ps.checkFwdCache(msg) {
t.Fatalf("message %v should have expired from cache but checkCache returned true", msg) t.Fatalf("message %v should have expired from cache but checkCache returned true", msg)
} }
if _, ok := ps.fwdCache[digestthree]; !ok {
t.Fatalf("unexpired message should be in the cache: %v", digestthree)
}
if _, ok := ps.fwdCache[digesttwo]; ok {
t.Fatalf("expired message should have been cleared from the cache: %v", digesttwo)
}
} }
// matching of address hints; whether a message could be or is for the node // matching of address hints; whether a message could be or is for the node
@ -1309,6 +1336,7 @@ func newTestPss(privkey *ecdsa.PrivateKey, overlay network.Overlay, ppextra *Pss
pp.SymKeyCacheCapacity = ppextra.SymKeyCacheCapacity pp.SymKeyCacheCapacity = ppextra.SymKeyCacheCapacity
} }
ps := NewPss(overlay, pp) ps := NewPss(overlay, pp)
ps.Start(nil)
return ps return ps
} }