Debugging

This commit is contained in:
2022-03-13 07:06:39 +13:00
parent 8bc3cee328
commit dc8de8b4b9
31 changed files with 952 additions and 1040 deletions

View File

@@ -1,55 +1,55 @@
package helpers
// package helpers
import (
"crypto/rand"
"encoding/hex"
"log"
"path/filepath"
"runtime"
// import (
// "crypto/rand"
// "encoding/hex"
// "log"
// "path/filepath"
// "runtime"
"golang.org/x/crypto/bcrypt"
)
// "golang.org/x/crypto/bcrypt"
// )
//CheckError checks for errors and logs them and stops the program
func CheckError(err error) bool {
if err != nil {
log.Fatal(err)
return false
}
return true
}
// //CheckError checks for errors and logs them and stops the program
// func CheckError(err error) bool {
// if err != nil {
// log.Fatal(err)
// return false
// }
// return true
// }
func GetRootDir() string {
_, b, _, _ := runtime.Caller(0)
dir := filepath.Dir(b)
return filepath.Dir(filepath.Dir(dir))
}
// func GetRootDir() string {
// _, b, _, _ := runtime.Caller(0)
// dir := filepath.Dir(b)
// return filepath.Dir(filepath.Dir(dir))
// }
func GetAssets() string {
return GetRootDir()
}
// func GetAssets() string {
// return GetRootDir()
// }
func HashPassword(password string) (string, error) {
hash, err := bcrypt.GenerateFromPassword([]byte(password), 14)
return string(hash), err
}
// func HashPassword(password string) (string, error) {
// hash, err := bcrypt.GenerateFromPassword([]byte(password), 14)
// return string(hash), err
// }
func CheckPasswordHash(password, hash string) error {
err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
return err
}
// func CheckPasswordHash(password, hash string) error {
// err := bcrypt.CompareHashAndPassword([]byte(hash), []byte(password))
// return err
// }
func RandHex() string {
bytes := make([]byte, 12)
rand.Read(bytes)
return hex.EncodeToString(bytes)
}
// func RandHex() string {
// bytes := make([]byte, 12)
// rand.Read(bytes)
// return hex.EncodeToString(bytes)
// }
func Bytes(n int) ([]byte, error) {
b := make([]byte, n)
_, err := rand.Read(b)
if err != nil {
return nil, err
}
return b, nil
}
// func Bytes(n int) ([]byte, error) {
// b := make([]byte, n)
// _, err := rand.Read(b)
// if err != nil {
// return nil, err
// }
// return b, nil
// }

View File

@@ -1,35 +1,35 @@
package helpers_test
// package helpers_test
import (
"testing"
// import (
// "testing"
"git.1248.nz/1248/Otfe/misc/helpers"
"git.1248.nz/1248/Otfe/models"
)
// "git.technical.kiwi/go/otfe/misc/helpers"
// "git.technical.kiwi/go/otfe/models"
// )
func TestGetRootDir(t *testing.T) {
t.Log("Root path:", helpers.GetRootDir())
}
// func TestGetRootDir(t *testing.T) {
// t.Log("Root path:", helpers.GetRootDir())
// }
func TestHashPassword(t *testing.T) {
user := models.User{Email: "a@a.com", Username: "a"}
user.Delete("username", "a")
var err error
password := "43539jgifdkvnm4935078uJKJR**$ufjqd98438uiAHFJean89q34JKDFJ"
user.Password, err = helpers.HashPassword(password)
if err != nil {
t.Fail()
}
user.Create()
var user2 models.User
user2.Read("username", "a")
// func TestHashPassword(t *testing.T) {
// user := models.User{Email: "a@a.com", Username: "a"}
// user.Delete("username", "a")
// var err error
// password := "43539jgifdkvnm4935078uJKJR**$ufjqd98438uiAHFJean89q34JKDFJ"
// user.Password, err = helpers.HashPassword(password)
// if err != nil {
// t.Fail()
// }
// user.Create()
// var user2 models.User
// user2.Read("username", "a")
t.Log(helpers.CheckPasswordHash(password, user2.Password))
// t.Log(helpers.CheckPasswordHash(password, user2.Password))
}
// }
func TestRandHex(t *testing.T) {
for i := 0; i < 20; i++ {
t.Log(helpers.RandHex())
}
}
// func TestRandHex(t *testing.T) {
// for i := 0; i < 20; i++ {
// t.Log(helpers.RandHex())
// }
// }

View File

@@ -1,36 +1,36 @@
package helpers
// package helpers
import (
"fmt"
"path/filepath"
"reflect"
"runtime"
"testing"
)
// import (
// "fmt"
// "path/filepath"
// "reflect"
// "runtime"
// "testing"
// )
// assert fails the test if the condition is false.
func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
if !condition {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
tb.FailNow()
}
}
// // assert fails the test if the condition is false.
// func Assert(tb testing.TB, condition bool, msg string, v ...interface{}) {
// if !condition {
// _, file, line, _ := runtime.Caller(1)
// fmt.Printf("\033[31m%s:%d: "+msg+"\033[39m\n\n", append([]interface{}{filepath.Base(file), line}, v...)...)
// tb.FailNow()
// }
// }
// ok fails the test if an err is not nil.
func Ok(tb testing.TB, err error) {
if err != nil {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
tb.FailNow()
}
}
// // ok fails the test if an err is not nil.
// func Ok(tb testing.TB, err error) {
// if err != nil {
// _, file, line, _ := runtime.Caller(1)
// fmt.Printf("\033[31m%s:%d: unexpected error: %s\033[39m\n\n", filepath.Base(file), line, err.Error())
// tb.FailNow()
// }
// }
// equals fails the test if exp is not equal to act.
func Equals(tb testing.TB, exp, act interface{}) {
if !reflect.DeepEqual(exp, act) {
_, file, line, _ := runtime.Caller(1)
fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
tb.FailNow()
}
}
// // equals fails the test if exp is not equal to act.
// func Equals(tb testing.TB, exp, act interface{}) {
// if !reflect.DeepEqual(exp, act) {
// _, file, line, _ := runtime.Caller(1)
// fmt.Printf("\033[31m%s:%d:\n\n\texp: %#v\n\n\tgot: %#v\033[39m\n\n", filepath.Base(file), line, exp, act)
// tb.FailNow()
// }
// }