-
Notifications
You must be signed in to change notification settings - Fork 174
/
Copy pathsmtp_test.go
189 lines (141 loc) · 5.24 KB
/
smtp_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
// Copyright (c) 2025 Proton AG
//
// This file is part of Proton Mail Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package tests
import (
"encoding/base64"
"fmt"
"net/smtp"
"os"
"path/filepath"
"strings"
"github.com/ProtonMail/proton-bridge/v3/internal/constants"
"github.com/cucumber/godog"
)
func (s *scenario) userConnectsSMTPClient(username, clientID string) error {
return s.t.newSMTPClient(s.t.getUserByName(username).getUserID(), clientID)
}
func (s *scenario) userConnectsSMTPClientOnPort(username, clientID string, port int) error {
return s.t.newSMTPClientOnPort(s.t.getUserByName(username).getUserID(), clientID, port)
}
func (s *scenario) userConnectsAndAuthenticatesSMTPClient(username, clientID string) error {
return s.userConnectsAndAuthenticatesSMTPClientWithAddress(username, clientID, s.t.getUserByName(username).getEmails()[0])
}
func (s *scenario) userConnectsAndAuthenticatesSMTPClientWithAddress(username, clientID, address string) error {
if err := s.t.newSMTPClient(s.t.getUserByName(username).getUserID(), clientID); err != nil {
return err
}
userID, client := s.t.getSMTPClient(clientID)
s.t.pushError(client.Auth(smtp.PlainAuth("", address, s.t.getUserByID(userID).getBridgePass(), constants.Host)))
return nil
}
func (s *scenario) smtpClientCanAuthenticate(clientID string) error {
userID, client := s.t.getSMTPClient(clientID)
if err := client.Auth(smtp.PlainAuth("", s.t.getUserByID(userID).getEmails()[0], s.t.getUserByID(userID).getBridgePass(), constants.Host)); err != nil {
return fmt.Errorf("expected no error, got %v", err)
}
return nil
}
func (s *scenario) smtpClientCannotAuthenticate(clientID string) error {
userID, client := s.t.getSMTPClient(clientID)
if err := client.Auth(smtp.PlainAuth("", s.t.getUserByID(userID).getEmails()[0], s.t.getUserByID(userID).getBridgePass(), constants.Host)); err == nil {
return fmt.Errorf("expected error, got nil")
}
return nil
}
func (s *scenario) smtpClientCannotAuthenticateWithIncorrectUsername(clientID string) error {
userID, client := s.t.getSMTPClient(clientID)
if err := client.Auth(smtp.PlainAuth("", s.t.getUserByID(userID).getEmails()[0]+"bad", s.t.getUserByID(userID).getBridgePass(), constants.Host)); err == nil {
return fmt.Errorf("expected error, got nil")
}
return nil
}
func (s *scenario) smtpClientCannotAuthenticateWithIncorrectPassword(clientID string) error {
userID, client := s.t.getSMTPClient(clientID)
badPass := base64.StdEncoding.EncodeToString([]byte("bad_password"))
if err := client.Auth(smtp.PlainAuth("", s.t.getUserByID(userID).getEmails()[0], badPass, constants.Host)); err == nil {
return fmt.Errorf("expected error, got nil")
}
return nil
}
func (s *scenario) smtpClientSendsMailFrom(clientID, from string) error {
_, client := s.t.getSMTPClient(clientID)
s.t.pushError(client.Mail(from))
return nil
}
func (s *scenario) smtpClientSendsRcptTo(clientID, to string) error {
_, client := s.t.getSMTPClient(clientID)
s.t.pushError(client.Rcpt(to))
return nil
}
func (s *scenario) smtpClientSendsData(clientID string, data *godog.DocString) error {
_, client := s.t.getSMTPClient(clientID)
rc, err := client.Data()
if err != nil {
s.t.pushError(err)
} else if _, err := rc.Write([]byte(data.Content)); err != nil {
s.t.pushError(err)
} else if err := rc.Close(); err != nil {
s.t.pushError(err)
}
return nil
}
func (s *scenario) smtpClientSendsReset(clientID string) error {
_, client := s.t.getSMTPClient(clientID)
s.t.pushError(client.Reset())
return nil
}
func (s *scenario) smtpClientSendsTheFollowingMessageFromTo(clientID, from, to string, message *godog.DocString) error {
_, client := s.t.getSMTPClient(clientID)
if err := clientSend(client, from, to, message.Content); err != nil {
s.t.pushError(err)
}
return nil
}
func (s *scenario) smtpClientSendsTheFollowingEmlFromTo(clientID, file, from, to string) error {
_, client := s.t.getSMTPClient(clientID)
b, err := os.ReadFile(filepath.Join("testdata", file))
if err != nil {
return err
}
if err := clientSend(client, from, to, s.t.replace(string(b))); err != nil {
s.t.pushError(err)
}
return nil
}
func (s *scenario) smtpClientLogsOut(clientID string) error {
_, client := s.t.getSMTPClient(clientID)
s.t.pushError(client.Quit())
return nil
}
func clientSend(client *smtp.Client, from, to, message string) error {
if err := client.Mail(from); err != nil {
return err
}
for _, to := range strings.Split(to, ", ") {
if err := client.Rcpt(to); err != nil {
return err
}
}
wc, err := client.Data()
if err != nil {
return err
}
if _, err := wc.Write([]byte(message)); err != nil {
return err
}
return wc.Close()
}