From 330548689afdd6df3e886035bd80b67b10419c75 Mon Sep 17 00:00:00 2001 From: brent s Date: Tue, 28 Dec 2021 02:50:54 -0500 Subject: [PATCH] tests done --- TODO | 6 -- blob_funcs.go | 26 ++++++++ blob_funcs_test.go | 49 ++++++++++++-- consts_test.go | 31 ++++++++- folder_funcs.go | 52 +++++++++++++++ folder_funcs_test.go | 126 ++++++++++++++++++++++++++++++++++++ funcs_test.go | 1 - map_funcs.go | 26 ++++++++ map_funcs_test.go | 54 ++++++++++++++++ password_funcs.go | 26 ++++++++ password_funcs_test.go | 52 +++++++++++++++ types_test.go | 12 ++++ unknownitem_funcs.go | 26 ++++++++ unknownitem_funcs_test.go | 54 ++++++++++++++++ utils_test.go | 45 +++++++++++-- wallet_funcs.go | 15 ++++- wallet_funcs_test.go | 88 +++++++++++++++++++++++++ walletmanager_funcs.go | 14 ++++ walletmanager_funcs_test.go | 1 - 19 files changed, 677 insertions(+), 27 deletions(-) delete mode 100644 funcs_test.go create mode 100644 types_test.go delete mode 100644 walletmanager_funcs_test.go diff --git a/TODO b/TODO index f5ebb6a..ef0485a 100644 --- a/TODO +++ b/TODO @@ -1,9 +1,3 @@ - XML import/export? -- compat with kwalletmanager --- Will require conversion to different struct model. - -- walletCheck on all items' and folders' operations? - -- close Dbus.Conn - -- TESTS diff --git a/blob_funcs.go b/blob_funcs.go index 8aaabb0..ebb693e 100644 --- a/blob_funcs.go +++ b/blob_funcs.go @@ -51,6 +51,28 @@ func (b *Blob) Delete() (err error) { return } +// Exists returns true if this Blob actually exists. +func (b *Blob) Exists() (exists bool, err error) { + + if exists, err = b.folder.HasEntry(b.Name); err != nil { + return + } + + return +} + +// Rename renames this Blob (changes its key). +func (b *Blob) Rename(newName string) (err error) { + + if err = b.folder.RenameEntry(b.Name, newName); err != nil { + return + } + + b.Name = newName + + return +} + // SetValue will replace this Blob's Blob.Value. func (b *Blob) SetValue(newValue []byte) (err error) { @@ -69,6 +91,10 @@ func (b *Blob) Update() (err error) { var call *dbus.Call var v dbus.Variant + if err = b.folder.wallet.walletCheck(); err != nil { + return + } + if call = b.Dbus.Call( DbusWMReadEntry, 0, b.folder.wallet.handle, b.folder.Name, b.Name, b.folder.wallet.wm.AppID, ); call.Err != nil { diff --git a/blob_funcs_test.go b/blob_funcs_test.go index eb3d671..6ef3630 100644 --- a/blob_funcs_test.go +++ b/blob_funcs_test.go @@ -1,20 +1,55 @@ package gokwallet import ( + "bytes" "testing" ) -func TestNewBlob(t *testing.T) { +// TestBlob tests all functions of a Blob. +func TestBlob(t *testing.T) { - var err error - var f *Folder var b *Blob + var e *testEnv + var err error + var i int - if _, _, f, err = getTestEnv(t); err != nil { + if e, err = getTestEnv(t); err != nil { t.Fatalf("failure getting test env: %v", err) } + defer e.cleanup(t) + + if b, err = NewBlob(e.f, blobTest.String(), e.r); err != nil { + t.Fatalf("failure getting Blob: %v", err) + } + + t.Logf("created Blob '%v:%v:%v'; initialized: %v", e.w.Name, e.f.Name, b.Name, b.isInit) + + if err = b.SetValue(testBytes); err != nil { + t.Errorf("failed to set value for Blob '%v:%v:%v': %v", e.w.Name, e.f.Name, b.Name, err) + } + + if i = bytes.Compare(b.Value, testBytes); i != 0 { + t.Errorf("value '%#v' does not match expected value '%#v'", b.Value, testBytes) + } else { + t.Logf("value for Blob '%v:%v:%v': %#v", e.w.Name, e.f.Name, b.Name, b.Value) + } + + if err = b.SetValue(testBytesReplace); err != nil { + t.Errorf("failed to set replacement value for Blob '%v:%v:%v': %v", e.w.Name, e.f.Name, b.Name, err) + } + + if err = b.Update(); err != nil { + t.Errorf("failed to update Blob '%v:%v:%v': %v", e.w.Name, e.f.Name, b.Name, err) + } + + t.Logf("replacement value for Blob '%v:%v:%v': %v", e.w.Name, e.f.Name, b.Name, b.Value) + + if i = bytes.Compare(b.Value, testBytesReplace); i != 0 { + t.Errorf("value '%#v' does not match expected value '%#v'", b.Value, testBytesReplace) + } + + if err = b.Delete(); err != nil { + t.Errorf("failed to delete Blob '%v:%v:%v': %v", e.w.Name, e.f.Name, b.Name, err) + } - // if b, err = NewBlob(f) - _ = f - _ = b } diff --git a/consts_test.go b/consts_test.go index d4c8340..68e1305 100644 --- a/consts_test.go +++ b/consts_test.go @@ -6,10 +6,35 @@ import ( // Strings. const ( - appIdTest string = "GoKwallet_Test" + appIdTest string = "GoKwallet_Test" + appIdTestAlt string = "GoKwallet_Test_Alternate" ) +// Identifiers/names/keys. var ( - walletTest uuid.UUID = uuid.New() - folderTest uuid.UUID = uuid.New() + walletTest uuid.UUID = uuid.New() + folderTest uuid.UUID = uuid.New() + blobTest uuid.UUID = uuid.New() + mapTest uuid.UUID = uuid.New() + passwordTest uuid.UUID = uuid.New() + passwordTestRename uuid.UUID = uuid.New() + unknownItemTest uuid.UUID = uuid.New() +) + +// Values. +var ( + testBytes []byte = []byte(uuid.New().String()) + testBytesReplace []byte = []byte(uuid.New().String()) + testPassword string = uuid.New().String() + testPasswordReplace string = uuid.New().String() + testMap map[string]string = map[string]string{ + uuid.New().String(): uuid.New().String(), + uuid.New().String(): uuid.New().String(), + uuid.New().String(): uuid.New().String(), + } + testMapReplace map[string]string = map[string]string{ + uuid.New().String(): uuid.New().String(), + uuid.New().String(): uuid.New().String(), + uuid.New().String(): uuid.New().String(), + } ) diff --git a/folder_funcs.go b/folder_funcs.go index ff549d8..6538c23 100644 --- a/folder_funcs.go +++ b/folder_funcs.go @@ -66,6 +66,10 @@ func (f *Folder) HasEntry(entryName string) (hasEntry bool, err error) { var call *dbus.Call + if err = f.wallet.walletCheck(); err != nil { + return + } + if call = f.Dbus.Call( DbusWMHasEntry, 0, f.wallet.handle, f.Name, entryName, f.wallet.wm.AppID, ); call.Err != nil { @@ -88,6 +92,10 @@ func (f *Folder) KeyNotExist(entryName string) (doesNotExist bool, err error) { var call *dbus.Call + if err = f.wallet.walletCheck(); err != nil { + return + } + if call = f.Dbus.Call( DbusWMKeyNotExist, 0, f.wallet.Name, f.Name, entryName, ); call.Err != nil { @@ -106,6 +114,10 @@ func (f *Folder) ListEntries() (entryNames []string, err error) { var call *dbus.Call + if err = f.wallet.walletCheck(); err != nil { + return + } + if call = f.Dbus.Call( DbusWMEntryList, 0, f.wallet.handle, f.Name, f.wallet.wm.AppID, ); call.Err != nil { @@ -125,6 +137,10 @@ func (f *Folder) RemoveEntry(entryName string) (err error) { var call *dbus.Call var rslt int32 + if err = f.wallet.walletCheck(); err != nil { + return + } + if call = f.Dbus.Call( DbusWMRemoveEntry, 0, f.wallet.handle, f.Name, entryName, f.wallet.wm.AppID, ); call.Err != nil { @@ -146,6 +162,10 @@ func (f *Folder) RenameEntry(entryName, newEntryName string) (err error) { var call *dbus.Call var rslt int32 + if err = f.wallet.walletCheck(); err != nil { + return + } + if call = f.Dbus.Call( DbusWMRenameEntry, 0, f.wallet.handle, f.Name, entryName, newEntryName, f.wallet.wm.AppID, ); call.Err != nil { @@ -207,6 +227,10 @@ func (f *Folder) UpdateBlobs() (err error) { var variant dbus.Variant var errs []error = make([]error, 0) + if err = f.wallet.walletCheck(); err != nil { + return + } + if !f.isInit { err = ErrInitFolder return @@ -258,6 +282,10 @@ func (f *Folder) UpdateMaps() (err error) { var variant dbus.Variant var errs []error = make([]error, 0) + if err = f.wallet.walletCheck(); err != nil { + return + } + if call = f.Dbus.Call( DbusWMMapList, 0, f.wallet.handle, f.Name, f.wallet.wm.AppID, ); call.Err != nil { @@ -295,6 +323,10 @@ func (f *Folder) UpdatePasswords() (err error) { var variant dbus.Variant var errs []error = make([]error, 0) + if err = f.wallet.walletCheck(); err != nil { + return + } + if !f.isInit { err = ErrInitFolder return @@ -338,6 +370,10 @@ func (f *Folder) UpdateUnknowns() (err error) { var variant dbus.Variant var errs []error = make([]error, 0) + if err = f.wallet.walletCheck(); err != nil { + return + } + if !f.isInit { err = ErrInitFolder return @@ -405,6 +441,10 @@ func (f *Folder) WriteEntry(entryName string, entryType kwalletdEnumType, entryV var call *dbus.Call var rslt int32 + if err = f.wallet.walletCheck(); err != nil { + return + } + if entryType == KwalletdEnumTypeUnused { err = ErrNoCreate return @@ -432,6 +472,10 @@ func (f *Folder) WriteMap(entryName string, entryValue map[string]string) (m *Ma var rslt int32 var b []byte + if err = f.wallet.walletCheck(); err != nil { + return + } + if b, err = mapToBytes(entryValue); err != nil { return } @@ -461,6 +505,10 @@ func (f *Folder) WritePassword(entryName, entryValue string) (p *Password, err e var call *dbus.Call var rslt int32 + if err = f.wallet.walletCheck(); err != nil { + return + } + if call = f.Dbus.Call( DbusWMWritePassword, 0, f.wallet.handle, f.Name, entryName, entryValue, f.wallet.wm.AppID, ); call.Err != nil { @@ -483,6 +531,10 @@ func (f *Folder) WritePassword(entryName, entryValue string) (p *Password, err e // WriteUnknown adds or replaces an UnknownItem to/in a Folder. func (f *Folder) WriteUnknown(entryName string, entryValue []byte) (u *UnknownItem, err error) { + if err = f.wallet.walletCheck(); err != nil { + return + } + if err = f.WriteEntry(entryName, KwalletdEnumTypeUnknown, entryValue); err != nil { return } diff --git a/folder_funcs_test.go b/folder_funcs_test.go index 240e6ee..c756e45 100644 --- a/folder_funcs_test.go +++ b/folder_funcs_test.go @@ -1 +1,127 @@ package gokwallet + +import ( + "testing" +) + +func TestFolder(t *testing.T) { + + var r *RecurseOpts = DefaultRecurseOpts + var wm *WalletManager + var w *Wallet + var f *Folder + var p *Password + var b bool + var entries []string + var err error + + r.AllWalletItems = true + + if wm, err = NewWalletManager(r, appIdTest); err != nil { + t.Errorf("failure when getting WalletManager '%v': %v", appIdTest, err) + return + } + defer wm.Close() + + if w, err = NewWallet(wm, walletTest.String(), wm.Recurse); err != nil { + t.Errorf("failure when getting Wallet '%v:%v': %v", appIdTest, walletTest.String(), err) + return + } + + if f, err = NewFolder(w, folderTest.String(), w.Recurse); err != nil { + t.Errorf("failure when getting Folder '%v:%v:%v': %v", appIdTest, walletTest.String(), folderTest.String(), err) + return + } + + t.Logf("created Folder '%v:%v'; initialized: %v", w.Name, f.Name, f.isInit) + + if err = f.Update(); err != nil { + t.Errorf("failed to update Folder '%v:%v': %v", w.Name, f.Name, err) + } + + if _, err = f.WriteBlob(blobTest.String(), testBytes); err != nil { + t.Errorf("failed to WriteBlob '%#v' in '%v:%v:%v': %v", testBytes, w.Name, f.Name, blobTest.String(), err) + } + if _, err = f.WriteMap(mapTest.String(), testMap); err != nil { + t.Errorf("failed to WriteMap '%#v' in '%v:%v:%v': %v", testMap, w.Name, f.Name, mapTest.String(), err) + } + if p, err = f.WritePassword(passwordTest.String(), testPassword); err != nil { + t.Errorf("failed to WritePassword '%#v' in '%v:%v:%v': %v", testPassword, w.Name, f.Name, passwordTest.String(), err) + } + if _, err = f.WriteUnknown(unknownItemTest.String(), testBytes); err != nil { + t.Errorf("failed to WriteUnknown '%#v' in '%v:%v:%v': %v", testBytes, w.Name, f.Name, unknownItemTest.String(), err) + } + + if err = f.UpdateBlobs(); err != nil { + t.Errorf("failed to update Blobs in Folder '%v:%v': %v", w.Name, f.Name, err) + } + if err = f.UpdateMaps(); err != nil { + t.Errorf("failed to update Map in Folder '%v:%v': %v", w.Name, f.Name, err) + } + if err = f.UpdatePasswords(); err != nil { + t.Errorf("failed to update Passwords in Folder '%v:%v': %v", w.Name, f.Name, err) + } + if err = f.UpdateUnknowns(); err != nil { + t.Errorf("failed to update UnknownItems in Folder '%v:%v': %v", w.Name, f.Name, err) + } + + if b, err = f.HasEntry(p.Name); err != nil { + t.Errorf("failed to run HasEntry in Folder '%v:%v' for key '%v': %v", w.Name, f.Name, p.Name, err) + } else if !b { + t.Errorf("failed to find entry '%v' via HasEntry in Folder '%v:%v': %v", p.Name, w.Name, f.Name, err) + } + + if b, err = f.KeyNotExist(p.Name); err != nil { + t.Errorf("failed to run KeyNotExist in Folder '%v:%v' for key '%v': %v", w.Name, f.Name, p.Name, err) + } else if b { + t.Errorf("failed to get false for '%v' via KeyNotExist in Folder '%v:%v': %v", p.Name, w.Name, f.Name, err) + } + + if entries, err = f.ListEntries(); err != nil { + t.Errorf("failed to run ListEntries in Folder '%v:%v': %v", w.Name, f.Name, err) + } else if len(entries) != 1 { + t.Errorf("ListEntries for Folder '%v:%v' contains %v entries; should be 1", w.Name, f.Name, len(entries)) + } + + if p.Value != entries[0] { + t.Errorf( + "received incorrect value for test password in '%v:%v'; should be '%#v' but received '%#v'", + w.Name, f.Name, p.Value, entries[0], + ) + } + + // This tests the parent folder's Rename method. + if err = p.Rename(passwordTestRename.String()); err != nil { + t.Errorf( + "failed to RenameEntry '%v' to '%v' in Folder '%v:%v': %v", + p.Name, passwordTestRename.String(), w.Name, f.Name, err, + ) + } else { + t.Logf("checking existence for key '%v'", p.Name) + // This runs HasEntry via the parent folder. + if b, err = p.Exists(); err != nil { + t.Errorf("failed to run HasEntry in Folder '%v:%v' for key '%v': %v", w.Name, f.Name, passwordTestRename.String(), err) + } else if !b { + t.Errorf("failed to find entry '%v' via HasEntry in Folder '%v:%v': %v", passwordTestRename.String(), w.Name, f.Name, err) + } else { + p.Name = passwordTestRename.String() + } + } + + if err = f.RemoveEntry(p.Name); err != nil { + t.Errorf("failed to RemoveEntry entry '%v' in Folder '%v:%v': %v", p.Name, w.Name, f.Name, err) + } else { + if b, err = f.HasEntry(p.Name); err != nil { + t.Errorf("failed to run HasEntry in Folder '%v:%v' for key '%v': %v", w.Name, f.Name, p.Name, err) + } else if b { + t.Errorf("failed to successfully remove entry '%v' via RemoveEntry in Folder '%v:%v': %v", p.Name, w.Name, f.Name, err) + } + } + + if err = f.Delete(); err != nil { + t.Errorf("failed to delete Folder '%v:%v': %v", w.Name, f.Name, err) + } + if err = w.Delete(); err != nil { + t.Errorf("failed to delete Wallet '%v': %v", w.Name, err) + } +} diff --git a/funcs_test.go b/funcs_test.go deleted file mode 100644 index 240e6ee..0000000 --- a/funcs_test.go +++ /dev/null @@ -1 +0,0 @@ -package gokwallet diff --git a/map_funcs.go b/map_funcs.go index 55773ff..2a1d2e5 100644 --- a/map_funcs.go +++ b/map_funcs.go @@ -52,6 +52,28 @@ func (m *Map) Delete() (err error) { return } +// Exists returns true if this Map actually exists. +func (m *Map) Exists() (exists bool, err error) { + + if exists, err = m.folder.HasEntry(m.Name); err != nil { + return + } + + return +} + +// Rename renames this Map (changes its key). +func (m *Map) Rename(newName string) (err error) { + + if err = m.folder.RenameEntry(m.Name, newName); err != nil { + return + } + + m.Name = newName + + return +} + // SetValue will replace this Map's Map.Value. func (m *Map) SetValue(newValue map[string]string) (err error) { @@ -70,6 +92,10 @@ func (m *Map) Update() (err error) { var call *dbus.Call var b []byte + if err = m.folder.wallet.walletCheck(); err != nil { + return + } + if call = m.Dbus.Call( DbusWMReadMap, 0, m.folder.wallet.handle, m.folder.Name, m.Name, m.folder.wallet.wm.AppID, ); call.Err != nil { diff --git a/map_funcs_test.go b/map_funcs_test.go index 240e6ee..690221a 100644 --- a/map_funcs_test.go +++ b/map_funcs_test.go @@ -1 +1,55 @@ package gokwallet + +import ( + "reflect" + "testing" +) + +// TestMap tests all functions of Map. +func TestMap(t *testing.T) { + + var m *Map + var e *testEnv + var b bool + var err error + + if e, err = getTestEnv(t); err != nil { + t.Fatalf("failure getting test env: %v", err) + } + defer e.cleanup(t) + + if m, err = NewMap(e.f, mapTest.String(), e.r); err != nil { + t.Fatalf("failure getting Map: %v", err) + } + + t.Logf("created Map '%v:%v:%v'; initialized: %v", e.w.Name, e.f.Name, m.Name, m.isInit) + + if err = m.SetValue(testMap); err != nil { + t.Errorf("failed to set value for Map '%v:%v:%v': %v", e.w.Name, e.f.Name, m.Name, err) + } + + if b = reflect.DeepEqual(m.Value, testMap); !b { + t.Errorf("value '%#v' does not match expected value '%#v'", m.Value, testMap) + } else { + t.Logf("value for Map '%v:%v:%v': %#v", e.w.Name, e.f.Name, m.Name, m.Value) + } + + if err = m.SetValue(testMapReplace); err != nil { + t.Errorf("failed to set replacement value for Map '%v:%v:%v': %v", e.w.Name, e.f.Name, m.Name, err) + } + + if err = m.Update(); err != nil { + t.Errorf("failed to update Map '%v:%v:%v': %v", e.w.Name, e.f.Name, m.Name, err) + } + + t.Logf("replacement value for Map '%v:%v:%v': %v", e.w.Name, e.f.Name, m.Name, m.Value) + + if b = reflect.DeepEqual(m.Value, testMapReplace); !b { + t.Errorf("value '%#v' does not match expected value '%#v'", m.Value, testMapReplace) + } + + if err = m.Delete(); err != nil { + t.Errorf("failed to delete Map '%v:%v:%v': %v", e.w.Name, e.f.Name, m.Name, err) + } + +} diff --git a/password_funcs.go b/password_funcs.go index 8fe45f1..1782cd6 100644 --- a/password_funcs.go +++ b/password_funcs.go @@ -52,6 +52,28 @@ func (p *Password) Delete() (err error) { return } +// Exists returns true if this Password actually exists. +func (p *Password) Exists() (exists bool, err error) { + + if exists, err = p.folder.HasEntry(p.Name); err != nil { + return + } + + return +} + +// Rename renames this Password (changes its key). +func (p *Password) Rename(newName string) (err error) { + + if err = p.folder.RenameEntry(p.Name, newName); err != nil { + return + } + + p.Name = newName + + return +} + // SetValue will replace this Password's Password.Value. func (p *Password) SetValue(newValue string) (err error) { @@ -70,6 +92,10 @@ func (p *Password) Update() (err error) { var call *dbus.Call var b []byte + if err = p.folder.wallet.walletCheck(); err != nil { + return + } + if call = p.Dbus.Call( DbusWMReadPassword, 0, p.folder.wallet.handle, p.folder.Name, p.Name, p.folder.wallet.wm.AppID, ); call.Err != nil { diff --git a/password_funcs_test.go b/password_funcs_test.go index 240e6ee..8922dca 100644 --- a/password_funcs_test.go +++ b/password_funcs_test.go @@ -1 +1,53 @@ package gokwallet + +import ( + "testing" +) + +// TestPassword tests all functions of a Password. +func TestPassword(t *testing.T) { + + var p *Password + var e *testEnv + var err error + + if e, err = getTestEnv(t); err != nil { + t.Fatalf("failure getting test env: %v", err) + } + defer e.cleanup(t) + + if p, err = NewPassword(e.f, passwordTest.String(), e.r); err != nil { + t.Fatalf("failure getting Password: %v", err) + } + + t.Logf("created Password '%v:%v:%v'; initialized: %v", e.w.Name, e.f.Name, p.Name, p.isInit) + + if err = p.SetValue(testPassword); err != nil { + t.Errorf("failed to set value for Password '%v:%v:%v': %v", e.w.Name, e.f.Name, p.Name, err) + } + + if p.Value != testPassword { + t.Errorf("value '%#v' does not match expected value '%#v'", p.Value, testPassword) + } else { + t.Logf("value for Password '%v:%v:%v': %#v", e.w.Name, e.f.Name, p.Name, p.Value) + } + + if err = p.SetValue(testPasswordReplace); err != nil { + t.Errorf("failed to set replacement value for Password '%v:%v:%v': %v", e.w.Name, e.f.Name, p.Name, err) + } + + if err = p.Update(); err != nil { + t.Errorf("failed to update Password '%v:%v:%v': %v", e.w.Name, e.f.Name, p.Name, err) + } + + t.Logf("replacement value for Password '%v:%v:%v': %v", e.w.Name, e.f.Name, p.Name, p.Value) + + if p.Value != testPasswordReplace { + t.Errorf("value '%#v' does not match expected value '%#v'", p.Value, testPasswordReplace) + } + + if err = p.Delete(); err != nil { + t.Errorf("failed to delete Password '%v:%v:%v': %v", e.w.Name, e.f.Name, p.Name, err) + } + +} diff --git a/types_test.go b/types_test.go new file mode 100644 index 0000000..c5e06fb --- /dev/null +++ b/types_test.go @@ -0,0 +1,12 @@ +package gokwallet + +/* + testEnv is an environment to use for tests. + It's returned by getTestEnv. +*/ +type testEnv struct { + wm *WalletManager + w *Wallet + f *Folder + r *RecurseOpts +} diff --git a/unknownitem_funcs.go b/unknownitem_funcs.go index d21617f..ccd2715 100644 --- a/unknownitem_funcs.go +++ b/unknownitem_funcs.go @@ -52,6 +52,28 @@ func (u *UnknownItem) Delete() (err error) { return } +// Exists returns true if this UnknownItem actually exists. +func (u *UnknownItem) Exists() (exists bool, err error) { + + if exists, err = u.folder.HasEntry(u.Name); err != nil { + return + } + + return +} + +// Rename renames this UnknownItem (changes its key). +func (u *UnknownItem) Rename(newName string) (err error) { + + if err = u.folder.RenameEntry(u.Name, newName); err != nil { + return + } + + u.Name = newName + + return +} + // SetValue will replace this UnknownItem's UnknownItem.Value. func (u *UnknownItem) SetValue(newValue []byte) (err error) { @@ -70,6 +92,10 @@ func (u *UnknownItem) Update() (err error) { var call *dbus.Call var v dbus.Variant + if err = u.folder.wallet.walletCheck(); err != nil { + return + } + if call = u.Dbus.Call( DbusWMReadEntry, 0, u.folder.wallet.handle, u.folder.Name, u.Name, u.folder.wallet.wm.AppID, ); call.Err != nil { diff --git a/unknownitem_funcs_test.go b/unknownitem_funcs_test.go index 240e6ee..8d8f8d9 100644 --- a/unknownitem_funcs_test.go +++ b/unknownitem_funcs_test.go @@ -1 +1,55 @@ package gokwallet + +import ( + "bytes" + "testing" +) + +// TestUnknownItem tests all functions of an UnknownItem. +func TestUnknownItem(t *testing.T) { + + var u *UnknownItem + var e *testEnv + var err error + var i int + + if e, err = getTestEnv(t); err != nil { + t.Fatalf("failure getting test env: %v", err) + } + defer e.cleanup(t) + + if u, err = NewUnknownItem(e.f, unknownItemTest.String(), e.r); err != nil { + t.Fatalf("failure getting UnknownItem: %v", err) + } + + t.Logf("created UnknownItem '%v:%v:%v'; initialized: %v", e.w.Name, e.f.Name, u.Name, u.isInit) + + if err = u.SetValue(testBytes); err != nil { + t.Errorf("failed to set value for UnknownItem '%v:%v:%v': %v", e.w.Name, e.f.Name, u.Name, err) + } + + if i = bytes.Compare(u.Value, testBytes); i != 0 { + t.Errorf("value '%#v' does not match expected value '%#v'", u.Value, testBytes) + } else { + t.Logf("value for UnknownItem '%v:%v:%v': %#v", e.w.Name, e.f.Name, u.Name, u.Value) + } + + if err = u.SetValue(testBytesReplace); err != nil { + t.Errorf("failed to set replacement value for UnknownItem '%v:%v:%v': %v", e.w.Name, e.f.Name, u.Name, err) + } + + if err = u.Update(); err != nil { + t.Errorf("failed to update UnknownItem '%v:%v:%v': %v", e.w.Name, e.f.Name, u.Name, err) + } + + t.Logf("replacement value for UnknownItem '%v:%v:%v': %v", e.w.Name, e.f.Name, u.Name, u.Value) + + if i = bytes.Compare(u.Value, testBytesReplace); i != 0 { + t.Errorf("value '%#v' does not match expected value '%#v'", u.Value, testBytesReplace) + } + + if err = u.Delete(); err != nil { + t.Errorf("failed to delete UnknownItem '%v:%v:%v': %v", e.w.Name, e.f.Name, u.Name, err) + } + +} diff --git a/utils_test.go b/utils_test.go index 977fd53..398059a 100644 --- a/utils_test.go +++ b/utils_test.go @@ -4,26 +4,59 @@ import ( "testing" ) -func getTestEnv(t *testing.T) (wm *WalletManager, w *Wallet, f *Folder, err error) { +func getTestEnv(t *testing.T) (e *testEnv, err error) { - var r *RecurseOpts = DefaultRecurseOpts + e = &testEnv{ + wm: nil, + w: nil, + f: nil, + r: DefaultRecurseOpts, + } - r.AllWalletItems = true + e.r.AllWalletItems = true - if wm, err = NewWalletManager(r, appIdTest); err != nil { + if e.wm, err = NewWalletManager(e.r, appIdTest); err != nil { t.Errorf("failure when getting WalletManager '%v': %v", appIdTest, err) return } - if w, err = NewWallet(wm, walletTest.String(), wm.Recurse); err != nil { + if e.w, err = NewWallet(e.wm, walletTest.String(), e.r); err != nil { t.Errorf("failure when getting Wallet '%v:%v': %v", appIdTest, walletTest.String(), err) return } - if f, err = NewFolder(w, folderTest.String(), w.Recurse); err != nil { + if e.f, err = NewFolder(e.w, folderTest.String(), e.r); err != nil { t.Errorf("failure when getting Folder '%v:%v:%v': %v", appIdTest, walletTest.String(), folderTest.String(), err) return } return } + +// cleanup closes connections and deletes created folders/wallets in a testEnv. +func (e *testEnv) cleanup(t *testing.T) (err error) { + + var errs []error = make([]error, 0) + + if err = e.f.Delete(); err != nil { + errs = append(errs, err) + err = nil + } + + if err = e.w.Delete(); err != nil { + errs = append(errs, err) + err = nil + } + + if err = e.wm.Close(); err != nil { + errs = append(errs, err) + err = nil + } + + if errs != nil && len(errs) > 0 { + err = NewErrors(errs...) + return + } + + return +} diff --git a/wallet_funcs.go b/wallet_funcs.go index 60ea213..da41b3b 100644 --- a/wallet_funcs.go +++ b/wallet_funcs.go @@ -44,7 +44,10 @@ func NewWallet(wm *WalletManager, name string, recursion *RecurseOpts) (wallet * return } -// Disconnect disconnects this Wallet from its parent WalletManager. +/* + Disconnect disconnects this Wallet from its parent WalletManager. + You may need to run WalletManager.Update after this to clear the WalletManager cache. +*/ func (w *Wallet) Disconnect() (err error) { var call *dbus.Call @@ -169,7 +172,10 @@ func (w *Wallet) Connections() (connList []string, err error) { return } -// CreateFolder creates a new Folder in a Wallet. +/* + CreateFolder creates a new Folder in a Wallet. + If you need a Folder object, use NewFolder instead. +*/ func (w *Wallet) CreateFolder(name string) (err error) { var call *dbus.Call @@ -223,7 +229,10 @@ func (w *Wallet) Delete() (err error) { return } -// FolderExists indicates if a Folder exists in a Wallet or not. +/* + FolderExists indicates if a Folder exists in a Wallet or not. + Similar to Wallet.HasFolder but does not need the Wallet to be opened/unlocked. +*/ func (w *Wallet) FolderExists(folderName string) (exists bool, err error) { var call *dbus.Call diff --git a/wallet_funcs_test.go b/wallet_funcs_test.go index 240e6ee..fb45263 100644 --- a/wallet_funcs_test.go +++ b/wallet_funcs_test.go @@ -1 +1,89 @@ package gokwallet + +import ( + "testing" +) + +// TestWallet tests all functions of a Wallet. +func TestWallet(t *testing.T) { + + var err error + var b bool + var conns []string + var folders []string + var r *RecurseOpts = DefaultRecurseOpts + var wm *WalletManager + var w *Wallet + var w2 *Wallet + + r.AllWalletItems = true + + if wm, err = NewWalletManager(r, appIdTest); err != nil { + t.Fatalf("failed to get WalletManager '%v' for TestWallet: %v", appIdTest, err) + } + defer wm.Close() + + if w, err = NewWallet(wm, walletTest.String(), r); err != nil { + t.Fatalf("failed to get Wallet '%v:%v' for TestWallet: %v", appIdTest, walletTest.String(), err) + } + defer w.Disconnect() + + // We test Disconnect above but we also need to test explicit disconnect by application name. + if w2, err = NewWallet(wm, walletTest.String(), r); err != nil { + t.Fatalf("failed to get Wallet '%v:%v' for TestWallet: %v", appIdTestAlt, walletTest.String(), err) + } + if err = w2.DisconnectApplication(appIdTest); err != nil { + t.Errorf( + "failed to execute DisconnectApplication for '%v:%v' successfully: %v", appIdTestAlt, walletTest.String(), err, + ) + } + + if err = w.ChangePassword(); err != nil { + t.Errorf("failed to change password for wallet '%v:%v': %v", appIdTest, walletTest.String(), err) + } + + if conns, err = w.Connections(); err != nil { + t.Errorf("failed to get Connections for '%v:%v': %v", appIdTest, walletTest.String(), err) + } else { + if conns == nil || len(conns) == 0 { + t.Errorf("failed to get at least one connection for '%v:%v'. Connections: %#v", appIdTest, walletTest.String(), conns) + } else { + t.Logf("Connections for '%v:%v': %#v", appIdTest, walletTest.String(), conns) + } + } + + if err = w.CreateFolder(folderTest.String()); err != nil { + t.Errorf("error when creating folder '%v:%v:%v': %v", appIdTest, walletTest.String(), folderTest.String(), err) + } else { + t.Logf("created folder '%v:%v:%v'", appIdTest, walletTest.String(), folderTest.String()) + } + + if b, err = w.FolderExists(folderTest.String()); err != nil { + t.Errorf( + "error when running FolderExists for '%v:%v:%v': %v", appIdTest, walletTest.String(), folderTest.String(), err, + ) + } else if !b { + t.Errorf( + "did not detecting existing folder '%v:%v:%v' in FolderExists", appIdTest, walletTest.String(), folderTest.String(), + ) + } + if b, err = w.HasFolder(folderTest.String()); err != nil { + t.Errorf( + "error when running HasFolder for '%v:%v:%v': %v", appIdTest, walletTest.String(), folderTest.String(), err, + ) + } else if !b { + t.Errorf( + "did not detecting existing folder '%v:%v:%v' in HasFolder", appIdTest, walletTest.String(), folderTest.String(), + ) + } + + if folders, err = w.ListFolders(); err != nil { + t.Errorf("error when running ListFolders for wallet '%v:%v': %v", appIdTest, walletTest.String(), err) + } else { + t.Logf("ListFolders returned for wallet '%v:%v': %v", appIdTest, walletTest.String(), folders) + } + + if err = w.RemoveFolder(folderTest.String()); err != nil { + t.Errorf("failed running RemoveFolder in Wallet for '%v:%v:%v': %v", appIdTest, walletTest.String(), folderTest.String(), err) + } +} diff --git a/walletmanager_funcs.go b/walletmanager_funcs.go index 22c36b0..5b0be2c 100644 --- a/walletmanager_funcs.go +++ b/walletmanager_funcs.go @@ -74,6 +74,20 @@ func NewWalletManagerFiles(recursion *RecurseOpts, appId string, filePaths ...st } */ +/* + Close closes the Dbus connection. + This does NOT close wallets; use WalletManager.CloseWallet, WalletManager.ForceCloseWallet, or + WalletManager.CloseAllWallets instead for that. +*/ +func (wm *WalletManager) Close() (err error) { + + if err = wm.Conn.Close(); err != nil { + return + } + + return +} + /* CloseWallet closes a Wallet. Unlike Wallet.Close, this closes access for ALL applications/WalletManagers diff --git a/walletmanager_funcs_test.go b/walletmanager_funcs_test.go deleted file mode 100644 index 240e6ee..0000000 --- a/walletmanager_funcs_test.go +++ /dev/null @@ -1 +0,0 @@ -package gokwallet