From 511865496ed470b4917d0d4bbfa3a31f11443573 Mon Sep 17 00:00:00 2001 From: Ada Date: Sun, 24 Mar 2024 20:56:03 +0100 Subject: [PATCH] :recycle: Restructure test Specific test module require to unnecessary export function --- internal/secret/crypto_test.go | 81 ++++++++++++++++++++++++++ internal/utils/utils.go | 6 +- {test => internal}/utils/utils_test.go | 40 ++++++------- test/secret/secret_test.go | 47 --------------- 4 files changed, 103 insertions(+), 71 deletions(-) create mode 100644 internal/secret/crypto_test.go rename {test => internal}/utils/utils_test.go (73%) delete mode 100644 test/secret/secret_test.go diff --git a/internal/secret/crypto_test.go b/internal/secret/crypto_test.go new file mode 100644 index 0000000..f983646 --- /dev/null +++ b/internal/secret/crypto_test.go @@ -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) + } +} diff --git a/internal/utils/utils.go b/internal/utils/utils.go index fc86af6..506f07f 100644 --- a/internal/utils/utils.go +++ b/internal/utils/utils.go @@ -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 diff --git a/test/utils/utils_test.go b/internal/utils/utils_test.go similarity index 73% rename from test/utils/utils_test.go rename to internal/utils/utils_test.go index 3525443..f5ae6fe 100644 --- a/test/utils/utils_test.go +++ b/internal/utils/utils_test.go @@ -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 { diff --git a/test/secret/secret_test.go b/test/secret/secret_test.go deleted file mode 100644 index fad6ba6..0000000 --- a/test/secret/secret_test.go +++ /dev/null @@ -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) - } -}