From aa94f070815493ba2ee67a34c174bb1744ca80f3 Mon Sep 17 00:00:00 2001 From: Roland Shoemaker Date: Wed, 6 Jan 2016 17:13:04 -0800 Subject: [PATCH] Review fixes --- mail/mailer.go | 33 +++++++++++++++++---------------- mail/mailer_test.go | 9 ++++----- 2 files changed, 21 insertions(+), 21 deletions(-) diff --git a/mail/mailer.go b/mail/mailer.go index bbb40e264..e34042878 100644 --- a/mail/mailer.go +++ b/mail/mailer.go @@ -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 } diff --git a/mail/mailer_test.go b/mail/mailer_test.go index 275d70365..2b09d7c3e 100644 --- a/mail/mailer_test.go +++ b/mail/mailer_test.go @@ -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")