2017-05-26 09:25:29 +00:00
// Copyright © 2017 Aqua Security Software Ltd. <info@aquasec.com>
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package check
import (
"io/ioutil"
2017-08-12 18:54:33 +00:00
"os"
"strings"
2017-05-26 09:25:29 +00:00
"testing"
)
var (
in [ ] byte
controls * Controls
)
func init ( ) {
var err error
in , err = ioutil . ReadFile ( "data" )
if err != nil {
panic ( "Failed reading test data: " + err . Error ( ) )
}
2017-08-12 18:54:33 +00:00
// substitute variables in data file
user := os . Getenv ( "USER" )
s := strings . Replace ( string ( in ) , "$user" , user , - 1 )
controls , err = NewControls ( MASTER , [ ] byte ( s ) )
// controls, err = NewControls(MASTER, in)
2017-06-23 11:04:46 +00:00
if err != nil {
panic ( "Failed creating test controls: " + err . Error ( ) )
}
2017-05-26 09:25:29 +00:00
}
func TestTestExecute ( t * testing . T ) {
2017-08-12 18:54:33 +00:00
2017-05-26 09:25:29 +00:00
cases := [ ] struct {
2017-08-12 18:54:33 +00:00
* Check
str string
2017-05-26 09:25:29 +00:00
} {
{
2017-08-12 18:54:33 +00:00
controls . Groups [ 0 ] . Checks [ 0 ] ,
2017-05-26 09:25:29 +00:00
"2:45 ../kubernetes/kube-apiserver --allow-privileged=false --option1=20,30,40" ,
} ,
{
2017-08-12 18:54:33 +00:00
controls . Groups [ 0 ] . Checks [ 1 ] ,
2017-05-26 09:25:29 +00:00
"2:45 ../kubernetes/kube-apiserver --allow-privileged=false" ,
} ,
{
2017-08-12 18:54:33 +00:00
controls . Groups [ 0 ] . Checks [ 2 ] ,
2017-05-26 09:25:29 +00:00
"niinai 13617 2635 99 19:26 pts/20 00:03:08 ./kube-apiserver --insecure-port=0 --anonymous-auth" ,
} ,
{
2017-08-12 18:54:33 +00:00
controls . Groups [ 0 ] . Checks [ 3 ] ,
2017-05-26 09:25:29 +00:00
"2:45 ../kubernetes/kube-apiserver --secure-port=0 --audit-log-maxage=40 --option" ,
} ,
{
2017-08-12 18:54:33 +00:00
controls . Groups [ 0 ] . Checks [ 4 ] ,
2017-05-26 09:25:29 +00:00
"2:45 ../kubernetes/kube-apiserver --max-backlog=20 --secure-port=0 --audit-log-maxage=40 --option" ,
} ,
{
2017-08-12 18:54:33 +00:00
controls . Groups [ 0 ] . Checks [ 5 ] ,
2017-05-26 09:25:29 +00:00
"2:45 ../kubernetes/kube-apiserver --option --admission-control=WebHook,RBAC ---audit-log-maxage=40" ,
} ,
{
2017-08-12 18:54:33 +00:00
controls . Groups [ 0 ] . Checks [ 6 ] ,
2017-05-26 09:25:29 +00:00
"2:45 .. --kubelet-clientkey=foo --kubelet-client-certificate=bar --admission-control=Webhook,RBAC" ,
} ,
{
2017-08-12 18:54:33 +00:00
controls . Groups [ 0 ] . Checks [ 7 ] ,
2017-05-26 09:25:29 +00:00
"2:45 .. --secure-port=0 --kubelet-client-certificate=bar --admission-control=Webhook,RBAC" ,
} ,
{
2017-08-12 18:54:33 +00:00
controls . Groups [ 0 ] . Checks [ 8 ] ,
2017-05-26 09:25:29 +00:00
"644" ,
} ,
2017-08-15 17:34:07 +00:00
{
controls . Groups [ 0 ] . Checks [ 9 ] ,
"640" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 9 ] ,
"600" ,
} ,
2017-09-13 14:32:33 +00:00
{
controls . Groups [ 0 ] . Checks [ 10 ] ,
"2:45 ../kubernetes/kube-apiserver --option --admission-control=WebHook,RBAC ---audit-log-maxage=40" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 11 ] ,
"2:45 ../kubernetes/kube-apiserver --option --admission-control=WebHook,RBAC ---audit-log-maxage=40" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 12 ] ,
"2:45 ../kubernetes/kube-apiserver --option --admission-control=WebHook,Something,RBAC ---audit-log-maxage=40" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 13 ] ,
"2:45 ../kubernetes/kube-apiserver --option --admission-control=Something ---audit-log-maxage=40" ,
} ,
2019-03-11 18:05:33 +00:00
{
2019-04-10 22:47:26 +00:00
// check for ':' as argument-value separator, with space between arg and val
2019-03-11 18:05:33 +00:00
controls . Groups [ 0 ] . Checks [ 14 ] ,
2019-04-10 22:47:26 +00:00
"2:45 kube-apiserver some-arg: some-val --admission-control=Something ---audit-log-maxage=40" ,
2019-03-11 18:05:33 +00:00
} ,
{
2019-04-10 22:47:26 +00:00
// check for ':' as argument-value separator, with no space between arg and val
controls . Groups [ 0 ] . Checks [ 14 ] ,
"2:45 kube-apiserver some-arg:some-val --admission-control=Something ---audit-log-maxage=40" ,
2019-04-11 09:21:19 +00:00
} ,
{
2019-03-11 18:05:33 +00:00
controls . Groups [ 0 ] . Checks [ 15 ] ,
2019-04-11 09:03:07 +00:00
"{\"readOnlyPort\": 15000}" ,
2019-03-11 18:05:33 +00:00
} ,
{
controls . Groups [ 0 ] . Checks [ 16 ] ,
2019-04-11 09:03:07 +00:00
"{\"stringValue\": \"WebHook,Something,RBAC\"}" ,
2019-03-11 18:05:33 +00:00
} ,
{
controls . Groups [ 0 ] . Checks [ 17 ] ,
2019-04-11 09:03:07 +00:00
"{\"trueValue\": true}" ,
2019-03-11 18:05:33 +00:00
} ,
{
controls . Groups [ 0 ] . Checks [ 18 ] ,
2019-04-11 09:03:07 +00:00
"{\"readOnlyPort\": 15000}" ,
2019-03-11 18:05:33 +00:00
} ,
{
controls . Groups [ 0 ] . Checks [ 19 ] ,
2019-04-11 09:03:07 +00:00
"{\"authentication\": { \"anonymous\": {\"enabled\": false}}}" ,
2019-03-11 18:05:33 +00:00
} ,
{
controls . Groups [ 0 ] . Checks [ 20 ] ,
"readOnlyPort: 15000" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 21 ] ,
2019-04-11 09:03:07 +00:00
"readOnlyPort: 15000" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 22 ] ,
2019-03-11 18:05:33 +00:00
"authentication:\n anonymous:\n enabled: false" ,
} ,
2019-06-05 11:23:59 +00:00
{
controls . Groups [ 0 ] . Checks [ 26 ] ,
"currentMasterVersion: 1.12.7" ,
} ,
2020-03-03 16:54:38 +00:00
{
controls . Groups [ 0 ] . Checks [ 27 ] ,
"--peer-client-cert-auth" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 27 ] ,
"--abc=true --peer-client-cert-auth --efg=false" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 27 ] ,
"--abc --peer-client-cert-auth --efg" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 27 ] ,
"--peer-client-cert-auth=true" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 27 ] ,
"--abc --peer-client-cert-auth=true --efg" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 28 ] ,
"--abc --peer-client-cert-auth=false --efg" ,
} ,
2017-05-26 09:25:29 +00:00
}
for _ , c := range cases {
2018-07-30 12:16:28 +00:00
res := c . Tests . execute ( c . str ) . testResult
2017-05-26 09:25:29 +00:00
if ! res {
2017-08-12 18:54:33 +00:00
t . Errorf ( "%s, expected:%v, got:%v\n" , c . Text , true , res )
2017-05-26 09:25:29 +00:00
}
}
}
2019-03-11 18:05:33 +00:00
func TestTestExecuteExceptions ( t * testing . T ) {
cases := [ ] struct {
* Check
str string
} {
{
2019-04-11 09:21:19 +00:00
controls . Groups [ 0 ] . Checks [ 23 ] ,
2019-03-11 18:05:33 +00:00
"this is not valid json {} at all" ,
} ,
{
2019-04-11 09:21:19 +00:00
controls . Groups [ 0 ] . Checks [ 24 ] ,
2019-03-11 18:05:33 +00:00
"{\"key\": \"value\"}" ,
} ,
{
2019-04-11 09:21:19 +00:00
controls . Groups [ 0 ] . Checks [ 25 ] ,
2019-03-11 18:05:33 +00:00
"broken } yaml\nenabled: true" ,
} ,
2019-06-05 11:23:59 +00:00
{
controls . Groups [ 0 ] . Checks [ 26 ] ,
"currentMasterVersion: 1.11" ,
} ,
{
controls . Groups [ 0 ] . Checks [ 26 ] ,
"currentMasterVersion: " ,
} ,
2019-03-11 18:05:33 +00:00
}
for _ , c := range cases {
res := c . Tests . execute ( c . str ) . testResult
if res {
t . Errorf ( "%s, expected:%v, got:%v\n" , c . Text , false , res )
}
}
}
2019-07-12 06:09:27 +00:00
func TestTestUnmarshal ( t * testing . T ) {
type kubeletConfig struct {
Kind string
ApiVersion string
Address string
}
cases := [ ] struct {
content string
jsonInterface interface { }
expectedToFail bool
} {
{
` {
"kind" : "KubeletConfiguration" ,
"apiVersion" : "kubelet.config.k8s.io/v1beta1" ,
"address" : "0.0.0.0"
}
` ,
kubeletConfig { } ,
false ,
} , {
`
kind : KubeletConfiguration
address : 0.0 .0 .0
apiVersion : kubelet . config . k8s . io / v1beta1
authentication :
anonymous :
enabled : false
webhook :
cacheTTL : 2 m0s
enabled : true
x509 :
clientCAFile : / etc / kubernetes / pki / ca . crt
tlsCipherSuites :
- TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256
- TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256
` ,
kubeletConfig { } ,
false ,
} ,
{
`
kind : ddress : 0.0 .0 .0
apiVersion : kubelet . config . k8s . io / v1beta
` ,
kubeletConfig { } ,
true ,
} ,
}
for _ , c := range cases {
err := unmarshal ( c . content , & c . jsonInterface )
if err != nil {
if ! c . expectedToFail {
t . Errorf ( "%s, expectedToFail:%v, got:%v\n" , c . content , c . expectedToFail , err )
}
} else {
if c . expectedToFail {
t . Errorf ( "%s, expectedToFail:%v, got:Did not fail\n" , c . content , c . expectedToFail )
}
}
}
}
func TestExecuteJSONPath ( t * testing . T ) {
type kubeletConfig struct {
Kind string
ApiVersion string
Address string
}
cases := [ ] struct {
jsonPath string
jsonInterface kubeletConfig
expectedResult string
expectedToFail bool
} {
{
// JSONPath parse works, results don't match
"{.Kind}" ,
kubeletConfig {
Kind : "KubeletConfiguration" ,
ApiVersion : "kubelet.config.k8s.io/v1beta1" ,
Address : "127.0.0.0" ,
} ,
"blah" ,
true ,
} ,
{
// JSONPath parse works, results match
"{.Kind}" ,
kubeletConfig {
Kind : "KubeletConfiguration" ,
ApiVersion : "kubelet.config.k8s.io/v1beta1" ,
Address : "127.0.0.0" ,
} ,
"KubeletConfiguration" ,
false ,
} ,
{
// JSONPath parse fails
"{.ApiVersion" ,
kubeletConfig {
Kind : "KubeletConfiguration" ,
ApiVersion : "kubelet.config.k8s.io/v1beta1" ,
Address : "127.0.0.0" ,
} ,
"" ,
true ,
} ,
}
for _ , c := range cases {
result , err := executeJSONPath ( c . jsonPath , c . jsonInterface )
if err != nil && ! c . expectedToFail {
t . Fatalf ( "jsonPath:%q, expectedResult:%q got:%v\n" , c . jsonPath , c . expectedResult , err )
}
if c . expectedResult != result && ! c . expectedToFail {
t . Errorf ( "jsonPath:%q, expectedResult:%q got:%q\n" , c . jsonPath , c . expectedResult , result )
}
}
}
2019-07-17 14:08:11 +00:00
2019-07-26 18:11:59 +00:00
func TestAllElementsValid ( t * testing . T ) {
cases := [ ] struct {
source [ ] string
target [ ] string
valid bool
} {
{
source : [ ] string { } ,
target : [ ] string { } ,
valid : true ,
} ,
{
source : [ ] string { "blah" } ,
target : [ ] string { } ,
valid : false ,
} ,
{
source : [ ] string { } ,
target : [ ] string { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" , "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" ,
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305" , "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" ,
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305" , "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" ,
"TLS_RSA_WITH_AES_256_GCM_SHA384" , "TLS_RSA_WITH_AES_128_GCM_SHA256" } ,
valid : false ,
} ,
{
source : [ ] string { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" , "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" } ,
target : [ ] string { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" , "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" ,
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305" , "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" ,
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305" , "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" ,
"TLS_RSA_WITH_AES_256_GCM_SHA384" , "TLS_RSA_WITH_AES_128_GCM_SHA256" } ,
valid : true ,
} ,
{
source : [ ] string { "blah" } ,
target : [ ] string { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" , "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" ,
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305" , "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" ,
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305" , "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" ,
"TLS_RSA_WITH_AES_256_GCM_SHA384" , "TLS_RSA_WITH_AES_128_GCM_SHA256" } ,
valid : false ,
} ,
{
source : [ ] string { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" , "blah" } ,
target : [ ] string { "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256" , "TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256" ,
"TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305" , "TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384" ,
"TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305" , "TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384" ,
"TLS_RSA_WITH_AES_256_GCM_SHA384" , "TLS_RSA_WITH_AES_128_GCM_SHA256" } ,
valid : false ,
} ,
}
for _ , c := range cases {
if ! allElementsValid ( c . source , c . target ) && c . valid {
t . Errorf ( "Not All Elements in %q are found in %q \n" , c . source , c . target )
}
}
}
func TestSplitAndRemoveLastSeparator ( t * testing . T ) {
cases := [ ] struct {
source string
valid bool
elementCnt int
} {
{
source : "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_ECDSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_RSA_WITH_AES_256_GCM_SHA384,TLS_ECDHE_RSA_WITH_CHACHA20_POLY1305,TLS_ECDHE_ECDSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_256_GCM_SHA384,TLS_RSA_WITH_AES_128_GCM_SHA256" ,
valid : true ,
elementCnt : 8 ,
} ,
{
source : "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256," ,
valid : true ,
elementCnt : 2 ,
} ,
{
source : "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256, TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256," ,
valid : true ,
elementCnt : 2 ,
} ,
{
source : "TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256, " ,
valid : true ,
elementCnt : 2 ,
} ,
{
source : " TLS_ECDHE_ECDSA_WITH_AES_128_GCM_SHA256,TLS_ECDHE_RSA_WITH_AES_128_GCM_SHA256," ,
valid : true ,
elementCnt : 2 ,
} ,
}
for _ , c := range cases {
as := splitAndRemoveLastSeparator ( c . source , defaultArraySeparator )
if len ( as ) == 0 && c . valid {
t . Errorf ( "Split did not work with %q \n" , c . source )
}
if c . elementCnt != len ( as ) {
t . Errorf ( "Split did not work with %q expected: %d got: %d\n" , c . source , c . elementCnt , len ( as ) )
}
}
}
2019-07-17 14:08:11 +00:00
func TestCompareOp ( t * testing . T ) {
cases := [ ] struct {
label string
op string
flagVal string
compareValue string
expectedResultPattern string
testResult bool
} {
// Test Op not matching
{ label : "empty - op" , op : "" , flagVal : "" , compareValue : "" , expectedResultPattern : "" , testResult : false } ,
{ label : "op=blah" , op : "blah" , flagVal : "foo" , compareValue : "bar" , expectedResultPattern : "" , testResult : false } ,
// Test Op "eq"
{ label : "op=eq, both empty" , op : "eq" , flagVal : "" , compareValue : "" , expectedResultPattern : "'' is equal to ''" , testResult : true } ,
{ label : "op=eq, true==true" , op : "eq" , flagVal : "true" ,
compareValue : "true" ,
expectedResultPattern : "'true' is equal to 'true'" ,
testResult : true } ,
{ label : "op=eq, false==false" , op : "eq" , flagVal : "false" ,
compareValue : "false" ,
expectedResultPattern : "'false' is equal to 'false'" ,
testResult : true } ,
{ label : "op=eq, false==true" , op : "eq" , flagVal : "false" ,
compareValue : "true" ,
expectedResultPattern : "'false' is equal to 'true'" ,
testResult : false } ,
{ label : "op=eq, strings match" , op : "eq" , flagVal : "KubeletConfiguration" ,
compareValue : "KubeletConfiguration" ,
expectedResultPattern : "'KubeletConfiguration' is equal to 'KubeletConfiguration'" ,
testResult : true } ,
{ label : "op=eq, flagVal=empty" , op : "eq" , flagVal : "" ,
compareValue : "KubeletConfiguration" ,
expectedResultPattern : "'' is equal to 'KubeletConfiguration'" ,
testResult : false } ,
{ label : "op=eq, compareValue=empty" , op : "eq" , flagVal : "KubeletConfiguration" ,
compareValue : "" ,
expectedResultPattern : "'KubeletConfiguration' is equal to ''" ,
testResult : false } ,
// Test Op "noteq"
{ label : "op=noteq, both empty" , op : "noteq" , flagVal : "" ,
compareValue : "" , expectedResultPattern : "'' is not equal to ''" ,
testResult : false } ,
{ label : "op=noteq, true!=true" , op : "noteq" , flagVal : "true" ,
compareValue : "true" ,
expectedResultPattern : "'true' is not equal to 'true'" ,
testResult : false } ,
{ label : "op=noteq, false!=false" , op : "noteq" , flagVal : "false" ,
compareValue : "false" ,
expectedResultPattern : "'false' is not equal to 'false'" ,
testResult : false } ,
{ label : "op=noteq, false!=true" , op : "noteq" , flagVal : "false" ,
compareValue : "true" ,
expectedResultPattern : "'false' is not equal to 'true'" ,
testResult : true } ,
{ label : "op=noteq, strings match" , op : "noteq" , flagVal : "KubeletConfiguration" ,
compareValue : "KubeletConfiguration" ,
expectedResultPattern : "'KubeletConfiguration' is not equal to 'KubeletConfiguration'" ,
testResult : false } ,
{ label : "op=noteq, flagVal=empty" , op : "noteq" , flagVal : "" ,
compareValue : "KubeletConfiguration" ,
expectedResultPattern : "'' is not equal to 'KubeletConfiguration'" ,
testResult : true } ,
{ label : "op=noteq, compareValue=empty" , op : "noteq" , flagVal : "KubeletConfiguration" ,
compareValue : "" ,
expectedResultPattern : "'KubeletConfiguration' is not equal to ''" ,
testResult : true } ,
// Test Op "gt"
2020-06-28 14:29:55 +00:00
{ label : "op=gt, both empty" , op : "gt" , flagVal : "" ,
compareValue : "" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
2019-07-17 14:08:11 +00:00
{ label : "op=gt, 0 > 0" , op : "gt" , flagVal : "0" ,
compareValue : "0" , expectedResultPattern : "0 is greater than 0" ,
testResult : false } ,
{ label : "op=gt, 4 > 5" , op : "gt" , flagVal : "4" ,
compareValue : "5" , expectedResultPattern : "4 is greater than 5" ,
testResult : false } ,
{ label : "op=gt, 5 > 4" , op : "gt" , flagVal : "5" ,
compareValue : "4" , expectedResultPattern : "5 is greater than 4" ,
testResult : true } ,
{ label : "op=gt, 5 > 5" , op : "gt" , flagVal : "5" ,
compareValue : "5" , expectedResultPattern : "5 is greater than 5" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=gt, Pikachu > 5" , op : "gt" , flagVal : "Pikachu" ,
compareValue : "5" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
{ label : "op=gt, 5 > Bulbasaur" , op : "gt" , flagVal : "5" ,
compareValue : "Bulbasaur" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
2019-07-17 14:08:11 +00:00
// Test Op "lt"
2020-06-28 14:29:55 +00:00
{ label : "op=lt, both empty" , op : "lt" , flagVal : "" ,
compareValue : "" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
{ label : "op=lt, 0 < 0" , op : "lt" , flagVal : "0" ,
2019-07-17 14:08:11 +00:00
compareValue : "0" , expectedResultPattern : "0 is lower than 0" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=lt, 4 < 5" , op : "lt" , flagVal : "4" ,
2019-07-17 14:08:11 +00:00
compareValue : "5" , expectedResultPattern : "4 is lower than 5" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=lt, 5 < 4" , op : "lt" , flagVal : "5" ,
2019-07-17 14:08:11 +00:00
compareValue : "4" , expectedResultPattern : "5 is lower than 4" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=lt, 5 < 5" , op : "lt" , flagVal : "5" ,
2019-07-17 14:08:11 +00:00
compareValue : "5" , expectedResultPattern : "5 is lower than 5" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=lt, Charmander < 5" , op : "lt" , flagVal : "Charmander" ,
compareValue : "5" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
{ label : "op=lt, 5 < Charmeleon" , op : "lt" , flagVal : "5" ,
compareValue : "Charmeleon" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
2019-07-17 14:08:11 +00:00
// Test Op "gte"
2020-06-28 14:29:55 +00:00
{ label : "op=gte, both empty" , op : "gte" , flagVal : "" ,
compareValue : "" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
{ label : "op=gte, 0 >= 0" , op : "gte" , flagVal : "0" ,
2019-07-17 14:08:11 +00:00
compareValue : "0" , expectedResultPattern : "0 is greater or equal to 0" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=gte, 4 >= 5" , op : "gte" , flagVal : "4" ,
2019-07-17 14:08:11 +00:00
compareValue : "5" , expectedResultPattern : "4 is greater or equal to 5" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=gte, 5 >= 4" , op : "gte" , flagVal : "5" ,
2019-07-17 14:08:11 +00:00
compareValue : "4" , expectedResultPattern : "5 is greater or equal to 4" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=gte, 5 >= 5" , op : "gte" , flagVal : "5" ,
2019-07-17 14:08:11 +00:00
compareValue : "5" , expectedResultPattern : "5 is greater or equal to 5" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=gte, Ekans >= 5" , op : "gte" , flagVal : "Ekans" ,
compareValue : "5" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
{ label : "op=gte, 4 >= Zubat" , op : "gte" , flagVal : "4" ,
compareValue : "Zubat" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
2019-07-17 14:08:11 +00:00
// Test Op "lte"
2020-06-28 14:29:55 +00:00
{ label : "op=lte, both empty" , op : "lte" , flagVal : "" ,
compareValue : "" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
{ label : "op=lte, 0 <= 0" , op : "lte" , flagVal : "0" ,
2019-07-17 14:08:11 +00:00
compareValue : "0" , expectedResultPattern : "0 is lower or equal to 0" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=lte, 4 <= 5" , op : "lte" , flagVal : "4" ,
2019-07-17 14:08:11 +00:00
compareValue : "5" , expectedResultPattern : "4 is lower or equal to 5" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=lte, 5 <= 4" , op : "lte" , flagVal : "5" ,
2019-07-17 14:08:11 +00:00
compareValue : "4" , expectedResultPattern : "5 is lower or equal to 4" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=lte, 5 <= 5" , op : "lte" , flagVal : "5" ,
2019-07-17 14:08:11 +00:00
compareValue : "5" , expectedResultPattern : "5 is lower or equal to 5" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=lte, Venomoth <= 4" , op : "lte" , flagVal : "Venomoth" ,
compareValue : "4" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
{ label : "op=lte, 5 <= Meowth" , op : "lte" , flagVal : "5" ,
compareValue : "Meowth" , expectedResultPattern : "Invalid Number(s) used for comparison" ,
testResult : false } ,
2019-07-17 14:08:11 +00:00
// Test Op "has"
2020-06-28 14:29:55 +00:00
{ label : "op=has, both empty" , op : "has" , flagVal : "" ,
2019-07-17 14:08:11 +00:00
compareValue : "" , expectedResultPattern : "'' has ''" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=has, flagVal=empty" , op : "has" , flagVal : "" ,
2019-07-17 14:08:11 +00:00
compareValue : "blah" , expectedResultPattern : "'' has 'blah'" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=has, compareValue=empty" , op : "has" , flagVal : "blah" ,
2019-07-17 14:08:11 +00:00
compareValue : "" , expectedResultPattern : "'blah' has ''" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=has, 'blah' has 'la'" , op : "has" , flagVal : "blah" ,
2019-07-17 14:08:11 +00:00
compareValue : "la" , expectedResultPattern : "'blah' has 'la'" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=has, 'blah' has 'LA'" , op : "has" , flagVal : "blah" ,
2019-07-17 14:08:11 +00:00
compareValue : "LA" , expectedResultPattern : "'blah' has 'LA'" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=has, 'blah' has 'lo'" , op : "has" , flagVal : "blah" ,
2019-07-17 14:08:11 +00:00
compareValue : "lo" , expectedResultPattern : "'blah' has 'lo'" ,
testResult : false } ,
// Test Op "nothave"
2020-06-28 14:29:55 +00:00
{ label : "op=nothave, both empty" , op : "nothave" , flagVal : "" ,
2019-07-17 14:08:11 +00:00
compareValue : "" , expectedResultPattern : " '' not have ''" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=nothave, flagVal=empty" , op : "nothave" , flagVal : "" ,
2019-07-17 14:08:11 +00:00
compareValue : "blah" , expectedResultPattern : " '' not have 'blah'" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=nothave, compareValue=empty" , op : "nothave" , flagVal : "blah" ,
2019-07-17 14:08:11 +00:00
compareValue : "" , expectedResultPattern : " 'blah' not have ''" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=nothave, 'blah' not have 'la'" , op : "nothave" , flagVal : "blah" ,
2019-07-17 14:08:11 +00:00
compareValue : "la" , expectedResultPattern : " 'blah' not have 'la'" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=nothave, 'blah' not have 'LA'" , op : "nothave" , flagVal : "blah" ,
2019-07-17 14:08:11 +00:00
compareValue : "LA" , expectedResultPattern : " 'blah' not have 'LA'" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=nothave, 'blah' not have 'lo'" , op : "nothave" , flagVal : "blah" ,
2019-07-17 14:08:11 +00:00
compareValue : "lo" , expectedResultPattern : " 'blah' not have 'lo'" ,
testResult : true } ,
// Test Op "regex"
2020-06-28 14:29:55 +00:00
{ label : "op=regex, both empty" , op : "regex" , flagVal : "" ,
2019-07-17 14:08:11 +00:00
compareValue : "" , expectedResultPattern : " '' matched by ''" ,
testResult : true } ,
2020-06-28 14:29:55 +00:00
{ label : "op=regex, flagVal=empty" , op : "regex" , flagVal : "" ,
2019-07-17 14:08:11 +00:00
compareValue : "blah" , expectedResultPattern : " '' matched by 'blah'" ,
testResult : false } ,
2019-07-26 18:11:59 +00:00
// Test Op "valid_elements"
{ label : "op=valid_elements, valid_elements both empty" , op : "valid_elements" , flagVal : "" ,
compareValue : "" , expectedResultPattern : "'' contains valid elements from ''" ,
testResult : true } ,
{ label : "op=valid_elements, valid_elements flagVal empty" , op : "valid_elements" , flagVal : "" ,
compareValue : "a,b" , expectedResultPattern : "'' contains valid elements from 'a,b'" ,
testResult : false } ,
{ label : "op=valid_elements, valid_elements expectedResultPattern empty" , op : "valid_elements" , flagVal : "a,b" ,
compareValue : "" , expectedResultPattern : "'a,b' contains valid elements from ''" ,
testResult : false } ,
2020-03-16 12:25:46 +00:00
// Test Op "bitmask"
{ label : "op=bitmask, 644 AND 640" , op : "bitmask" , flagVal : "640" ,
compareValue : "644" , expectedResultPattern : "bitmask '640' AND '644'" ,
testResult : true } ,
{ label : "op=bitmask, 644 AND 777" , op : "bitmask" , flagVal : "777" ,
compareValue : "644" , expectedResultPattern : "bitmask '777' AND '644'" ,
testResult : false } ,
{ label : "op=bitmask, 644 AND 444" , op : "bitmask" , flagVal : "444" ,
compareValue : "644" , expectedResultPattern : "bitmask '444' AND '644'" ,
testResult : true } ,
{ label : "op=bitmask, 644 AND 211" , op : "bitmask" , flagVal : "211" ,
compareValue : "644" , expectedResultPattern : "bitmask '211' AND '644'" ,
testResult : false } ,
2020-06-28 14:29:55 +00:00
{ label : "op=bitmask, Harry AND 211" , op : "bitmask" , flagVal : "Harry" ,
compareValue : "644" , expectedResultPattern : "Not numeric value - flag: Harry" ,
testResult : false } ,
{ label : "op=bitmask, 644 AND Potter" , op : "bitmask" , flagVal : "211" ,
compareValue : "Potter" , expectedResultPattern : "Not numeric value - flag: Potter" ,
testResult : false } ,
2019-07-17 14:08:11 +00:00
}
for _ , c := range cases {
expectedResultPattern , testResult := compareOp ( c . op , c . flagVal , c . compareValue )
if expectedResultPattern != c . expectedResultPattern {
t . Errorf ( "'expectedResultPattern' did not match - label: %q op: %q expected 'expectedResultPattern':%q got:%q\n" , c . label , c . op , c . expectedResultPattern , expectedResultPattern )
}
if testResult != c . testResult {
t . Errorf ( "'testResult' did not match - label: %q op: %q expected 'testResult':%t got:%t\n" , c . label , c . op , c . testResult , testResult )
}
}
}
2019-10-12 22:46:19 +00:00
func TestToNumeric ( t * testing . T ) {
cases := [ ] struct {
firstValue string
secondValue string
expectedToFail bool
} {
{
firstValue : "a" ,
secondValue : "b" ,
expectedToFail : true ,
} ,
{
firstValue : "5" ,
secondValue : "b" ,
expectedToFail : true ,
} ,
{
firstValue : "5" ,
secondValue : "6" ,
expectedToFail : false ,
} ,
}
for _ , c := range cases {
f , s , err := toNumeric ( c . firstValue , c . secondValue )
if c . expectedToFail && err == nil {
t . Errorf ( "TestToNumeric - Expected error while converting %s and %s" , c . firstValue , c . secondValue )
}
if ! c . expectedToFail && ( f != 5 || s != 6 ) {
t . Errorf ( "TestToNumeric - Expected to return %d,%d , but instead got %d,%d" , 5 , 6 , f , s )
}
}
}