コード例 #1
0
ファイル: store_test.go プロジェクト: keep94/vsafe
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)
	}
}
コード例 #2
0
ファイル: store_test.go プロジェクト: keep94/vsafe
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")
	}
}
コード例 #3
0
ファイル: store_test.go プロジェクト: keep94/vsafe
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)
	}
}
コード例 #4
0
ファイル: single.go プロジェクト: keep94/vsafe
func (h *Handler) doPost(w http.ResponseWriter, r *http.Request, id int64) {
	var err error
	session := common.GetUserSession(r)
	if !common.VerifyXsrfToken(r, kSingle) {
		err = common.ErrXsrf
	} else if http_util.HasParam(r.Form, "delete") {
		if isIdValid(id) {
			err = h.Store.RemoveEntry(nil, id, session.User.GetOwner())
		}
	} else if http_util.HasParam(r.Form, "cancel") {
		// Do nothing
	} else {
		var mutation functional.Filterer
		mutation, err = toEntry(r.Form)
		if err == nil {
			if isIdValid(id) {
				tag, _ := strconv.ParseUint(r.Form.Get("etag"), 10, 64)
				err = h.Doer.Do(func(t db.Transaction) error {
					return vsafedb.UpdateEntryWithEtag(
						h.Store, t, id, tag, session.Key(), mutation)
				})
			} else {
				var newId int64
				var entry vsafe.Entry
				mutation.Filter(&entry)
				newId, err = vsafedb.AddEntry(h.Store, nil, session.Key(), &entry)
				if err == nil {
					id = newId
				}
			}
		}
	}
	if err == vsafedb.ErrConcurrentModification {
		err = errors.New("Someone else updated this entry after you started. Click cancel and try again.")
	}
	if err != nil {
		http_util.WriteTemplate(
			w,
			kTemplate,
			newView(
				r.Form,
				isIdValid(id),
				session.Key().Id,
				common.NewXsrfToken(r, kSingle),
				err))
	} else {
		goBack(w, r, id)
	}
}
コード例 #5
0
ファイル: store_test.go プロジェクト: keep94/vsafe
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")
	}
}
コード例 #6
0
ファイル: vsafeimport.go プロジェクト: keep94/vsafe
func doSingleImport(
	t db.Transaction,
	store vsafedb.AddEntryRunner,
	jentry *jsonEntry,
	key *vsafe.Key) error {
	var entry vsafe.Entry
	var err error
	entry.Url, err = url.Parse(jentry.Url)
	if err != nil {
		return err
	}
	entry.Title = jentry.Title
	entry.Desc = jentry.Desc
	entry.UName = jentry.UName
	entry.Password = jentry.Password
	entry.Special = jentry.Special
	_, err = vsafedb.AddEntry(store, t, key, &entry)
	return err
}
コード例 #7
0
ファイル: store_test.go プロジェクト: keep94/vsafe
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)
	}
}
コード例 #8
0
ファイル: store_test.go プロジェクト: keep94/vsafe
func TestEntries(t *testing.T) {
	yahoo, _ := url.Parse("http://www.yahoo.com")
	google, _ := url.Parse("http://www.google.com")
	var store FakeStore
	entry1 := vsafe.Entry{Title: " First", Url: yahoo, Desc: "the SeconD   oNe"}
	entry2 := vsafe.Entry{Title: "aGAiN  sEcond", Url: google, Desc: "a desc"}
	entry3 := vsafe.Entry{Title: "third again", Desc: "foo bar"}
	vsafedb.AddEntry(&store, nil, kKey, &entry1)
	vsafedb.AddEntry(&store, nil, kKey, &entry2)
	vsafedb.AddEntry(&store, nil, kKey, &entry3)
	entries, err := vsafedb.Entries(store, kKey.Id, "")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 3 {
		t.Errorf("Expected 3 entries, got %v", len(entries))
	}
	if entries[0].Title != entry1.Title || entries[1].Title != entry2.Title || entries[2].Title != entry3.Title {
		t.Error("Returned 3 entries in wrong order")
	}
	entries, err = vsafedb.Entries(store, kKey.Id, "  first")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 1 {
		t.Errorf("Expected 1 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, "second  ")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 2 {
		t.Errorf("Expected 2 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, "google")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 1 {
		t.Errorf("Expected 1 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, "biz")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 0 {
		t.Errorf("Expected 0 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, " eCond  one ")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 1 {
		t.Errorf("Expected 1 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, " Gain   SEco ")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 1 {
		t.Errorf("Expected 1 entries, got %v", len(entries))
	}
	entries, err = vsafedb.Entries(store, kKey.Id, " hain   SEco ")
	if err != nil {
		t.Fatalf("Got error fetching entries: %v", err)
	}
	if len(entries) != 0 {
		t.Errorf("Expected 0 entries, got %v", len(entries))
	}
}