advsql/db_test.go
Timon Ringwald d6e73f69c0 rewrite
2022-07-05 12:38:39 +02:00

59 lines
1.2 KiB
Go

package advsql
import (
"crypto/sha512"
"encoding/hex"
"fmt"
"testing"
_ "github.com/go-sql-driver/mysql"
)
type User struct {
Name string
Hash []byte
Salt string
}
func ScanUser(u *User, scan ScanFunc) error {
return scan(&u.Name, &u.Hash, &u.Salt)
}
func ExecUser(u *User, exec ExecFunc) error {
return exec(u.Name, u.Hash, u.Salt)
}
func UpdateUser(u *User, exec ExecFunc) error {
return exec(u.Salt, u.Name)
}
func TestDB(t *testing.T) {
db, err := NewDatabase("192.168.178.2", 3306, "root", "ZAuJsLdPYFSxHdZon7xpMyh5LW7TPhmM", "users")
if err != nil {
t.Fatal(err)
}
defer db.Close()
insertUser := Insert(db, "INSERT INTO users VALUES (?, ?, ?)", ExecUser)
updateUser := Insert(db, "UPDATE users SET salt = ? WHERE name = ?", UpdateUser)
getUsers := Query(db, "SELECT * FROM users WHERE name = ?", ScanUser)
pw := sha512.Sum512([]byte("weiter"))
timon := &User{
Name: "timon",
Hash: pw[:],
Salt: "salt",
}
fmt.Println("insert:", insertUser(timon))
timon.Hash = []byte("asd")
fmt.Println("update:", updateUser(timon))
for user := range getUsers("tordarus") {
fmt.Printf("name: \"%s\" | hash: \"%s\" | salt: \"%s\"\n", user.Name, hex.EncodeToString(user.Hash), user.Salt)
}
}