Пример #1
0
func TestEnvelopeReceived(t *testing.T) {
	addr, closer := runsslserver(t, &smtpd.Server{
		Hostname: "foobar.example.net",
		DeliveryChain: []smtpd.Delivery{
			smtpd.DeliveryFunc(func(next func(conn *smtpd.Connection)) func(conn *smtpd.Connection) {
				return func(conn *smtpd.Connection) {
					conn.Envelope.AddReceivedLine(conn)
					if !bytes.HasPrefix(conn.Envelope.Data, []byte("Received: from localhost [127.0.0.1] by foobar.example.net with ESMTP;")) {
						t.Fatal("Wrong received line.")
					}
					next(conn)
				}
			}),
		},
		ForceTLS: true,
	})
	defer closer()

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.StartTLS(&tls.Config{InsecureSkipVerify: true}); err != nil {
		t.Fatalf("STARTTLS failed: %v", err)
	}

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("MAIL failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err != nil {
		t.Fatalf("RCPT failed: %v", err)
	}

	wc, err := c.Data()
	if err != nil {
		t.Fatalf("Data failed: %v", err)
	}

	_, err = fmt.Fprintf(wc, "This is the email body")
	if err != nil {
		t.Fatalf("Data body failed: %v", err)
	}

	err = wc.Close()
	if err != nil {
		t.Fatalf("Data close failed: %v", err)
	}

	if err := c.Quit(); err != nil {
		t.Fatalf("QUIT failed: %v", err)
	}
}
Пример #2
0
func TestRejectHandler(t *testing.T) {
	addr, closer := runserver(t, &smtpd.Server{
		DeliveryChain: []smtpd.Delivery{
			smtpd.DeliveryFunc(func(next func(conn *smtpd.Connection)) func(conn *smtpd.Connection) {
				return func(conn *smtpd.Connection) {
					conn.Error(smtpd.ErrServerError)
				}
			}),
		},
	})
	defer closer()

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("MAIL failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err != nil {
		t.Fatalf("RCPT failed: %v", err)
	}

	wc, err := c.Data()
	if err != nil {
		t.Fatalf("Data failed: %v", err)
	}

	_, err = fmt.Fprintf(wc, "This is the email body")
	if err != nil {
		t.Fatalf("Data body failed: %v", err)
	}

	err = wc.Close()
	if err == nil {
		t.Fatal("Unexpected accept of data")
	}

	if err := c.Quit(); err != nil {
		t.Fatalf("QUIT failed: %v", err)
	}
}
Пример #3
0
func TestInterruptedDATA(t *testing.T) {
	addr, closer := runserver(t, &smtpd.Server{
		DeliveryChain: []smtpd.Delivery{
			smtpd.DeliveryFunc(func(next func(conn *smtpd.Connection)) func(conn *smtpd.Connection) {
				return func(conn *smtpd.Connection) {
					t.Fatal("Accepted DATA despite disconnection")
					next(conn)
				}
			}),
		},
	})
	defer closer()

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}
	defer c.Close()

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("MAIL failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err != nil {
		t.Fatalf("RCPT failed: %v", err)
	}

	wc, err := c.Data()
	if err != nil {
		t.Fatalf("Data failed: %v", err)
	}

	_, err = fmt.Fprintf(wc, "This is the email body")
	if err != nil {
		t.Fatalf("Data body failed: %v", err)
	}
}
Пример #4
0
func TestHandlerAndWrapper(t *testing.T) {
	addr, closer := runserver(t, &smtpd.Server{
		WrapperChain: []smtpd.Wrapper{
			smtpd.WrapperFunc(func(next func()) func() {
				return func() {
					log.Print("Hello from inside the wrapper!")
					next()
				}
			}),
		},
		DeliveryChain: []smtpd.Delivery{
			smtpd.DeliveryFunc(func(next func(conn *smtpd.Connection)) func(conn *smtpd.Connection) {
				return func(conn *smtpd.Connection) {
					if conn.Envelope.Sender != "*****@*****.**" {
						t.Fatalf("Unknown sender: %v", conn.Envelope.Sender)
					}

					if len(conn.Envelope.Recipients) != 1 {
						t.Fatalf("Too many recipients: %d", len(conn.Envelope.Recipients))
					}

					if conn.Envelope.Recipients[0] != "*****@*****.**" {
						t.Fatalf("Unknown recipient: %v", conn.Envelope.Recipients[0])
					}

					if string(conn.Envelope.Data) != "This is the email body\n" {
						t.Fatalf("Wrong message body: %v", string(conn.Envelope.Data))
					}

					next(conn)
				}
			}),
		},
	})
	defer closer()

	c, err := smtp.Dial(addr)
	if err != nil {
		t.Fatalf("Dial failed: %v", err)
	}

	if err := c.Mail("*****@*****.**"); err != nil {
		t.Fatalf("MAIL failed: %v", err)
	}

	if err := c.Rcpt("*****@*****.**"); err != nil {
		t.Fatalf("RCPT failed: %v", err)
	}

	wc, err := c.Data()
	if err != nil {
		t.Fatalf("Data failed: %v", err)
	}

	_, err = fmt.Fprintf(wc, "This is the email body")
	if err != nil {
		t.Fatalf("Data body failed: %v", err)
	}

	err = wc.Close()
	if err != nil {
		t.Fatalf("Data close failed: %v", err)
	}

	if err := c.Quit(); err != nil {
		t.Fatalf("QUIT failed: %v", err)
	}
}