-
Notifications
You must be signed in to change notification settings - Fork 23
/
Copy pathactivity_test.go
156 lines (128 loc) · 3.65 KB
/
activity_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
package sqld
import (
"fmt"
"testing"
"github.com/project-flogo/core/activity"
"github.com/project-flogo/core/data"
"github.com/project-flogo/core/data/mapper"
"github.com/project-flogo/core/data/metadata"
logger "github.com/project-flogo/core/support/log"
"github.com/project-flogo/core/support/trace"
"github.com/stretchr/testify/assert"
)
type initContext struct {
settings map[string]interface{}
}
func newInitContext(values map[string]interface{}) *initContext {
if values == nil {
values = make(map[string]interface{})
}
return &initContext{
settings: values,
}
}
func (i *initContext) Settings() map[string]interface{} {
return i.settings
}
func (i *initContext) MapperFactory() mapper.Factory {
return nil
}
func (i *initContext) Logger() logger.Logger {
return logger.RootLogger()
}
type activityContext struct {
input map[string]interface{}
output map[string]interface{}
}
func newActivityContext(values map[string]interface{}) *activityContext {
if values == nil {
values = make(map[string]interface{})
}
return &activityContext{
input: values,
output: make(map[string]interface{}),
}
}
func (a *activityContext) ActivityHost() activity.Host {
return a
}
func (a *activityContext) Name() string {
return "test"
}
func (a *activityContext) GetInput(name string) interface{} {
return a.input[name]
}
func (a *activityContext) SetOutput(name string, value interface{}) error {
a.output[name] = value
return nil
}
func (a *activityContext) GetInputObject(input data.StructValue) error {
return input.FromMap(a.input)
}
func (a *activityContext) SetOutputObject(output data.StructValue) error {
a.output = output.ToMap()
return nil
}
func (a *activityContext) GetSharedTempData() map[string]interface{} {
return nil
}
func (a *activityContext) ID() string {
return "test"
}
func (a *activityContext) IOMetadata() *metadata.IOMetadata {
return nil
}
func (a *activityContext) Reply(replyData map[string]interface{}, err error) {
}
func (a *activityContext) Return(returnData map[string]interface{}, err error) {
}
func (a *activityContext) Scope() data.Scope {
return nil
}
func (a *activityContext) Logger() logger.Logger {
return logger.RootLogger()
}
func (a *activityContext) GetTracingContext() trace.TracingContext {
return nil
}
func TestSQLD(t *testing.T) {
activity, err := New(newInitContext(nil))
assert.Nil(t, err)
test := func(a string, attack bool) {
var payload interface{} = map[string]interface{}{
"content": map[string]interface{}{
"test": a,
},
}
ctx := newActivityContext(map[string]interface{}{"payload": payload})
_, err = activity.Eval(ctx)
assert.Nil(t, err)
value, attackValues := ctx.output["attack"].(float32), ctx.output["attackValues"].(map[string]interface{})
if attack {
assert.Condition(t, func() (success bool) {
return value > 50
}, fmt.Sprint("should be an attack", a, value))
assert.Condition(t, func() (success bool) {
return attackValues["content"].(map[string]interface{})["test"].(float64) > 50
}, fmt.Sprint("should be an attack", a, value))
} else {
assert.Condition(t, func() (success bool) {
return value < 50
}, fmt.Sprint("should not be an attack", a, value))
assert.Condition(t, func() (success bool) {
return attackValues["content"].(map[string]interface{})["test"].(float64) < 50
}, fmt.Sprint("should not be an attack", a, value))
}
}
test("test or 1337=1337 --\"", true)
test(" or 1=1 ", true)
test("/**/or/**/1337=1337", true)
test("abc123", false)
test("abc123 123abc", false)
test("123", false)
test("abcorabc", false)
test("available", false)
test("orcat1", false)
test("cat1or", false)
test("cat1orcat1", false)
}