forked from Konstantin8105/DDoS
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ddos_test.go
82 lines (73 loc) · 1.79 KB
/
ddos_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
package ddos_test
import (
"fmt"
"net/http"
"strconv"
"testing"
"time"
ddos "github.com/Konstantin8105/DDoS"
freeport "github.com/Konstantin8105/FreePort"
)
func TestNewDDoS(t *testing.T) {
d, err := ddos.New("http://127.0.0.1", 1)
if err != nil {
t.Error("Cannot create a new ddos structure. Error = ", err)
}
if d == nil {
t.Error("Cannot create a new ddos structure")
}
}
func TestDDoS(t *testing.T) {
port, err := freeport.Get()
if err != nil {
t.Errorf("Cannot found free tcp port. Error = ", err)
}
createServer(port, t)
url := "http://127.0.0.1:" + strconv.Itoa(port)
d, err := ddos.New(url, 100)
if err != nil {
t.Error("Cannot create a new ddos structure")
}
d.Run()
time.Sleep(time.Second)
d.Stop()
success, amount := d.Result()
if success == 0 || amount == 0 {
t.Errorf("Negative result of DDoS attack.\nSuccess requests = %v.\nAmount requests = %v", success, amount)
}
}
// Create a simple go server
func createServer(port int, t *testing.T) {
go func() {
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
fmt.Fprintf(w, "Hi there, I love %s!", r.URL.Path[1:])
})
if err := http.ListenAndServe(":"+strconv.Itoa(port), nil); err != nil {
t.Fatalf("Server is down. %v", err)
}
}()
}
func TestWorkers(t *testing.T) {
_, err := ddos.New("127.0.0.1", 0)
if err == nil {
t.Error("Cannot create a new ddos structure")
}
}
func TestUrl(t *testing.T) {
_, err := ddos.New("some_strange_host", 1)
if err == nil {
t.Error("Cannot create a new ddos structure")
}
}
func ExampleNew() {
workers := 100
d, err := ddos.New("http://127.0.0.1:80", workers)
if err != nil {
panic(err)
}
d.Run()
time.Sleep(time.Second)
d.Stop()
fmt.Println("DDoS attack server: http://127.0.0.1:80")
// Output: DDoS attack server: http://127.0.0.1:80
}