Skip to content

Commit

Permalink
increase coverage (#26)
Browse files Browse the repository at this point in the history
* increase coverage

* add testing for constants

* exclude internal/raw/constants.go from sonar coverage report

* try to cover all cases

* sleep before running new case

* modify actions

* modify actions

* revert

* decrease floodMilliSeconds

* call cancel() function manually

* remove unneccassary select statement

* comment out unneccassary steps

* update ulimit command

* decrease file limit

* print limits

* append sysctl

* use sudo

* append to another file

* fix path

* change command

* use sh while running ulimit command

* play with file limits

* add another command

* add another command

* manual garbage collection

* remove bash commands

* remove manual GC

* decrease flood milliseconds

* decrease payloadLength

* fix changes

* use mutex to lock goroutine
  • Loading branch information
bilalcaliskan authored Jan 3, 2022
1 parent a0e1c08 commit 083bd65
Show file tree
Hide file tree
Showing 5 changed files with 63 additions and 6 deletions.
3 changes: 0 additions & 3 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,8 @@ ineffassign:
ineffassign ./...

test:
sudo sysctl -w fs.file-max=100000000
sudo sysctl -p
sudo go test ./... -v


build:
go build -o bin/main main.go

Expand Down
2 changes: 1 addition & 1 deletion internal/raw/raw.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func StartFlooding(destinationHost string, destinationPort, payloadLength int, f

destinationHost = resolveHost(destinationHost)

description := fmt.Sprintf("Flood is in progress, target=%s:%d, floodType=%s, payloadLength=%d",
description := fmt.Sprintf("Flood is in progress, target=%s:%d, floodType=%s, payloadLength=%d\n",
destinationHost, destinationPort, floodType, payloadLength)
bar := progressbar.DefaultBytes(-1, description)

Expand Down
5 changes: 4 additions & 1 deletion internal/raw/raw_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,11 +4,13 @@ import (
"context"
"github.com/stretchr/testify/assert"
"math/rand"
"sync"
"testing"
"time"
)

func TestStartFlooding(t *testing.T) {
var m sync.Mutex
srcIps := getIps()
srcPorts := getPorts()
macAddrs := getMacAddrs()
Expand All @@ -30,12 +32,13 @@ func TestStartFlooding(t *testing.T) {
defer cancel()
t.Logf("starting flood, caseName=%s, floodType=%s, floodMilliSeconds=%d\n", tc.name, tc.floodType, tc.floodMilliSeconds)
go func() {
m.Lock()
defer m.Unlock()
err := StartFlooding(tc.dstIp, tc.dstPort, tc.payloadLength, tc.floodType)
assert.Nil(t, err)
}()

select {

case <-time.After(120 * time.Second):
t.Log("overslept")
case <-ctx.Done():
Expand Down
57 changes: 57 additions & 0 deletions internal/raw/utils_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
package raw

import (
"github.com/stretchr/testify/assert"
"testing"
)

func TestResolveHost(t *testing.T) {
cases := []struct {
caseName string
host string
}{
{"case1", "example.com"},
{"case2", "93.184.216.34"},
}

for _, tc := range cases {
t.Run(tc.caseName, func(t *testing.T) {
res := resolveHost(tc.host)
assert.NotNil(t, res)
})
}
}

func TestIsDNS(t *testing.T) {
cases := []struct {
caseName string
host string
expected bool
}{
{"case1", "example.com", true},
{"case2", "93.184.216.34", false},
}

for _, tc := range cases {
t.Run(tc.caseName, func(t *testing.T) {
assert.Equal(t, isDNS(tc.host), tc.expected)
})
}
}

func TestIsIP(t *testing.T) {
cases := []struct {
caseName string
host string
expected bool
}{
{"case1", "example.com", false},
{"case2", "93.184.216.34", true},
}

for _, tc := range cases {
t.Run(tc.caseName, func(t *testing.T) {
assert.Equal(t, isIP(tc.host), tc.expected)
})
}
}
2 changes: 1 addition & 1 deletion sonar-project.properties
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,4 @@ sonar.tests=.
sonar.test.inclusions=**/*_test.go
sonar.test.exclusions=**/vendor/**
sonar.go.coverage.reportPaths=/github/workspace/coverage.txt
sonar.coverage.exclusions=cmd/**/*
sonar.coverage.exclusions=cmd/**/*,main.go,internal/raw/constants.go

0 comments on commit 083bd65

Please sign in to comment.