forked from r00t2/gosecret
1
0
Fork 0

improvements, start integration tests

improved various funcs, removed extraneous things (e.g. Item.Attrs).

first integration test. more will come.
This commit is contained in:
brent s. 2021-12-07 02:56:15 -05:00
parent 0fc0e0c269
commit b6ba0f9736
Signed by untrusted user: bts
GPG Key ID: 8C004C2F93481F6B
10 changed files with 70 additions and 31 deletions

4
.gitignore vendored
View File

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

1
TODO
View File

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

View File

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


View File

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

6
conts_test.go Normal file
View File

@ -0,0 +1,6 @@
package gosecret

// Paths.
const (
DbusDefaultCollectionPath string = DbusPath + "/collections/login"
)

View File

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


View File

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

15
service_funcs_test.go Normal file
View File

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

}

View File

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


View File

@ -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"`
/*