2017-01-15 15:52:13 +00:00
|
|
|
// Copyright 2017 clair authors
|
2016-01-19 20:16:45 +00:00
|
|
|
//
|
|
|
|
// 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.
|
|
|
|
|
2016-01-18 23:52:16 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"math/rand"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-05-02 22:33:03 +00:00
|
|
|
"github.com/pborman/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
2019-02-19 21:42:00 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-05-02 22:33:03 +00:00
|
|
|
|
2016-01-18 23:52:16 +00:00
|
|
|
"github.com/coreos/clair/database"
|
2017-07-26 23:23:54 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt"
|
2017-01-03 21:00:20 +00:00
|
|
|
"github.com/coreos/clair/ext/versionfmt/dpkg"
|
2017-07-26 23:23:54 +00:00
|
|
|
"github.com/coreos/clair/pkg/strutil"
|
2016-01-18 23:52:16 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
numVulnerabilities = 100
|
2017-07-26 23:23:54 +00:00
|
|
|
numFeatures = 100
|
2016-01-18 23:52:16 +00:00
|
|
|
)
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func testGenRandomVulnerabilityAndNamespacedFeature(t *testing.T, store database.Datastore) ([]database.NamespacedFeature, []database.VulnerabilityWithAffected) {
|
|
|
|
tx, err := store.Begin()
|
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
t.FailNow()
|
2016-01-18 23:52:16 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
featureName := "TestFeature"
|
|
|
|
featureVersionFormat := dpkg.ParserName
|
|
|
|
// Insert the namespace on which we'll work.
|
|
|
|
namespace := database.Namespace{
|
|
|
|
Name: "TestRaceAffectsFeatureNamespace1",
|
|
|
|
VersionFormat: dpkg.ParserName,
|
2016-01-18 23:52:16 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
if !assert.Nil(t, tx.PersistNamespaces([]database.Namespace{namespace})) {
|
|
|
|
t.FailNow()
|
2016-01-18 23:52:16 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize random generator and enforce max procs.
|
|
|
|
rand.Seed(time.Now().UnixNano())
|
|
|
|
runtime.GOMAXPROCS(runtime.NumCPU())
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// Generate Distinct random features
|
|
|
|
features := make([]database.Feature, numFeatures)
|
|
|
|
nsFeatures := make([]database.NamespacedFeature, numFeatures)
|
|
|
|
for i := 0; i < numFeatures; i++ {
|
|
|
|
version := rand.Intn(numFeatures)
|
|
|
|
|
2019-02-19 21:42:00 +00:00
|
|
|
features[i] = *database.NewSourcePackage(featureName, strconv.Itoa(version), featureVersionFormat)
|
2017-07-26 23:23:54 +00:00
|
|
|
nsFeatures[i] = database.NamespacedFeature{
|
|
|
|
Namespace: namespace,
|
|
|
|
Feature: features[i],
|
2016-01-18 23:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if !assert.Nil(t, tx.PersistFeatures(features)) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
|
2016-01-18 23:52:16 +00:00
|
|
|
// Generate vulnerabilities.
|
2017-07-26 23:23:54 +00:00
|
|
|
vulnerabilities := []database.VulnerabilityWithAffected{}
|
2016-01-18 23:52:16 +00:00
|
|
|
for i := 0; i < numVulnerabilities; i++ {
|
2017-07-26 23:23:54 +00:00
|
|
|
// any version less than this is vulnerable
|
|
|
|
version := rand.Intn(numFeatures) + 1
|
|
|
|
|
|
|
|
vulnerability := database.VulnerabilityWithAffected{
|
|
|
|
Vulnerability: database.Vulnerability{
|
|
|
|
Name: uuid.New(),
|
|
|
|
Namespace: namespace,
|
|
|
|
Severity: database.UnknownSeverity,
|
|
|
|
},
|
|
|
|
Affected: []database.AffectedFeature{
|
2016-02-25 00:29:36 +00:00
|
|
|
{
|
2017-07-26 23:23:54 +00:00
|
|
|
Namespace: namespace,
|
|
|
|
FeatureName: featureName,
|
2019-02-19 21:42:00 +00:00
|
|
|
FeatureType: database.SourcePackage,
|
2017-07-26 23:23:54 +00:00
|
|
|
AffectedVersion: strconv.Itoa(version),
|
|
|
|
FixedInVersion: strconv.Itoa(version),
|
2016-01-18 23:52:16 +00:00
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
vulnerabilities = append(vulnerabilities, vulnerability)
|
|
|
|
}
|
|
|
|
tx.Commit()
|
|
|
|
|
|
|
|
return nsFeatures, vulnerabilities
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestConcurrency(t *testing.T) {
|
2019-03-06 18:44:23 +00:00
|
|
|
store, cleanup := createTestPgSQL(t, "concurrency")
|
|
|
|
defer cleanup()
|
|
|
|
|
2016-01-18 23:52:16 +00:00
|
|
|
var wg sync.WaitGroup
|
2019-03-06 18:44:23 +00:00
|
|
|
// there's a limit on the number of concurrent connections in the pool
|
|
|
|
wg.Add(30)
|
|
|
|
for i := 0; i < 30; i++ {
|
2017-07-26 23:23:54 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
nsNamespaces := genRandomNamespaces(t, 100)
|
|
|
|
tx, err := store.Begin()
|
2019-03-06 18:44:23 +00:00
|
|
|
require.Nil(t, err)
|
|
|
|
require.Nil(t, tx.PersistNamespaces(nsNamespaces))
|
|
|
|
require.Nil(t, tx.Commit())
|
2017-07-26 23:23:54 +00:00
|
|
|
}()
|
|
|
|
}
|
2019-03-06 18:44:23 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
wg.Wait()
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCaching(t *testing.T) {
|
2019-03-06 18:44:23 +00:00
|
|
|
store, cleanup := createTestPgSQL(t, "caching")
|
|
|
|
defer cleanup()
|
2019-02-19 22:57:38 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
nsFeatures, vulnerabilities := testGenRandomVulnerabilityAndNamespacedFeature(t, store)
|
2019-02-19 21:42:00 +00:00
|
|
|
tx, err := store.Begin()
|
2019-02-19 22:57:38 +00:00
|
|
|
require.Nil(t, err)
|
2017-07-26 23:23:54 +00:00
|
|
|
|
2019-02-19 21:42:00 +00:00
|
|
|
require.Nil(t, tx.PersistNamespacedFeatures(nsFeatures))
|
2019-02-19 22:57:38 +00:00
|
|
|
require.Nil(t, tx.Commit())
|
2017-07-26 23:23:54 +00:00
|
|
|
|
2019-02-19 21:42:00 +00:00
|
|
|
tx, err = store.Begin()
|
|
|
|
require.Nil(t, tx.InsertVulnerabilities(vulnerabilities))
|
2019-02-19 22:57:38 +00:00
|
|
|
require.Nil(t, tx.Commit())
|
2016-01-18 23:52:16 +00:00
|
|
|
|
2019-02-19 21:42:00 +00:00
|
|
|
tx, err = store.Begin()
|
2019-02-19 22:57:38 +00:00
|
|
|
require.Nil(t, err)
|
2017-07-26 23:23:54 +00:00
|
|
|
defer tx.Rollback()
|
2016-01-18 23:52:16 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
affected, err := tx.FindAffectedNamespacedFeatures(nsFeatures)
|
2019-02-19 22:57:38 +00:00
|
|
|
require.Nil(t, err)
|
2016-01-18 23:52:16 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
for _, ansf := range affected {
|
2019-02-19 22:57:38 +00:00
|
|
|
require.True(t, ansf.Valid)
|
2016-01-18 23:52:16 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
expectedAffectedNames := []string{}
|
|
|
|
for _, vuln := range vulnerabilities {
|
|
|
|
if ok, err := versionfmt.InRange(dpkg.ParserName, ansf.Version, vuln.Affected[0].AffectedVersion); err == nil {
|
|
|
|
if ok {
|
|
|
|
expectedAffectedNames = append(expectedAffectedNames, vuln.Name)
|
|
|
|
}
|
2016-01-18 23:52:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
actualAffectedNames := []string{}
|
|
|
|
for _, s := range ansf.AffectedBy {
|
|
|
|
actualAffectedNames = append(actualAffectedNames, s.Name)
|
2016-01-18 23:52:16 +00:00
|
|
|
}
|
|
|
|
|
2019-02-19 21:42:00 +00:00
|
|
|
require.Len(t, strutil.Difference(expectedAffectedNames, actualAffectedNames), 0, "\nvulns: %#v\nfeature:%#v\nexpected:%#v\nactual:%#v", vulnerabilities, ansf.NamespacedFeature, expectedAffectedNames, actualAffectedNames)
|
|
|
|
require.Len(t, strutil.Difference(actualAffectedNames, expectedAffectedNames), 0)
|
2016-01-18 23:52:16 +00:00
|
|
|
}
|
|
|
|
}
|