diff options
Diffstat (limited to 'dedupe/dedupe_test.go')
| -rw-r--r-- | dedupe/dedupe_test.go | 55 |
1 files changed, 44 insertions, 11 deletions
diff --git a/dedupe/dedupe_test.go b/dedupe/dedupe_test.go index cc6ce70..5891429 100644 --- a/dedupe/dedupe_test.go +++ b/dedupe/dedupe_test.go @@ -1,16 +1,15 @@ -package dedupe_test +package dedup import ( - "crypto/md5" - "github.com/crypto-smoke/meshtastic-go/dedupe" "testing" + "testing/quick" "time" ) +const expiresAfter = time.Millisecond + func TestPacketDeduplicatorSeen(t *testing.T) { - hasher := md5.New() - expiresAfter := 100 * time.Millisecond - dedup := dedupe.NewDeduplicator(hasher, expiresAfter) + dedup := NewDeduplicator(expiresAfter) sender := uint32(1) packetID := uint32(1) @@ -26,7 +25,7 @@ func TestPacketDeduplicatorSeen(t *testing.T) { } // Wait for expiration - time.Sleep(expiresAfter + 100*time.Millisecond) + time.Sleep(expiresAfter + time.Millisecond) // Test Seen with same packetID after expiration if dedup.Seen(sender, packetID) { @@ -34,10 +33,44 @@ func TestPacketDeduplicatorSeen(t *testing.T) { } } +func TestDuplicatorProp(t *testing.T) { + if err := quick.Check(func(s, p uint32) bool { + dedup := NewDeduplicator(expiresAfter) + // Test Seen with new packetID + if dedup.Seen(s, p) { + t.Error("Expected the packet to not have been seen") + return false + } + + // Test Seen with same packetID again + if !dedup.Seen(s, p) { + t.Error("Expected the packet to have been seen") + return false + } + return true + }, nil); err != nil { + t.Error(err) + } +} + +func FuzzDup(f *testing.F) { + f.Fuzz(func(t *testing.T, s uint32, p uint32) { + dedup := NewDeduplicator(10*time.Second) + // Test Seen with new packetID + if dedup.Seen(s, p) { + t.Errorf("Expected the packet %v/%v to not have been seen the first time", s, p) + } + + // Test Seen with same packetID again + if !dedup.Seen(s, p) { + t.Errorf("Expected the packet %v/%v to have been seen", s, p) + } + + }) +} + func TestPacketDeduplicatorSeenData(t *testing.T) { - hasher := md5.New() - expiresAfter := 100 * time.Millisecond - dedup := dedupe.NewDeduplicator(hasher, expiresAfter) + dedup := NewDeduplicator(expiresAfter) data := []byte("test data") @@ -52,7 +85,7 @@ func TestPacketDeduplicatorSeenData(t *testing.T) { } // Wait for expiration - time.Sleep(expiresAfter + 100*time.Millisecond) + time.Sleep(expiresAfter + time.Millisecond) // Test SeenData with same data after expiration if dedup.SeenData(data) { |
