summaryrefslogtreecommitdiffstats
path: root/vendor/gopkg.in/gomail.v2/auth_test.go
blob: 20b4772141a3e8e779b2c4cf5154c76370ea22d7 (plain)
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
package gomail

import (
	"net/smtp"
	"testing"
)

const (
	testUser = "user"
	testPwd  = "pwd"
	testHost = "smtp.example.com"
)

var testAuth = &plainAuth{
	username: testUser,
	password: testPwd,
	host:     testHost,
}

type plainAuthTest struct {
	auths      []string
	challenges []string
	tls        bool
	wantProto  string
	wantData   []string
	wantError  bool
}

func TestNoAdvertisement(t *testing.T) {
	testPlainAuth(t, &plainAuthTest{
		auths:      []string{},
		challenges: []string{"Username:", "Password:"},
		tls:        false,
		wantProto:  "PLAIN",
		wantError:  true,
	})
}

func TestNoAdvertisementTLS(t *testing.T) {
	testPlainAuth(t, &plainAuthTest{
		auths:      []string{},
		challenges: []string{"Username:", "Password:"},
		tls:        true,
		wantProto:  "PLAIN",
		wantData:   []string{"\x00" + testUser + "\x00" + testPwd},
	})
}

func TestPlain(t *testing.T) {
	testPlainAuth(t, &plainAuthTest{
		auths:      []string{"PLAIN"},
		challenges: []string{"Username:", "Password:"},
		tls:        false,
		wantProto:  "PLAIN",
		wantData:   []string{"\x00" + testUser + "\x00" + testPwd},
	})
}

func TestPlainTLS(t *testing.T) {
	testPlainAuth(t, &plainAuthTest{
		auths:      []string{"PLAIN"},
		challenges: []string{"Username:", "Password:"},
		tls:        true,
		wantProto:  "PLAIN",
		wantData:   []string{"\x00" + testUser + "\x00" + testPwd},
	})
}

func TestPlainAndLogin(t *testing.T) {
	testPlainAuth(t, &plainAuthTest{
		auths:      []string{"PLAIN", "LOGIN"},
		challenges: []string{"Username:", "Password:"},
		tls:        false,
		wantProto:  "PLAIN",
		wantData:   []string{"\x00" + testUser + "\x00" + testPwd},
	})
}

func TestPlainAndLoginTLS(t *testing.T) {
	testPlainAuth(t, &plainAuthTest{
		auths:      []string{"PLAIN", "LOGIN"},
		challenges: []string{"Username:", "Password:"},
		tls:        true,
		wantProto:  "PLAIN",
		wantData:   []string{"\x00" + testUser + "\x00" + testPwd},
	})
}

func TestLogin(t *testing.T) {
	testPlainAuth(t, &plainAuthTest{
		auths:      []string{"LOGIN"},
		challenges: []string{"Username:", "Password:"},
		tls:        false,
		wantProto:  "LOGIN",
		wantData:   []string{"", testUser, testPwd},
	})
}

func TestLoginTLS(t *testing.T) {
	testPlainAuth(t, &plainAuthTest{
		auths:      []string{"LOGIN"},
		challenges: []string{"Username:", "Password:"},
		tls:        true,
		wantProto:  "LOGIN",
		wantData:   []string{"", testUser, testPwd},
	})
}

func testPlainAuth(t *testing.T, test *plainAuthTest) {
	auth := &plainAuth{
		username: testUser,
		password: testPwd,
		host:     testHost,
	}
	server := &smtp.ServerInfo{
		Name: testHost,
		TLS:  test.tls,
		Auth: test.auths,
	}
	proto, toServer, err := auth.Start(server)
	if err != nil && !test.wantError {
		t.Fatalf("plainAuth.Start(): %v", err)
	}
	if err != nil && test.wantError {
		return
	}
	if proto != test.wantProto {
		t.Errorf("invalid protocol, got %q, want %q", proto, test.wantProto)
	}

	i := 0
	got := string(toServer)
	if got != test.wantData[i] {
		t.Errorf("Invalid response, got %q, want %q", got, test.wantData[i])
	}

	if proto == "PLAIN" {
		return
	}

	for _, challenge := range test.challenges {
		i++
		if i >= len(test.wantData) {
			t.Fatalf("unexpected challenge: %q", challenge)
		}

		toServer, err = auth.Next([]byte(challenge), true)
		if err != nil {
			t.Fatalf("plainAuth.Auth(): %v", err)
		}
		got = string(toServer)
		if got != test.wantData[i] {
			t.Errorf("Invalid response, got %q, want %q", got, test.wantData[i])
		}
	}
}