Debian Codegraph @

pkg/services/email/smtp/smtp_login_test.go

raw symbols SCIP
1 package smtp
2
3 import (
4 "errors"
5 "net/smtp"
6 "strings"
7 "testing"
8 )
9
10 func TestLoginAuthStart(t *testing.T) {
11 t.Parallel()
12
13 tests := []struct {
14 name string
15 host string
16 server *smtp.ServerInfo
17 wantErr error
18 }{
19 {
20 name: "TLS and matching host",
21 host: "mail.example.com",
22 server: &smtp.ServerInfo{Name: "mail.example.com", TLS: true},
23 },
24 {
25 name: "localhost without TLS",
26 host: "localhost",
27 server: &smtp.ServerInfo{Name: "localhost", TLS: false},
28 },
29 {
30 name: "127.0.0.1 without TLS",
31 host: "127.0.0.1",
32 server: &smtp.ServerInfo{Name: "127.0.0.1", TLS: false},
33 },
34 {
35 name: "unencrypted remote",
36 host: "mail.example.com",
37 server: &smtp.ServerInfo{Name: "mail.example.com", TLS: false},
38 wantErr: errUnencryptedConnection,
39 },
40 {
41 name: "wrong host",
42 host: "mail.example.com",
43 server: &smtp.ServerInfo{Name: "evil.example.com", TLS: true},
44 wantErr: errWrongHostName,
45 },
46 }
47
48 for _, tt := range tests {
49 t.Run(tt.name, func(t *testing.T) {
50 t.Parallel()
51
52 auth := LoginAuth("user", "pass", tt.host)
53
54 mech, resp, err := auth.Start(tt.server)
55 if !errors.Is(err, tt.wantErr) {
56 t.Fatalf("Start() error = %v, want %v", err, tt.wantErr)
57 }
58
59 if tt.wantErr != nil {
60 return
61 }
62
63 if mech != "LOGIN" {
64 t.Errorf("Start() mech = %q, want LOGIN", mech)
65 }
66
67 if resp != nil {
68 t.Errorf("Start() resp = %v, want nil", resp)
69 }
70 })
71 }
72 }
73
74 func TestLoginAuthNext(t *testing.T) {
75 t.Parallel()
76
77 auth := LoginAuth("alice", "s3cret", "mail.example.com").(*loginAuth)
78
79 // Successful two-step exchange. Challenge text is ignored.
80 resp, err := auth.Next([]byte("User Name\x00"), true)
81 if err != nil {
82 t.Fatalf("step 0: unexpected error: %v", err)
83 }
84
85 if string(resp) != "alice" {
86 t.Fatalf("step 0: resp = %q, want alice", resp)
87 }
88
89 resp, err = auth.Next([]byte("password:"), true)
90 if err != nil {
91 t.Fatalf("step 1: unexpected error: %v", err)
92 }
93
94 if string(resp) != "s3cret" {
95 t.Fatalf("step 1: resp = %q, want s3cret", resp)
96 }
97
98 // An extra challenge must abort with a non-nil error, not a dummy response.
99 resp, err = auth.Next([]byte("Username:"), true)
100 if err == nil {
101 t.Fatal("step 2: expected error, got nil")
102 }
103
104 if resp != nil {
105 t.Fatalf("step 2: resp = %v, want nil so net/smtp aborts", resp)
106 }
107
108 if !errors.Is(err, errUnexpectedServerChallenge) {
109 t.Fatalf("step 2: error = %v, want %v", err, errUnexpectedServerChallenge)
110 }
111
112 if got := err.Error(); !strings.Contains(got, "Username:") {
113 t.Fatalf("step 2: error = %q, want challenge quoted in message", got)
114 }
115
116 // more=false ends the exchange cleanly.
117 resp, err = auth.Next(nil, false)
118 if err != nil || resp != nil {
119 t.Fatalf("more=false: resp=%v err=%v, want nil,nil", resp, err)
120 }
121 }
122
123 func TestLoginAuthNextResetsWithStart(t *testing.T) {
124 t.Parallel()
125
126 auth := LoginAuth("u", "p", "localhost").(*loginAuth)
127 if _, err := auth.Next(nil, true); err != nil {
128 t.Fatalf("first Next: %v", err)
129 }
130
131 if _, _, err := auth.Start(&smtp.ServerInfo{Name: "localhost", TLS: false}); err != nil {
132 t.Fatalf("Start: %v", err)
133 }
134
135 if auth.respStep != 0 {
136 t.Fatalf("respStep after Start = %d, want 0", auth.respStep)
137 }
138
139 resp, err := auth.Next([]byte("Username:"), true)
140 if err != nil {
141 t.Fatalf("Next after Start: %v", err)
142 }
143
144 if string(resp) != "u" {
145 t.Fatalf("Next after Start: resp = %q, want u", resp)
146 }
147 }
148
149 func TestIsLocalhost(t *testing.T) {
150 t.Parallel()
151
152 for _, name := range []string{"localhost", "127.0.0.1", "::1"} {
153 if !isLocalhost(name) {
154 t.Errorf("isLocalhost(%q) = false, want true", name)
155 }
156 }
157
158 if isLocalhost("example.com") {
159 t.Error("isLocalhost(example.com) = true, want false")
160 }
161 }