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 22, 2024
1 parent ceb3e46 commit 56d047d
Show file tree
Hide file tree
Showing 10 changed files with 468 additions and 15 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
16 changes: 12 additions & 4 deletions internal/quantitative/leipzig/corpus.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,22 +73,24 @@ func NewLeipzigCorpus() corpus.Corpus {
return leipzig
}

// size returns the size of the corpus
// Size returns the size of the corpus
func (c *LeipzigCorpus) Size() string {
return c.size
}

// WithSize sets the size of the corpus
func (c *LeipzigCorpus) WithSize(size string) corpus.Corpus {
c.size = size
c.regenerateFileNames()
return c
}

// year returns the year of the corpus
// Year returns the year of the corpus
func (c *LeipzigCorpus) Year() string {
return c.year
}

// WithYear sets the year of the corpus
func (c *LeipzigCorpus) WithYear(year string) corpus.Corpus {
c.year = year
c.regenerateFileNames()
Expand All @@ -112,17 +114,19 @@ func (c *LeipzigCorpus) Source() string {
return c.source
}

// WithSource sets the source of the corpus
func (c *LeipzigCorpus) WithSource(source string) corpus.Corpus {
c.source = source
c.regenerateFileNames()
return c
}

// Lang returns the language of the corpus
// Language returns the language of the corpus
func (c *LeipzigCorpus) Language() string {
return c.lang
}

// WithLanguage sets the language of the corpus
func (c *LeipzigCorpus) WithLanguage(lang string) corpus.Corpus {
c.lang = lang
c.regenerateFileNames()
Expand Down Expand Up @@ -165,7 +169,11 @@ func (c *LeipzigCorpus) FetchCorpusFile() corpus.File {
log.Fatal().Err(err).Msg("Could not create destination directory")
}

cache := NewFile().WithCacheDir(cacheDir)
cache := NewFile().WithCacheDir(cacheDir).WithFilePath(c.filename)

if cache.FilePath() == "" {
log.Fatal().Msg("Cache file path is empty")
}

if info, err := os.Stat(path.Join(home, ".ftw", cache.FilePath())); err == nil {
log.Debug().Msgf("filename %s already exists", info.Name())
Expand Down
22 changes: 21 additions & 1 deletion internal/quantitative/leipzig/corpus_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,28 @@ 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.corpus = s.corpus.WithSize("10K")
s.cache = s.corpus.FetchCorpusFile()
s.iter = s.corpus.GetIterator(s.cache)
}
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
Loading

0 comments on commit 56d047d

Please sign in to comment.