| 1 |
package smtp |
| 2 |
|
| 3 |
import ( |
| 4 |
"net/smtp" |
| 5 |
|
| 6 |
ginkgo "github.com/onsi/ginkgo/v2" |
| 7 |
gomega "github.com/onsi/gomega" |
| 8 |
) |
| 9 |
|
| 10 |
var _ = ginkgo.Describe("loginAuth", func() { |
| 11 |
ginkgo.Describe("newLoginAuth", func() { |
| 12 |
ginkgo.It("should store credentials and reset the response step", func() { |
| 13 |
got := newLoginAuth("alice", "s3cret", "mail.example.com") |
| 14 |
auth, ok := got.(*loginAuth) |
| 15 |
gomega.Expect(ok).To(gomega.BeTrue()) |
| 16 |
gomega.Expect(auth.username).To(gomega.Equal("alice")) |
| 17 |
gomega.Expect(auth.password).To(gomega.Equal("s3cret")) |
| 18 |
gomega.Expect(auth.host).To(gomega.Equal("mail.example.com")) |
| 19 |
gomega.Expect(auth.respStep).To(gomega.BeZero()) |
| 20 |
}) |
| 21 |
}) |
| 22 |
|
| 23 |
ginkgo.Describe("Start", func() { |
| 24 |
ginkgo.It("should accept TLS and a matching host", func() { |
| 25 |
auth := newLoginAuth("user", "pass", "mail.example.com") |
| 26 |
mech, resp, err := auth.Start(&smtp.ServerInfo{Name: "mail.example.com", TLS: true}) |
| 27 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 28 |
gomega.Expect(mech).To(gomega.Equal("LOGIN")) |
| 29 |
gomega.Expect(resp).To(gomega.BeNil()) |
| 30 |
}) |
| 31 |
|
| 32 |
ginkgo.It("should accept localhost without TLS", func() { |
| 33 |
auth := newLoginAuth("user", "pass", "localhost") |
| 34 |
_, _, err := auth.Start(&smtp.ServerInfo{Name: "localhost", TLS: false}) |
| 35 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 36 |
}) |
| 37 |
|
| 38 |
ginkgo.It("should accept 127.0.0.1 without TLS", func() { |
| 39 |
auth := newLoginAuth("user", "pass", "127.0.0.1") |
| 40 |
_, _, err := auth.Start(&smtp.ServerInfo{Name: "127.0.0.1", TLS: false}) |
| 41 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 42 |
}) |
| 43 |
|
| 44 |
ginkgo.It("should accept ::1 without TLS", func() { |
| 45 |
auth := newLoginAuth("user", "pass", "::1") |
| 46 |
_, _, err := auth.Start(&smtp.ServerInfo{Name: "::1", TLS: false}) |
| 47 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 48 |
}) |
| 49 |
|
| 50 |
ginkgo.It("should reject an unencrypted remote server", func() { |
| 51 |
auth := newLoginAuth("user", "pass", "mail.example.com") |
| 52 |
_, _, err := auth.Start(&smtp.ServerInfo{Name: "mail.example.com", TLS: false}) |
| 53 |
gomega.Expect(err).To(gomega.MatchError(errUnencryptedConnection)) |
| 54 |
}) |
| 55 |
|
| 56 |
ginkgo.It("should reject a mismatched host", func() { |
| 57 |
auth := newLoginAuth("user", "pass", "mail.example.com") |
| 58 |
_, _, err := auth.Start(&smtp.ServerInfo{Name: "evil.example.com", TLS: true}) |
| 59 |
gomega.Expect(err).To(gomega.MatchError(errWrongHostName)) |
| 60 |
}) |
| 61 |
|
| 62 |
ginkgo.It("should reset respStep so a later Next starts at the username", func() { |
| 63 |
auth := newLoginAuth("u", "p", "localhost").(*loginAuth) |
| 64 |
_, err := auth.Next(nil, true) |
| 65 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 66 |
|
| 67 |
_, _, err = auth.Start(&smtp.ServerInfo{Name: "localhost", TLS: false}) |
| 68 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 69 |
gomega.Expect(auth.respStep).To(gomega.BeZero()) |
| 70 |
|
| 71 |
resp, err := auth.Next([]byte("Username:"), true) |
| 72 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 73 |
gomega.Expect(string(resp)).To(gomega.Equal("u")) |
| 74 |
}) |
| 75 |
}) |
| 76 |
|
| 77 |
ginkgo.Describe("Next", func() { |
| 78 |
ginkgo.It("should send the username then the password and reject extra challenges", func() { |
| 79 |
auth := newLoginAuth("alice", "s3cret", "mail.example.com").(*loginAuth) |
| 80 |
|
| 81 |
resp, err := auth.Next([]byte("User Name\x00"), true) |
| 82 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 83 |
gomega.Expect(string(resp)).To(gomega.Equal("alice")) |
| 84 |
|
| 85 |
resp, err = auth.Next([]byte("password:"), true) |
| 86 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 87 |
gomega.Expect(string(resp)).To(gomega.Equal("s3cret")) |
| 88 |
|
| 89 |
resp, err = auth.Next([]byte("Username:"), true) |
| 90 |
gomega.Expect(err).To(gomega.MatchError(errUnexpectedServerChallenge)) |
| 91 |
gomega.Expect(resp).To(gomega.BeNil()) |
| 92 |
gomega.Expect(err.Error()).To(gomega.ContainSubstring("Username:")) |
| 93 |
|
| 94 |
resp, err = auth.Next(nil, false) |
| 95 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 96 |
gomega.Expect(resp).To(gomega.BeNil()) |
| 97 |
}) |
| 98 |
|
| 99 |
ginkgo.It("should send passwords that contain spaces and unicode", func() { |
| 100 |
auth := newLoginAuth("alice", "päss word", "mail.example.com").(*loginAuth) |
| 101 |
|
| 102 |
resp, err := auth.Next(nil, true) |
| 103 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 104 |
gomega.Expect(string(resp)).To(gomega.Equal("alice")) |
| 105 |
|
| 106 |
resp, err = auth.Next(nil, true) |
| 107 |
gomega.Expect(err).NotTo(gomega.HaveOccurred()) |
| 108 |
gomega.Expect(string(resp)).To(gomega.Equal("päss word")) |
| 109 |
}) |
| 110 |
}) |
| 111 |
|
| 112 |
ginkgo.Describe("isLocalhost", func() { |
| 113 |
ginkgo.It("should recognize loopback identities", func() { |
| 114 |
gomega.Expect(isLocalhost("localhost")).To(gomega.BeTrue()) |
| 115 |
gomega.Expect(isLocalhost("127.0.0.1")).To(gomega.BeTrue()) |
| 116 |
gomega.Expect(isLocalhost("::1")).To(gomega.BeTrue()) |
| 117 |
}) |
| 118 |
|
| 119 |
ginkgo.It("should reject a remote hostname", func() { |
| 120 |
gomega.Expect(isLocalhost("example.com")).To(gomega.BeFalse()) |
| 121 |
gomega.Expect(isLocalhost("")).To(gomega.BeFalse()) |
| 122 |
}) |
| 123 |
}) |
| 124 |
}) |