2013-08-14 17:30:13 +02:00
|
|
|
package main
|
|
|
|
|
|
|
|
|
|
import (
|
|
|
|
|
"bytes"
|
|
|
|
|
"net/http"
|
|
|
|
|
"net/http/httptest"
|
2013-08-14 21:03:03 +02:00
|
|
|
"os"
|
2013-08-14 17:30:13 +02:00
|
|
|
"sync/atomic"
|
|
|
|
|
"testing"
|
|
|
|
|
"time"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAttackRate(t *testing.T) {
|
|
|
|
|
hitCount := uint64(0)
|
|
|
|
|
server := httptest.NewServer(
|
|
|
|
|
http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
|
atomic.AddUint64(&hitCount, 1)
|
|
|
|
|
}),
|
|
|
|
|
)
|
|
|
|
|
targets, err := NewTargets(bytes.NewBufferString("GET " + server.URL + "\n"))
|
|
|
|
|
if err != nil {
|
|
|
|
|
t.Fatal(err)
|
|
|
|
|
}
|
2013-08-17 13:47:51 +02:00
|
|
|
rate := uint64(5000)
|
2013-08-14 21:03:03 +02:00
|
|
|
rep := NewTextReporter()
|
|
|
|
|
attack(targets, "random", rate, 1*time.Second, rep)
|
2013-08-17 13:47:51 +02:00
|
|
|
if hits := atomic.LoadUint64(&hitCount); hits != rate {
|
2013-08-14 21:03:03 +02:00
|
|
|
rep.Report(os.Stdout)
|
|
|
|
|
t.Fatalf("Wrong number of hits: want %d, got %d\n", rate, hits)
|
2013-08-14 17:30:13 +02:00
|
|
|
}
|
|
|
|
|
}
|