Review fixes

This commit is contained in:
Roland Shoemaker 2016-01-06 17:13:04 -08:00
parent 95f5a5b0a0
commit aa94f07081
2 changed files with 21 additions and 21 deletions

View File

@ -9,7 +9,6 @@ import (
"bytes"
"crypto/rand"
"fmt"
"io"
"math"
"math/big"
"mime/quotedprintable"
@ -17,18 +16,25 @@ import (
"net/smtp"
"strconv"
"strings"
"time"
"github.com/letsencrypt/boulder/Godeps/_workspace/src/github.com/jmhodges/clock"
)
type csprg interface {
Int(io.Reader, *big.Int) (*big.Int, error)
type idGenerator interface {
generate() *big.Int
}
var maxBigInt = big.NewInt(math.MaxInt64)
type realSource struct{}
func (s realSource) Int(reader io.Reader, max *big.Int) (*big.Int, error) {
return rand.Int(reader, max)
func (s realSource) generate() *big.Int {
randInt, err := rand.Int(rand.Reader, maxBigInt)
if err != nil {
panic(err)
}
return randInt
}
// Mailer provides the interface for a mailer
@ -43,7 +49,7 @@ type MailerImpl struct {
Auth smtp.Auth
From string
clk clock.Clock
csprgSource csprg
csprgSource idGenerator
}
// New constructs a Mailer to represent an account on a particular mail
@ -60,26 +66,21 @@ func New(server, port, username, password string) MailerImpl {
}
}
var maxBigInt = big.NewInt(math.MaxInt64) // ???
func (m *MailerImpl) generateMessage(to []string, subject, body string) ([]byte, error) {
mid, err := m.csprgSource.Int(rand.Reader, maxBigInt)
if err != nil {
return nil, err
}
mid := m.csprgSource.generate()
now := m.clk.Now().UTC()
addrs := []string{}
for _, a := range to {
addrs = append(addrs, strconv.QuoteToASCII(a))
addrs = append(addrs, strconv.Quote(a))
}
headers := []string{
fmt.Sprintf("To: %s", strings.Join(addrs, ", ")),
fmt.Sprintf("From: %s", m.From),
fmt.Sprintf("Subject: %s", subject),
fmt.Sprintf("Date: %s", now.Format("Mon Jan 2 2006 15:04:05 -0700")),
fmt.Sprintf("Date: %s", now.Format(time.RFC822)),
fmt.Sprintf("Message-Id: <%s.%s.%s>", now.Format("20060102T150405"), mid.String(), m.From),
"MIME-Version: 1.0",
"Content-Type: text/plain",
"Content-Type: text/plain; charset=UTF-8",
"Content-Transfer-Encoding: quoted-printable",
}
for i := range headers[1:] {
@ -88,7 +89,7 @@ func (m *MailerImpl) generateMessage(to []string, subject, body string) ([]byte,
}
bodyBuf := new(bytes.Buffer)
mimeWriter := quotedprintable.NewWriter(bodyBuf)
_, err = mimeWriter.Write([]byte(body))
_, err := mimeWriter.Write([]byte(body))
if err != nil {
return nil, err
}

View File

@ -7,7 +7,6 @@ package mail
import (
"fmt"
"io"
"math/big"
"strings"
"testing"
@ -19,8 +18,8 @@ import (
type fakeSource struct{}
func (f fakeSource) Int(reader io.Reader, max *big.Int) (*big.Int, error) {
return big.NewInt(1991), nil
func (f fakeSource) generate() *big.Int {
return big.NewInt(1991)
}
func TestGenerateMessage(t *testing.T) {
@ -35,10 +34,10 @@ func TestGenerateMessage(t *testing.T) {
test.AssertEquals(t, fields[0], "To: \"recv@email.com\"")
test.AssertEquals(t, fields[1], "From: send@email.com")
test.AssertEquals(t, fields[2], "Subject: test subject")
test.AssertEquals(t, fields[3], "Date: Thu Jan 1 1970 00:00:00 +0000")
test.AssertEquals(t, fields[3], "Date: 01 Jan 70 00:00 UTC")
test.AssertEquals(t, fields[4], "Message-Id: <19700101T000000.1991.send@email.com>")
test.AssertEquals(t, fields[5], "MIME-Version: 1.0")
test.AssertEquals(t, fields[6], "Content-Type: text/plain")
test.AssertEquals(t, fields[6], "Content-Type: text/plain; charset=UTF-8")
test.AssertEquals(t, fields[7], "Content-Transfer-Encoding: quoted-printable")
test.AssertEquals(t, fields[8], "")
test.AssertEquals(t, fields[9], "this is the body")