forked from mailproto/smtpd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver_test.go
148 lines (111 loc) · 3.26 KB
/
server_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
package smtpd_test
import (
"fmt"
"net/smtp"
"testing"
"time"
"github.com/mailproto/smtpd"
)
type MessageRecorder struct {
Messages []*smtpd.Message
}
func (m *MessageRecorder) Record(msg *smtpd.Message) error {
m.Messages = append(m.Messages, msg)
return nil
}
func TestSMTPServer(t *testing.T) {
recorder := &MessageRecorder{}
server := smtpd.NewServer(recorder.Record)
go server.ListenAndServe("localhost:0")
defer server.Close()
WaitUntilAlive(server)
// Connect to the remote SMTP server.
c, err := smtp.Dial(server.Address())
if err != nil {
t.Errorf("Should be able to dial localhost: %v", err)
}
// Set the sender and recipient first
if err := c.Mail("sender@example.org"); err != nil {
t.Errorf("Should be able to set a sender: %v", err)
}
if err := c.Rcpt("recipient@example.net"); err != nil {
t.Errorf("Should be able to set a RCPT: %v", err)
}
if err := c.Rcpt("bcc@example.net"); err != nil {
t.Errorf("Should be able to set a second RCPT: %v", err)
}
// Send the email body.
wc, err := c.Data()
if err != nil {
t.Errorf("Error creating the data body: %v", err)
}
var emailBody = "This is the email body"
_, err = fmt.Fprintf(wc, `From: sender@example.org
To: recipient@example.net
Content-Type: text/html
%v`, emailBody)
if err != nil {
t.Errorf("Error writing email: %v", err)
}
if err := wc.Close(); err != nil {
t.Error(err)
}
// Send the QUIT command and close the connection.
if err := c.Quit(); err != nil {
t.Errorf("Server wouldn't accept QUIT: %v", err)
}
if len(recorder.Messages) != 1 {
t.Fatalf("Expected 1 message, got: %v", len(recorder.Messages))
}
if h, err := recorder.Messages[0].HTML(); err == nil {
if string(h) != emailBody {
t.Errorf("Wrong body - want: %v, got: %v", emailBody, string(h))
}
} else {
t.Fatalf("Error getting HTML body: %v", err)
}
bcc := recorder.Messages[0].BCC()
if len(bcc) != 1 {
t.Fatalf("Expected 1 BCC, got: %v", len(bcc))
}
if bcc[0].Address != "bcc@example.net" {
t.Errorf("wrong BCC value, want: bcc@example.net, got: %v", bcc[0].Address)
}
}
func TestSMTPServerTimeout(t *testing.T) {
recorder := &MessageRecorder{}
server := smtpd.NewServer(recorder.Record)
// Set some really short timeouts
server.ReadTimeout = time.Millisecond * 1
server.WriteTimeout = time.Millisecond * 1
go server.ListenAndServe("localhost:0")
defer server.Close()
WaitUntilAlive(server)
// Connect to the remote SMTP server.
c, err := smtp.Dial(server.Address())
if err != nil {
t.Errorf("Should be able to dial localhost: %v", err)
}
// Sleep for twice the timeout
time.Sleep(time.Millisecond * 20)
// Set the sender and recipient first
if err := c.Hello("sender@example.org"); err == nil {
t.Errorf("Should have gotten a timeout from the upstream server")
}
}
func TestSMTPServerNoTLS(t *testing.T) {
recorder := &MessageRecorder{}
server := smtpd.NewServer(recorder.Record)
go server.ListenAndServe("localhost:0")
defer server.Close()
WaitUntilAlive(server)
// Connect to the remote SMTP server.
c, err := smtp.Dial(server.Address())
if err != nil {
t.Errorf("Should be able to dial localhost: %v", err)
}
err = c.StartTLS(nil)
if err == nil {
t.Error("Server should return a failure for a TLS request when there is no config available")
}
}