-
Notifications
You must be signed in to change notification settings - Fork 35
/
smtp_test.go
205 lines (188 loc) · 5.72 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
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
package gold
import (
"bytes"
"crypto/tls"
"errors"
"fmt"
"io"
"net"
"strings"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
var (
tlsConfig = &tls.Config{
NextProtos: []string{"http/1.1"},
}
tlsTestCert = []byte(`-----BEGIN CERTIFICATE-----
MIIB4TCCAUygAwIBAgIBADALBgkqhkiG9w0BAQUwEjEQMA4GA1UEChMHQWNtZSBD
bzAeFw0xNDAxMzAyMzUyMTlaFw0yNDAxMjgyMzUyMTlaMBIxEDAOBgNVBAoTB0Fj
bWUgQ28wgZ8wDQYJKoZIhvcNAQEBBQADgY0AMIGJAoGBAMs8NmXX55GqvTRcIE2K
8ZoElA7xRuiIYPXFl6Zlt/xCYUzcxEEz2pKOX3jgYEzx4wG0hQ5bcNQMJWPftZ7K
6QBvDRWs8wVgrbeN8o9LelPDrPl40Zk96howpgek/nPd5AUt6y0/hV4CNVt07y+D
13BxZSEj1E8ZTwCwhQ9uGltPAgMBAAGjSzBJMA4GA1UdDwEB/wQEAwIAoDATBgNV
HSUEDDAKBggrBgEFBQcDATAMBgN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tVPMCx3O10
LMEOXuCrAMT/nApK629bgSlTU6P9PZd+05yRbHt4Ds1S
-----END RSA PRIVATE KEY-----`)
)
type smtpServer struct {
//array of commands
Commands map[string]func(payload []byte) ([]byte, error)
//function to be called, when no corresponding command is found
CatchAll func(payload []byte) ([]byte, error)
//error reporting function
ErrorReporter func(err error) []byte
//separator between command and payload
Separator []byte
}
// Listen creates a server that listens for incoming SMTP connections
func (srv *smtpServer) Listen(network, address string, secure bool) error {
var l net.Listener
var err error
if secure {
tlsConfig.Certificates = make([]tls.Certificate, 1)
tlsConfig.Certificates[0], err = tls.X509KeyPair(tlsTestCert, tlsTestKey)
if err != nil {
return err
}
var tcpL net.Listener
tcpL, err = net.Listen("tcp", address)
if err != nil {
return err
}
l = tls.NewListener(tcpL, tlsConfig)
defer l.Close()
} else {
l, err = net.Listen(network, address)
if err != nil {
return err
}
defer l.Close()
}
for {
conn, err := l.Accept()
if err != nil {
return err
}
go func(c net.Conn) {
defer func() {
e := recover()
if e != nil {
errorRecovered := e.(string)
if errorRecovered != "" {
fmt.Println(errorRecovered)
c.Write(srv.ErrorReporter(errors.New(errorRecovered)))
c.Close()
}
}
}()
c.Write([]byte("Commands available: \n"))
for command := range srv.Commands {
c.Write([]byte("# "))
c.Write([]byte(command))
c.Write([]byte(" [payload] \n"))
}
c.Write([]byte("What to do? ...\n"))
//reading
buf := make([]byte, 0, 4096)
tmp := make([]byte, 256)
for {
n, err := conn.Read(tmp)
if err != nil {
if err != io.EOF {
panic(err.Error())
}
break
}
buf = append(buf, tmp[:n]...)
}
parsed := bytes.Split(buf, srv.Separator)
command := strings.ToLower(string(parsed[0]))
payload := bytes.Join(parsed[1:], srv.Separator)
action, ok := srv.Commands[command]
if !ok {
action = srv.CatchAll
}
answer, err := action(payload)
if err != nil {
panic(err.Error())
} else {
c.Write(answer)
c.Close()
}
}(conn)
}
}
func catchAllFunc(payload []byte) ([]byte, error) {
return []byte(fmt.Sprintf("Catch all command with payload of %v", string(payload))), nil
}
func helloFunc(payload []byte) ([]byte, error) {
return []byte(fmt.Sprintf("Hello command with payload of %v", string(payload))), nil
}
func badFunc(payload []byte) ([]byte, error) {
return []byte(fmt.Sprintf("Bad command with payload of %v", string(payload))), errors.New("Ups, sorry!")
}
func errorReporter(err error) []byte {
return []byte("Error processing the command: " + err.Error())
}
func TestStartFakeSMTPServer(t *testing.T) {
t.Parallel()
var commands = make(map[string](func(payload []byte) ([]byte, error)))
commands["hello"] = helloFunc
commands["bad"] = badFunc
separator := []byte(" ")
go func() {
serv := smtpServer{commands, catchAllFunc, errorReporter, separator}
err := serv.Listen("tcp", ":3000", false)
assert.NoError(t, err)
}()
}
func TestStartFakeSecureSMTPServer(t *testing.T) {
t.Parallel()
var commands = make(map[string](func(payload []byte) ([]byte, error)))
commands["hello"] = helloFunc
commands["bad"] = badFunc
separator := []byte(" ")
go func() {
serv := smtpServer{commands, catchAllFunc, errorReporter, separator}
err := serv.Listen("tcp", ":3030", true)
assert.NoError(t, err)
}()
}
func TestFakeSMTPDial(t *testing.T) {
t.Parallel()
// give the server some time to start
time.Sleep(200 * time.Millisecond)
_, err := net.Dial("tcp", "localhost:3000")
assert.NoError(t, err)
}
//func TestFakeSMTPSecureDial(t *testing.T) {
// t.Parallel()
// // give the server some time to start
// time.Sleep(200 * time.Millisecond)
// tlsconfig := &tls.Config{
// InsecureSkipVerify: true,
// ServerName: "localhost",
// }
// _, err := tls.Dial("tcp", "localhost:3030", tlsconfig)
// assert.NoError(t, err)
//}