2018-09-06 23:15:06 +00:00
|
|
|
// Copyright 2018 clair authors
|
2016-05-17 21:22:18 +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-26 22:57:32 +00:00
|
|
|
package pgsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
2016-05-02 22:33:03 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
2016-01-26 22:57:32 +00:00
|
|
|
"github.com/coreos/clair/database"
|
|
|
|
)
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func TestPagination(t *testing.T) {
|
|
|
|
datastore, tx := openSessionForTest(t, "Pagination", true)
|
|
|
|
defer closeTest(t, datastore, tx)
|
|
|
|
|
|
|
|
ns := database.Namespace{
|
|
|
|
Name: "debian:7",
|
|
|
|
VersionFormat: "dpkg",
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
vNew := database.Vulnerability{
|
|
|
|
Namespace: ns,
|
|
|
|
Name: "CVE-OPENSSL-1-DEB7",
|
|
|
|
Description: "A vulnerability affecting OpenSSL < 2.0 on Debian 7.0",
|
|
|
|
Link: "http://google.com/#q=CVE-OPENSSL-1-DEB7",
|
|
|
|
Severity: database.HighSeverity,
|
2016-02-04 20:12:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
vOld := database.Vulnerability{
|
|
|
|
Namespace: ns,
|
|
|
|
Name: "CVE-NOPE",
|
|
|
|
Description: "A vulnerability affecting nothing",
|
|
|
|
Severity: database.UnknownSeverity,
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
noti, ok, err := tx.FindVulnerabilityNotification("test", 1, "", "")
|
|
|
|
oldPage := database.PagedVulnerableAncestries{
|
|
|
|
Vulnerability: vOld,
|
|
|
|
Limit: 1,
|
|
|
|
Affected: make(map[int]string),
|
|
|
|
End: true,
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
newPage1 := database.PagedVulnerableAncestries{
|
|
|
|
Vulnerability: vNew,
|
|
|
|
Limit: 1,
|
|
|
|
Affected: map[int]string{3: "ancestry-3"},
|
|
|
|
End: false,
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
newPage2 := database.PagedVulnerableAncestries{
|
|
|
|
Vulnerability: vNew,
|
|
|
|
Limit: 1,
|
|
|
|
Affected: map[int]string{4: "ancestry-4"},
|
|
|
|
Next: "",
|
|
|
|
End: true,
|
2016-02-04 20:12:41 +00:00
|
|
|
}
|
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
if assert.Nil(t, err) && assert.True(t, ok) {
|
|
|
|
assert.Equal(t, "test", noti.Name)
|
|
|
|
if assert.NotNil(t, noti.Old) && assert.NotNil(t, noti.New) {
|
2018-09-06 23:15:06 +00:00
|
|
|
oldPage, err := PageFromPageNumber(tx.key, noti.Old.Current)
|
2017-07-26 23:23:54 +00:00
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
assert.FailNow(t, "")
|
|
|
|
}
|
|
|
|
|
2018-09-06 23:15:06 +00:00
|
|
|
assert.Equal(t, int64(0), oldPage.StartID)
|
|
|
|
newPage, err := PageFromPageNumber(tx.key, noti.New.Current)
|
2017-07-26 23:23:54 +00:00
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
assert.FailNow(t, "")
|
|
|
|
}
|
2018-09-06 23:15:06 +00:00
|
|
|
newPageNext, err := PageFromPageNumber(tx.key, noti.New.Next)
|
2017-07-26 23:23:54 +00:00
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
assert.FailNow(t, "")
|
|
|
|
}
|
2018-09-06 23:15:06 +00:00
|
|
|
assert.Equal(t, int64(0), newPage.StartID)
|
|
|
|
assert.Equal(t, int64(4), newPageNext.StartID)
|
2017-07-26 23:23:54 +00:00
|
|
|
|
|
|
|
noti.Old.Current = ""
|
|
|
|
noti.New.Current = ""
|
|
|
|
noti.New.Next = ""
|
|
|
|
assert.Equal(t, oldPage, *noti.Old)
|
|
|
|
assert.Equal(t, newPage1, *noti.New)
|
|
|
|
}
|
2016-02-04 16:34:01 +00:00
|
|
|
}
|
|
|
|
|
2018-09-06 23:15:06 +00:00
|
|
|
pageNum1, err := Page{0}.PageNumber(tx.key)
|
2017-07-26 23:23:54 +00:00
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
assert.FailNow(t, "")
|
2016-02-04 16:34:01 +00:00
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2018-09-06 23:15:06 +00:00
|
|
|
pageNum2, err := Page{4}.PageNumber(tx.key)
|
2017-07-26 23:23:54 +00:00
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
assert.FailNow(t, "")
|
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2018-09-06 23:15:06 +00:00
|
|
|
noti, ok, err = tx.FindVulnerabilityNotification("test", 1, pageNum1, pageNum2)
|
2017-07-26 23:23:54 +00:00
|
|
|
if assert.Nil(t, err) && assert.True(t, ok) {
|
|
|
|
assert.Equal(t, "test", noti.Name)
|
|
|
|
if assert.NotNil(t, noti.Old) && assert.NotNil(t, noti.New) {
|
2018-09-06 23:15:06 +00:00
|
|
|
oldCurrentPage, err := PageFromPageNumber(tx.key, noti.Old.Current)
|
2017-07-26 23:23:54 +00:00
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
assert.FailNow(t, "")
|
|
|
|
}
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2018-09-06 23:15:06 +00:00
|
|
|
newCurrentPage, err := PageFromPageNumber(tx.key, noti.New.Current)
|
2017-07-26 23:23:54 +00:00
|
|
|
if !assert.Nil(t, err) {
|
|
|
|
assert.FailNow(t, "")
|
|
|
|
}
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Equal(t, int64(0), oldCurrentPage.StartID)
|
|
|
|
assert.Equal(t, int64(4), newCurrentPage.StartID)
|
|
|
|
noti.Old.Current = ""
|
|
|
|
noti.New.Current = ""
|
|
|
|
assert.Equal(t, oldPage, *noti.Old)
|
|
|
|
assert.Equal(t, newPage2, *noti.New)
|
2016-02-04 20:12:41 +00:00
|
|
|
}
|
2017-07-26 23:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func TestInsertVulnerabilityNotifications(t *testing.T) {
|
|
|
|
datastore, tx := openSessionForTest(t, "InsertVulnerabilityNotifications", true)
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
n1 := database.VulnerabilityNotification{}
|
|
|
|
n3 := database.VulnerabilityNotification{
|
|
|
|
NotificationHook: database.NotificationHook{
|
|
|
|
Name: "random name",
|
|
|
|
Created: time.Now(),
|
|
|
|
},
|
|
|
|
Old: nil,
|
|
|
|
New: &database.Vulnerability{},
|
|
|
|
}
|
|
|
|
n4 := database.VulnerabilityNotification{
|
|
|
|
NotificationHook: database.NotificationHook{
|
|
|
|
Name: "random name",
|
|
|
|
Created: time.Now(),
|
|
|
|
},
|
|
|
|
Old: nil,
|
|
|
|
New: &database.Vulnerability{
|
|
|
|
Name: "CVE-OPENSSL-1-DEB7",
|
|
|
|
Namespace: database.Namespace{
|
|
|
|
Name: "debian:7",
|
|
|
|
VersionFormat: "dpkg",
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// invalid case
|
|
|
|
err := tx.InsertVulnerabilityNotifications([]database.VulnerabilityNotification{n1})
|
|
|
|
assert.NotNil(t, err)
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// invalid case: unknown vulnerability
|
|
|
|
err = tx.InsertVulnerabilityNotifications([]database.VulnerabilityNotification{n3})
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
// invalid case: duplicated input notification
|
|
|
|
err = tx.InsertVulnerabilityNotifications([]database.VulnerabilityNotification{n4, n4})
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
tx = restartSession(t, datastore, tx, false)
|
|
|
|
|
|
|
|
// valid case
|
|
|
|
err = tx.InsertVulnerabilityNotifications([]database.VulnerabilityNotification{n4})
|
|
|
|
assert.Nil(t, err)
|
|
|
|
// invalid case: notification is already in database
|
|
|
|
err = tx.InsertVulnerabilityNotifications([]database.VulnerabilityNotification{n4})
|
|
|
|
assert.NotNil(t, err)
|
|
|
|
|
|
|
|
closeTest(t, datastore, tx)
|
|
|
|
}
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func TestFindNewNotification(t *testing.T) {
|
|
|
|
datastore, tx := openSessionForTest(t, "FindNewNotification", true)
|
|
|
|
defer closeTest(t, datastore, tx)
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
noti, ok, err := tx.FindNewNotification(time.Now())
|
|
|
|
if assert.Nil(t, err) && assert.True(t, ok) {
|
|
|
|
assert.Equal(t, "test", noti.Name)
|
|
|
|
assert.Equal(t, time.Time{}, noti.Notified)
|
|
|
|
assert.Equal(t, time.Time{}, noti.Created)
|
|
|
|
assert.Equal(t, time.Time{}, noti.Deleted)
|
2016-02-04 16:34:01 +00:00
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// can't find the notified
|
|
|
|
assert.Nil(t, tx.MarkNotificationNotified("test"))
|
|
|
|
// if the notified time is before
|
|
|
|
noti, ok, err = tx.FindNewNotification(time.Now().Add(-time.Duration(10 * time.Second)))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.False(t, ok)
|
|
|
|
// can find the notified after a period of time
|
|
|
|
noti, ok, err = tx.FindNewNotification(time.Now().Add(time.Duration(1000)))
|
|
|
|
if assert.Nil(t, err) && assert.True(t, ok) {
|
|
|
|
assert.Equal(t, "test", noti.Name)
|
|
|
|
assert.NotEqual(t, time.Time{}, noti.Notified)
|
|
|
|
assert.Equal(t, time.Time{}, noti.Created)
|
|
|
|
assert.Equal(t, time.Time{}, noti.Deleted)
|
2016-02-04 20:12:41 +00:00
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
assert.Nil(t, tx.DeleteNotification("test"))
|
|
|
|
// can't find in any time
|
|
|
|
noti, ok, err = tx.FindNewNotification(time.Now().Add(-time.Duration(1000)))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.False(t, ok)
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
noti, ok, err = tx.FindNewNotification(time.Now().Add(time.Duration(1000)))
|
|
|
|
assert.Nil(t, err)
|
|
|
|
assert.False(t, ok)
|
|
|
|
}
|
2016-01-26 22:57:32 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
func TestMarkNotificationNotified(t *testing.T) {
|
|
|
|
datastore, tx := openSessionForTest(t, "MarkNotificationNotified", true)
|
|
|
|
defer closeTest(t, datastore, tx)
|
2016-02-04 20:12:41 +00:00
|
|
|
|
2017-07-26 23:23:54 +00:00
|
|
|
// invalid case: notification doesn't exist
|
|
|
|
assert.NotNil(t, tx.MarkNotificationNotified("non-existing"))
|
|
|
|
// valid case
|
|
|
|
assert.Nil(t, tx.MarkNotificationNotified("test"))
|
|
|
|
// valid case
|
|
|
|
assert.Nil(t, tx.MarkNotificationNotified("test"))
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestDeleteNotification(t *testing.T) {
|
|
|
|
datastore, tx := openSessionForTest(t, "DeleteNotification", true)
|
|
|
|
defer closeTest(t, datastore, tx)
|
|
|
|
|
|
|
|
// invalid case: notification doesn't exist
|
|
|
|
assert.NotNil(t, tx.DeleteNotification("non-existing"))
|
|
|
|
// valid case
|
|
|
|
assert.Nil(t, tx.DeleteNotification("test"))
|
|
|
|
// invalid case: notification is already deleted
|
|
|
|
assert.NotNil(t, tx.DeleteNotification("test"))
|
2016-01-26 22:57:32 +00:00
|
|
|
}
|