| 1 |
package pop3 |
| 2 |
|
| 3 |
import ( |
| 4 |
"bufio" |
| 5 |
"bytes" |
| 6 |
"fmt" |
| 7 |
"io" |
| 8 |
"log" |
| 9 |
"net/smtp" |
| 10 |
"strings" |
| 11 |
"testing" |
| 12 |
|
| 13 |
"github.com/emersion/go-message" |
| 14 |
"github.com/emersion/go-message/mail" |
| 15 |
) |
| 16 |
|
| 17 |
const MSG = `Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do.` |
| 18 |
|
| 19 |
// n represents number of messages to add to the testuser's inbox |
| 20 |
func add_messages(n int) error { |
| 21 |
to := []string{"[email protected]"} |
| 22 |
msgs := make([][]byte, 0) |
| 23 |
for i := 0; i < 5; i++ { |
| 24 |
to := "To: [email protected]\r\n" |
| 25 |
subject := fmt.Sprintf("Subject: Subject %d\r\n", i) |
| 26 |
mime := "MIME-version: 1.0;\nContent-Type: text/plain; charset=\"UTF-8\";\n\n" |
| 27 |
body := fmt.Sprintf("Message %d.\r\n"+MSG+"\r\n", i) |
| 28 |
msg := []byte(to + subject + mime + body) |
| 29 |
msgs = append(msgs, msg) |
| 30 |
} |
| 31 |
|
| 32 |
for _, msg := range msgs { |
| 33 |
err := smtp.SendMail("localhost:2500", nil, "[email protected]", to, msg) |
| 34 |
if err != nil { |
| 35 |
log.Fatal(err) |
| 36 |
} |
| 37 |
} |
| 38 |
return nil |
| 39 |
} |
| 40 |
|
| 41 |
func getConnection() (*Conn, error) { |
| 42 |
p := New(Opt{ |
| 43 |
Host: "localhost", |
| 44 |
Port: 1100, |
| 45 |
TLSEnabled: false, |
| 46 |
}) |
| 47 |
|
| 48 |
c, err := p.NewConn() |
| 49 |
if err != nil { |
| 50 |
return nil, err |
| 51 |
} |
| 52 |
return c, nil |
| 53 |
} |
| 54 |
|
| 55 |
func readAndCompareMessageBody(m *message.Entity, msg string) error { |
| 56 |
mr := mail.NewReader(m) |
| 57 |
if mr != nil { |
| 58 |
// This is a multipart message |
| 59 |
for { |
| 60 |
p, err := mr.NextPart() |
| 61 |
if err == io.EOF { |
| 62 |
break |
| 63 |
} else if err != nil { |
| 64 |
return err |
| 65 |
} |
| 66 |
b, err := io.ReadAll(p.Body) |
| 67 |
if err != nil { |
| 68 |
return err |
| 69 |
} |
| 70 |
if !strings.EqualFold(string(b), msg) { |
| 71 |
return fmt.Errorf("expected message body:\n%sreceived:\n%s", msg, string(b)) |
| 72 |
} |
| 73 |
} |
| 74 |
return nil |
| 75 |
} else { |
| 76 |
t, _, _ := m.Header.ContentType() |
| 77 |
log.Println("This is a non-multipart message with type", t) |
| 78 |
return nil |
| 79 |
} |
| 80 |
} |
| 81 |
|
| 82 |
func TestAll(t *testing.T) { |
| 83 |
c, err := getConnection() |
| 84 |
if err != nil { |
| 85 |
t.Fatal("error establishing connection to pop3 server ", err) |
| 86 |
} |
| 87 |
|
| 88 |
err = add_messages(5) |
| 89 |
if err != nil { |
| 90 |
t.Fatal("unable to send messages to the mail server", err) |
| 91 |
} |
| 92 |
|
| 93 |
// testing Auth |
| 94 |
if err := c.Auth("recipient", "password"); err != nil { |
| 95 |
t.Fatal(err) |
| 96 |
} |
| 97 |
|
| 98 |
// testing Stat |
| 99 |
count, size, err := c.Stat() |
| 100 |
if err != nil { |
| 101 |
t.Fatal("error using Stat", err) |
| 102 |
} |
| 103 |
log.Printf("count: %d, size: %d\n", count, size) |
| 104 |
|
| 105 |
// testing Uidl |
| 106 |
msgIds, err := c.Uidl(0) |
| 107 |
if err != nil { |
| 108 |
t.Fatal("error using Uidl(0)", err) |
| 109 |
} |
| 110 |
|
| 111 |
if len(msgIds) != count { |
| 112 |
t.Fatalf("Uidl returned: %d number of messages, but actually there are %d messages\n", len(msgIds), 5) |
| 113 |
} |
| 114 |
|
| 115 |
msgId, err := c.Uidl(msgIds[0].ID) |
| 116 |
if err != nil { |
| 117 |
t.Fatal("error using Uidl for positive message ID", err) |
| 118 |
} |
| 119 |
if len(msgId) != 1 { |
| 120 |
t.Fatalf("Uidl returns a list of (message ID, message UID) pairs. If the optional msgID is > 0, then only that particular message is listed but it returned %d pair\n", len(msgId)) |
| 121 |
} |
| 122 |
|
| 123 |
// testing List |
| 124 |
msgs, err := c.List(0) |
| 125 |
if err != nil { |
| 126 |
t.Fatal("error using List(0)", err) |
| 127 |
} |
| 128 |
if len(msgs) != 5 { |
| 129 |
t.Fatalf("List(0) returned incorrect number of messages got: %d actual: %d\n", len(msgs), 5) |
| 130 |
} |
| 131 |
msgId, err = c.List(msgs[1].ID) |
| 132 |
if err != nil { |
| 133 |
t.Fatal("error using List for positive message ID", err) |
| 134 |
} |
| 135 |
if len(msgId) != 1 { |
| 136 |
t.Fatalf("List returns a list of (message ID, message UID) pairs. If the optional msgID is > 0, then only that particular message is listed but it returned %d pair\n", len(msgId)) |
| 137 |
} |
| 138 |
|
| 139 |
// testing Retr |
| 140 |
m, err := c.Retr(msgs[0].ID) |
| 141 |
if err != nil { |
| 142 |
t.Fatal("error using Retr", err) |
| 143 |
} |
| 144 |
if m.Header.Get("subject") != "Subject 0" { |
| 145 |
t.Fatalf("Retr returned wrong subject returned: %s, expected: Subject 0 ", m.Header.Get("subject")) |
| 146 |
} |
| 147 |
err = readAndCompareMessageBody(m, "Message 0.\r\n"+MSG+"\r\n") |
| 148 |
if err != nil { |
| 149 |
t.Fatal(err) |
| 150 |
} |
| 151 |
|
| 152 |
// testing RetrRaw |
| 153 |
mb, err := c.RetrRaw(msgs[0].ID) |
| 154 |
if err != nil { |
| 155 |
t.Fatal("error using RetrRaw", err) |
| 156 |
} |
| 157 |
b := mb.Bytes() |
| 158 |
if !bytes.Contains(b, []byte("Message 0.\r\n"+MSG+"\r\n")) { |
| 159 |
t.Fatalf("expected message body:\n%s, received:\n%s", "Message 0.\r\n"+MSG+"\r\n", string(b)) |
| 160 |
} |
| 161 |
|
| 162 |
// testing Top |
| 163 |
m, err = c.Top(msgs[0].ID, 1) |
| 164 |
if err != nil { |
| 165 |
t.Fatal("error using Top", err) |
| 166 |
} |
| 167 |
err = readAndCompareMessageBody(m, "Message 0.\r\n") |
| 168 |
if err != nil { |
| 169 |
t.Fatal(err) |
| 170 |
} |
| 171 |
|
| 172 |
// testing Noop |
| 173 |
err = c.Noop() |
| 174 |
if err != nil { |
| 175 |
t.Fatal("error in using Noop", err) |
| 176 |
} |
| 177 |
|
| 178 |
// testing Dele |
| 179 |
err = c.Dele([]int{1, 2}...) |
| 180 |
if err != nil { |
| 181 |
t.Fatal("error using Dele", err) |
| 182 |
} |
| 183 |
msgs, _ = c.List(0) |
| 184 |
if len(msgs) != 3 { |
| 185 |
t.Fatalf("after deleting 2 messages number of messages in inbox should be 3 but got %d", len(msgs)) |
| 186 |
} |
| 187 |
// testing Rset, list |
| 188 |
err = c.Rset() |
| 189 |
if err != nil { |
| 190 |
t.Fatal("error using Rset", err) |
| 191 |
} |
| 192 |
msgs, _ = c.List(0) |
| 193 |
if len(msgs) != 5 { |
| 194 |
t.Fatalf("after Rseting number of messages in inbox should be 5 but got %d", len(msgs)) |
| 195 |
} |
| 196 |
|
| 197 |
// testing Quit |
| 198 |
err = c.Quit() |
| 199 |
if err != nil { |
| 200 |
t.Fatal("error using Quit method", err) |
| 201 |
} |
| 202 |
} |
| 203 |
|
| 204 |
// newTestConn creates a Conn with a bufio.Reader wrapping the given data. |
| 205 |
func newTestConn(data string) *Conn { |
| 206 |
return &Conn{ |
| 207 |
r: bufio.NewReader(strings.NewReader(data)), |
| 208 |
} |
| 209 |
} |
| 210 |
|
| 211 |
func TestReadAllMultiline(t *testing.T) { |
| 212 |
// ".." should become "." and "..foo" should become ".foo". |
| 213 |
input := "line1\r\n..dot-prefixed\r\n...\r\nnormal line\r\n.\r\n" |
| 214 |
c := newTestConn(input) |
| 215 |
|
| 216 |
buf, err := c.ReadAll() |
| 217 |
if err != nil { |
| 218 |
t.Fatal(err) |
| 219 |
} |
| 220 |
|
| 221 |
expected := "line1\r\n.dot-prefixed\r\n..\r\nnormal line\r\n" |
| 222 |
if buf.String() != expected { |
| 223 |
t.Fatalf("ReadAll dot-destuffing failed.\nexpected: %q\ngot: %q", expected, buf.String()) |
| 224 |
} |
| 225 |
} |
| 226 |
|
| 227 |
func TestReadAllLongLines(t *testing.T) { |
| 228 |
longLine := strings.Repeat("A", 5000) |
| 229 |
input := longLine + "\r\n.\r\n" |
| 230 |
c := newTestConn(input) |
| 231 |
|
| 232 |
buf, err := c.ReadAll() |
| 233 |
if err != nil { |
| 234 |
t.Fatal(err) |
| 235 |
} |
| 236 |
|
| 237 |
expected := longLine + "\r\n" |
| 238 |
if buf.String() != expected { |
| 239 |
t.Fatalf("ReadAll long line failed. expected length %d, got %d", len(expected), len(buf.String())) |
| 240 |
} |
| 241 |
} |
| 242 |
|
| 243 |
func TestReadAllMultipartContentDisposition(t *testing.T) { |
| 244 |
boundary := "----=_Part_123" |
| 245 |
rawMsg := fmt.Sprintf("MIME-Version: 1.0\r\n"+ |
| 246 |
"Content-Type: multipart/mixed; boundary=\"%s\"\r\n"+ |
| 247 |
"\r\n"+ |
| 248 |
"--%s\r\n"+ |
| 249 |
"Content-Type: text/plain\r\n"+ |
| 250 |
"\r\n"+ |
| 251 |
"Hello\r\n"+ |
| 252 |
"--%s\r\n"+ |
| 253 |
"Content-Type: application/octet-stream\r\n"+ |
| 254 |
"Content-Disposition: attachment; filename=\"test.txt\"\r\n"+ |
| 255 |
"\r\n"+ |
| 256 |
"file content\r\n"+ |
| 257 |
"--%s--\r\n", boundary, boundary, boundary, boundary) |
| 258 |
|
| 259 |
// Add the final POP3 terminator. |
| 260 |
input := rawMsg + ".\r\n" |
| 261 |
|
| 262 |
c := newTestConn(input) |
| 263 |
buf, err := c.ReadAll() |
| 264 |
if err != nil { |
| 265 |
t.Fatal(err) |
| 266 |
} |
| 267 |
|
| 268 |
entity, err := message.Read(buf) |
| 269 |
if err != nil { |
| 270 |
t.Fatal("failed to parse MIME message:", err) |
| 271 |
} |
| 272 |
|
| 273 |
rd := entity.MultipartReader() |
| 274 |
if rd == nil { |
| 275 |
t.Fatal("expected multipart message, got nil reader") |
| 276 |
} |
| 277 |
|
| 278 |
i := 0 |
| 279 |
for { |
| 280 |
p, err := rd.NextPart() |
| 281 |
if err == io.EOF { |
| 282 |
break |
| 283 |
} |
| 284 |
if err != nil { |
| 285 |
t.Fatal("error reading part:", err) |
| 286 |
} |
| 287 |
|
| 288 |
if i == 1 { |
| 289 |
// Second part should have `Content-Disposition``. |
| 290 |
dispVal, params, err := p.Header.ContentDisposition() |
| 291 |
if err != nil { |
| 292 |
t.Fatal("error getting Content-Disposition:", err) |
| 293 |
} |
| 294 |
if dispVal != "attachment" { |
| 295 |
t.Fatalf("expected Content-Disposition 'attachment', got %q", dispVal) |
| 296 |
} |
| 297 |
if params["filename"] != "test.txt" { |
| 298 |
t.Fatalf("expected filename 'test.txt', got %q", params["filename"]) |
| 299 |
} |
| 300 |
} |
| 301 |
i++ |
| 302 |
} |
| 303 |
|
| 304 |
if i < 2 { |
| 305 |
t.Fatalf("expected at least 2 parts, got %d", i) |
| 306 |
} |
| 307 |
} |