Skip to content

Commit 283134b

Browse files
committed
fix ci error with golang 1.22; better tests
1 parent 27a99cd commit 283134b

File tree

4 files changed

+45
-33
lines changed

4 files changed

+45
-33
lines changed

.github/workflows/ci.yml

+1-1
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ jobs:
2525
- name: Set up Go
2626
uses: actions/setup-go@v5
2727
with:
28-
go-version: 1.21
28+
go-version: 1.22
2929

3030
# Download all the tools used in the steps that follow
3131
- name: Set up Tools

cmd/password-generator/main.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ var (
1717
flagNoAmbiguity = flag.Bool("no-ambiguity", false, "Avoid Ambiguous Characters?")
1818
flagNumChars = flag.Int("num-chars", 12, "Number of characters in Password")
1919
flagPrintIndex = flag.Bool("index", false, "Print Index Value (1-indexed)")
20-
flagSeed = flag.Int64("seed", 0, "Seed value for non-sequenced mode (ignored if zero)")
20+
flagSeed = flag.Uint64("seed", 0, "Seed value for non-sequenced mode (ignored if zero)")
2121
flagSequenced = flag.Bool("sequenced", false, "Generate passwords in a sequence")
2222
flagStartIdx = flag.String("start", "0", "Index to start from in the sequence (1-indexed)")
2323

@@ -49,8 +49,8 @@ func main() {
4949
}
5050
printIndex := *flagPrintIndex
5151
seed := *flagSeed
52-
if seed <= 0 {
53-
seed = time.Now().UnixNano()
52+
if seed == 0 {
53+
seed = uint64(time.Now().UnixNano())
5454
}
5555
startIdx, ok := new(big.Int).SetString(*flagStartIdx, 10)
5656
if !ok {
@@ -76,7 +76,7 @@ func main() {
7676
}
7777
}
7878

79-
func generateRandomPasswords(charset password.Charset, numChars int, count *big.Int, printIndex bool, seed int64) {
79+
func generateRandomPasswords(charset password.Charset, numChars int, count *big.Int, printIndex bool, seed uint64) {
8080
generator, err := password.NewGenerator(
8181
password.WithCharset(charset),
8282
password.WithLength(numChars),

password/generator.go

+21-9
Original file line numberDiff line numberDiff line change
@@ -67,17 +67,29 @@ func (g *generator) Generate() string {
6767
password := g.pool.Get().([]rune)
6868
defer g.pool.Put(password)
6969

70+
// init the filler
7071
idx := 0
71-
fillPassword := func(count int, runes []rune) {
72-
for ; count > 0 && idx < len(password); count-- {
72+
fillPassword := func(runes []rune, count int) {
73+
for ; idx < len(password) && count > 0; count-- {
7374
password[idx] = runes[g.rng.IntN(len(runes))]
7475
idx++
7576
}
7677
}
77-
fillPassword(g.minLowerCase, g.charsetCaseLower)
78-
fillPassword(g.minUpperCase, g.charsetCaseUpper)
79-
fillPassword(g.numSymbolsToGenerate(), g.charsetSymbols)
80-
fillPassword(len(password)-idx, g.charsetNonSymbols)
78+
79+
// fill it with minimum requirements first
80+
if g.minLowerCase > 0 {
81+
fillPassword(g.charsetCaseLower, g.minLowerCase)
82+
}
83+
if g.minUpperCase > 0 {
84+
fillPassword(g.charsetCaseUpper, g.minUpperCase)
85+
}
86+
if numSymbols := g.numSymbolsToGenerate(); numSymbols > 0 {
87+
fillPassword(g.charsetSymbols, numSymbols)
88+
}
89+
// fill the rest with non-symbols (as symbols has a max)
90+
if remainingChars := len(password) - idx; remainingChars > 0 {
91+
fillPassword(g.charsetNonSymbols, remainingChars)
92+
}
8193

8294
// shuffle it all
8395
g.rng.Shuffle(len(password), func(i, j int) {
@@ -119,15 +131,15 @@ func (g *generator) sanitize() (Generator, error) {
119131
if g.minUpperCase > g.numChars {
120132
return nil, ErrMinUpperCaseTooLong
121133
}
134+
if g.minSymbols > 0 && len(g.charsetSymbols) == 0 {
135+
return nil, ErrNoSymbolsInCharset
136+
}
122137
if g.minSymbols > g.numChars {
123138
return nil, ErrMinSymbolsTooLong
124139
}
125140
if g.minLowerCase+g.minUpperCase+g.minSymbols > g.numChars {
126141
return nil, ErrRequirementsNotMet
127142
}
128-
if g.minSymbols > 0 && len(g.charsetSymbols) == 0 {
129-
return nil, ErrNoSymbolsInCharset
130-
}
131143
return g, nil
132144
}
133145

password/generator_test.go

+19-19
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ package password
22

33
import (
44
"fmt"
5-
"strings"
5+
"slices"
66
"testing"
77
"unicode"
88

@@ -42,19 +42,19 @@ func TestGenerator_Generate(t *testing.T) {
4242
"uCFmDFDAoLZY",
4343
"pMgNoVa9z5Vv",
4444
}
45-
sb := strings.Builder{}
45+
var actualPasswords []string
4646
for idx := 0; idx < 100; idx++ {
4747
password := g.Generate()
4848
assert.NotEmpty(t, password)
4949
if idx < len(expectedPasswords) {
50+
actualPasswords = append(actualPasswords, password)
5051
assert.Equal(t, expectedPasswords[idx], password)
51-
if expectedPasswords[idx] != password {
52-
sb.WriteString(fmt.Sprintf("%#v,\n", password))
53-
}
5452
}
5553
}
56-
if sb.Len() > 0 {
57-
fmt.Println(sb.String())
54+
if !slices.Equal(expectedPasswords, actualPasswords) {
55+
for _, pw := range actualPasswords {
56+
fmt.Printf("%#v,\n", pw)
57+
}
5858
}
5959
}
6060

@@ -81,15 +81,13 @@ func TestGenerator_Generate_WithAMixOfEverything(t *testing.T) {
8181
"bVrPjBRC<bqy",
8282
"f?orrWDzVYjx",
8383
}
84-
sb := strings.Builder{}
84+
var actualPasswords []string
8585
for idx := 0; idx < 100; idx++ {
8686
password := g.Generate()
8787
assert.NotEmpty(t, password)
8888
if idx < len(expectedPasswords) {
89+
actualPasswords = append(actualPasswords, password)
8990
assert.Equal(t, expectedPasswords[idx], password)
90-
if expectedPasswords[idx] != password {
91-
sb.WriteString(fmt.Sprintf("%#v,\n", password))
92-
}
9391
}
9492

9593
numLowerCase := len(filterRunes([]rune(password), unicode.IsLower))
@@ -99,8 +97,10 @@ func TestGenerator_Generate_WithAMixOfEverything(t *testing.T) {
9997
numSymbols := len(filterRunes([]rune(password), Symbols.Contains))
10098
assert.True(t, numSymbols == 1, password)
10199
}
102-
if sb.Len() > 0 {
103-
fmt.Println(sb.String())
100+
if !slices.Equal(expectedPasswords, actualPasswords) {
101+
for _, pw := range actualPasswords {
102+
fmt.Printf("%#v,\n", pw)
103+
}
104104
}
105105
}
106106

@@ -126,22 +126,22 @@ func TestGenerator_Generate_WithSymbols(t *testing.T) {
126126
"-e3a6cda4#!1",
127127
"162e6bb#ee53",
128128
}
129-
sb := strings.Builder{}
129+
var actualPasswords []string
130130
for idx := 0; idx < 100; idx++ {
131131
password := g.Generate()
132132
assert.NotEmpty(t, password)
133133
if idx < len(expectedPasswords) {
134+
actualPasswords = append(actualPasswords, password)
134135
assert.Equal(t, expectedPasswords[idx], password)
135-
if expectedPasswords[idx] != password {
136-
sb.WriteString(fmt.Sprintf("%#v,\n", password))
137-
}
138136
}
139137

140138
numSymbols := getNumSymbols(password)
141139
assert.True(t, numSymbols >= 0 && numSymbols <= 3, password)
142140
}
143-
if sb.Len() > 0 {
144-
fmt.Println(sb.String())
141+
if !slices.Equal(expectedPasswords, actualPasswords) {
142+
for _, pw := range actualPasswords {
143+
fmt.Printf("%#v,\n", pw)
144+
}
145145
}
146146
})
147147

0 commit comments

Comments
 (0)