certificates/db/db_test.go

161 lines
3.5 KiB
Go
Raw Normal View History

package db
import (
"errors"
"testing"
"github.com/smallstep/assert"
"github.com/smallstep/nosql/database"
)
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-05-27 00:41:10 +00:00
db: &DB{&MockNoSQLDB{Err: database.ErrNotFound, Ret1: nil}, true},
},
"error/checking bucket": {
key: "sn",
2019-05-27 00:41:10 +00:00
db: &DB{&MockNoSQLDB{Err: errors.New("force"), Ret1: nil}, true},
err: errors.New("error checking revocation bucket: force"),
},
"true": {
key: "sn",
2019-05-27 00:41:10 +00:00
db: &DB{&MockNoSQLDB{Ret1: []byte("value")}, true},
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"},
db: &DB{&MockNoSQLDB{
2019-05-27 00:41:10 +00:00
MCmpAndSwap: func(bucket, sn, old, newval []byte) ([]byte, bool, error) {
return nil, false, errors.New("force")
},
}, true},
2019-05-27 00:41:10 +00:00
err: errors.New("error AuthDB CmpAndSwap: force"),
},
"error/was already revoked": {
rci: &RevokedCertificateInfo{Serial: "sn"},
db: &DB{&MockNoSQLDB{
2019-05-27 00:41:10 +00:00
MCmpAndSwap: func(bucket, sn, old, newval []byte) ([]byte, bool, error) {
return []byte("foo"), false, nil
},
}, true},
err: ErrAlreadyExists,
},
"ok": {
rci: &RevokedCertificateInfo{Serial: "sn"},
db: &DB{&MockNoSQLDB{
2019-05-27 00:41:10 +00:00
MCmpAndSwap: func(bucket, sn, old, newval []byte) ([]byte, bool, error) {
return []byte("foo"), true, nil
},
}, true},
},
}
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)
}
})
}
}
func TestUseToken(t *testing.T) {
type result struct {
err error
ok bool
}
tests := map[string]struct {
id, tok string
db *DB
want result
}{
2019-06-10 20:21:06 +00:00
"fail/force-CmpAndSwap-error": {
id: "id",
tok: "token",
db: &DB{&MockNoSQLDB{
2019-05-27 00:41:10 +00:00
MCmpAndSwap: func(bucket, key, old, newval []byte) ([]byte, bool, error) {
return nil, false, errors.New("force")
},
}, true},
want: result{
ok: false,
2019-06-10 20:21:06 +00:00
err: errors.New("error storing used token used_ott/id"),
},
},
2019-06-10 20:21:06 +00:00
"fail/CmpAndSwap-already-exists": {
id: "id",
tok: "token",
db: &DB{&MockNoSQLDB{
2019-05-27 00:41:10 +00:00
MCmpAndSwap: func(bucket, key, old, newval []byte) ([]byte, bool, error) {
2019-06-10 20:21:06 +00:00
return []byte("foo"), false, nil
},
}, true},
want: result{
ok: false,
},
},
2019-06-10 20:21:06 +00:00
"ok/cmpAndSwap-success": {
id: "id",
tok: "token",
db: &DB{&MockNoSQLDB{
2019-05-27 00:41:10 +00:00
MCmpAndSwap: func(bucket, key, old, newval []byte) ([]byte, bool, error) {
2019-06-10 20:21:06 +00:00
return []byte("bar"), true, nil
},
}, true},
want: result{
ok: true,
},
},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
ok, err := tc.db.UseToken(tc.id, tc.tok)
if err != nil {
if assert.NotNil(t, tc.want.err) {
2019-06-10 20:21:06 +00:00
assert.HasPrefix(t, err.Error(), tc.want.err.Error())
}
assert.False(t, ok)
2019-06-10 20:21:06 +00:00
} else if ok {
assert.True(t, tc.want.ok)
} else {
2019-06-10 20:21:06 +00:00
assert.False(t, tc.want.ok)
}
})
}
}