func TestUpdateEntryWithEtag(t *testing.T) { origEntry := *kOrigEntry var store FakeStore newId, err := vsafedb.AddEntry(&store, nil, kKey, &origEntry) if err != nil { t.Fatalf("Error saving original entry %v", err) } var origEntryWithEtag vsafe.EntryWithEtag if err := vsafedb.EntryByIdWithEtag( store, nil, newId, kKey, &origEntryWithEtag); err != nil { t.Fatalf("Error reading original entry %v", err) } update := functional.NewFilterer(changeToAnEntry) if err := vsafedb.UpdateEntryWithEtag( store, kTransaction, newId, origEntryWithEtag.Etag, kKey, update); err != nil { t.Fatalf("Error updating store: %v", err) } var readEntry vsafe.Entry if err := vsafedb.EntryById(store, nil, newId, kKey, &readEntry); err != nil { t.Fatalf("Error reading store: %v", err) } entry := *kAnEntry entry.Owner = readEntry.Owner entry.Id = readEntry.Id if readEntry != entry { t.Errorf("Expected %v, got %v", entry, readEntry) } }
func TestUpdateEntry(t *testing.T) { store := make(FakeStore, 1) origEntry := *kAnEntry origEntry.Id = 1 entry := origEntry if err := vsafedb.UpdateEntry(store, nil, kKey, &entry); err != nil { t.Fatalf("Error updating store: %v", err) } // entry should not change as side effect if entry != origEntry { t.Errorf("Expected %v, got %v", *kAnEntry, entry) } // Key Id stored with entry if out := store[0].Owner; out != kKey.Id { t.Errorf("Expected %d, got %d", kKey.Id, out) } if store[0].UName == entry.UName || store[0].Password == entry.Password || store[0].Special == entry.Special { t.Error("Expected database to be encrypted.") } var readEntry vsafe.Entry if err := vsafedb.EntryById(store, nil, 1, kKey, &readEntry); err != nil { t.Fatalf("Error reading store: %v", err) } origEntry.Owner = kKey.Id if readEntry != origEntry { t.Errorf("Expected %v, got %v", origEntry, readEntry) } }
func TestUpdateEntryWithEtagBadKey(t *testing.T) { origEntry := *kOrigEntry var store FakeStore newId, err := vsafedb.AddEntry(&store, nil, kKey, &origEntry) if err != nil { t.Fatalf("Error saving original entry %v", err) } var origEntryWithEtag vsafe.EntryWithEtag if err := vsafedb.EntryByIdWithEtag( store, nil, newId, kKey, &origEntryWithEtag); err != nil { t.Fatalf("Error readingoriginal entry %v", err) } update := functional.NewFilterer(changeToAnEntry) badKey := *kKey badKey.Id++ if err := vsafedb.UpdateEntryWithEtag( store, kTransaction, newId, origEntryWithEtag.Etag, &badKey, update); err != vsafedb.ErrNoSuchId { t.Errorf("Expected ErrNoSuchId, got %v", err) } var readEntry vsafe.Entry if err := vsafedb.EntryById(store, nil, newId, kKey, &readEntry); err != nil { t.Fatalf("Error reading store: %v", err) } if readEntry != origEntryWithEtag.Entry { t.Errorf("Entry should not have been updated") } }
func TestEntryByIdKeyMismatch(t *testing.T) { var store FakeStore entry := *kAnEntry vsafedb.AddEntry(&store, nil, kKey, &entry) var readEntry vsafe.Entry badKey := *kKey badKey.Id++ if err := vsafedb.EntryById( store, nil, 1, &badKey, &readEntry); err != vsafedb.ErrNoSuchId { t.Errorf("Expected ErrNoSuchId, got %v", err) } }
func TestAddEntry(t *testing.T) { var store FakeStore entry := *kAnEntry var id int64 var err error if id, err = vsafedb.AddEntry(&store, nil, kKey, &entry); err != nil { t.Fatalf("Error adding tostore: %v", err) } if id != 1 { t.Errorf("expected 1, got %d", id) } // entry should not change as side effect if entry != *kAnEntry { t.Errorf("Expected %v, got %v", *kAnEntry, entry) } // Key Id should be stored with entry. if out := store[0].Owner; out != kKey.Id { t.Errorf("Expected %d, got %d", kKey.Id, out) } if store[0].UName == entry.UName || store[0].Password == entry.Password || store[0].Special == entry.Special { t.Error("Expected database to be encrypted.") } var readEntry vsafe.Entry if err = vsafedb.EntryById(store, nil, 1, kKey, &readEntry); err != nil { t.Fatalf("Error reading store: %v", err) } origEntry := *kAnEntry origEntry.Id = 1 origEntry.Owner = kKey.Id if readEntry != origEntry { t.Errorf("Expected %v, got %v", origEntry, readEntry) } if err = vsafedb.EntryById( store, nil, 9999, kKey, &readEntry); err != vsafedb.ErrNoSuchId { t.Errorf("Expected ErrNoSuchId, got %v", err) } }
func TestUpdateEntryConcurrent(t *testing.T) { origEntry := *kOrigEntry var store FakeStore newId, err := vsafedb.AddEntry(&store, nil, kKey, &origEntry) if err != nil { t.Fatalf("Error saving original entry %v", err) } var origEntryWithEtag vsafe.EntryWithEtag if err := vsafedb.EntryByIdWithEtag( store, nil, newId, kKey, &origEntryWithEtag); err != nil { t.Fatalf("Error reading original entry %v", err) } update := functional.NewFilterer(changeToAnEntry) updateSkipped := functional.NewFilterer(func(ptr interface{}) error { entryPtr := ptr.(*vsafe.Entry) *entryPtr = *kAnEntry return functional.Skipped }) // An update that skips shouldn't throw an error even if etag is wrong if err := vsafedb.UpdateEntryWithEtag( store, kTransaction, newId, origEntryWithEtag.Etag+1, kKey, updateSkipped); err != nil { t.Fatalf("Error updating store: %v", err) } if err := vsafedb.UpdateEntryWithEtag( store, kTransaction, newId, origEntryWithEtag.Etag+1, kKey, update); err != vsafedb.ErrConcurrentModification { t.Errorf("Expected ErrConcurrentModfication, got %v", err) } var readEntry vsafe.Entry if err := vsafedb.EntryById(store, nil, newId, kKey, &readEntry); err != nil { t.Fatalf("Error reading store: %v", err) } if readEntry != origEntryWithEtag.Entry { t.Errorf("Entry should not have been updated") } }