mirror of
https://github.com/wneessen/go-mail.git
synced 2024-11-22 13:50:49 +01:00
Closes #74
This fix makes sure that generated message IDs via SetMessageID() are truly random and unique
This commit is contained in:
parent
c321aeeccc
commit
021e003e78
2 changed files with 24 additions and 4 deletions
8
msg.go
8
msg.go
|
@ -357,12 +357,12 @@ func (m *Msg) SetMessageID() {
|
|||
if err != nil {
|
||||
hn = "localhost.localdomain"
|
||||
}
|
||||
ct := time.Now().Unix()
|
||||
ct := time.Now().UnixNano()
|
||||
r := rand.New(rand.NewSource(ct))
|
||||
rn := r.Int()
|
||||
rn := r.Int63()
|
||||
pid := os.Getpid()
|
||||
|
||||
mid := fmt.Sprintf("%d.%d.%d@%s", pid, rn, ct, hn)
|
||||
cts := fmt.Sprintf("%d", ct)
|
||||
mid := fmt.Sprintf("%d.%d.%s@%s", pid, rn, cts[:15], hn)
|
||||
m.SetMessageIDWithValue(mid)
|
||||
}
|
||||
|
||||
|
|
20
msg_test.go
20
msg_test.go
|
@ -695,6 +695,26 @@ func TestMsg_SetMessageIDWithValue(t *testing.T) {
|
|||
}
|
||||
}
|
||||
|
||||
// TestMsg_SetMessageIDRandomness tests the randomness of Msg.SetMessageID methods
|
||||
func TestMsg_SetMessageIDRandomness(t *testing.T) {
|
||||
var mids []string
|
||||
for i := 0; i < 100; i++ {
|
||||
m := NewMsg()
|
||||
m.SetMessageID()
|
||||
mid := m.GetGenHeader(HeaderMessageID)
|
||||
mids = append(mids, mid[0])
|
||||
}
|
||||
c := make(map[string]int)
|
||||
for i := range mids {
|
||||
c[mids[i]]++
|
||||
}
|
||||
for k, v := range c {
|
||||
if v > 1 {
|
||||
t.Errorf("MessageID randomness not give. MessageID %q was generated %d times", k, v)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// TestMsg_FromFormat tests the FromFormat and EnvelopeFrom methods for the Msg object
|
||||
func TestMsg_FromFormat(t *testing.T) {
|
||||
tests := []struct {
|
||||
|
|
Loading…
Reference in a new issue