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 (
|
|
|
|
"fmt"
|
|
|
|
"math/rand"
|
|
|
|
"runtime"
|
|
|
|
"strconv"
|
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-05-02 22:33:03 +00:00
|
|
|
"github.com/pborman/uuid"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
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)
|
|
|
|
|
|
|
|
features[i] = database.Feature{
|
|
|
|
Name: featureName,
|
|
|
|
VersionFormat: featureVersionFormat,
|
|
|
|
Version: strconv.Itoa(version),
|
|
|
|
}
|
2016-01-18 23:52:16 +00:00
|
|
|
|
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
|
|
|
// insert features
|
|
|
|
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,
|
|
|
|
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) {
|
|
|
|
store, err := openDatabaseForTest("Concurrency", false)
|
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
t.FailNow()
|
2016-01-18 23:52:16 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
defer store.Close()
|
2016-01-18 23:52:16 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
start := time.Now()
|
2016-01-18 23:52:16 +00:00
|
|
|
var wg sync.WaitGroup
|
2017-07-26 23:23:54 +00:00
|
|
|
wg.Add(100)
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
|
|
|
nsNamespaces := genRandomNamespaces(t, 100)
|
|
|
|
tx, err := store.Begin()
|
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
assert.Nil(t, tx.PersistNamespaces(nsNamespaces))
|
|
|
|
tx.Commit()
|
|
|
|
}()
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
fmt.Println("total", time.Since(start))
|
|
|
|
}
|
|
|
|
|
|
|
|
func genRandomNamespaces(t *testing.T, count int) []database.Namespace {
|
|
|
|
r := make([]database.Namespace, count)
|
|
|
|
for i := 0; i < count; i++ {
|
|
|
|
r[i] = database.Namespace{
|
|
|
|
Name: uuid.New(),
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestCaching(t *testing.T) {
|
|
|
|
store, err := openDatabaseForTest("Caching", false)
|
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
defer store.Close()
|
2016-01-18 23:52:16 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
nsFeatures, vulnerabilities := testGenRandomVulnerabilityAndNamespacedFeature(t, store)
|
|
|
|
|
|
|
|
fmt.Printf("%d features, %d vulnerabilities are generated", len(nsFeatures), len(vulnerabilities))
|
|
|
|
|
|
|
|
var wg sync.WaitGroup
|
|
|
|
wg.Add(2)
|
2016-01-18 23:52:16 +00:00
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2017-07-26 23:23:54 +00:00
|
|
|
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
|
|
|
|
|
|
|
assert.Nil(t, tx.PersistNamespacedFeatures(nsFeatures))
|
|
|
|
fmt.Println("finished to insert namespaced features")
|
|
|
|
|
|
|
|
tx.Commit()
|
2016-01-18 23:52:16 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2017-07-26 23:23:54 +00:00
|
|
|
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
|
|
|
|
|
|
|
assert.Nil(t, tx.InsertVulnerabilities(vulnerabilities))
|
|
|
|
fmt.Println("finished to insert vulnerabilities")
|
|
|
|
tx.Commit()
|
|
|
|
|
2016-01-18 23:52:16 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
wg.Wait()
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
tx, err := store.Begin()
|
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
|
|
|
defer tx.Rollback()
|
2016-01-18 23:52:16 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// Verify consistency now.
|
|
|
|
affected, err := tx.FindAffectedNamespacedFeatures(nsFeatures)
|
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
2016-01-18 23:52:16 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
for _, ansf := range affected {
|
|
|
|
if !assert.True(t, ansf.Valid) {
|
|
|
|
t.FailNow()
|
|
|
|
}
|
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
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Len(t, strutil.CompareStringLists(expectedAffectedNames, actualAffectedNames), 0)
|
|
|
|
assert.Len(t, strutil.CompareStringLists(actualAffectedNames, expectedAffectedNames), 0)
|
2016-01-18 23:52:16 +00:00
|
|
|
}
|
|
|
|
}
|