github.com/quay/claircore@v1.5.28/pkg/ovalutil/operation_test.go (about)

     1  package ovalutil
     2  
     3  import (
     4  	"testing"
     5  
     6  	"github.com/quay/goval-parser/oval"
     7  )
     8  
     9  // TestOperation tests if given value match with requited based on operation type
    10  func TestOperation(t *testing.T) {
    11  	t.Parallel()
    12  
    13  	type testcase struct {
    14  		Name          string
    15  		Value         string
    16  		RequiredValue string
    17  		Operation     oval.Operation
    18  		Want          bool
    19  	}
    20  	testcases := []testcase{
    21  		{
    22  			Name:          "equal",
    23  			Value:         "foo",
    24  			RequiredValue: "foo",
    25  			Operation:     oval.OpEquals,
    26  			Want:          true,
    27  		},
    28  		{
    29  			Name:          "equal-false",
    30  			Value:         "Foo",
    31  			RequiredValue: "foo",
    32  			Operation:     oval.OpEquals,
    33  			Want:          false,
    34  		},
    35  		{
    36  			Name:          "not-match",
    37  			Value:         "foo",
    38  			RequiredValue: "bar",
    39  			Operation:     oval.OpNotEquals,
    40  			Want:          true,
    41  		},
    42  		{
    43  			Name:          "not-match-false",
    44  			Value:         "foo",
    45  			RequiredValue: "foo",
    46  			Operation:     oval.OpNotEquals,
    47  			Want:          false,
    48  		},
    49  		{
    50  			Name:          "OpCaseInsensitiveEquals",
    51  			Value:         "foo",
    52  			RequiredValue: "FOO",
    53  			Operation:     oval.OpCaseInsensitiveEquals,
    54  			Want:          true,
    55  		},
    56  		{
    57  			Name:          "OpCaseInsensitiveNotEquals",
    58  			Value:         "foo2",
    59  			RequiredValue: "FOO",
    60  			Operation:     oval.OpCaseInsensitiveNotEquals,
    61  			Want:          true,
    62  		},
    63  		{
    64  			Name:          "OpGreaterThan",
    65  			Value:         "2",
    66  			RequiredValue: "1",
    67  			Operation:     oval.OpGreaterThan,
    68  			Want:          true,
    69  		},
    70  		{
    71  			Name:          "OpGreaterThan-false",
    72  			Value:         "1",
    73  			RequiredValue: "2",
    74  			Operation:     oval.OpGreaterThan,
    75  			Want:          false,
    76  		},
    77  		{
    78  			Name:          "OpLessThan",
    79  			Value:         "1",
    80  			RequiredValue: "2",
    81  			Operation:     oval.OpLessThan,
    82  			Want:          true,
    83  		},
    84  		{
    85  			Name:          "OpLessThan-false",
    86  			Value:         "2",
    87  			RequiredValue: "1",
    88  			Operation:     oval.OpLessThan,
    89  			Want:          false,
    90  		},
    91  		{
    92  			Name:          "OpGreaterThanOrEqual",
    93  			Value:         "2",
    94  			RequiredValue: "1",
    95  			Operation:     oval.OpGreaterThanOrEqual,
    96  			Want:          true,
    97  		},
    98  		{
    99  			Name:          "OpGreaterThanOrEqual-false",
   100  			Value:         "1",
   101  			RequiredValue: "2",
   102  			Operation:     oval.OpGreaterThanOrEqual,
   103  			Want:          false,
   104  		},
   105  		{
   106  			Name:          "OpLessThanOrEqual",
   107  			Value:         "1",
   108  			RequiredValue: "2",
   109  			Operation:     oval.OpLessThanOrEqual,
   110  			Want:          true,
   111  		},
   112  		{
   113  			Name:          "OpLessThanOrEqual-false",
   114  			Value:         "2",
   115  			RequiredValue: "1",
   116  			Operation:     oval.OpLessThanOrEqual,
   117  			Want:          false,
   118  		},
   119  		{
   120  			Name:          "pattern-match",
   121  			Value:         "foo",
   122  			RequiredValue: "foo|bar",
   123  			Operation:     oval.OpPatternMatch,
   124  			Want:          true,
   125  		},
   126  		{
   127  			Name:          "pattern-not-match",
   128  			Value:         "foo",
   129  			RequiredValue: "bar|bar",
   130  			Operation:     oval.OpPatternMatch,
   131  			Want:          false,
   132  		},
   133  	}
   134  
   135  	for _, testCase := range testcases {
   136  		t.Run(
   137  			testCase.Name,
   138  			func(t *testing.T) {
   139  				got := Operation(
   140  					testCase.Value,
   141  					testCase.RequiredValue,
   142  					testCase.Operation,
   143  				)
   144  				if got != testCase.Want {
   145  					t.Errorf("got: %v, want: %v", got, testCase.Want)
   146  				}
   147  			},
   148  		)
   149  	}
   150  
   151  }