Skip to content

Commit

Permalink
test(teler): update malformed dataset case
Browse files Browse the repository at this point in the history
Signed-off-by: Dwi Siswanto <[email protected]>
  • Loading branch information
dwisiswant0 committed Feb 26, 2024
1 parent 40fa69b commit 875328a
Showing 1 changed file with 56 additions and 20 deletions.
76 changes: 56 additions & 20 deletions teler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -212,30 +212,66 @@ func TestNewWithWhitelist(t *testing.T) {
}

func TestNewWithMalformedDataset(t *testing.T) {
// Remove CVEs dataset
cvesFile := filepath.Join(homeDir, cacheDir, "cves.json")
if err := os.Remove(cvesFile); err != nil {
panic(err)
}
cvesPath := filepath.Join(homeDir, cacheDir, "cves.json")

// Initialize teler
telerMiddleware := New(Options{NoStderr: true})
wrappedHandler := telerMiddleware.Handler(handler)
t.Run("nonexistent", func(t *testing.T) {
// Remove CVEs dataset
err := os.Remove(cvesPath)
if err != nil && !os.IsNotExist(err) {
panic(err)
}

// Create a test server with the wrapped handler
ts := httptest.NewServer(wrappedHandler)
defer ts.Close()
// Initialize teler
telerMiddleware := New(Options{NoStderr: true})
wrappedHandler := telerMiddleware.Handler(handler)

// Create a request to send to the test server
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Fatal(err)
}
// Create a test server with the wrapped handler
ts := httptest.NewServer(wrappedHandler)
defer ts.Close()

_, err = client.Do(req)
if err != nil {
t.Fatal(err)
}
// Create a request to send to the test server
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Fatal(err)
}

_, err = client.Do(req)
if err != nil {
t.Fatal(err)
}
})

t.Run("malformed", func(t *testing.T) {
// Append CVEs dataset
f, err := os.OpenFile(cvesPath, os.O_APPEND|os.O_CREATE|os.O_WRONLY, 0644)
if err != nil {
t.Fatal(err)
}
defer f.Close()

if _, err := f.WriteString("AAAAAAAAAAAAAAAAAAaaaaaaaa\n"); err != nil {
t.Fatal(err)
}

// Initialize teler
telerMiddleware := New(Options{NoStderr: true})
wrappedHandler := telerMiddleware.Handler(handler)

// Create a test server with the wrapped handler
ts := httptest.NewServer(wrappedHandler)
defer ts.Close()

// Create a request to send to the test server
req, err := http.NewRequest("GET", ts.URL, nil)
if err != nil {
t.Fatal(err)
}

_, err = client.Do(req)
if err != nil {
t.Fatal(err)
}
})
}

func TestNewWithInMemory(t *testing.T) {
Expand Down

2 comments on commit 875328a

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 875328a Previous: fc389f7 Ratio
BenchmarkInitializeCommonWebAttack 59598341 ns/op 44601120 B/op 98674 allocs/op 28559147 ns/op 43950038 B/op 97947 allocs/op 2.09
BenchmarkInitializeCommonWebAttack - ns/op 59598341 ns/op 28559147 ns/op 2.09
BenchmarkInitializeCVE 61121850 ns/op 44601684 B/op 98673 allocs/op 28822842 ns/op 43949636 B/op 97946 allocs/op 2.12
BenchmarkInitializeCVE - ns/op 61121850 ns/op 28822842 ns/op 2.12
BenchmarkInitializeBadIPAddress 63657506 ns/op 44602965 B/op 98679 allocs/op 30122270 ns/op 43949158 B/op 97945 allocs/op 2.11
BenchmarkInitializeBadIPAddress - ns/op 63657506 ns/op 30122270 ns/op 2.11
BenchmarkInitializeBadReferrer 60633060 ns/op 44603138 B/op 98681 allocs/op 29976159 ns/op 43949640 B/op 97946 allocs/op 2.02
BenchmarkInitializeBadReferrer - ns/op 60633060 ns/op 29976159 ns/op 2.02
BenchmarkInitializeBadCrawler 60666102 ns/op 44603249 B/op 98680 allocs/op 28862908 ns/op 43949102 B/op 97944 allocs/op 2.10
BenchmarkInitializeBadCrawler - ns/op 60666102 ns/op 28862908 ns/op 2.10
BenchmarkInitializeWithoutBadIPAddress 60034378 ns/op 44603358 B/op 98681 allocs/op 29704793 ns/op 43948404 B/op 97942 allocs/op 2.02
BenchmarkInitializeWithoutBadIPAddress - ns/op 60034378 ns/op 29704793 ns/op 2.02
BenchmarkInitializeWithoutDirectoryBruteforce 60062093 ns/op 44603234 B/op 98681 allocs/op 29404007 ns/op 43949140 B/op 97944 allocs/op 2.04
BenchmarkInitializeWithoutDirectoryBruteforce - ns/op 60062093 ns/op 29404007 ns/op 2.04
BenchmarkInitializeCustomRules 60473394 ns/op 44605550 B/op 98712 allocs/op 29870038 ns/op 43953081 B/op 97982 allocs/op 2.02
BenchmarkInitializeCustomRules - ns/op 60473394 ns/op 29870038 ns/op 2.02

This comment was automatically generated by workflow using github-action-benchmark.

@github-actions
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

⚠️ Performance Alert ⚠️

Possible performance regression was detected for benchmark.
Benchmark result of this commit is worse than the previous benchmark result exceeding threshold 2.

Benchmark suite Current: 875328a Previous: fc389f7 Ratio
BenchmarkInitializeBadCrawler 59260922 ns/op 44602904 B/op 98677 allocs/op 28862908 ns/op 43949102 B/op 97944 allocs/op 2.05
BenchmarkInitializeBadCrawler - ns/op 59260922 ns/op 28862908 ns/op 2.05
BenchmarkInitializeWithoutBadCrawler 59284396 ns/op 44602714 B/op 98678 allocs/op 29275224 ns/op 43948716 B/op 97944 allocs/op 2.03
BenchmarkInitializeWithoutBadCrawler - ns/op 59284396 ns/op 29275224 ns/op 2.03
BenchmarkInitializeCustomRules 60601520 ns/op 44606981 B/op 98713 allocs/op 29870038 ns/op 43953081 B/op 97982 allocs/op 2.03
BenchmarkInitializeCustomRules - ns/op 60601520 ns/op 29870038 ns/op 2.03

This comment was automatically generated by workflow using github-action-benchmark.

Please sign in to comment.