WIP: ♻️ Restructure test #36

Closed
ada wants to merge 46 commits from ada/rewrite-test into main
4 changed files with 103 additions and 71 deletions
Showing only changes of commit 511865496e - Show all commits

View file

@ -0,0 +1,81 @@
package secret
import (
"errors"
"fmt"
"regexp"
"testing"
"git.gnous.eu/gnouseu/plakken/internal/constant"
"golang.org/x/crypto/argon2"
)
func TestPasswordFormat(t *testing.T) {
regex := fmt.Sprintf("\\$argon2id\\$v=%d\\$m=%d,t=%d,p=%d\\$[A-Za-z0-9+/]*\\$[A-Za-z0-9+/]*$", argon2.Version, constant.ArgonMemory, constant.ArgonIterations, constant.ArgonThreads)
got, err := Password("Password!")
if err != nil {
t.Fatal(err)
}
result, _ := regexp.MatchString(regex, got)
if !result {
t.Fatal("Error in Password, format is not valid "+": ", got)
}
}
func TestVerifyPassword(t *testing.T) {
result, err := VerifyPassword("Password!", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0")
if err != nil {
t.Fatal(err)
}
if !result {
t.Fatal("Error in VerifyPassword, got:", result, "want: ", true)
}
}
func TestVerifyPasswordInvalid(t *testing.T) {
result, err := VerifyPassword("notsamepassword", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0")
if err != nil {
t.Fatal(err)
}
if result {
t.Fatal("Error in VerifyPassword, got:", result, "want: ", false)
}
}
func TestParseHash(t *testing.T) {
_, config, err := parseHash("$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0")
if err != nil {
t.Fatal(err)
}
if config.saltLength != constant.ArgonSaltSize {
t.Fatal("Error in VerifyPassword: config.saltLength are not correct, go: ", config.saltLength, "want: ", constant.ArgonSaltSize)
}
if config.keyLength != constant.ArgonKeyLength {
t.Fatal("Error in VerifyPassword: config.keyLength are not correct, go: ", config.saltLength, "want: ", constant.ArgonKeyLength)
}
if config.threads != constant.ArgonThreads {
t.Fatal("Error in VerifyPassword: config.threads are not correct, go: ", config.threads, "want: ", constant.ArgonThreads)
}
if config.memory != constant.ArgonMemory {
t.Fatal("Error in VerifyPassword: config.memory are not correct, go: ", config.memory, "want: ", constant.ArgonMemory)
}
if config.iterations != constant.ArgonIterations {
t.Fatal("Error in VerifyPassword: config.iterations are not correct, go: ", config.iterations, "want: ", constant.ArgonIterations)
}
}
func TestParseBadHashAlgo(t *testing.T) {
_, _, err := parseHash("$notvalid$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0")
want := &parseError{message: "Algorithm is not valid"}
if !errors.As(err, &want) {
t.Fatal("Error in parseHash, want :", want, "got: ", err)
}
}

View file

@ -19,13 +19,13 @@ func GenerateUrl(length uint8) string {
return string(b)
}
// CheckCharRedundant verify is a character is redundant in a string
func CheckCharRedundant(source string, char string) bool { // Verify if a char is redundant
// checkCharRedundant verify is a character is redundant in a string
func checkCharRedundant(source string, char string) bool { // Verify if a char is redundant
return strings.Count(source, char) > 1
}
func parseIntBeforeSeparator(source *string, sep string) (int, error) { // return 0 & error if error, only accept positive number
if CheckCharRedundant(*source, sep) {
if checkCharRedundant(*source, sep) {
return 0, &parseIntBeforeSeparatorError{message: *source + ": cannot parse value as int"}
}
var value int

View file

@ -1,30 +1,28 @@
package utils_test
package utils
import (
"errors"
"testing"
"git.gnous.eu/gnouseu/plakken/internal/utils"
)
func TestCheckCharNotRedundantTrue(t *testing.T) { // Test CheckCharRedundant with redundant char
func TestCheckCharNotRedundantTrue(t *testing.T) { // Test checkCharRedundant with redundant char
want := true
got := utils.CheckCharRedundant("2d1h3md4h7s", "h")
got := checkCharRedundant("2d1h3md4h7s", "h")
if got != want {
t.Fatal("Error in parseExpirationFull, want : ", want, "got : ", got)
}
}
func TestCheckCharNotRedundantFalse(t *testing.T) { // Test CheckCharRedundant with not redundant char
func TestCheckCharNotRedundantFalse(t *testing.T) { // Test checkCharRedundant with not redundant char
want := false
got := utils.CheckCharRedundant("2d1h3m47s", "h")
got := checkCharRedundant("2d1h3m47s", "h")
if got != want {
t.Fatal("Error in parseExpirationFull, want : ", want, "got : ", got)
}
}
func TestParseExpirationFull(t *testing.T) { // test parseExpirationFull with all valid separator
result, _ := utils.ParseExpiration("2d1h3m47s")
result, _ := ParseExpiration("2d1h3m47s")
correctValue := 176627
if result != correctValue {
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
@ -32,7 +30,7 @@ func TestParseExpirationFull(t *testing.T) { // test parseExpirationFull with al
}
func TestParseExpirationMissing(t *testing.T) { // test parseExpirationFull with all valid separator
result, _ := utils.ParseExpiration("1h47s")
result, _ := ParseExpiration("1h47s")
correctValue := 3647
if result != correctValue {
t.Fatal("Error in ParseExpirationFull, want : ", correctValue, "got : ", result)
@ -40,7 +38,7 @@ func TestParseExpirationMissing(t *testing.T) { // test parseExpirationFull with
}
func TestParseExpirationWithCaps(t *testing.T) { // test parseExpirationFull with all valid separator
result, _ := utils.ParseExpiration("2D1h3M47s")
result, _ := ParseExpiration("2D1h3M47s")
correctValue := 176627
if result != correctValue {
t.Fatal("Error in parseExpirationFull, want : ", correctValue, "got : ", result)
@ -48,7 +46,7 @@ func TestParseExpirationWithCaps(t *testing.T) { // test parseExpirationFull wit
}
func TestParseExpirationNull(t *testing.T) { // test ParseExpirationFull with all valid separator
result, _ := utils.ParseExpiration("0")
result, _ := ParseExpiration("0")
correctValue := 0
if result != correctValue {
t.Fatal("Error in ParseExpirationFull, want: ", correctValue, "got: ", result)
@ -56,16 +54,16 @@ func TestParseExpirationNull(t *testing.T) { // test ParseExpirationFull with al
}
func TestParseExpirationNegative(t *testing.T) { // test ParseExpirationFull with all valid separator
_, got := utils.ParseExpiration("-42h1m4s")
want := &utils.ParseExpirationError{}
_, got := ParseExpiration("-42h1m4s")
want := &ParseExpirationError{}
if !errors.As(got, &want) {
t.Fatal("Error in ParseExpirationFull, want : ", want, "got : ", got)
}
}
func TestParseExpirationInvalid(t *testing.T) { // test ParseExpirationFull with all valid separator
_, got := utils.ParseExpiration("8h42h1m1d4s")
want := &utils.ParseExpirationError{}
_, got := ParseExpiration("8h42h1m1d4s")
want := &ParseExpirationError{}
if !errors.As(got, &want) {
t.Fatal("Error in ParseExpirationFull, want : ", want, "got : ", got)
}
@ -73,23 +71,23 @@ func TestParseExpirationInvalid(t *testing.T) { // test ParseExpirationFull with
}
func TestParseExpirationInvalidRedundant(t *testing.T) { // test ParseExpirationFull with all valid separator
_, got := utils.ParseExpiration("8h42h1m1h4s")
want := &utils.ParseExpirationError{}
_, got := ParseExpiration("8h42h1m1h4s")
want := &ParseExpirationError{}
if !errors.As(got, &want) {
t.Fatal("Error in ParseExpirationFull, want : ", want, "got : ", got)
}
}
func TestParseExpirationInvalidTooHigh(t *testing.T) { // test ParseExpirationFull with all valid separator
_, got := utils.ParseExpiration("2d1h3m130s")
want := &utils.ParseExpirationError{}
_, got := ParseExpiration("2d1h3m130s")
want := &ParseExpirationError{}
if !errors.As(got, &want) {
t.Fatal("Error in ParseExpirationFull, want : ", want, "got : ", got)
}
}
func TestValidKey(t *testing.T) { // test ValidKey with a valid key
got := utils.ValidKey("ab_a-C42")
got := ValidKey("ab_a-C42")
want := true
if got != want {
@ -98,7 +96,7 @@ func TestValidKey(t *testing.T) { // test ValidKey with a valid key
}
func TestInValidKey(t *testing.T) { // test ValidKey with invalid key
got := utils.ValidKey("ab_?a-C42")
got := ValidKey("ab_?a-C42")
want := false
if got != want {

View file

@ -1,47 +0,0 @@
package secret_test
import (
"fmt"
"regexp"
"testing"
"git.gnous.eu/gnouseu/plakken/internal/constant"
"git.gnous.eu/gnouseu/plakken/internal/secret"
"golang.org/x/crypto/argon2"
)
func TestPasswordFormat(t *testing.T) {
regex := fmt.Sprintf("\\$argon2id\\$v=%d\\$m=%d,t=%d,p=%d\\$[A-Za-z0-9+/]*\\$[A-Za-z0-9+/]*$", argon2.Version, constant.ArgonMemory, constant.ArgonIterations, constant.ArgonThreads)
got, err := secret.Password("Password!")
if err != nil {
t.Fatal(err)
}
result, _ := regexp.MatchString(regex, got)
if !result {
t.Fatal("Error in Password, format is not valid "+": ", got)
}
}
func TestVerifyPassword(t *testing.T) {
result, err := secret.VerifyPassword("Password!", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0")
if err != nil {
t.Fatal(err)
}
if !result {
t.Fatal("Error in VerifyPassword, got:", result, "want: ", true)
}
}
func TestVerifyPasswordInvalid(t *testing.T) {
result, err := secret.VerifyPassword("notsamepassword", "$argon2id$v=19$m=65536,t=2,p=4$A+t5YGpyy1BHCbvk/LP1xQ$eNuUj6B2ZqXlGi6KEqep39a7N4nysUIojuPXye+Ypp0")
if err != nil {
t.Fatal(err)
}
if result {
t.Fatal("Error in VerifyPassword, got:", result, "want: ", false)
}
}