diff options
Diffstat (limited to 'libgo/go/net/smtp')
-rw-r--r-- | libgo/go/net/smtp/auth.go | 11 | ||||
-rw-r--r-- | libgo/go/net/smtp/smtp_test.go | 35 |
2 files changed, 45 insertions, 1 deletions
diff --git a/libgo/go/net/smtp/auth.go b/libgo/go/net/smtp/auth.go index d401e3c..3f1339e 100644 --- a/libgo/go/net/smtp/auth.go +++ b/libgo/go/net/smtp/auth.go @@ -54,7 +54,16 @@ func PlainAuth(identity, username, password, host string) Auth { func (a *plainAuth) Start(server *ServerInfo) (string, []byte, error) { if !server.TLS { - return "", nil, errors.New("unencrypted connection") + advertised := false + for _, mechanism := range server.Auth { + if mechanism == "PLAIN" { + advertised = true + break + } + } + if !advertised { + return "", nil, errors.New("unencrypted connection") + } } if server.Name != a.host { return "", nil, errors.New("wrong host name") diff --git a/libgo/go/net/smtp/smtp_test.go b/libgo/go/net/smtp/smtp_test.go index 8317428..c190b32 100644 --- a/libgo/go/net/smtp/smtp_test.go +++ b/libgo/go/net/smtp/smtp_test.go @@ -57,6 +57,41 @@ testLoop: } } +func TestAuthPlain(t *testing.T) { + auth := PlainAuth("foo", "bar", "baz", "servername") + + tests := []struct { + server *ServerInfo + err string + }{ + { + server: &ServerInfo{Name: "servername", TLS: true}, + }, + { + // Okay; explicitly advertised by server. + server: &ServerInfo{Name: "servername", Auth: []string{"PLAIN"}}, + }, + { + server: &ServerInfo{Name: "servername", Auth: []string{"CRAM-MD5"}}, + err: "unencrypted connection", + }, + { + server: &ServerInfo{Name: "attacker", TLS: true}, + err: "wrong host name", + }, + } + for i, tt := range tests { + _, _, err := auth.Start(tt.server) + got := "" + if err != nil { + got = err.Error() + } + if got != tt.err { + t.Errorf("%d. got error = %q; want %q", i, got, tt.err) + } + } +} + type faker struct { io.ReadWriter } |