-
Notifications
You must be signed in to change notification settings - Fork 1
/
server_test.go
134 lines (130 loc) · 2.81 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
package smtpsrv
import (
"errors"
"fmt"
"net"
"net/smtp"
"reflect"
"testing"
"time"
)
var (
testEmail1 = "a@localhost"
testEmail2 = "b@localhost"
testEmail3 = "c@localhost"
content = "this\r\nis\r\na\r\ntest"
message = &Message{
From: testEmail1,
To: []string{
testEmail2,
testEmail3,
},
Body: content,
}
)
func TestResponse(t *testing.T) {
var (
m *Message
s, err = NewServer(&Config{
Addr: "127.0.0.1:0",
})
)
if err != nil {
t.Fatal(err)
}
// Spawn a goroutine to capture any new message
go func() {
m = <-s.NewMessage
}()
// Connect to the server using its address
c, err := smtp.Dial(s.listener.Addr().String())
if err != nil {
t.Fatal(err)
}
// Begin by saying hello
if err := c.Hello("localhost"); err != nil {
t.Fatal(err)
}
// Now make an out-of-sequence call to RCPT
if err := c.Rcpt(""); err == nil {
t.Fatal(errors.New("RCPT should not have succeeded"))
}
// Make a correct call to MAIL but with an invalid address
if err := c.Mail(""); err == nil {
t.Fatal(errors.New("MAIL should not have accepted malformed address"))
}
// Now issue a legit email
if err := c.Mail(testEmail1); err != nil {
t.Fatal(err)
}
// Call MAIL again (which should fail)
if err := c.Mail(testEmail1); err == nil {
t.Fatal(errors.New("MAIL should not be accepted twice"))
}
// Reset...
if err := c.Reset(); err != nil {
t.Fatal(err)
}
// ...and try again
if err := c.Mail(testEmail1); err != nil {
t.Fatal(err)
}
// DATA should not succeed here
if _, err := c.Data(); err == nil {
t.Fatal(errors.New("DATA should not have succeeded"))
}
// Send the recipient
if err := c.Rcpt(testEmail2); err != nil {
t.Fatal(err)
}
// Send the second recipient
if err := c.Rcpt(testEmail3); err != nil {
t.Fatal(err)
}
// Now send the data
if w, err := c.Data(); err != nil {
t.Fatal(err)
} else {
w.Write([]byte(content))
w.Close()
}
// Say goodbye...
if err := c.Quit(); err != nil {
t.Fatal(err)
}
// Shut 'er down
s.Close(false)
// Ensure a message was received
if m == nil {
t.Fatal(errors.New("message expected"))
}
// Ensure it matches
if !reflect.DeepEqual(m, message) {
t.Fatal(fmt.Errorf("%t != %t", m, message))
}
}
func TestTimeout(t *testing.T) {
s, err := NewServer(&Config{
Addr: "127.0.0.1:0",
ReadTimeout: 50 * time.Millisecond,
})
if err != nil {
t.Fatal(err)
}
// Connect to the server using its address
conn, err := net.Dial("tcp", s.listener.Addr().String())
if err != nil {
t.Fatal(err)
}
c, err := smtp.NewClient(conn, "localhost")
if err != nil {
t.Fatal(err)
}
// Hang for 100ms to trigger the timeout
time.Sleep(100 * time.Millisecond)
conn.SetDeadline(time.Now().Add(100 * time.Millisecond))
if err := c.Hello("localhost"); err == nil {
t.Fatal(errors.New("timeout expected"))
}
s.Close(false)
}