From eda17774311fd8b6126b61af74544f073724492d Mon Sep 17 00:00:00 2001 From: brent s Date: Wed, 8 Dec 2021 02:34:27 -0500 Subject: [PATCH] checking in more tests for Service. --- go.mod | 5 +- go.sum | 2 + service_funcs.go | 12 +-- service_funcs_test.go | 179 +++++++++++++++++++++++++++++++++++++++++- session_funcs.go | 6 +- 5 files changed, 194 insertions(+), 10 deletions(-) diff --git a/go.mod b/go.mod index a3e3af5..0325efa 100644 --- a/go.mod +++ b/go.mod @@ -2,4 +2,7 @@ module r00t2.io/gosecret go 1.17 -require github.com/godbus/dbus/v5 v5.0.6 +require ( + github.com/godbus/dbus/v5 v5.0.6 + github.com/google/uuid v1.3.0 +) diff --git a/go.sum b/go.sum index aeec758..4c667d8 100644 --- a/go.sum +++ b/go.sum @@ -1,2 +1,4 @@ github.com/godbus/dbus/v5 v5.0.6 h1:mkgN1ofwASrYnJ5W6U/BxG15eXXXjirgZc7CLqkcaro= github.com/godbus/dbus/v5 v5.0.6/go.mod h1:xhWf0FNVPg57R7Z0UbKHbJfkEywrmjJnf7w5xrFpKfA= +github.com/google/uuid v1.3.0 h1:t6JiXgmwXMjEs8VusXIJk2BXHsn+wx8BZdTaoZ5fu7I= +github.com/google/uuid v1.3.0/go.mod h1:TIyPZe4MgqvfeYDBFedMoGGpEw/LqOeaOT+nhxU+yHo= diff --git a/service_funcs.go b/service_funcs.go index fe009df..ece7742 100644 --- a/service_funcs.go +++ b/service_funcs.go @@ -1,10 +1,10 @@ package gosecret import ( - `errors` - `fmt` - `path/filepath` - `strings` + "errors" + "fmt" + "path/filepath" + "strings" "github.com/godbus/dbus/v5" ) @@ -259,7 +259,7 @@ func (s *Service) OpenSession(algo, input string) (session *Session, output dbus return } - session = NewSession(s, path) + session, err = NewSession(s, path) return } @@ -395,7 +395,7 @@ func (s *Service) SetAlias(alias string, objectPath dbus.ObjectPath) (err error) DbusServiceSetAlias, 0, alias, objectPath, ) - _ = c + err = c.Err return } diff --git a/service_funcs_test.go b/service_funcs_test.go index ace5a9c..d5e98c0 100644 --- a/service_funcs_test.go +++ b/service_funcs_test.go @@ -1,15 +1,190 @@ package gosecret import ( - `testing` + "testing" + + "github.com/google/uuid" ) +var ( + collectionName uuid.UUID = uuid.New() + collectionAlias uuid.UUID = uuid.New() +) + +const ( + defaultCollection string = "Login" + testAlias string = "GOSECRET_TESTING_ALIAS" +) + +/* + 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 + var svc *Service - if _, err = NewService(); err != nil { + 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()) + } + + if _, err = svc.Collections(); err != nil { + 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). +*/ +func TestService_CreateAliasedCollection(t *testing.T) { + + var err error + 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.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 { + if err = collection.Delete(); err != nil { + t.Errorf( + "error when deleting aliased collection '%v' with alias '%v': %v", + collectionName.String(), collectionAlias.String(), err.Error(), + ) + } + } + + 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(), + ) + } + + if err = svc.Close(); err != nil { + t.Errorf("could not close Service.Session: %v", err.Error()) + } +} + +/* + 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 + +*/ +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 + Service.GetSecrets + +*/ +/* TODO: left off on this. +func TestService_Secrets(t *testing.T) { + + var err error + var svc *Service + var collection *Collection + var itemPaths []dbus.ObjectPath + + 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 { + t.Errorf("could not create collection '%v': %v", collectionName.String(), err.Error()) + } + + if err = svc.Close(); err != nil { + t.Errorf("could not close Service.Session: %v", err.Error()) + } +} +*/ + +// service.Lock & service.Unlock diff --git a/session_funcs.go b/session_funcs.go index e3dbd00..e8210a5 100644 --- a/session_funcs.go +++ b/session_funcs.go @@ -10,7 +10,11 @@ import ( NewSession returns a pointer to a new Session based on a Service and a dbus.ObjectPath. You will almost always want to use Service.GetSession or Service.OpenSession instead. */ -func NewSession(service *Service, path dbus.ObjectPath) (session *Session) { +func NewSession(service *Service, path dbus.ObjectPath) (session *Session, err error) { + + if _, err = validConnPath(service.Conn, path); err != nil { + return + } var ssn Session = Session{ DbusObject: &DbusObject{