-
Notifications
You must be signed in to change notification settings - Fork 9
/
Copy pathsmtpproxy_test.go
65 lines (61 loc) · 1.33 KB
/
smtpproxy_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
package main
import (
"bytes"
"fmt"
"net"
"net/smtp"
"os"
"testing"
"github.com/jorgenschaefer/smtpproxy/config"
"github.com/jorgenschaefer/smtpproxy/smtpd"
)
func TestSMTPProxy(t *testing.T) {
// Start relay
smtpln, err := net.Listen("tcp", "")
if err != nil {
t.Error(err)
}
var buf bytes.Buffer
go readMail(smtpln, &buf)
os.Setenv("RELAY_HOST", smtpln.Addr().String())
config.Check()
proxyln, err := net.Listen("tcp", "")
if err != nil {
t.Error(err)
}
// Start proxy server
go func() {
conn, err := proxyln.Accept()
if err != nil {
panic(err)
}
handleConnection(smtpd.NewConnection(conn))
}()
// Send mail to the proxy server
err = smtp.SendMail(proxyln.Addr().String(), nil, "[email protected]",
[]string{"[email protected]"}, []byte("Hello"))
if err != nil {
t.Error(err)
}
data := buf.String()
expected := "EHLO localhost\r\nMAIL FROM:<[email protected]>\r\nRCPT TO:<[email protected]>\r\nDATA\r\nHello\r\n.\r\nQUIT\r\n"
if data != expected {
t.Errorf("Expected a mail, got %#v", data)
}
}
func readMail(ln net.Listener, buf *bytes.Buffer) {
conn, err := ln.Accept()
if err != nil {
panic(err)
}
defer conn.Close()
fmt.Fprintf(conn, "220 Hi\r\n250 Ok\r\n250 Ok\r\n250 Ok\r\n354 Ok\r\n250 Ok\r\n221 Ok\r\n")
b := make([]byte, 4096)
for {
n, err := conn.Read(b)
buf.Write(b[:n])
if err != nil {
return
}
}
}