Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

More test improvements #352

Merged
merged 5 commits into from
Nov 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -50,7 +50,7 @@ jobs:
check-latest: true
- name: Install sendmail
run: |
sudo apt-get -y update >/dev/null && sudo apt-get -y upgrade >/dev/null && sudo DEBIAN_FRONTEND=noninteractive apt-get -y install nullmailer >/dev/null && which sendmail
sudo apt-get -y update && sudo DEBIAN_FRONTEND=noninteractive apt-get -y install nullmailer && which sendmail
- name: Run go test
if: success()
run: |
Expand Down
86 changes: 61 additions & 25 deletions random_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,45 +5,81 @@
package mail

import (
"crypto/rand"
"errors"
"strings"
"testing"
)

// TestRandomStringSecure tests the randomStringSecure method
func TestRandomStringSecure(t *testing.T) {
tt := []struct {
testName string
length int
mustNotMatch string
}{
{"20 chars", 20, "'"},
{"100 chars", 100, "'"},
{"1000 chars", 1000, "'"},
}
t.Run("randomStringSecure with varying length", func(t *testing.T) {
tt := []struct {
testName string
length int
mustNotMatch string
}{
{"20 chars", 20, "'"},
{"100 chars", 100, "'"},
{"1000 chars", 1000, "'"},
}

for _, tc := range tt {
t.Run(tc.testName, func(t *testing.T) {
rs, err := randomStringSecure(tc.length)
if err != nil {
t.Errorf("random string generation failed: %s", err)
}
if strings.Contains(rs, tc.mustNotMatch) {
t.Errorf("random string contains unexpected character. got: %s, not-expected: %s",
rs, tc.mustNotMatch)
}
if len(rs) != tc.length {
t.Errorf("random string length does not match. expected: %d, got: %d", tc.length, len(rs))
}
})
}
for _, tc := range tt {
t.Run(tc.testName, func(t *testing.T) {
rs, err := randomStringSecure(tc.length)
if err != nil {
t.Errorf("random string generation failed: %s", err)
}
if strings.Contains(rs, tc.mustNotMatch) {
t.Errorf("random string contains unexpected character. got: %s, not-expected: %s",
rs, tc.mustNotMatch)
}
if len(rs) != tc.length {
t.Errorf("random string length does not match. expected: %d, got: %d", tc.length, len(rs))
}
})
}
})
t.Run("randomStringSecure fails on broken rand Reader (first read)", func(t *testing.T) {
defaultRandReader := rand.Reader
t.Cleanup(func() { rand.Reader = defaultRandReader })
rand.Reader = &randReader{failon: 1}
if _, err := randomStringSecure(22); err == nil {
t.Fatalf("expected failure on broken rand Reader")
}
})
t.Run("randomStringSecure fails on broken rand Reader (second read)", func(t *testing.T) {
defaultRandReader := rand.Reader
t.Cleanup(func() { rand.Reader = defaultRandReader })
rand.Reader = &randReader{failon: 0}
if _, err := randomStringSecure(22); err == nil {
t.Fatalf("expected failure on broken rand Reader")
}
})
}

func BenchmarkGenerator_RandomStringSecure(b *testing.B) {
b.ReportAllocs()
for i := 0; i < b.N; i++ {
_, err := randomStringSecure(22)
_, err := randomStringSecure(10)
if err != nil {
b.Errorf("RandomStringFromCharRange() failed: %s", err)
}
}
}

// randReader is type that satisfies the io.Reader interface. It can fail on a specific read
// operations and is therefore useful to test consecutive reads with errors
type randReader struct {
failon uint8
call uint8
}

// Read implements the io.Reader interface for the randReader type
func (r *randReader) Read(p []byte) (int, error) {
if r.call == r.failon {
r.call++
return len(p), nil
}
return 0, errors.New("broken reader")
}
4 changes: 2 additions & 2 deletions senderror.go
Original file line number Diff line number Diff line change
Expand Up @@ -81,7 +81,7 @@ type SendErrReason int
// Returns:
// - A string representing the error message.
func (e *SendError) Error() string {
if e.Reason > 10 {
if e.Reason > ErrAmbiguous {
return "unknown reason"
}

Expand All @@ -93,7 +93,7 @@ func (e *SendError) Error() string {
errMessage.WriteRune(' ')
errMessage.WriteString(e.errlist[i].Error())
if i != len(e.errlist)-1 {
errMessage.WriteString(", ")
errMessage.WriteString(",")
}
}
}
Expand Down
Loading
Loading