Add password generation algorithm option and corresponding tests

The password generation algorithm is now customizable via the `WithAlgorithm()` function, giving users more control over password generation. Tests have been added to 'config_test.go' to ensure correct functionality of the new option. Additionally, the variable naming in 'config.go' has been improved for better readability.
This commit is contained in:
Winni Neessen 2024-03-07 21:16:04 +01:00
parent 8dce4d2eb9
commit 86ca627fed
Signed by: wneessen
GPG key ID: 5F3AF39B820C119D
2 changed files with 53 additions and 16 deletions

View file

@ -48,8 +48,8 @@ type Option func(*Config)
// NewConfig creates a new Config instance and pre-fills it with sane
// default settings. The Config is returned as pointer value
func NewConfig(o ...Option) *Config {
c := &Config{
func NewConfig(opts ...Option) *Config {
config := &Config{
MaxLength: DefaultMaxLength,
MinLength: DefaultMinLength,
Mode: DefaultMode,
@ -57,32 +57,39 @@ func NewConfig(o ...Option) *Config {
}
// Override defaults with optionally provided config.Option functions
for _, co := range o {
if co == nil {
for _, opt := range opts {
if opt == nil {
continue
}
co(c)
opt(config)
}
return config
}
// WithAlgorithm overrides the algorithm mode for the password generation
func WithAlgorithm(algo Algorithm) Option {
return func(config *Config) {
config.Algorithm = algo
}
return c
}
// WithMinLength overrides the minimum password length
func WithMinLength(l int64) Option {
return func(c *Config) {
c.MinLength = l
func WithMinLength(length int64) Option {
return func(config *Config) {
config.MinLength = length
}
}
// WithMaxLength overrides the maximum password length
func WithMaxLength(l int64) Option {
return func(c *Config) {
c.MaxLength = l
func WithMaxLength(length int64) Option {
return func(config *Config) {
config.MaxLength = length
}
}
// WithNumberPass overrides the number of generated passwords setting
func WithNumberPass(n int64) Option {
return func(c *Config) {
c.NumberPass = n
// WithNumberPass overrides the amount of generated passwords setting
func WithNumberPass(amount int64) Option {
return func(config *Config) {
config.NumberPass = amount
}
}

View file

@ -29,6 +29,36 @@ func TestNewConfig(t *testing.T) {
}
}
func TestWithAlgorithm(t *testing.T) {
tests := []struct {
name string
algo Algorithm
want int
}{
{"Pronouncble passwords", AlgoPronouncable, 0},
{"Random passwords", AlgoRandom, 1},
{"Coinflip", AlgoCoinFlip, 2},
{"Unsupported", AlgoUnsupported, 3},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
c := NewConfig(WithAlgorithm(tt.algo))
if c == nil {
t.Errorf("NewConfig(WithAlgorithm()) failed, expected config pointer but got nil")
return
}
if c.Algorithm != tt.algo {
t.Errorf("NewConfig(WithAlgorithm()) failed, expected algo: %d, got: %d",
tt.algo, c.Algorithm)
}
if IntToAlgo(tt.want) != c.Algorithm {
t.Errorf("IntToAlgo() failed, expected algo: %d, got: %d",
tt.want, c.Algorithm)
}
})
}
}
func TestWithMaxLength(t *testing.T) {
var e int64 = 123
c := NewConfig(WithMaxLength(e))