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