github.com/quay/claircore@v1.5.28/python/matcher_test.go (about)

     1  package python_test
     2  
     3  import (
     4  	"context"
     5  	"testing"
     6  
     7  	"github.com/google/go-cmp/cmp"
     8  	"github.com/quay/zlog"
     9  
    10  	"github.com/quay/claircore"
    11  	"github.com/quay/claircore/libvuln/driver"
    12  	"github.com/quay/claircore/python"
    13  )
    14  
    15  type matcherTestcase struct {
    16  	Matcher driver.Matcher
    17  	Name    string
    18  	R       claircore.IndexRecord
    19  	V       claircore.Vulnerability
    20  	Want    bool
    21  }
    22  
    23  func (tc matcherTestcase) Run(t *testing.T) {
    24  	t.Parallel()
    25  	ctx := zlog.Test(context.Background(), t)
    26  	got, err := tc.Matcher.Vulnerable(ctx, &tc.R, &tc.V)
    27  	if err != nil {
    28  		t.Error(err)
    29  	}
    30  	want := tc.Want
    31  	if got != want {
    32  		t.Errorf("got: %v, want: %v", got, want)
    33  		t.Logf("record:\n%#+v", &tc.R)
    34  		t.Logf("package:\n%#+v\n%#+v", tc.R.Package, tc.R.Package.NormalizedVersion)
    35  		t.Logf("vulnerability:\n%#+v", &tc.V)
    36  		t.Logf("range:\n%#+v", tc.V.Range)
    37  	}
    38  }
    39  
    40  // TestMatcher tests the python matcher.
    41  func TestMatcher(t *testing.T) {
    42  	t.Parallel()
    43  	testcases := []matcherTestcase{
    44  		{
    45  			Name: "Simple",
    46  			R: claircore.IndexRecord{
    47  				Package: &claircore.Package{
    48  					Version: "0.9.8",
    49  				},
    50  			},
    51  			V: claircore.Vulnerability{
    52  				Package: &claircore.Package{
    53  					Name: "test2",
    54  				},
    55  				FixedInVersion: "fixed=1.0.0&introduced=0.7.0",
    56  			},
    57  			Want:    true,
    58  			Matcher: &python.Matcher{},
    59  		},
    60  		{
    61  			Name: "BoundedHit",
    62  			R: claircore.IndexRecord{
    63  				Package: &claircore.Package{
    64  					Version: "1.4.3",
    65  				},
    66  			},
    67  			V: claircore.Vulnerability{
    68  				Package: &claircore.Package{
    69  					Name: "test3",
    70  				},
    71  				FixedInVersion: "fixed=1.4.4&introduced=0",
    72  			},
    73  			Want:    true,
    74  			Matcher: &python.Matcher{},
    75  		},
    76  		{
    77  			Name: "BoundedMiss",
    78  			R: claircore.IndexRecord{
    79  				Package: &claircore.Package{
    80  					Version: "1.4.3",
    81  				},
    82  			},
    83  			V: claircore.Vulnerability{
    84  				Package: &claircore.Package{
    85  					Name: "test4",
    86  				},
    87  				FixedInVersion: "fixed=1.4.3&introduced=0",
    88  			},
    89  			Want:    false,
    90  			Matcher: &python.Matcher{},
    91  		},
    92  		{
    93  			Name: "Test0",
    94  			R: claircore.IndexRecord{
    95  				Package: &claircore.Package{
    96  					Version: "1.0a1",
    97  				},
    98  			},
    99  			V: claircore.Vulnerability{
   100  				Package: &claircore.Package{
   101  					Name: "testPkg0",
   102  				},
   103  				FixedInVersion: "fixed=1.0b1&introduced=0",
   104  			},
   105  			Want:    true,
   106  			Matcher: &python.Matcher{},
   107  		},
   108  		{
   109  			Name: "Test1",
   110  			R: claircore.IndexRecord{
   111  				Package: &claircore.Package{
   112  					Version: "1.0.post1",
   113  				},
   114  			},
   115  			V: claircore.Vulnerability{
   116  				Package: &claircore.Package{
   117  					Name: "testPkg1",
   118  				},
   119  				FixedInVersion: "fixed=1.0.post2&introduced=0",
   120  			},
   121  			Want:    true,
   122  			Matcher: &python.Matcher{},
   123  		},
   124  		{
   125  			Name: "Test2",
   126  			R: claircore.IndexRecord{
   127  				Package: &claircore.Package{
   128  					Version: "1.0+local.1",
   129  				},
   130  			},
   131  			V: claircore.Vulnerability{
   132  				Package: &claircore.Package{
   133  					Name: "testPkg2",
   134  				},
   135  				FixedInVersion: "fixed=1.0+local.2&introduced=0",
   136  			},
   137  			Want:    false,
   138  			Matcher: &python.Matcher{},
   139  		},
   140  		{
   141  			Name: "Test3",
   142  			R: claircore.IndexRecord{
   143  				Package: &claircore.Package{
   144  					Version: "0.5+local.1",
   145  				},
   146  			},
   147  			V: claircore.Vulnerability{
   148  				Package: &claircore.Package{
   149  					Name: "testPkg3",
   150  				},
   151  				FixedInVersion: "fixed=1.0+local.2&introduced=0.8",
   152  			},
   153  			Want:    false,
   154  			Matcher: &python.Matcher{},
   155  		},
   156  		{
   157  			Name: "Test4",
   158  			R: claircore.IndexRecord{
   159  				Package: &claircore.Package{
   160  					Version: "1.0.0",
   161  				},
   162  			},
   163  			V: claircore.Vulnerability{
   164  				Package: &claircore.Package{
   165  					Name: "testPkg4",
   166  				},
   167  				FixedInVersion: "introduced=2.2.0&lastAffected=3.0.1",
   168  			},
   169  			Want:    false,
   170  			Matcher: &python.Matcher{},
   171  		},
   172  		{
   173  			Name: "Test5",
   174  			R: claircore.IndexRecord{
   175  				Package: &claircore.Package{
   176  					Version: "3.0.1",
   177  				},
   178  			},
   179  			V: claircore.Vulnerability{
   180  				Package: &claircore.Package{
   181  					Name: "testPkg5",
   182  				},
   183  				FixedInVersion: "introduced=2.2.0&lastAffected=3.0.1",
   184  			},
   185  			Want:    true,
   186  			Matcher: &python.Matcher{},
   187  		},
   188  		{
   189  			Name: "Test6",
   190  			R: claircore.IndexRecord{
   191  				Package: &claircore.Package{
   192  					Version: "3.0.2",
   193  				},
   194  			},
   195  			V: claircore.Vulnerability{
   196  				Package: &claircore.Package{
   197  					Name: "testPkg6",
   198  				},
   199  				FixedInVersion: "introduced=2.2.0&lastAffected=3.0.1",
   200  			},
   201  			Want:    false,
   202  			Matcher: &python.Matcher{},
   203  		},
   204  		{
   205  			Name: "Test7",
   206  			R: claircore.IndexRecord{
   207  				Package: &claircore.Package{
   208  					Version: "3.0.2",
   209  				},
   210  			},
   211  			V: claircore.Vulnerability{
   212  				Package: &claircore.Package{
   213  					Name: "testPkg7",
   214  				},
   215  				FixedInVersion: "introduced=2.2.0",
   216  			},
   217  			Want:    true,
   218  			Matcher: &python.Matcher{},
   219  		},
   220  		{
   221  			Name: "Test8",
   222  			R: claircore.IndexRecord{
   223  				Package: &claircore.Package{
   224  					Version: "3.0.2",
   225  				},
   226  			},
   227  			V: claircore.Vulnerability{
   228  				Package: &claircore.Package{
   229  					Name: "testPkg8",
   230  				},
   231  				FixedInVersion: "introduced=3.2.0",
   232  			},
   233  			Want:    false,
   234  			Matcher: &python.Matcher{},
   235  		},
   236  	}
   237  
   238  	for _, testcase := range testcases {
   239  		t.Run(testcase.Name, func(t *testing.T) {
   240  			got, err := testcase.Matcher.Vulnerable(context.Background(), &testcase.R, &testcase.V)
   241  			if err != nil {
   242  				t.Fatal(err)
   243  			}
   244  			if !cmp.Equal(got, testcase.Want) {
   245  				t.Error(cmp.Diff(got, testcase.Want))
   246  			}
   247  		})
   248  	}
   249  }