Skip to content

Commit

Permalink
test: add more coverage
Browse files Browse the repository at this point in the history
Signed-off-by: Felipe Zipitria <felipe.zipitria@owasp.org>
  • Loading branch information
fzipi committed Sep 21, 2024
1 parent ceb3e46 commit b921abe
Show file tree
Hide file tree
Showing 9 changed files with 455 additions and 10 deletions.
15 changes: 12 additions & 3 deletions cmd/quantitative.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/spf13/cobra"

"github.com/coreruleset/go-ftw/experimental/corpus"
"github.com/coreruleset/go-ftw/internal/quantitative"
"github.com/coreruleset/go-ftw/output"
)
Expand Down Expand Up @@ -43,7 +44,7 @@ func NewQuantitativeCmd() *cobra.Command {
func runQuantitativeE(cmd *cobra.Command, _ []string) error {
cmd.SilenceUsage = true

corpus, _ := cmd.Flags().GetString("corpus")
corpusTypeAsString, _ := cmd.Flags().GetString("corpus")
corpusSize, _ := cmd.Flags().GetString("corpus-size")
corpusLang, _ := cmd.Flags().GetString("corpus-lang")
corpusYear, _ := cmd.Flags().GetString("corpus-year")
Expand Down Expand Up @@ -75,8 +76,16 @@ func runQuantitativeE(cmd *cobra.Command, _ []string) error {
}
out := output.NewOutput(wantedOutput, outputFile)

params := quantitative.QuantitativeParams{
Corpus: corpus,
var corpusType corpus.Type
if corpusTypeAsString != "" {
err = corpusType.Set(corpusTypeAsString)
if err != nil {
return err
}
}

params := quantitative.Params{
Corpus: corpusType,
CorpusSize: corpusSize,
CorpusYear: corpusYear,
CorpusLang: corpusLang,
Expand Down
18 changes: 17 additions & 1 deletion experimental/corpus/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,13 +18,29 @@
// interface is subject to change.
package corpus

// Define an enum for CorpusType
import "fmt"

// Type is the type of the corpus
type Type string

const (
Leipzig Type = "leipzig"
)

func (t *Type) String() string {
return string(*t)
}

func (t *Type) Set(value string) error {
switch value {
case "leipzig":
*t = Leipzig
return nil
default:
return fmt.Errorf("invalid option for Type: '%s'", value)
}
}

// File interface is used to interact with Corpus files.
// It provides methods for setting the cache directory and file path.
type File interface {
Expand Down
20 changes: 20 additions & 0 deletions internal/quantitative/leipzig/corpus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,26 @@ func (s *leipzigCorpusTestSuite) TestWithSize() {
s.Require().Equal("300K", s.corpus.Size())
}

func (s *leipzigCorpusTestSuite) TestWithYear() {
s.corpus.WithYear("2024")
s.Require().Equal("2024", s.corpus.Year())
}

func (s *leipzigCorpusTestSuite) TestWithSource() {
s.corpus.WithSource("news")
s.Require().Equal("news", s.corpus.Source())
}

func (s *leipzigCorpusTestSuite) TestWithLanguage() {
s.corpus.WithLanguage("eng")
s.Require().Equal("eng", s.corpus.Language())
}

func (s *leipzigCorpusTestSuite) TestWithURL() {
s.corpus.WithURL("https://downloads.wortschatz-leipzig.de/corpora")
s.Require().Equal("https://downloads.wortschatz-leipzig.de/corpora", s.corpus.URL())
}

func (s *leipzigCorpusTestSuite) TestGetIterator() {
s.corpus.WithSize("10K")
s.cache = s.corpus.FetchCorpusFile()
Expand Down
186 changes: 186 additions & 0 deletions internal/quantitative/leipzig/file_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,186 @@
// Copyright 2024 OWASP CRS Project
// SPDX-License-Identifier: Apache-2.0

package leipzig

import (
"reflect"
"testing"

"github.com/coreruleset/go-ftw/experimental/corpus"
)

func TestFile_CacheDir(t *testing.T) {
type fields struct {
cacheDir string
filePath string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1",
fields: fields{
cacheDir: "cacheDir",
filePath: "filePath",
},
want: "cacheDir",
},
{
name: "Test 2",
fields: fields{
cacheDir: "cacheDir2",
filePath: "filePath2",
},
want: "cacheDir2",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := File{
cacheDir: tt.fields.cacheDir,
filePath: tt.fields.filePath,
}
if got := f.CacheDir(); got != tt.want {
t.Errorf("CacheDir() = %v, want %v", got, tt.want)
}
})
}
}

func TestFile_FilePath(t *testing.T) {
type fields struct {
cacheDir string
filePath string
}
tests := []struct {
name string
fields fields
want string
}{
{
name: "Test 1",
fields: fields{
cacheDir: "cacheDir",
filePath: "filePath",
},
want: "filePath",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := File{
cacheDir: tt.fields.cacheDir,
filePath: tt.fields.filePath,
}
if got := f.FilePath(); got != tt.want {
t.Errorf("FilePath() = %v, want %v", got, tt.want)
}
})
}
}

func TestFile_WithCacheDir(t *testing.T) {
type fields struct {
cacheDir string
filePath string
}
type args struct {
cacheDir string
}
tests := []struct {
name string
fields fields
args args
want corpus.File
}{
{
name: "Test 1",
fields: fields{
cacheDir: "cacheDir1",
filePath: "filePath",
},
args: args{
cacheDir: "cacheDir10",
},
want: File{
cacheDir: "cacheDir10",
filePath: "filePath",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := File{
cacheDir: tt.fields.cacheDir,
filePath: tt.fields.filePath,
}
if got := f.WithCacheDir(tt.args.cacheDir); !reflect.DeepEqual(got, tt.want) {
t.Errorf("WithCacheDir() = %v, want %v", got, tt.want)
}
})
}
}

func TestFile_WithFilePath(t *testing.T) {
type fields struct {
cacheDir string
filePath string
}
type args struct {
filePath string
}
tests := []struct {
name string
fields fields
args args
want corpus.File
}{
{
name: "Test 1",
fields: fields{
cacheDir: "cacheDir",
filePath: "filePath1",
},
args: args{
filePath: "filePath2",
},
want: File{
cacheDir: "cacheDir",
filePath: "filePath2",
},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
f := File{
cacheDir: tt.fields.cacheDir,
filePath: tt.fields.filePath,
}
if got := f.WithFilePath(tt.args.filePath); !reflect.DeepEqual(got, tt.want) {
t.Errorf("WithFilePath() = %v, want %v", got, tt.want)
}
})
}
}

func TestNewFile(t *testing.T) {
tests := []struct {
name string
want corpus.File
}{
{
name: "Test 1",
want: File{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if got := NewFile(); !reflect.DeepEqual(got, tt.want) {
t.Errorf("NewFile() = %v, want %v", got, tt.want)
}
})
}
}
8 changes: 6 additions & 2 deletions internal/quantitative/local_engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
package quantitative

import (
"fmt"
"net/http"
"os"
"path"
Expand All @@ -13,7 +14,10 @@ import (
"github.com/stretchr/testify/suite"
)

const crsUrl = "https://github.com/coreruleset/coreruleset/releases/download/v4.6.0/coreruleset-4.6.0-minimal.tar.gz"
const (
crsUrl = "https://github.com/coreruleset/coreruleset/releases/download/v4.6.0/coreruleset-4.6.0-minimal.tar.gz"
crsTestVersion = "4.6.0"
)

type localEngineTestSuite struct {
suite.Suite
Expand All @@ -36,7 +40,7 @@ func (s *localEngineTestSuite) SetupTest() {

err := client.Get()
s.Require().NoError(err)
s.engine = NewEngine(path.Join(s.dir, "coreruleset-4.6.0"), 1)
s.engine = NewEngine(path.Join(s.dir, fmt.Sprintf("coreruleset-%s", crsTestVersion)), 1)
s.Require().NotNil(s.engine)
}

Expand Down
8 changes: 4 additions & 4 deletions internal/quantitative/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@ import (
"github.com/coreruleset/go-ftw/output"
)

// QuantitativeParams holds the parameters for the quantitative tests
type QuantitativeParams struct {
// Params holds the parameters for the quantitative tests
type Params struct {
// Lines is the number of lines of input to process before stopping
Lines int
// Fast is the process 1 in every X lines of input ('fast run' mode)
Expand All @@ -33,7 +33,7 @@ type QuantitativeParams struct {
// CorpusSize is the corpus size to use for the quantitative tests
CorpusSize string
// Corpus is the corpus to use for the quantitative tests
Corpus string
Corpus corpus.Type
// CorpusLang is the language of the corpus
CorpusLang string
// CorpusYear is the year of the corpus
Expand All @@ -54,7 +54,7 @@ func NewCorpus(corpusType corpus.Type) corpus.Corpus {
}

// RunQuantitativeTests runs all quantitative tests
func RunQuantitativeTests(params QuantitativeParams, out *output.Output) error {
func RunQuantitativeTests(params Params, out *output.Output) error {
out.Println("Running quantitative tests")

log.Trace().Msgf("Lines: %d", params.Lines)
Expand Down
Loading

0 comments on commit b921abe

Please sign in to comment.