2022-07-05 12:38:39 +02:00
|
|
|
package advsql
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/sha512"
|
|
|
|
"encoding/hex"
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
_ "github.com/go-sql-driver/mysql"
|
|
|
|
)
|
|
|
|
|
2022-07-12 20:56:33 +02:00
|
|
|
var TestDatabase *Database //= new(Database)
|
|
|
|
|
2022-07-05 12:38:39 +02:00
|
|
|
type User struct {
|
|
|
|
Name string
|
|
|
|
Hash []byte
|
|
|
|
Salt string
|
|
|
|
}
|
|
|
|
|
2022-07-12 20:56:33 +02:00
|
|
|
func ScanUserPkFirst(u *User, encode ScanFunc) error {
|
|
|
|
return encode(&u.Name, &u.Hash, &u.Salt)
|
2022-07-05 12:38:39 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 20:56:33 +02:00
|
|
|
func ScanUserPkLast(u *User, encode ScanFunc) error {
|
|
|
|
return encode(&u.Hash, &u.Salt, &u.Name)
|
2022-07-05 12:38:39 +02:00
|
|
|
}
|
|
|
|
|
2022-07-12 20:56:33 +02:00
|
|
|
var (
|
|
|
|
InsertUser = Insert(TestDatabase, "INSERT INTO users VALUES (?, ?, ?)", ScanUserPkFirst)
|
|
|
|
UpdateUser = Update(TestDatabase, "UPDATE users SET hash = ?, salt = ? WHERE name = ?", ScanUserPkLast)
|
|
|
|
GetUserByName = QueryOne(TestDatabase, "SELECT * FROM users WHERE namea = ?", ScanUserPkFirst)
|
|
|
|
)
|
2022-07-05 12:38:39 +02:00
|
|
|
|
|
|
|
func TestDB(t *testing.T) {
|
2022-07-12 20:56:33 +02:00
|
|
|
InitMysqlDatabase(TestDatabase, "ip", 3306, "username", "password", "database")
|
|
|
|
defer TestDatabase.Close()
|
2022-07-05 12:38:39 +02:00
|
|
|
|
|
|
|
pw := sha512.Sum512([]byte("weiter"))
|
|
|
|
timon := &User{
|
|
|
|
Name: "timon",
|
|
|
|
Hash: pw[:],
|
|
|
|
Salt: "salt",
|
|
|
|
}
|
2022-07-12 20:56:33 +02:00
|
|
|
fmt.Println("insert:", InsertUser(timon))
|
2022-07-05 12:38:39 +02:00
|
|
|
|
|
|
|
timon.Hash = []byte("asd")
|
2022-07-12 20:56:33 +02:00
|
|
|
fmt.Println("update:", UpdateUser(timon))
|
2022-07-05 12:38:39 +02:00
|
|
|
|
2022-07-12 20:56:33 +02:00
|
|
|
user := GetUserByName("tordarus")
|
|
|
|
fmt.Printf("name: \"%s\" | hash: \"%s\" | salt: \"%s\"\n", user.Name, hex.EncodeToString(user.Hash), user.Salt)
|
2022-07-05 12:38:39 +02:00
|
|
|
}
|