Merge pull request #5 from wneessen/dev

Tests cleanup. Used structs for tests instead of repeating code
This commit is contained in:
Winni Neessen 2021-03-24 15:02:45 +01:00 committed by GitHub
commit 7ff2a85f3e
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23

View file

@ -1,7 +1,6 @@
package main package main
import ( import (
"math/big"
"testing" "testing"
) )
@ -13,36 +12,42 @@ var _ = func() bool {
// Test getRandNum with max 1000 // Test getRandNum with max 1000
func TestGetRandNum(t *testing.T) { func TestGetRandNum(t *testing.T) {
t.Run("maxNum_is_1000", func(t *testing.T) { testTable := []struct {
randNum, err := getRandNum(1000) testName string
if err != nil { givenVal int
t.Fatalf("Random number generation failed: %v", err.Error()) maxRet int
} minRet int
if randNum > 1000 { shouldFail bool
t.Fatalf("Generated random number between 0 and 1000 is too big: %v", randNum) }{
} {"randNum up to 1000", 1000, 1000, 0, false},
if randNum < 0 { {"randNum should be 1", 1, 1, 0, false},
t.Fatalf("Generated random number between 0 and 1000 is too small: %v", randNum) {"randNum should fail on 0", 0, 0, 0, true},
} {"randNum should fail on negative", -1, 0, 0, true},
}) }
t.Run("maxNum_is_1", func(t *testing.T) {
randNum, err := getRandNum(1) for _, testCase := range testTable {
if err != nil { t.Run(testCase.testName, func(t *testing.T) {
t.Fatalf("Random number generation failed: %v", err.Error()) randNum, err := getRandNum(testCase.givenVal)
} if testCase.shouldFail {
if randNum > 1 { if err == nil {
t.Fatalf("Generated random number between 0 and 1000 is too big: %v", randNum) t.Errorf("Random number generation succeeded but was expected to fail. Given: %v, returned: %v",
} testCase.givenVal, randNum)
if randNum < 0 { }
t.Fatalf("Generated random number between 0 and 1000 is too small: %v", randNum) } else {
} if err != nil {
}) t.Errorf("Random number generation failed: %v", err.Error())
t.Run("maxNum_is_0", func(t *testing.T) { }
randNum, err := getRandNum(0) if randNum > testCase.maxRet {
if err == nil { t.Errorf("Random number generation returned too big value. Given %v, expected max: %v, got: %v",
t.Fatalf("Random number expected to fail, but provided a value instead: %v", randNum) testCase.givenVal, testCase.maxRet, randNum)
} }
}) if randNum < testCase.minRet {
t.Errorf("Random number generation returned too small value. Given %v, expected max: %v, got: %v",
testCase.givenVal, testCase.minRet, randNum)
}
}
})
}
} }
// Test getRandChar // Test getRandChar
@ -82,196 +87,89 @@ func TestGetRandChar(t *testing.T) {
// Test getCharRange() with different config settings // Test getCharRange() with different config settings
func TestGetCharRange(t *testing.T) { func TestGetCharRange(t *testing.T) {
lowerCaseBytes := []int{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r',
t.Run("lower_case_only", func(t *testing.T) { 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
// Lower case only lowerCaseHumanBytes := []int{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r',
allowedBytes := []int{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j', 'k', 'l', 'm', 'n', 'o', 'p', 'q', 'r', 's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'} upperCaseBytes := []int{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
config.useLowerCase = true 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
config.useUpperCase = false upperCaseHumanBytes := []int{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q',
config.useNumber = false 'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
config.useSpecial = false numberBytes := []int{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
config.humanReadable = false numberHumanBytes := []int{'2', '3', '4', '5', '6', '7', '8', '9'}
charRange := getCharRange() specialBytes := []int{'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':',
for _, curChar := range charRange { ';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'}
searchAllowedBytes := containsByte(allowedBytes, int(curChar)) specialHumanBytes := []int{'"', '#', '%', '*', '+', '-', '/', ':', ';', '=', '\\', '_', '|', '~'}
if !searchAllowedBytes { testTable := []struct {
t.Fatalf("Character range for lower-case only returned invalid value: %v", testName string
string(curChar)) allowedBytes []int
useLowerCase bool
useUpperCase bool
useNumber bool
useSpecial bool
humanReadable bool
}{
{"lowercase_only", lowerCaseBytes, true, false, false, false, false},
{"lowercase_only_human", lowerCaseHumanBytes, true, false, false, false, true},
{"uppercase_only", upperCaseBytes, false, true, false, false, false},
{"uppercase_only_human", upperCaseHumanBytes, false, true, false, false, true},
{"number_only", numberBytes, false, false, true, false, false},
{"number_only_human", numberHumanBytes, false, false, true, false, true},
{"special_only", specialBytes, false, false, false, true, false},
{"special_only_human", specialHumanBytes, false, false, false, true, true},
}
for _, testCase := range testTable {
t.Run(testCase.testName, func(t *testing.T) {
config.useLowerCase = testCase.useLowerCase
config.useUpperCase = testCase.useUpperCase
config.useNumber = testCase.useNumber
config.useSpecial = testCase.useSpecial
config.humanReadable = testCase.humanReadable
charRange := getCharRange()
for _, curChar := range charRange {
searchAllowedBytes := containsByte(testCase.allowedBytes, int(curChar))
if !searchAllowedBytes {
t.Errorf("Character range returned invalid value: %v", string(curChar))
}
} }
} })
}) }
// Lower case only (human readable)
t.Run("lower_case_only_human_readable", func(t *testing.T) {
allowedBytes := []int{'a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'j', 'k', 'm', 'n', 'p', 'q', 'r',
's', 't', 'u', 'v', 'w', 'x', 'y', 'z'}
config.useLowerCase = true
config.useUpperCase = false
config.useNumber = false
config.useSpecial = false
config.humanReadable = true
charRange := getCharRange()
for _, curChar := range charRange {
searchAllowedBytes := containsByte(allowedBytes, int(curChar))
if !searchAllowedBytes {
t.Fatalf("Character range for lower-case only (human readable) returned invalid value: %v",
string(curChar))
}
}
})
// Upper case only
t.Run("upper_case_only", func(t *testing.T) {
allowedBytes := []int{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'I', 'J', 'K', 'L', 'M', 'N', 'O', 'P', 'Q', 'R',
'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
config.useLowerCase = false
config.useUpperCase = true
config.useNumber = false
config.useSpecial = false
config.humanReadable = false
charRange := getCharRange()
for _, curChar := range charRange {
searchAllowedBytes := containsByte(allowedBytes, int(curChar))
if !searchAllowedBytes {
t.Fatalf("Character range for upper-case only returned invalid value: %v",
string(curChar))
}
}
})
// Upper case only (human readable)
t.Run("upper_case_only_human_readable", func(t *testing.T) {
allowedBytes := []int{'A', 'B', 'C', 'D', 'E', 'F', 'G', 'H', 'J', 'K', 'M', 'N', 'P', 'Q',
'R', 'S', 'T', 'U', 'V', 'W', 'X', 'Y', 'Z'}
config.useLowerCase = false
config.useUpperCase = true
config.useNumber = false
config.useSpecial = false
config.humanReadable = true
charRange := getCharRange()
for _, curChar := range charRange {
searchAllowedBytes := containsByte(allowedBytes, int(curChar))
if !searchAllowedBytes {
t.Fatalf("Character range for upper-case only (human readable) returned invalid value: %v",
string(curChar))
}
}
})
// Numbers only
t.Run("numbers_only", func(t *testing.T) {
allowedBytes := []int{'0', '1', '2', '3', '4', '5', '6', '7', '8', '9'}
config.useLowerCase = false
config.useUpperCase = false
config.useNumber = true
config.useSpecial = false
config.humanReadable = false
charRange := getCharRange()
for _, curChar := range charRange {
searchAllowedBytes := containsByte(allowedBytes, int(curChar))
if !searchAllowedBytes {
t.Fatalf("Character range for numbers only returned invalid value: %v",
string(curChar))
}
}
})
// Numbers only (human readable)
t.Run("numbers_only_human_readable", func(t *testing.T) {
allowedBytes := []int{'2', '3', '4', '5', '6', '7', '8', '9'}
config.useLowerCase = false
config.useUpperCase = false
config.useNumber = true
config.useSpecial = false
config.humanReadable = true
charRange := getCharRange()
for _, curChar := range charRange {
searchAllowedBytes := containsByte(allowedBytes, int(curChar))
if !searchAllowedBytes {
t.Fatalf("Character range for numbers (human readable) only returned invalid value: %v",
string(curChar))
}
}
})
// Special characters only
t.Run("special_chars_only", func(t *testing.T) {
allowedBytes := []int{'!', '"', '#', '$', '%', '&', '\'', '(', ')', '*', '+', ',', '-', '.', '/', ':',
';', '<', '=', '>', '?', '@', '[', '\\', ']', '^', '_', '`', '{', '|', '}', '~'}
config.useLowerCase = false
config.useUpperCase = false
config.useNumber = false
config.useSpecial = true
config.humanReadable = false
charRange := getCharRange()
for _, curChar := range charRange {
searchAllowedBytes := containsByte(allowedBytes, int(curChar))
if !searchAllowedBytes {
t.Fatalf("Character range for special characters only returned invalid value: %v",
string(curChar))
}
}
})
// Special characters only (human readable)
t.Run("special_chars_only_human_readable", func(t *testing.T) {
allowedBytes := []int{'"', '#', '%', '*', '+', '-', '/', ':', ';', '=', '\\', '_', '|', '~'}
config.useLowerCase = false
config.useUpperCase = false
config.useNumber = false
config.useSpecial = true
config.humanReadable = true
charRange := getCharRange()
for _, curChar := range charRange {
searchAllowedBytes := containsByte(allowedBytes, int(curChar))
if !searchAllowedBytes {
t.Fatalf("Character range for special characters only returned invalid value: %v",
string(curChar))
}
}
})
} }
// Test Conversions // Test Conversions
func TestConvert(t *testing.T) { func TestConvert(t *testing.T) {
t.Run("convert_A_to_Alfa", func(t *testing.T) { testTable := []struct {
charToString, err := convertCharToName('A') testName string
if err != nil { givenVal byte
t.Fatalf("Character to string conversion failed: %v", err.Error()) expVal string
} shouldFail bool
if charToString != "Alfa" { }{
t.Fatalf("Converting 'A' to string did not return the correct value of 'Alfa': %q", charToString) {"convert_A_to_Alfa", 'A', "Alfa", false},
} {"convert_a_to_alfa", 'a', "alfa", false},
}) {"convert_0_to_ZERO", '0', "ZERO", false},
t.Run("convert_a_to_alfa", func(t *testing.T) { {"convert_/_to_SLASH", '/', "SLASH", false},
charToString, err := convertCharToName('a') }
if err != nil { for _, testCase := range testTable {
t.Fatalf("Character to string conversion failed: %v", err.Error()) t.Run(testCase.testName, func(t *testing.T) {
} charToString, err := convertCharToName(testCase.givenVal)
if charToString != "alfa" { if testCase.shouldFail {
t.Fatalf("Converting 'a' to string did not return the correct value of 'alfa': %q", charToString) if err == nil {
} t.Errorf("Character to string conversion succeeded but was expected to fail. Given: %v, returned: %v",
}) testCase.givenVal, charToString)
t.Run("convert_0_to_ZERO", func(t *testing.T) { }
charToString, err := convertCharToName('0') } else {
if err != nil { if err != nil {
t.Fatalf("Character to string conversion failed: %v", err.Error()) t.Errorf("Character to string conversion failed: %v", err.Error())
} }
if charToString != "ZERO" { if charToString != testCase.expVal {
t.Fatalf("Converting '0' to string did not return the correct value of 'ZERO': %q", charToString) t.Errorf("Character to String conversion fail. Given: %q, expected: %q, got: %q",
} testCase.givenVal, testCase.expVal, charToString)
}) }
t.Run("convert_/_to_SLASH", func(t *testing.T) { }
charToString, err := convertCharToName('/') })
if err != nil { }
t.Fatalf("Character to string conversion failed: %v", err.Error())
} t.Run("all_chars_must_return_a_conversion_string", func(t *testing.T) {
if charToString != "SLASH" {
t.Fatalf("Converting '/' to string did not return the correct value of 'SLASH': %q", charToString)
}
})
t.Run("all_chars_convert_to_string", func(t *testing.T) {
config.useUpperCase = true config.useUpperCase = true
config.useLowerCase = true config.useLowerCase = true
config.useNumber = true config.useNumber = true
@ -299,25 +197,6 @@ func TestConvert(t *testing.T) {
}) })
} }
// Forced failures
func TestForceFailures(t *testing.T) {
t.Run("too_big_big.NewInt_value", func(t *testing.T) {
maxNum := 9223372036854775807
maxNumBigInt := big.NewInt(int64(maxNum) + 1)
if maxNumBigInt.IsUint64() {
t.Fatalf("Calling big.NewInt() with too large number expected to fail: %v", maxNumBigInt)
}
})
t.Run("negative value for big.NewInt()", func(t *testing.T) {
randNum, err := getRandNum(-20000)
if err == nil {
t.Fatalf("Calling getRandNum() with negative value is expected to fail, but returned value: %v",
randNum)
}
})
}
// Benchmark: Random number generation // Benchmark: Random number generation
func BenchmarkGetRandNum(b *testing.B) { func BenchmarkGetRandNum(b *testing.B) {
for i := 0; i < b.N; i++ { for i := 0; i < b.N; i++ {