gosecret/service_funcs_test.go

348 lines
10 KiB
Go
Raw Normal View History

package gosecret
import (
2021-12-08 02:34:27 -05:00
"testing"
2021-12-10 02:50:30 -05:00
`github.com/godbus/dbus/v5`
)
2021-12-08 02:34:27 -05:00
/*
TestNewService tests the following internal functions/methods via nested calls:
NewService
Service.GetSession
Service.OpenSession
NewSession
validConnPath
connIsValid
pathIsValid
Service.Close
Session.Close
*/
func TestNewService(t *testing.T) {
var err error
2021-12-08 02:34:27 -05:00
var svc *Service
if svc, err = NewService(); err != nil {
t.Fatalf("could not get new Service via NewService: %v", err.Error())
}
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
}
/*
TestService_Collections tests the following internal functions/methods via nested calls:
(all calls in TestNewService)
Service.Collections
NewCollection
Collection.Modified
NewErrors
*/
func TestService_Collections(t *testing.T) {
var err error
var svc *Service
var colls []*Collection
if svc, err = NewService(); err != nil {
t.Fatalf("could not get new Service via NewService: %v", err.Error())
}
2021-12-10 02:50:30 -05:00
if colls, err = svc.Collections(); err != nil {
2021-12-08 02:34:27 -05:00
t.Errorf("could not get Service.Collections: %v", err.Error())
} else {
t.Logf("found %v collections via Service.Collections", len(colls))
}
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
}
/*
TestService_CreateAliasedCollection tests the following internal functions/methods via nested calls:
(all calls in TestNewService)
Service.CreateAliasedCollection
NewCollection
Collection.Modified
Collection.Delete
Service.SetAlias
(By extension, Service.CreateCollection is also tested as it's a very thin wrapper
around Service.CreateAliasedCollection).
*/
2021-12-10 02:50:30 -05:00
/* DISABLED. Currently, *only* the alias "default" is allowed. TODO: revisit in future?
2021-12-08 02:34:27 -05:00
func TestService_CreateAliasedCollection(t *testing.T) {
var err error
var svc *Service
var collection *Collection
2021-12-08 02:34:27 -05:00
if svc, err = NewService(); err != nil {
t.Fatalf("could not get new Service via NewService: %v", err.Error())
}
2021-12-08 02:34:27 -05:00
if collection, err = svc.CreateAliasedCollection(collectionName.String(), collectionAlias.String()); err != nil {
t.Errorf(
"error when creating aliased collection '%v' with alias '%v': %v",
collectionName.String(), collectionAlias.String(), err.Error(),
)
} else {
2021-12-10 02:50:30 -05:00
if err = svc.SetAlias(testAlias, collection.Dbus.Path()); err != nil {
t.Errorf(
"error when setting an alias '%v' for aliased collection '%v' (original alias '%v')",
testAlias, collectionName.String(), collectionAlias.String(),
)
}
2021-12-08 02:34:27 -05:00
if err = collection.Delete(); err != nil {
t.Errorf(
"error when deleting aliased collection '%v' with alias '%v': %v",
collectionName.String(), collectionAlias.String(), err.Error(),
)
}
t.Logf("created collection '%v' at path '%v' successfully", collectionName.String(), string(collection.Dbus.Path()))
2021-12-08 02:34:27 -05:00
}
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
}
2021-12-10 02:50:30 -05:00
*/
2021-12-08 02:34:27 -05:00
/*
TestService_GetCollection tests the following internal functions/methods via nested calls:
(all calls in TestService_CreateAliasedCollection)
Service.GetCollection
(all calls in TestService_Collections)
Service.ReadAlias
2021-12-10 02:50:30 -05:00
The default collection (login) is fetched instead of creating one as this collection should exist,
and tests fetching existing collections instead of newly-created ones.
2021-12-08 02:34:27 -05:00
*/
func TestService_GetCollection(t *testing.T) {
var err error
var svc *Service
var coll *Collection
if svc, err = NewService(); err != nil {
t.Fatalf("could not get new Service via NewService: %v", err.Error())
}
if coll, err = svc.GetCollection(defaultCollection); err != nil {
t.Errorf("failed to get collection '%v' via Service.GetCollection: %v", defaultCollection, err.Error())
} else {
t.Logf("got collection '%v' via reference '%v'", coll.name, defaultCollection)
}
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
}
/*
TestService_Secrets tests the following internal functions/methods via nested calls:
(all calls in TestNewService)
(all calls in TestService_CreateAliasedCollection)
Service.CreateCollection
Service.SearchItems
2021-12-10 02:50:30 -05:00
NewItem
NewErrors
2021-12-08 02:34:27 -05:00
Service.GetSecrets
2021-12-10 02:50:30 -05:00
NewSecret
Collection.CreateItem
Item.Label
2021-12-08 02:34:27 -05:00
*/
func TestService_Secrets(t *testing.T) {
var err error
var svc *Service
var collection *Collection
2021-12-10 02:50:30 -05:00
var itemResultsUnlocked []*Item
var itemResultsLocked []*Item
var itemName string
var resultItemName string
var testItem *Item
var testSecret *Secret
var secretsResult map[dbus.ObjectPath]*Secret
2021-12-08 02:34:27 -05:00
var itemPaths []dbus.ObjectPath
2021-12-10 02:50:30 -05:00
var itemAttrs map[string]string = map[string]string{
"GOSECRET": "yes",
}
2021-12-08 02:34:27 -05:00
if svc, err = NewService(); err != nil {
t.Fatalf("could not get new Service via NewService: %v", err.Error())
}
if collection, err = svc.CreateCollection(collectionName.String()); err != nil {
2021-12-10 02:50:30 -05:00
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
t.Fatalf("could not create collection '%v': %v", collectionName.String(), err.Error())
} else {
t.Logf("created collection '%v' at path '%v' successfully", collectionName.String(), string(collection.Dbus.Path()))
2021-12-10 02:50:30 -05:00
}
// Create a secret
testSecret = NewSecret(svc.Session, nil, []byte(testSecretContent), "text/plain")
if testItem, err = collection.CreateItem(testItemLabel, itemAttrs, testSecret, true); err != nil {
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
t.Fatalf("could not create Item in collection '%v': %v", collectionName.String(), err.Error())
}
if itemName, err = testItem.Label(); err != nil {
t.Errorf(
"could not get label for newly-created item '%v' in collection '%v': %v",
string(testItem.Dbus.Path()), collectionName.String(), err.Error(),
)
itemName = testItemLabel
}
// Search items
if itemResultsUnlocked, itemResultsLocked, err = svc.SearchItems(itemAttrs); err != nil {
t.Errorf("did not find Item '%v' in collection '%v' SearchItems: %v", itemName, collectionName.String(), err.Error())
} else {
if len(itemResultsLocked) != 0 && itemResultsUnlocked != nil {
t.Errorf("at least one locked item in collection '%v'", collectionName.String())
}
if len(itemResultsUnlocked) != 1 {
t.Errorf("number of unlocked items in collection '%v' is not equal to 1", collectionName.String())
}
if resultItemName, err = itemResultsUnlocked[0].Label(); err != nil {
t.Errorf("cannot fetch test Item name from collection '%v' in SearchItems: %v", collectionName.String(), err.Error())
} else {
if resultItemName != itemName {
t.Errorf("seem to have fetched an improper Item from collection '%v' in SearchItems", collectionName.String())
}
}
}
// Fetch secrets
itemPaths = make([]dbus.ObjectPath, len(itemResultsUnlocked))
if len(itemResultsUnlocked) >= 1 {
itemPaths[0] = itemResultsUnlocked[0].Dbus.Path()
if secretsResult, err = svc.GetSecrets(itemPaths...); err != nil {
t.Errorf("failed to fetch Item path '%v' via Service.GetSecrets: %v", string(itemPaths[0]), err.Error())
} else if len(secretsResult) != 1 {
t.Errorf("received %v secrets from Service.GetSecrets instead of 1", len(secretsResult))
}
}
// Delete the collection to clean up.
if err = collection.Delete(); err != nil {
t.Errorf(
"error when deleting collection '%v' when testing Service: %v",
collectionName.String(), err.Error(),
)
2021-12-08 02:34:27 -05:00
}
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
}
2021-12-08 02:34:27 -05:00
// service.Lock & service.Unlock
2021-12-10 02:50:30 -05:00
/*
TestService_Locking tests the following internal functions/methods via nested calls:
(all calls in TestNewService)
(all calls in TestService_CreateAliasedCollection)
Service.Lock
Service.Unlock
Collection.Locked
*/
func TestService_Locking(t *testing.T) {
var err error
var isLocked bool
var stateChangeLock bool
var svc *Service
var collection *Collection
if svc, err = NewService(); err != nil {
t.Fatalf("could not get new Service via NewService: %v", err.Error())
}
if collection, err = svc.CreateCollection(collectionName.String()); err != nil {
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
t.Errorf("could not create collection '%v': %v", collectionName.String(), err.Error())
} else {
t.Logf("created collection '%v' at path '%v' successfully", collectionName.String(), string(collection.Dbus.Path()))
2021-12-10 02:50:30 -05:00
}
if isLocked, err = collection.Locked(); err != nil {
t.Errorf("received error when checking collection '%v' lock status: %v", collectionName.String(), err.Error())
if err = collection.Delete(); err != nil {
t.Errorf(
"error when deleting collection '%v' when testing Service: %v",
collectionName.String(), err.Error(),
)
}
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
return
} else {
t.Logf("collection '%v' original lock status: %v", collectionName.String(), isLocked)
}
// Change the state.
if isLocked {
if err = svc.Unlock(collection.Dbus.Path()); err != nil {
t.Errorf("could not unlock collection '%v': %v", collectionName.String(), err.Error())
}
if stateChangeLock, err = collection.Locked(); err != nil {
t.Errorf("received error when checking collection '%v' lock status: %v", collectionName.String(), err.Error())
}
if err = svc.Lock(collection.Dbus.Path()); err != nil {
t.Errorf("could not lock collection '%v': %v", collectionName.String(), err.Error())
}
} else {
if err = svc.Lock(collection.Dbus.Path()); err != nil {
t.Errorf("could not lock collection '%v': %v", collectionName.String(), err.Error())
}
if stateChangeLock, err = collection.Locked(); err != nil {
t.Errorf("received error when checking collection '%v' lock status: %v", collectionName.String(), err.Error())
}
if err = svc.Unlock(collection.Dbus.Path()); err != nil {
t.Errorf("could not unlock collection '%v': %v", collectionName.String(), err.Error())
}
}
if stateChangeLock != !isLocked {
t.Errorf(
"flipped lock state for collection '%v' (locked: %v) is not opposite of original lock state (locked: %v)",
collectionName.String(), stateChangeLock, isLocked,
)
}
// Delete the collection to clean up.
if err = collection.Delete(); err != nil {
t.Errorf(
"error when deleting collection '%v' when testing Service: %v",
collectionName.String(), err.Error(),
)
}
if err = svc.Close(); err != nil {
t.Errorf("could not close Service.Session: %v", err.Error())
}
2021-12-10 02:50:30 -05:00
}