2019-03-05 08:07:13 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/smallstep/assert"
|
2019-04-25 00:42:14 +00:00
|
|
|
"github.com/smallstep/nosql/database"
|
2019-03-05 08:07:13 +00:00
|
|
|
)
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
type MockNoSQLDB struct {
|
2019-03-05 08:07:13 +00:00
|
|
|
err error
|
|
|
|
ret1, ret2 interface{}
|
|
|
|
get func(bucket, key []byte) ([]byte, error)
|
|
|
|
set func(bucket, key, value []byte) error
|
2019-04-25 00:42:14 +00:00
|
|
|
open func(dataSourceName string, opt ...database.Option) error
|
2019-03-05 08:07:13 +00:00
|
|
|
close func() error
|
|
|
|
createTable func(bucket []byte) error
|
|
|
|
deleteTable func(bucket []byte) error
|
|
|
|
del func(bucket, key []byte) error
|
2019-04-25 00:42:14 +00:00
|
|
|
list func(bucket []byte) ([]*database.Entry, error)
|
|
|
|
update func(tx *database.Tx) error
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
func (m *MockNoSQLDB) Get(bucket, key []byte) ([]byte, error) {
|
2019-03-05 08:07:13 +00:00
|
|
|
if m.get != nil {
|
|
|
|
return m.get(bucket, key)
|
|
|
|
}
|
|
|
|
if m.ret1 == nil {
|
|
|
|
return nil, m.err
|
|
|
|
}
|
|
|
|
return m.ret1.([]byte), m.err
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
func (m *MockNoSQLDB) Set(bucket, key, value []byte) error {
|
2019-03-05 08:07:13 +00:00
|
|
|
if m.set != nil {
|
|
|
|
return m.set(bucket, key, value)
|
|
|
|
}
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
func (m *MockNoSQLDB) Open(dataSourceName string, opt ...database.Option) error {
|
2019-03-05 08:07:13 +00:00
|
|
|
if m.open != nil {
|
2019-04-25 00:42:14 +00:00
|
|
|
return m.open(dataSourceName, opt...)
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
func (m *MockNoSQLDB) Close() error {
|
2019-03-05 08:07:13 +00:00
|
|
|
if m.close != nil {
|
|
|
|
return m.close()
|
|
|
|
}
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
func (m *MockNoSQLDB) CreateTable(bucket []byte) error {
|
2019-03-05 08:07:13 +00:00
|
|
|
if m.createTable != nil {
|
|
|
|
return m.createTable(bucket)
|
|
|
|
}
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
func (m *MockNoSQLDB) DeleteTable(bucket []byte) error {
|
2019-03-05 08:07:13 +00:00
|
|
|
if m.deleteTable != nil {
|
|
|
|
return m.deleteTable(bucket)
|
|
|
|
}
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
func (m *MockNoSQLDB) Del(bucket, key []byte) error {
|
2019-03-05 08:07:13 +00:00
|
|
|
if m.del != nil {
|
|
|
|
return m.del(bucket, key)
|
|
|
|
}
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
func (m *MockNoSQLDB) List(bucket []byte) ([]*database.Entry, error) {
|
2019-03-05 08:07:13 +00:00
|
|
|
if m.list != nil {
|
|
|
|
return m.list(bucket)
|
|
|
|
}
|
2019-04-25 00:42:14 +00:00
|
|
|
return m.ret1.([]*database.Entry), m.err
|
2019-03-05 08:07:13 +00:00
|
|
|
}
|
|
|
|
|
2019-04-25 19:37:25 +00:00
|
|
|
func (m *MockNoSQLDB) Update(tx *database.Tx) error {
|
2019-03-05 08:07:13 +00:00
|
|
|
if m.update != nil {
|
|
|
|
return m.update(tx)
|
|
|
|
}
|
|
|
|
return m.err
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestIsRevoked(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
|
|
|
key string
|
|
|
|
db *DB
|
|
|
|
isRevoked bool
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"false/nil db": {
|
|
|
|
key: "sn",
|
|
|
|
},
|
|
|
|
"false/ErrNotFound": {
|
|
|
|
key: "sn",
|
2019-04-25 19:37:25 +00:00
|
|
|
db: &DB{&MockNoSQLDB{err: database.ErrNotFound, ret1: nil}, true},
|
2019-03-05 08:07:13 +00:00
|
|
|
},
|
|
|
|
"error/checking bucket": {
|
|
|
|
key: "sn",
|
2019-04-25 19:37:25 +00:00
|
|
|
db: &DB{&MockNoSQLDB{err: errors.New("force"), ret1: nil}, true},
|
2019-03-05 08:07:13 +00:00
|
|
|
err: errors.New("error checking revocation bucket: force"),
|
|
|
|
},
|
|
|
|
"true": {
|
|
|
|
key: "sn",
|
2019-04-25 19:37:25 +00:00
|
|
|
db: &DB{&MockNoSQLDB{ret1: []byte("value")}, true},
|
2019-03-05 08:07:13 +00:00
|
|
|
isRevoked: true,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
isRevoked, err := tc.db.IsRevoked(tc.key)
|
|
|
|
if err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.HasPrefix(t, tc.err.Error(), err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
assert.Fatal(t, isRevoked == tc.isRevoked)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestRevoke(t *testing.T) {
|
|
|
|
tests := map[string]struct {
|
|
|
|
rci *RevokedCertificateInfo
|
|
|
|
db *DB
|
|
|
|
err error
|
|
|
|
}{
|
|
|
|
"error/force isRevoked": {
|
|
|
|
rci: &RevokedCertificateInfo{Serial: "sn"},
|
2019-04-25 19:37:25 +00:00
|
|
|
db: &DB{&MockNoSQLDB{
|
2019-03-05 08:07:13 +00:00
|
|
|
get: func(bucket []byte, sn []byte) ([]byte, error) {
|
|
|
|
return nil, errors.New("force IsRevoked")
|
|
|
|
},
|
2019-04-25 19:37:25 +00:00
|
|
|
}, true},
|
2019-03-05 08:07:13 +00:00
|
|
|
err: errors.New("error checking revocation bucket: force IsRevoked"),
|
|
|
|
},
|
|
|
|
"error/was already revoked": {
|
|
|
|
rci: &RevokedCertificateInfo{Serial: "sn"},
|
2019-04-25 19:37:25 +00:00
|
|
|
db: &DB{&MockNoSQLDB{
|
2019-03-05 08:07:13 +00:00
|
|
|
get: func(bucket []byte, sn []byte) ([]byte, error) {
|
|
|
|
return nil, nil
|
|
|
|
},
|
2019-04-25 19:37:25 +00:00
|
|
|
}, true},
|
2019-03-05 08:07:13 +00:00
|
|
|
err: ErrAlreadyExists,
|
|
|
|
},
|
|
|
|
"error/database set": {
|
|
|
|
rci: &RevokedCertificateInfo{Serial: "sn"},
|
2019-04-25 19:37:25 +00:00
|
|
|
db: &DB{&MockNoSQLDB{
|
2019-03-05 08:07:13 +00:00
|
|
|
get: func(bucket []byte, sn []byte) ([]byte, error) {
|
2019-04-25 00:42:14 +00:00
|
|
|
return nil, database.ErrNotFound
|
2019-03-05 08:07:13 +00:00
|
|
|
},
|
|
|
|
set: func(bucket []byte, key []byte, value []byte) error {
|
|
|
|
return errors.New("force")
|
|
|
|
},
|
2019-04-25 19:37:25 +00:00
|
|
|
}, true},
|
2019-03-05 08:07:13 +00:00
|
|
|
err: errors.New("database Set error: force"),
|
|
|
|
},
|
|
|
|
"ok": {
|
|
|
|
rci: &RevokedCertificateInfo{Serial: "sn"},
|
2019-04-25 19:37:25 +00:00
|
|
|
db: &DB{&MockNoSQLDB{
|
2019-03-05 08:07:13 +00:00
|
|
|
get: func(bucket []byte, sn []byte) ([]byte, error) {
|
2019-04-25 00:42:14 +00:00
|
|
|
return nil, database.ErrNotFound
|
2019-03-05 08:07:13 +00:00
|
|
|
},
|
|
|
|
set: func(bucket []byte, key []byte, value []byte) error {
|
|
|
|
return nil
|
|
|
|
},
|
2019-04-25 19:37:25 +00:00
|
|
|
}, true},
|
2019-03-05 08:07:13 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
for name, tc := range tests {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
if err := tc.db.Revoke(tc.rci); err != nil {
|
|
|
|
if assert.NotNil(t, tc.err) {
|
|
|
|
assert.HasPrefix(t, tc.err.Error(), err.Error())
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert.Nil(t, tc.err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|