func TestKey(t *testing.T) { user := vsafe.User{Id: 1} var err error if err = user.Init("keep94", "somepassword"); err != nil { t.Fatalf("Error creating user: %v", err) } user2 := vsafe.User{Id: 2} if err = user2.Init("keep94", "somepassword"); err != nil { t.Fatalf("Error creating user: %v", err) } var key *vsafe.Key if key, err = user.VerifyPassword("somepassword"); err != nil { t.Fatalf("Verify password failed: %v", err) } var keyAgain *vsafe.Key if keyAgain, err = user.VerifyPassword("somepassword"); err != nil { t.Fatalf("Verify password failed: %v", err) } var key2 *vsafe.Key if key2, err = user2.VerifyPassword("somepassword"); err != nil { t.Fatalf("Verify password failed: %v", err) } if !key.Equal(keyAgain) { t.Errorf("Expected equal keys") } if key.Equal(key2) { t.Errorf("Expected unequal keys") } }
func TestUser(t *testing.T) { user := vsafe.User{Id: 1} var err error var key *vsafe.Key var key2 *vsafe.Key if err = user.Init("keep94", "somepassword"); err != nil { t.Fatalf("Error creating user: %v", err) } if key, err = user.VerifyPassword("somepassword"); err != nil { t.Fatalf("Verify password failed: %v", err) } if _, err = user.VerifyPassword("aardvark"); err != vsafe.ErrWrongPassword { t.Errorf("Expected wrong password, got %v", err) } user2 := vsafe.User{Id: 2} if err = user2.InitWithKey("keep94+admin", "foo", key); err != nil { t.Fatalf("Error creating user: %v", err) } if key2, err = user2.VerifyPassword("foo"); err != nil { t.Fatalf("Verify password failed: %v", err) } if !key2.Equal(key) { t.Error("key and key2 should be equal.") } if key.Id != 1 { t.Error("Expected key Id to be 1") } }
func TestChangePassword(t *testing.T) { var user vsafe.User if err := user.Init("foo", "password"); err != nil { t.Fatalf("Error initializing user %v", err) } var store FakeUserStore if err := store.AddUser(nil, &user); err != nil { t.Fatalf("Error adding user %v", err) } if _, err := vsafedb.ChangePassword( store, kTransaction, user.Id+1, "password", "board"); err != vsafedb.ErrNoSuchId { t.Errorf("Expected ErrNoSuchId, got %v", err) } if _, err := vsafedb.ChangePassword( store, kTransaction, user.Id, "wrong", "board"); err != vsafe.ErrWrongPassword { t.Errorf("Expected ErrWrongPassword, got %v", err) } newUser, err := vsafedb.ChangePassword( store, kTransaction, user.Id, "password", "board") if err != nil { t.Errorf("Expected no error, got %v", err) } if _, err := newUser.VerifyPassword("board"); err != nil { t.Errorf("Got error verifying password, %v", err) } var readUser vsafe.User if err := store.UserById(nil, user.Id, &readUser); err != nil { t.Fatalf("Got error reading database, %v", err) } if _, err := readUser.VerifyPassword("board"); err != nil { t.Errorf("Got error verifying password, %v", err) } }
func TestChangePassword(t *testing.T) { user := vsafe.User{Id: 1} var err error if err = user.Init("keep94", "somepassword"); err != nil { t.Fatalf("Error creating user: %v", err) } if err = user.ChangePassword("somepassword", "another"); err != nil { t.Fatalf("Verify password failed: %v", err) } if _, err = user.VerifyPassword("another"); err != nil { t.Error("Expected password to change") } if err = user.ChangePassword("somepassword", "be"); err != vsafe.ErrWrongPassword { t.Errorf("Expected ErrWrongPassword, got %v", err) } }
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 } }