-
Notifications
You must be signed in to change notification settings - Fork 100
/
randoption_test.go
93 lines (85 loc) · 2.05 KB
/
randoption_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
package enmime_test
import (
"bytes"
"fmt"
"testing"
"time"
"github.com/jhillyerd/enmime/v2"
"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", "[email protected]").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()
}