github.com/database64128/shadowsocks-go@v1.10.2-0.20240315062903-143a773533f1/ss2022/saltpool_test.go (about)

     1  package ss2022
     2  
     3  import (
     4  	"crypto/rand"
     5  	"testing"
     6  	"time"
     7  )
     8  
     9  func TestSaltPoolAddDuplicateSalts(t *testing.T) {
    10  	const retention = 100 * time.Millisecond
    11  	var salt [32]byte
    12  	_, err := rand.Read(salt[:])
    13  	if err != nil {
    14  		t.Fatal(err)
    15  	}
    16  
    17  	pool := NewSaltPool[[32]byte](retention)
    18  
    19  	// Check fresh salt.
    20  	if !pool.Check(salt) {
    21  		t.Fatal("Denied fresh salt.")
    22  	}
    23  
    24  	// Add fresh salt.
    25  	pool.Add(salt)
    26  
    27  	// Check the same salt again.
    28  	if pool.Check(salt) {
    29  		t.Fatal("Accepted duplicate salt.")
    30  	}
    31  
    32  	// Wait until salt expires.
    33  	time.Sleep(2 * retention)
    34  
    35  	// Check the expired salt.
    36  	if !pool.Check(salt) {
    37  		t.Fatal("Denied expired salt.")
    38  	}
    39  }