示例#1
0
func TestTheDb(t *testing.T) {
	var wg1, wg2 sync.WaitGroup
	conn, _ := sqlite.Open(":memory:")
	db := New(conn)
	wg1.Add(2)
	wg2.Add(1)
	go func() {
		runAndStop(t, db, 0, 100)
		wg1.Done()
	}()
	go func() {
		runAndStop(t, db, 100, 200)
		wg1.Done()
	}()
	go func() {
		runForever(t, db)
		wg2.Done()
	}()
	wg1.Wait()
	if output := db.Close(); output != nil {
		t.Errorf("Expected nil got %v", output)
	}
	if output := conn.Close(); output == nil {
		t.Error("Expected connection to be closed.")
	}
	wg2.Wait()
}
示例#2
0
func main() {
	flag.Parse()
	if fDb == "" {
		fmt.Println("Need to specify -db flag.")
		flag.Usage()
		return
	}
	conn, err := sqlite.Open(fDb)
	if err != nil {
		fmt.Printf("Unable to open database - %s\n", fDb)
		return
	}
	dbase := sqlite_db.New(conn)
	defer dbase.Close()
	store := for_sqlite.New(dbase)
	var users []*vsafe.User
	if err = store.Users(nil, consume.AppendPtrsTo(&users, nil)); err != nil {
		fmt.Printf("Error fetching users - %v\n", err)
		return
	}
	usersById := make(map[int64]*vsafe.User)
	for _, user := range users {
		usersById[user.Id] = user
	}
	for _, user := range users {
		fmt.Printf("%s\t%s\n", user.Name, ownerStr(usersById, user.GetOwner()))
	}
}
示例#3
0
文件: vsafe.go 项目: keep94/vsafe
func setupDb(filepath string) {
	conn, err := sqlite.Open(filepath)
	if err != nil {
		panic(err)
	}
	dbase := sqlite_db.New(conn)
	kDoer = sqlite_db.NewDoer(dbase)
	kStore = for_sqlite.New(dbase)
}
示例#4
0
文件: db_test.go 项目: keep94/vsafe
func openDb(t *testing.T) *sqlite_db.Db {
	conn, err := sqlite.Open(":memory:")
	if err != nil {
		t.Fatalf("Error opening database: %v", err)
	}
	db := sqlite_db.New(conn)
	err = db.Do(func(conn *sqlite.Conn) error {
		return sqlite_setup.SetUpTables(conn)
	})
	if err != nil {
		t.Fatalf("Error creating tables: %v", err)
	}
	return db
}
示例#5
0
func main() {
	flag.Parse()
	if fDb == "" || fName == "" || fPassword == "" {
		fmt.Println("Need to specify -db, -name, and -password flag.")
		flag.Usage()
		return
	}
	conn, err := sqlite.Open(fDb)
	if err != nil {
		fmt.Printf("Unable to open database - %s\n", fDb)
		return
	}
	dbase := sqlite_db.New(conn)
	defer dbase.Close()
	err = dbase.Do(func(conn *sqlite.Conn) error {
		return sqlite_setup.SetUpTables(conn)
	})
	if err != nil {
		fmt.Printf("Unable to create tables - %v\n", err)
		return
	}
	store := for_sqlite.New(dbase)
	var user vsafe.User
	if fMasterName == "" {
		err = user.Init(fName, fPassword)
	} else {
		var master vsafe.User
		if err = store.UserByName(nil, fMasterName, &master); err != nil {
			fmt.Printf("Error retrieving master user - %v\n", err)
			return
		}
		var key *vsafe.Key
		if key, err = master.VerifyPassword(fMasterPassword); err != nil {
			fmt.Printf("Error verifying master password - %v\n", err)
			return
		}
		err = user.InitWithKey(fName, fPassword, key)
	}
	if err != nil {
		fmt.Printf("Error initializing user - %v\n", err)
		return
	}
	if err = store.AddUser(nil, &user); err != nil {
		fmt.Printf("Error storing user in database - %v\n", err)
		return
	}
}
示例#6
0
func main() {
	flag.Parse()
	if fDb == "" || fName == "" {
		fmt.Println("Need to specify -db and -name flag.")
		flag.Usage()
		return
	}
	conn, err := sqlite.Open(fDb)
	if err != nil {
		fmt.Printf("Unable to open database - %s\n", fDb)
		return
	}
	dbase := sqlite_db.New(conn)
	defer dbase.Close()
	store := for_sqlite.New(dbase)
	if err = store.RemoveUser(nil, fName); err != nil {
		fmt.Printf("Error removing user - %v\n", err)
		return
	}
}
示例#7
0
func main() {
	flag.Parse()
	if fDb == "" || fName == "" || fPassword == "" {
		fmt.Println("Need to specify -db, -name, and -password flag.")
		flag.Usage()
		return
	}
	conn, err := sqlite.Open(fDb)
	if err != nil {
		fmt.Printf("Unable to open database - %s\n", fDb)
		return
	}
	dbase := sqlite_db.New(conn)
	defer dbase.Close()
	store := for_sqlite.New(dbase)
	doer := sqlite_db.NewDoer(dbase)
	var user vsafe.User
	if err = store.UserByName(nil, fName, &user); err != nil {
		fmt.Printf("Error retrieving user - %v\n", err)
		return
	}
	var key *vsafe.Key
	if key, err = user.VerifyPassword(fPassword); err != nil {
		fmt.Printf("Error verifying user password - %v\n", err)
		return
	}
	decoder := json.NewDecoder(os.Stdin)
	var entryList []*jsonEntry
	if err = decoder.Decode(&entryList); err != nil {
		fmt.Printf("Error decoding json - %v\n", err)
		return
	}
	err = doer.Do(func(t db.Transaction) error {
		return doImport(t, store, entryList, key)
	})
	if err != nil {
		fmt.Printf("Import failed - %v\n", err)
	}
}