checking in more tests for Service.

This commit is contained in:
brent s. 2021-12-08 02:34:27 -05:00
parent b6ba0f9736
commit eda1777431
Signed by: bts
GPG Key ID: 8C004C2F93481F6B
5 changed files with 194 additions and 10 deletions

5
go.mod
View File

@ -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
)

2
go.sum
View File

@ -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=

View File

@ -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
}

View File

@ -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

View File

@ -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{