-
Notifications
You must be signed in to change notification settings - Fork 100
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feature: control randomness of boundary headers (#276)
Adds a RandSeed func to mail builder, allowing for reproducible output.
- Loading branch information
Showing
7 changed files
with
172 additions
and
15 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package stringutil | ||
|
||
import ( | ||
"math/rand" | ||
"sync" | ||
"time" | ||
) | ||
|
||
var globalRandSource rand.Source | ||
|
||
func init() { | ||
globalRandSource = NewLockedSource(time.Now().UTC().UnixNano()) | ||
} | ||
|
||
// NewLockedSource creates a source of randomness using the given seed. | ||
func NewLockedSource(seed int64) rand.Source64 { | ||
return &lockedSource{ | ||
lock: new(sync.Mutex), | ||
s: rand.NewSource(seed).(rand.Source64), | ||
} | ||
} | ||
|
||
type lockedSource struct { | ||
lock sync.Locker | ||
s rand.Source64 | ||
} | ||
|
||
func (x *lockedSource) Int63() int64 { | ||
x.lock.Lock() | ||
defer x.lock.Unlock() | ||
return x.s.Int63() | ||
} | ||
|
||
func (x *lockedSource) Uint64() uint64 { | ||
x.lock.Lock() | ||
defer x.lock.Unlock() | ||
return x.s.Uint64() | ||
} | ||
|
||
func (x *lockedSource) Seed(seed int64) { | ||
x.lock.Lock() | ||
defer x.lock.Unlock() | ||
x.s.Seed(seed) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,93 @@ | ||
package enmime_test | ||
|
||
import ( | ||
"bytes" | ||
"fmt" | ||
"testing" | ||
"time" | ||
|
||
"github.com/jhillyerd/enmime" | ||
"github.com/stretchr/testify/assert" | ||
) | ||
|
||
// TestRandOption checks that different randomness modes behave as expected, relative to one another. | ||
func TestRandOption(t *testing.T) { | ||
types := []ReproducibilityMode{ZeroSource, OneSource, DefaultSource, TimestampSource} | ||
for _, a := range types { | ||
for _, b := range types { | ||
ha, hb := buildEmail(t, a), buildEmail(t, b) | ||
if a == b && a.IsReproducible() { | ||
assert.Equal(t, ha, hb) | ||
} else { | ||
assert.NotEqual(t, ha, hb) | ||
} | ||
} | ||
} | ||
} | ||
|
||
type ReproducibilityMode int | ||
|
||
const ( | ||
ZeroSource ReproducibilityMode = iota | ||
OneSource | ||
DefaultSource | ||
TimestampSource | ||
) | ||
|
||
func (mode ReproducibilityMode) IsReproducible() bool { | ||
switch mode { | ||
case ZeroSource: | ||
return true | ||
case OneSource: | ||
return true | ||
case DefaultSource: | ||
return false | ||
case TimestampSource: | ||
return false | ||
default: | ||
panic(fmt.Errorf("illegal mode: %d", mode)) | ||
} | ||
} | ||
|
||
func (mode ReproducibilityMode) String() string { | ||
switch mode { | ||
case ZeroSource: | ||
return "ZeroSource" | ||
case OneSource: | ||
return "OneSource" | ||
case DefaultSource: | ||
return "DefaultSource" | ||
case TimestampSource: | ||
return "TimestampSource" | ||
default: | ||
panic(fmt.Errorf("illegal mode: %d", mode)) | ||
} | ||
} | ||
|
||
// buildEmail creates a string email, according to the given Reproducibilitymode. | ||
func buildEmail(t *testing.T, mode ReproducibilityMode) string { | ||
t.Helper() | ||
var b enmime.MailBuilder | ||
switch mode { | ||
case ZeroSource: | ||
b = enmime.Builder().RandSeed(0) | ||
case OneSource: | ||
b = enmime.Builder().RandSeed(1) | ||
case DefaultSource: | ||
b = enmime.Builder() | ||
case TimestampSource: | ||
b = enmime.Builder().RandSeed(time.Now().UTC().UnixNano()) | ||
default: | ||
panic(fmt.Errorf("illegal mode: %d", mode)) | ||
} | ||
b = b.From("name", "same").To("anon", "anon@example.com").AddAttachment([]byte("testing"), "text/plain", "test.txt") | ||
p, err := b.Build() | ||
if err != nil { | ||
t.Fatalf("can't build email: %v", err) | ||
} | ||
w := new(bytes.Buffer) | ||
if err := p.Encode(w); err != nil { | ||
t.Fatalf("can't encode part: %v", err) | ||
} | ||
return w.String() | ||
} |