Skip to content

Commit

Permalink
Tweak unit tests to increase code coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
mojotx committed Mar 10, 2024
1 parent b1a0098 commit dfe6248
Show file tree
Hide file tree
Showing 3 changed files with 183 additions and 62 deletions.
5 changes: 3 additions & 2 deletions pkg/CharVomit/cv.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,9 @@ const (
AllLowerCase = "abcdefghijklmnopqrstuvwxyz"
AllDigits = "0123456789"
DefaultSymbols = "!#%+:=?@"
DefaultChars = "!#%+23456789:=?@ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
WeakChars = "23456789ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
// cSpell:disable-next-line
DefaultChars = "!#%+23456789:=?@ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
WeakChars = "23456789ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
)

type CharVomit struct {
Expand Down
153 changes: 93 additions & 60 deletions pkg/CharVomit/cv_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ func TestCharVomit_Puke(t *testing.T) {
if err != nil {
t.Errorf("iteration i=%d err=%s", i, err.Error())
}
//assert.Nilf(t, err, "iteration i=%d err=%s", i, err.Error())

if len(pw) != i {
t.Errorf("i=%d: len(pw) = %d", i, len(pw))
Expand All @@ -41,15 +42,11 @@ func TestCharVomit_Puke(t *testing.T) {

// This should return error
_, err := cv.Puke(0)
if err == nil {
t.Errorf("err was nil for Puke(0)")
}
assert.NotNil(t, err, "err was nil for Puke(0)")

// Try negative
_, err = cv.Puke(-1)
if err == nil {
t.Errorf("err was nil for Puke(-1)")
}
assert.NotNil(t, err, "err was nil for Puke(-1)")
}
}

Expand Down Expand Up @@ -102,7 +99,7 @@ func TestWeakChars(t *testing.T) {

pw, err := cv.Puke(99)
if err != nil {
t.Errorf("Received error digit puker: %s", err.Error())
t.Errorf("Received error weak puker: %s", err.Error())
}

for i, c := range pw {
Expand All @@ -117,7 +114,7 @@ func TestDefaults(t *testing.T) {

pw, err := cv.Puke(99)
if err != nil {
t.Errorf("Received error digit puker: %s", err.Error())
t.Errorf("Received error default puker: %s", err.Error())
}

for i, c := range pw {
Expand All @@ -128,67 +125,103 @@ func TestDefaults(t *testing.T) {
}

func TestRemoveExcluded(t *testing.T) {
// Set up a CharVomit instance with a predefined set of acceptable characters.
cv := NewCharVomit(DefaultChars)

// Define a configuration that specifies characters to exclude.
config := arg.ConfigType{
PasswordLen: 18,
Digits: true,
ShowHelp: false,
LowerCase: true,
Symbols: true,
UpperCase: true,
WeakChars: false,
Version: false,
Excluded: "@?+=01oOl",
Excluded: "0O1l",
}

puker := NewCharVomit("")
// Call RemoveExcluded with the configuration.
err := cv.RemoveExcluded(config)

if err := puker.SetAcceptableChars(config); err != nil {
t.Errorf("cannot call config.SetAcceptableChars(): %s", err.Error())
// Check if an error occurred.
if err != nil {
t.Errorf("RemoveExcluded() returned an error: %v", err)
}

if err := puker.RemoveExcluded(config); err != nil {
t.Errorf("cannot call config.RemoveExcluded(): %s", err.Error())
// Check if the excluded characters have been removed from AcceptableChars.
// cSpell:disable-next-line
expectedChars := "!#%+23456789:=?@ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
if cv.AcceptableChars != expectedChars {
t.Errorf("RemoveExcluded() did not remove the expected characters. Expected: %s, Got: %s", expectedChars, cv.AcceptableChars)
}

assert.NotEmpty(t, puker.AcceptableChars, "must not be empty")
assert.NotNil(t, puker.AcceptableChars, "must not be nil")
assert.Equal(t, puker.AcceptableChars, "23456789ABCDEFGHIJKLMNPQRSTUVWXYZabcdefghijkmnpqrstuvwxyz!#%:", "test error messaige")

// Test weak with symbols
config.WeakChars = true
config.Symbols = true
if err := puker.SetAcceptableChars(config); err == nil {
t.Error("expected error not found, symbols + weak")
// Test with an empty Excluded string to ensure no error is returned.
config.Excluded = ""
err = cv.RemoveExcluded(config)
if err != nil {
t.Errorf("RemoveExcluded() returned an error when Excluded was empty: %s", err.Error())
}

// Test weak redundancy
config.WeakChars = true
config.Digits = true
config.UpperCase = true
config.LowerCase = true
config.Symbols = false
if err := puker.SetAcceptableChars(config); err == nil {
t.Error("expected error not found, weak + redundant")
}
t.Logf("\nPUKER: %+v\n\n", puker)
// Test with all characters excluded to ensure an error is returned.
config.Excluded = DefaultChars
err = cv.RemoveExcluded(config)
assert.NotNil(t, err, "RemoveExcluded() did not return an error when all characters were excluded")
}

// Test just weak
config = arg.ConfigType{
PasswordLen: 18,
Digits: false,
ShowHelp: false,
LowerCase: false,
Symbols: false,
UpperCase: false,
WeakChars: true,
Version: false,
Excluded: "",
}
if err := puker.SetAcceptableChars(config); err != nil {
t.Errorf("unexpected error: %s", err.Error())
}
if err := puker.RemoveExcluded(config); err != nil {
t.Errorf("cannot call config.RemoveExcluded(): %s", err.Error())
}
assert.Equal(t, puker.AcceptableChars, WeakChars, "acceptable chars should be weak")
func TestSetAcceptableChars(t *testing.T) {
t.Run("Default configuration", func(t *testing.T) {
cv := NewCharVomit("")
config := arg.ConfigType{}
err := cv.SetAcceptableChars(config)
assert.NoError(t, err)
assert.Equal(t, DefaultChars, cv.AcceptableChars)
})

t.Run("Weak characters only", func(t *testing.T) {
cv := NewCharVomit("")
config := arg.ConfigType{
WeakChars: true,
}
err := cv.SetAcceptableChars(config)
assert.NoError(t, err)
assert.Equal(t, WeakChars, cv.AcceptableChars)
})

t.Run("Digits, UpperCase, and LowerCase", func(t *testing.T) {
cv := NewCharVomit("")
config := arg.ConfigType{
Digits: true,
UpperCase: true,
LowerCase: true,
}
err := cv.SetAcceptableChars(config)
assert.NoError(t, err)
expectedChars := AllDigits + AllUpperCase + AllLowerCase
assert.Equal(t, expectedChars, cv.AcceptableChars)
})

t.Run("Symbols only", func(t *testing.T) {
cv := NewCharVomit("")
config := arg.ConfigType{
Symbols: true,
}
err := cv.SetAcceptableChars(config)
assert.NoError(t, err)
assert.Equal(t, DefaultSymbols, cv.AcceptableChars)
})

t.Run("Weak characters with other flags", func(t *testing.T) {
cv := NewCharVomit("")
config := arg.ConfigType{
WeakChars: true,
Digits: true,
}
err := cv.SetAcceptableChars(config)
assert.Error(t, err)
})

t.Run("Excluded characters", func(t *testing.T) {
cv := NewCharVomit("")
config := arg.ConfigType{
Excluded: "0O1l",
}
err := cv.SetAcceptableChars(config)
assert.NoError(t, err)
// cSpell:disable-next-line
expectedChars := "!#%+23456789:=?@ABCDEFGHJKLMNPRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
assert.Equal(t, expectedChars, cv.AcceptableChars)
})
}
87 changes: 87 additions & 0 deletions pkg/arg/parse_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -193,3 +193,90 @@ func TestParseVersion(t *testing.T) {
t.Errorf("invalid version, wanted '%s' got '%s'", Version(), version)
}
}

func TestParse(t *testing.T) {
// Save the original command-line arguments and restore them after the test.
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

// Set up a mock command-line input.
os.Args = []string{"CharVomit", "-u", "-l", "-d", "-s", "-w", "-x", "0O1l", "8"}

// Create a new FlagSet to simulate command-line arguments.
fs := flag.NewFlagSet("CharVomit", flag.ExitOnError)

// Call the Parse function with the mock FlagSet.
exitAfter, rc := Parse(fs)

// Check if the function exits as expected.
if exitAfter {
t.Errorf("Parse() exited unexpectedly")
}

// Check if the return code is as expected.
if rc != 0 {
t.Errorf("Parse() returned unexpected return code: %d", rc)
}

// Check if the parsed configuration matches the expected values.
expectedConfig := ConfigType{
PasswordLen: 8,
Digits: true,
ShowHelp: false,
LowerCase: true,
Symbols: true,
UpperCase: true,
WeakChars: true,
Version: false,
Excluded: "0O1l",
}

if Config != expectedConfig {
t.Errorf("Parse() parsed configuration does not match expected values, wanted '%+v' got '%+v'", expectedConfig, Config)
}
}

func TestParseHelp(t *testing.T) {
// Save the original command-line arguments and restore them after the test.
oldArgs := os.Args
defer func() { os.Args = oldArgs }()

// Initialize Config
Config = ConfigType{}

// Set up a mock command-line input.
os.Args = []string{"CharVomit", "-h"}

// Create a new FlagSet to simulate command-line arguments.
fs := flag.NewFlagSet("CharVomit", flag.ExitOnError)

// Call the Parse function with the mock FlagSet.
exitAfter, rc := Parse(fs)

// Check if the function exits as expected.
if !exitAfter {
t.Errorf("Parse() did not exit as expected")
}

// Check if the return code is as expected.
if rc != 0 {
t.Errorf("Parse() returned unexpected return code: %d", rc)
}

// Check if the parsed configuration matches the expected values.
expectedConfig := ConfigType{
PasswordLen: 0,
Digits: false,
ShowHelp: true,
LowerCase: false,
Symbols: false,
UpperCase: false,
WeakChars: false,
Version: false,
Excluded: "",
}

if Config != expectedConfig {
t.Errorf("Parse() parsed configuration does not match expected values, wanted '%+v' got '%+v'", expectedConfig, Config)
}
}

0 comments on commit dfe6248

Please sign in to comment.