Skip to content

Commit

Permalink
add test for spam filter
Browse files Browse the repository at this point in the history
  • Loading branch information
gabe committed Apr 12, 2024
1 parent 74fba5a commit 75f5733
Show file tree
Hide file tree
Showing 4 changed files with 39 additions and 3 deletions.
21 changes: 21 additions & 0 deletions impl/internal/did/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,27 @@ func TestClientInvalidGateway(t *testing.T) {
assert.Nil(t, g)
}

func TestClientGet(t *testing.T) {
client, err := NewGatewayClient("https://diddht.tbddev.org")
require.NoError(t, err)
require.NotNil(t, client)

// get the same DID 20 different times and log how long it takes each time
// aggregate the average time to get the DID after the loop
var total time.Duration
for i := 0; i < 20; i++ {
start := time.Now()
_, _, _, err := client.GetDIDDocument("did:dht:i9xkp8ddcbcg8jwq54ox699wuzxyifsqx4jru45zodqu453ksz6y")
require.NoError(t, err)
since := time.Since(start)
t.Logf("time to get DID: %s in round %d", since, i)
total += since

}
average := total / 20
t.Logf("average time to get DID: %s", average)
}

func TestInvalidDIDDocument(t *testing.T) {
client, err := NewGatewayClient("https://diddht.tbddev.test")
require.NoError(t, err)
Expand Down
2 changes: 1 addition & 1 deletion impl/pkg/server/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ func (r *PkarrRouter) GetRecord(c *gin.Context) {
resp, err := r.service.GetPkarr(c, *id)
if err != nil {
// TODO(gabe): provide a more maintainable way to handle custom errors
if strings.Contains("spam", err.Error()) {
if strings.Contains(err.Error(), "spam") {
LoggingRespondErrMsg(c, fmt.Sprintf("too many requests for bad key %s", *id), http.StatusTooManyRequests)
return
}
Expand Down
15 changes: 15 additions & 0 deletions impl/pkg/server/pkarr_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,21 @@ func TestPkarrRouter(t *testing.T) {
pkarrRouter.GetRecord(c)
assert.Equal(t, http.StatusNotFound, w.Result().StatusCode, "unexpected %s", w.Result().Status)
})

t.Run("test get not found spam", func(t *testing.T) {
w := httptest.NewRecorder()
suffix := "cz13drbfxy3ih6xun4mw3cyiexrtfcs9gyp46o4469e93y36zhsy"
req := httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", testServerURL, suffix), nil)
c := newRequestContextWithParams(w, req, map[string]string{IDParam: suffix})
pkarrRouter.GetRecord(c)
assert.Equal(t, http.StatusNotFound, w.Result().StatusCode, "unexpected %s", w.Result().Status)

w = httptest.NewRecorder()
req = httptest.NewRequest(http.MethodGet, fmt.Sprintf("%s/%s", testServerURL, suffix), nil)
c = newRequestContextWithParams(w, req, map[string]string{IDParam: suffix})
pkarrRouter.GetRecord(c)
assert.Equal(t, http.StatusTooManyRequests, w.Result().StatusCode, "unexpected %s", w.Result().Status)
})
}

func testPkarrService(t *testing.T) service.PkarrService {
Expand Down
4 changes: 2 additions & 2 deletions impl/pkg/service/pkarr.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,8 +53,8 @@ func NewPkarrService(cfg *config.Config, db storage.Storage, d *dht.DHT) (*Pkarr
}

// create a new cache for bad gets to prevent spamming the DHT
cacheConfig.LifeWindow = 120 * time.Second
cacheConfig.CleanWindow = 60 * time.Second
cacheConfig.LifeWindow = 60 * time.Second
cacheConfig.CleanWindow = 30 * time.Second
badGetCache, err := bigcache.New(context.Background(), cacheConfig)
if err != nil {
return nil, ssiutil.LoggingErrorMsg(err, "failed to instantiate badGetCache")
Expand Down

0 comments on commit 75f5733

Please sign in to comment.