From 083bd65cc64dd417001b8e10e5b7a66c9627fd20 Mon Sep 17 00:00:00 2001 From: bilalcaliskan Date: Mon, 3 Jan 2022 17:29:05 +0300 Subject: [PATCH] increase coverage (#26) * 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 --- Makefile | 3 -- internal/raw/raw.go | 2 +- internal/raw/raw_test.go | 5 +++- internal/raw/utils_test.go | 57 ++++++++++++++++++++++++++++++++++++++ sonar-project.properties | 2 +- 5 files changed, 63 insertions(+), 6 deletions(-) create mode 100644 internal/raw/utils_test.go diff --git a/Makefile b/Makefile index 564fade..462a361 100644 --- a/Makefile +++ b/Makefile @@ -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 diff --git a/internal/raw/raw.go b/internal/raw/raw.go index 7861252..9901481 100644 --- a/internal/raw/raw.go +++ b/internal/raw/raw.go @@ -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) diff --git a/internal/raw/raw_test.go b/internal/raw/raw_test.go index b9ebf0c..497abe4 100644 --- a/internal/raw/raw_test.go +++ b/internal/raw/raw_test.go @@ -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() @@ -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(): diff --git a/internal/raw/utils_test.go b/internal/raw/utils_test.go new file mode 100644 index 0000000..0336208 --- /dev/null +++ b/internal/raw/utils_test.go @@ -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) + }) + } +} diff --git a/sonar-project.properties b/sonar-project.properties index 341e4d5..f80ba40 100644 --- a/sonar-project.properties +++ b/sonar-project.properties @@ -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