From b6ba0f9736de1bdf7837b08cf4d748ebcc2c156f Mon Sep 17 00:00:00 2001 From: brent s Date: Tue, 7 Dec 2021 02:56:15 -0500 Subject: [PATCH] improvements, start integration tests improved various funcs, removed extraneous things (e.g. Item.Attrs). first integration test. more will come. --- .gitignore | 4 ++++ TODO | 1 + collection_funcs.go | 14 ++++++++++++-- consts.go | 7 +++++-- conts_test.go | 6 ++++++ item_funcs.go | 25 ++++++++++++++++--------- service_funcs.go | 19 ++++++++++--------- service_funcs_test.go | 15 +++++++++++++++ session_funcs.go | 2 +- types.go | 8 -------- 10 files changed, 70 insertions(+), 31 deletions(-) create mode 100644 conts_test.go create mode 100644 service_funcs_test.go diff --git a/.gitignore b/.gitignore index 289b5fb..667aec2 100644 --- a/.gitignore +++ b/.gitignore @@ -27,6 +27,10 @@ # Test binary, built with `go test -c` *.test +# But DO include the actual tests. +!_test.go +!*_test.go +!*_test/ # Output of the go coverage tool, specifically when used with LiteIDE *.out diff --git a/TODO b/TODO index 0cfa9c4..4f501eb 100644 --- a/TODO +++ b/TODO @@ -3,6 +3,7 @@ -- https://go.dev/doc/tutorial/add-a-test -- https://gobyexample.com/testing -- https://blog.alexellis.io/golang-writing-unit-tests/ +- Benchmarking? - Example usage - Merge master into V1 -- and tag release (v1.0.0) diff --git a/collection_funcs.go b/collection_funcs.go index b6874a5..1571ac4 100644 --- a/collection_funcs.go +++ b/collection_funcs.go @@ -7,8 +7,6 @@ import ( `github.com/godbus/dbus/v5` ) -// TODO: add method Relabel - /* NewCollection returns a pointer to a Collection based on a Service and a Dbus path. You will almost always want to use Service.GetCollection instead. @@ -188,6 +186,18 @@ func (c *Collection) Label() (label string, err error) { return } +// Relabel modifies the Collection's label in Dbus. +func (c *Collection) Relabel(newLabel string) (err error) { + + var variant dbus.Variant = dbus.MakeVariant(newLabel) + + if err = c.Dbus.SetProperty(DbusItemLabel, variant); err != nil { + return + } + + return +} + // Created returns the time.Time of when a Collection was created. func (c *Collection) Created() (created time.Time, err error) { diff --git a/consts.go b/consts.go index 7248ba5..a01ba95 100644 --- a/consts.go +++ b/consts.go @@ -47,7 +47,7 @@ const ( DbusServiceLock string = DbusInterfaceService + ".Lock" // DbusServiceLockService is [FUNCTION UNKNOWN/UNDOCUMENTED; TODO? NOT IMPLEMENTED.] - DbusServiceLockService string = DbusInterfaceService + ".LockService" + // DbusServiceLockService string = DbusInterfaceService + ".LockService" // DbusServiceOpenSession is used by Service.OpenSession. DbusServiceOpenSession string = DbusInterfaceService + ".OpenSession" @@ -147,7 +147,10 @@ const ( // DbusItemLocked is a Dbus boolean for Item.Locked. DbusItemLocked string = DbusInterfaceItem + ".Locked" - // DbusItemAttributes contains attributes (metadata, schema, etc.) for Item.Attrs. + /* + DbusItemAttributes contains attributes (metadata, schema, etc.) for + Item.Attributes, Item.ReplaceAttributes, and Item.ModifyAttributes. + */ DbusItemAttributes string = DbusInterfaceItem + ".Attributes" // DbusItemLabel is the name (label) for Item.Label. diff --git a/conts_test.go b/conts_test.go new file mode 100644 index 0000000..109b0a3 --- /dev/null +++ b/conts_test.go @@ -0,0 +1,6 @@ +package gosecret + +// Paths. +const ( + DbusDefaultCollectionPath string = DbusPath + "/collections/login" +) diff --git a/item_funcs.go b/item_funcs.go index 1829ac4..2afd645 100644 --- a/item_funcs.go +++ b/item_funcs.go @@ -8,8 +8,6 @@ import ( `github.com/godbus/dbus/v5` ) -// TODO: add method Relabel - // NewItem returns a pointer to an Item based on Collection and a Dbus path. func NewItem(collection *Collection, path dbus.ObjectPath) (item *Item, err error) { @@ -46,7 +44,7 @@ func NewItem(collection *Collection, path dbus.ObjectPath) (item *Item, err erro return } -// Attributes updates the Item.Attrs from Dbus (and returns them). +// Attributes returns the Item's attributes from Dbus. func (i *Item) Attributes() (attrs map[string]string, err error) { var variant dbus.Variant @@ -55,8 +53,7 @@ func (i *Item) Attributes() (attrs map[string]string, err error) { return } - i.Attrs = variant.Value().(map[string]string) - attrs = i.Attrs + attrs = variant.Value().(map[string]string) return } @@ -120,7 +117,7 @@ func (i *Item) Label() (label string, err error) { } /* - ModifyAttributes modifies the Item.Attrs, both in the object and in Dbus. + ModifyAttributes modifies the Item's attributes in Dbus. This is similar to Item.ReplaceAttributes but will only modify the map's given keys so you do not need to provide the entire attribute map. If you wish to remove an attribute, use the value "" (empty string). @@ -161,7 +158,19 @@ func (i *Item) ModifyAttributes(replaceAttrs map[string]string) (err error) { return } -// ReplaceAttributes replaces the Item.Attrs, both in the object and in Dbus. +// Relabel modifies the Item's label in Dbus. +func (i *Item) Relabel(newLabel string) (err error) { + + var variant dbus.Variant = dbus.MakeVariant(newLabel) + + if err = i.Dbus.SetProperty(DbusItemLabel, variant); err != nil { + return + } + + return +} + +// ReplaceAttributes replaces the Item's attributes in Dbus. func (i *Item) ReplaceAttributes(newAttrs map[string]string) (err error) { var label string @@ -178,8 +187,6 @@ func (i *Item) ReplaceAttributes(newAttrs map[string]string) (err error) { return } - i.Attrs = newAttrs - return } diff --git a/service_funcs.go b/service_funcs.go index c11619e..fe009df 100644 --- a/service_funcs.go +++ b/service_funcs.go @@ -9,17 +9,21 @@ import ( "github.com/godbus/dbus/v5" ) -// TODO: Lock method (DbusServiceLockService)? - // NewService returns a pointer to a new Service connection. func NewService() (service *Service, err error) { - var svc Service = Service{} + var svc Service = Service{ + DbusObject: &DbusObject{ + Conn: nil, + Dbus: nil, + }, + Session: nil, + } if svc.Conn, err = dbus.SessionBus(); err != nil { return } - svc.Dbus = service.Conn.Object(DbusService, dbus.ObjectPath(DbusPath)) + svc.Dbus = svc.Conn.Object(DbusService, dbus.ObjectPath(DbusPath)) if svc.Session, err = svc.GetSession(); err != nil { return @@ -237,13 +241,10 @@ func (s *Service) Lock(objectPaths ...dbus.ObjectPath) (err error) { func (s *Service) OpenSession(algo, input string) (session *Session, output dbus.Variant, err error) { var path dbus.ObjectPath - var algoVariant dbus.Variant var inputVariant dbus.Variant if strings.TrimSpace(algo) == "" { - algoVariant = dbus.MakeVariant("plain") - } else { - algoVariant = dbus.MakeVariant(algo) + algo = "plain" } inputVariant = dbus.MakeVariant(input) @@ -253,7 +254,7 @@ func (s *Service) OpenSession(algo, input string) (session *Session, output dbus // Possible flags are dbus.Flags consts: https://pkg.go.dev/github.com/godbus/dbus#Flags // Oddly, there is no "None" flag. So it's explicitly specified as a null byte. if err = s.Dbus.Call( - DbusServiceOpenSession, 0, algoVariant, inputVariant, + DbusServiceOpenSession, 0, algo, inputVariant, ).Store(&output, &path); err != nil { return } diff --git a/service_funcs_test.go b/service_funcs_test.go new file mode 100644 index 0000000..ace5a9c --- /dev/null +++ b/service_funcs_test.go @@ -0,0 +1,15 @@ +package gosecret + +import ( + `testing` +) + +func TestNewService(t *testing.T) { + + var err error + + if _, err = NewService(); err != nil { + t.Fatalf("could not get new Service via NewService: %v", err.Error()) + } + +} diff --git a/session_funcs.go b/session_funcs.go index ed1188f..e3dbd00 100644 --- a/session_funcs.go +++ b/session_funcs.go @@ -18,7 +18,7 @@ func NewSession(service *Service, path dbus.ObjectPath) (session *Session) { }, service: service, } - session.Dbus = session.Conn.Object(DbusInterfaceSession, path) + ssn.Dbus = ssn.Conn.Object(DbusInterfaceSession, path) session = &ssn diff --git a/types.go b/types.go index e1a309b..3c02b9f 100644 --- a/types.go +++ b/types.go @@ -6,8 +6,6 @@ import ( "github.com/godbus/dbus/v5" ) -// TODO: add label fields to Collection and Item, make their respective Label methods update the field. - /* MultiError is a type of error.Error that can contain multiple error.Errors. Confused? Don't worry about it. */ @@ -107,12 +105,6 @@ type Collection struct { */ type Item struct { *DbusObject - /* - Attrs are the attributes to assign to this Item. - They should be considered non-secret; they're primarily used to *look up* an Item. - *Do NOT put secret/sensitive data in an Item's Attrs!* - */ - Attrs map[string]string `json:"attributes"` // Secret is the corresponding Secret object. Secret *Secret `json:"secret"` /*