mirror of
https://github.com/wneessen/apg-go.git
synced 2024-11-09 15:52:54 +01:00
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:
parent
8dce4d2eb9
commit
86ca627fed
2 changed files with 53 additions and 16 deletions
39
config.go
39
config.go
|
@ -48,8 +48,8 @@ type Option func(*Config)
|
||||||
|
|
||||||
// NewConfig creates a new Config instance and pre-fills it with sane
|
// NewConfig creates a new Config instance and pre-fills it with sane
|
||||||
// default settings. The Config is returned as pointer value
|
// default settings. The Config is returned as pointer value
|
||||||
func NewConfig(o ...Option) *Config {
|
func NewConfig(opts ...Option) *Config {
|
||||||
c := &Config{
|
config := &Config{
|
||||||
MaxLength: DefaultMaxLength,
|
MaxLength: DefaultMaxLength,
|
||||||
MinLength: DefaultMinLength,
|
MinLength: DefaultMinLength,
|
||||||
Mode: DefaultMode,
|
Mode: DefaultMode,
|
||||||
|
@ -57,32 +57,39 @@ func NewConfig(o ...Option) *Config {
|
||||||
}
|
}
|
||||||
|
|
||||||
// Override defaults with optionally provided config.Option functions
|
// Override defaults with optionally provided config.Option functions
|
||||||
for _, co := range o {
|
for _, opt := range opts {
|
||||||
if co == nil {
|
if opt == nil {
|
||||||
continue
|
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
|
// WithMinLength overrides the minimum password length
|
||||||
func WithMinLength(l int64) Option {
|
func WithMinLength(length int64) Option {
|
||||||
return func(c *Config) {
|
return func(config *Config) {
|
||||||
c.MinLength = l
|
config.MinLength = length
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithMaxLength overrides the maximum password length
|
// WithMaxLength overrides the maximum password length
|
||||||
func WithMaxLength(l int64) Option {
|
func WithMaxLength(length int64) Option {
|
||||||
return func(c *Config) {
|
return func(config *Config) {
|
||||||
c.MaxLength = l
|
config.MaxLength = length
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// WithNumberPass overrides the number of generated passwords setting
|
// WithNumberPass overrides the amount of generated passwords setting
|
||||||
func WithNumberPass(n int64) Option {
|
func WithNumberPass(amount int64) Option {
|
||||||
return func(c *Config) {
|
return func(config *Config) {
|
||||||
c.NumberPass = n
|
config.NumberPass = amount
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
@ -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) {
|
func TestWithMaxLength(t *testing.T) {
|
||||||
var e int64 = 123
|
var e int64 = 123
|
||||||
c := NewConfig(WithMaxLength(e))
|
c := NewConfig(WithMaxLength(e))
|
||||||
|
|
Loading…
Reference in a new issue