Skip to content

Commit

Permalink
additional unit tests and fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
kaanaktas committed Aug 20, 2022
1 parent ff8313e commit 72c88ef
Show file tree
Hide file tree
Showing 13 changed files with 161 additions and 70 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,7 @@ In order for the newly created **custom_owasp_attack_sqli.json** file to be cons
`
## policy

We can create reusable policies in our common policy rule file (similar to **/testdata/common_policies.json**) and use them
We can create reusable policies in our common policy rule file (similar to **/testconfig/common_policies.json**) and use them
to combine different policies in **policy_rule_set.json**. This file can be named based on requirement and should be defined in the **GO_SLM_COMMON_POLICIES_PATH**
environment variable as in the example below.

Expand Down
14 changes: 14 additions & 0 deletions common_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
package main

import (
"os"
"testing"
)

func TestMain(m *testing.M) {
_ = os.Setenv("GO_SLM_POLICY_RULE_SET_PATH", "/testconfig/policy_rule_set.json")
_ = os.Setenv("GO_SLM_COMMON_POLICIES_PATH", "/testconfig/common_policies.json")
_ = os.Setenv("GO_SLM_CURRENT_MODULE_NAME", "github.com/kaanaktas/dummy")

os.Exit(m.Run())
}
6 changes: 3 additions & 3 deletions datafilter/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ func Load(dataFilterRuleSetPath string) {
}
err = json.Unmarshal(content, &customRuleSet)
if err != nil {
msg := fmt.Sprintf("Can't unmarshall the content of datafilter_rule_set.json. Error: %s", err)
msg := fmt.Sprintf("Can't unmarshall the content of %s. Error: %s", dataFilterRuleSetPath, err)
panic(msg)
}

Expand Down Expand Up @@ -85,12 +85,12 @@ func Load(dataFilterRuleSetPath string) {
if rule.CustomPath != "" {
content, err = config.ReadFile(filepath.Join(config.RootDirectory, rule.CustomPath))
if err != nil {
msg := fmt.Sprintf("Error while reading %s. Error: %s", dataFilterRuleSetPath, err)
msg := fmt.Sprintf("Error while reading %s. Error: %s", rule.CustomPath, err)
panic(msg)
}
err = json.Unmarshal(content, &customPatterns)
if err != nil {
msg := fmt.Sprintf("Can't unmarshall the content of datafilter_rule_set.json. Error: %s", err)
msg := fmt.Sprintf("Can't unmarshall the content of %s. Error: %s", rule.CustomPath, err)
panic(msg)
}
}
Expand Down
29 changes: 1 addition & 28 deletions datafilter_test.go
Original file line number Diff line number Diff line change
@@ -1,22 +1,11 @@
package main

import (
"github.com/kaanaktas/go-slm/cache"
"github.com/kaanaktas/go-slm/config"
"github.com/kaanaktas/go-slm/executor"
"github.com/labstack/gommon/log"
"os"
"testing"
)

func TestMain(m *testing.M) {
_ = os.Setenv("GO_SLM_POLICY_RULE_SET_PATH", "/policy/testdata/policy_rule_set.json")
_ = os.Setenv("GO_SLM_COMMON_RULES_PATH", "/policy/testdata/common_policies.json")
_ = os.Setenv("GO_SLM_CURRENT_MODULE_NAME", "github.com/kaanaktas/dummy")

os.Exit(m.Run())
}

func TestExecute(t *testing.T) {
type args struct {
data string
Expand Down Expand Up @@ -45,7 +34,7 @@ func TestExecute(t *testing.T) {
name: "test_pan_filter",
panic: true,
args: args{
data: "44044333322221111swfkjbfjksjkf4444333322221111dedeefefefe",
data: "44044 3360110004012 8888 88881881990139424332 2221111",
serviceName: "test",
}},
{
Expand All @@ -61,7 +50,6 @@ func TestExecute(t *testing.T) {
defer func() {
r := recover()
if (r != nil) && tt.panic == false {
log.Error(r)
t.Errorf("%s did panic", tt.name)
} else if (r == nil) && tt.panic == true {
t.Errorf("%s didn't panic", tt.name)
Expand All @@ -71,18 +59,3 @@ func TestExecute(t *testing.T) {
})
}
}

func TestCache(t *testing.T) {
_ = os.Setenv("GO_SLM_DATA_FILTER_RULE_SET_PATH", "/datafilter/testdata/datafilter_rule_set.json")

cacheIn := cache.NewInMemory()
cacheIn.Flush()

executor.Execute("test_sqli_filter", "test", config.Request)
if _, ok := cacheIn.Get("test_pan_process"); !ok {
t.Error("test_pan_process is not in the cache")
}
if _, ok := cacheIn.Get("pan_process"); !ok {
t.Error("pan_process is not in the cache")
}
}
2 changes: 1 addition & 1 deletion executor/executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ func Execute(data, serviceName, direction string) {
case <-closeCh:
}

log.Println("no_match with datafilter rules")
log.Println("no match with datafilter rules")
}

func processor(policies []policy.CommonPolicy, in chan<- datafilter.Validate, breaker <-chan string) {
Expand Down
21 changes: 10 additions & 11 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,34 +9,33 @@ import (
"time"
)

func init() {
_ = os.Setenv("GO_SLM_COMMON_POLICIES_PATH", "/policy/testdata/common_policies.json")
_ = os.Setenv("GO_SLM_POLICY_RULE_SET_PATH", "/policy/testdata/policy_rule_set.json")
_ = os.Setenv("GO_SLM_DATA_FILTER_RULE_SET_PATH", "/testdata/datafilter_rule_set.json")
//pretending to be imported by another project
_ = os.Setenv("GO_SLM_CURRENT_MODULE_NAME", "github.com/kaanaktas/dummy")
}

func main() {
defer config.Elapsed("Execution")()
defer func() {
time.Sleep(10 * time.Millisecond)
log.Println("All Channels were closed successfully. Number of goroutine:", runtime.NumGoroutine())
}()

_ = os.Setenv("GO_SLM_COMMON_POLICIES_PATH", "/testconfig/common_policies.json")
_ = os.Setenv("GO_SLM_POLICY_RULE_SET_PATH", "/testconfig/policy_rule_set.json")
_ = os.Setenv("GO_SLM_DATA_FILTER_RULE_SET_PATH", "/testconfig/custom_datafilter_rule_set.json")
//pretending to be imported by another project
_ = os.Setenv("GO_SLM_CURRENT_MODULE_NAME", "github.com/kaanaktas/dummy")

serviceName := "test"
testData := [...]string{
data := []string{
"clear data with no match",
"admin' AND 1=1 --",
"http://testing.com/book.html?default=<script>alert(document.cookie)</script>",
"https://testing.com/book.html?default=<script>alert(document.cookie)</script>",
"44044 3360110004012 8888 88881881990139424332 2221111"}

for _, data := range testData {
for _, data := range data {
func() {
defer func() {
if r := recover(); r != nil {
log.Println("Recovered in Execute", r)
}
log.Println("--------")
}()
log.Println("Filtering data:", data)
executor.Execute(data, serviceName, config.Request)
Expand Down
2 changes: 1 addition & 1 deletion policy/load.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
"path/filepath"
)

const Key = "access_rule"
const Key = "policy_rule"

var cacheIn = cache.NewInMemory()

Expand Down
113 changes: 113 additions & 0 deletions policy_load_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
package main

import (
"github.com/kaanaktas/go-slm/cache"
"github.com/kaanaktas/go-slm/config"
"github.com/kaanaktas/go-slm/datafilter"
"github.com/kaanaktas/go-slm/executor"
"github.com/kaanaktas/go-slm/policy"
"os"
"testing"
)

func TestDataFilterRuleLoad(t *testing.T) {
_ = os.Setenv("GO_SLM_DATA_FILTER_RULE_SET_PATH", "/testconfig/custom_datafilter_rule_set.json")

cacheIn := cache.NewInMemory()
cacheIn.Flush()

executor.Execute("test_data", "test", config.Request)

type cachedDataFilterRule struct {
name string
size int
}

tests := []struct {
name string
policy cachedDataFilterRule
}{
{
name: "cached_pan_process", policy: cachedDataFilterRule{
name: "pan_process",
size: 1,
},
},
{
name: "cached_custom_pan_process", policy: cachedDataFilterRule{
name: "custom_pan_process",
size: 1,
},
},
{
name: "cached_sqli", policy: cachedDataFilterRule{
name: "sqli",
size: 44,
},
},
{
name: "cached_xss", policy: cachedDataFilterRule{
name: "xss",
size: 27,
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if cachedData, ok := cacheIn.Get(test.policy.name); ok {
if len(cachedData.([]datafilter.Validate)) != test.policy.size {
t.Errorf("cached data size doesn't match up. Expected: %d, got:%d", test.policy.size,
len(cachedData.([]datafilter.Validate)))
}
} else {
t.Errorf("%s is not in the cache", test.policy.name)
}
})
}
}

func TestPolicyLoad(t *testing.T) {
cacheIn := cache.NewInMemory()
cacheIn.Flush()

executor.Execute("test_data", "test", config.Request)

type cachedPolicyRule struct {
name string
size int
policies []string
}

tests := []struct {
name string
policy cachedPolicyRule
}{
{
name: "cached_policy_rule", policy: cachedPolicyRule{
name: "policy_rule",
size: 4,
policies: []string{"test_request", "test_response", "test2_request", "test2_response"},
},
},
}

for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
if cachedData, ok := cacheIn.Get(test.policy.name); ok {
if len(cachedData.(policy.CommonPolicyMap)) != test.policy.size {
t.Errorf("cached data size doesn't match up. Expected: %d, got:%d", test.policy.size,
len(cachedData.(policy.CommonPolicyMap)))
}
for _, v := range test.policy.policies {
cachedPolicies := cachedData.(policy.CommonPolicyMap)
if _, exists := cachedPolicies[v]; !exists {
t.Errorf("%s is not in the policy rule set", v)
}
}
} else {
t.Errorf("%s is not in the policies", test.policy.name)
}
})
}
}
File renamed without changes.
17 changes: 17 additions & 0 deletions testconfig/custom_datafilter_rule_set.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[
{
"type": "pan",
"rules": [
{
"name": "pan_process",
"path": "rules/pan_process.json",
"custom_path": "/testconfig/custom_pan_process.json"
},
{
"name": "custom_pan_process",
"path": "rules/pan_process.json",
"custom_path": ""
}
]
}
]
File renamed without changes.
File renamed without changes.
25 changes: 0 additions & 25 deletions testdata/datafilter_rule_set.json

This file was deleted.

0 comments on commit 72c88ef

Please sign in to comment.