File: session_test.go

package info (click to toggle)
golang-github-protonmail-gluon 0.17.0-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 16,020 kB
  • sloc: sh: 55; makefile: 5
file content (449 lines) | stat: -rw-r--r-- 12,616 bytes parent folder | download
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
package tests

import (
	"context"
	"errors"
	"fmt"
	"io"
	"net"
	"os"
	"strings"
	"testing"
	"time"

	"github.com/ProtonMail/gluon"
	"github.com/ProtonMail/gluon/connector"
	"github.com/ProtonMail/gluon/db"
	"github.com/ProtonMail/gluon/events"
	"github.com/ProtonMail/gluon/imap"
	"github.com/ProtonMail/gluon/internal/utils"
	"github.com/ProtonMail/go-mbox"
	"github.com/emersion/go-imap/client"
	"github.com/stretchr/testify/require"
)

type Connector interface {
	connector.Connector

	SetFolderPrefix(string)
	SetLabelsPrefix(string)

	MailboxCreated(imap.Mailbox) error
	MailboxDeleted(imap.MailboxID) error
	SetMailboxVisibility(imap.MailboxID, imap.MailboxVisibility)

	SetAllowMessageCreateWithUnknownMailboxID(value bool)

	MessageCreated(imap.Message, []byte, []imap.MailboxID) error
	MessagesCreated([]imap.Message, [][]byte, [][]imap.MailboxID) error
	MessageUpdated(imap.Message, []byte, []imap.MailboxID) error
	MessageAdded(imap.MessageID, imap.MailboxID) error
	MessageRemoved(imap.MessageID, imap.MailboxID) error
	MessageSeen(imap.MessageID, bool) error
	MessageFlagged(imap.MessageID, bool) error
	MessageDeleted(imap.MessageID) error

	UIDValidityBumped()

	GetLastRecordedIMAPID() imap.IMAPID

	Sync(context.Context) error

	Flush()

	SetUpdatesAllowedToFail(bool)
}

type testSession struct {
	tb testing.TB

	listener    net.Listener
	server      *gluon.Server
	eventCh     <-chan events.Event
	reporter    *testReporter
	userIDs     map[string]string
	conns       map[string]Connector
	userDBPaths map[string]string
	options     *serverOptions
}

func newTestSession(
	tb testing.TB,
	listener net.Listener,
	server *gluon.Server,
	eventCh <-chan events.Event,
	reporter *testReporter,
	userIDs map[string]string,
	conns map[string]Connector,
	userDBPaths map[string]string,
	options *serverOptions,
) *testSession {
	return &testSession{
		tb:          tb,
		listener:    listener,
		server:      server,
		eventCh:     eventCh,
		reporter:    reporter,
		userIDs:     userIDs,
		conns:       conns,
		userDBPaths: userDBPaths,
		options:     options,
	}
}

func (s *testSession) newConnection() *testConnection {
	conn, err := net.Dial(s.listener.Addr().Network(), s.listener.Addr().String())
	require.NoError(s.tb, err)

	return newTestConnection(s.tb, conn).Sx(`\* OK.*`)
}

func (s *testSession) withConnection(user string, fn func(*testConnection)) {
	conn := s.newConnection()
	defer func() { require.NoError(s.tb, conn.disconnect()) }()

	fn(conn.Login(user, s.options.password(user)))
}

func (s *testSession) newClient() *client.Client {
	client, err := client.Dial(s.listener.Addr().String())
	require.NoError(s.tb, err)

	return client
}

func (s *testSession) withUserDB(user string, fn func(client db.Client, ctx context.Context)) error {
	userID, ok := s.userIDs[user]
	if !ok {
		return fmt.Errorf("User not found")
	}

	client, _, err := s.options.database.New(s.server.GetDatabasePath(), userID)
	if err != nil {
		return err
	}

	fn(client, context.Background())

	return client.Close()
}

func (s *testSession) setFolderPrefix(user, prefix string) {
	s.conns[s.userIDs[user]].SetFolderPrefix(prefix)
}

func (s *testSession) setLabelsPrefix(user, prefix string) {
	s.conns[s.userIDs[user]].SetLabelsPrefix(prefix)
}

func (s *testSession) mailboxCreated(user string, name []string, withData ...string) imap.MailboxID {
	return s.mailboxCreatedWithAttributes(user, name, defaultAttributes, withData...)
}

func (s *testSession) setAllowMessageCreateWithUnknownMailboxID(user string, value bool) {
	s.conns[s.userIDs[user]].SetAllowMessageCreateWithUnknownMailboxID(value)
}

func (s *testSession) mailboxDeleted(user string, id imap.MailboxID) {
	require.NoError(s.tb, s.conns[s.userIDs[user]].MailboxDeleted(id))
}

func (s *testSession) mailboxCreatedWithAttributes(user string, name []string, attributes imap.FlagSet, withData ...string) imap.MailboxID {
	mboxID := imap.MailboxID(utils.NewRandomMailboxID())

	require.NoError(s.tb, s.conns[s.userIDs[user]].MailboxCreated(imap.Mailbox{
		ID:             mboxID,
		Name:           name,
		Flags:          defaultFlags,
		PermanentFlags: defaultPermanentFlags,
		Attributes:     attributes,
	}))

	for _, data := range withData {
		s.messagesCreatedFromMBox(user, mboxID, data)
	}

	s.conns[s.userIDs[user]].Flush()

	return mboxID
}

func (s *testSession) batchMailboxCreated(user string, count int, mailboxNameGen func(number int) string) []imap.MailboxID {
	var mboxIDs []imap.MailboxID

	for i := 0; i < count; i++ {
		mboxID := imap.MailboxID(utils.NewRandomMailboxID())

		require.NoError(s.tb, s.conns[s.userIDs[user]].MailboxCreated(imap.Mailbox{
			ID:             mboxID,
			Name:           []string{mailboxNameGen(i)},
			Flags:          defaultFlags,
			PermanentFlags: defaultPermanentFlags,
			Attributes:     defaultAttributes,
		}))

		mboxIDs = append(mboxIDs, mboxID)
	}

	s.conns[s.userIDs[user]].Flush()

	return mboxIDs
}

func (s *testSession) mailboxCreatedCustom(user string, name []string, flags, permFlags, attrs imap.FlagSet) imap.MailboxID {
	mboxID := imap.MailboxID(utils.NewRandomMailboxID())

	require.NoError(s.tb, s.conns[s.userIDs[user]].MailboxCreated(imap.Mailbox{
		ID:             mboxID,
		Name:           name,
		Flags:          flags,
		PermanentFlags: permFlags,
		Attributes:     attrs,
	}))

	s.conns[s.userIDs[user]].Flush()

	return mboxID
}

func (s *testSession) messageCreatedWithMailboxes(user string, mailboxIDs []imap.MailboxID, literal []byte, internalDate time.Time, flags ...string) imap.MessageID {
	messageID := imap.MessageID(utils.NewRandomMessageID())

	require.NoError(s.tb, s.conns[s.userIDs[user]].MessageCreated(
		imap.Message{
			ID:    messageID,
			Flags: imap.NewFlagSetFromSlice(flags),
			Date:  internalDate,
		},
		literal,
		mailboxIDs,
	))

	s.conns[s.userIDs[user]].Flush()

	return messageID
}

func (s *testSession) messageCreated(user string, mailboxID imap.MailboxID, literal []byte, internalDate time.Time, flags ...string) imap.MessageID {
	messageID := imap.MessageID(utils.NewRandomMessageID())

	s.messageCreatedWithID(user, messageID, mailboxID, literal, internalDate, flags...)

	return messageID
}

func (s *testSession) messageCreatedWithID(user string, messageID imap.MessageID, mailboxID imap.MailboxID, literal []byte, internalDate time.Time, flags ...string) {
	require.NoError(s.tb, s.conns[s.userIDs[user]].MessageCreated(
		imap.Message{
			ID:    messageID,
			Flags: imap.NewFlagSetFromSlice(flags),
			Date:  internalDate,
		},
		literal,
		[]imap.MailboxID{mailboxID},
	))

	s.conns[s.userIDs[user]].Flush()
}

func (s *testSession) messageUpdatedWithID(user string, messageID imap.MessageID, mailboxID imap.MailboxID, literal []byte, internalDate time.Time, flags ...string) {
	require.NoError(s.tb, s.conns[s.userIDs[user]].MessageUpdated(
		imap.Message{
			ID:    messageID,
			Flags: imap.NewFlagSetFromSlice(flags),
			Date:  internalDate,
		},
		literal,
		[]imap.MailboxID{mailboxID},
	))

	s.conns[s.userIDs[user]].Flush()
}

func (s *testSession) batchMessageCreated(user string, mailboxID imap.MailboxID, count int, createMessage func(int) ([]byte, []string)) []imap.MessageID {
	return s.batchMessageCreatedWithID(user, mailboxID, count, func(i int) (imap.MessageID, []byte, []string) {
		messageID := imap.MessageID(utils.NewRandomMessageID())
		literal, flags := createMessage(i)

		return messageID, literal, flags
	})
}

func (s *testSession) batchMessageCreatedWithID(user string, mailboxID imap.MailboxID, count int, createMessage func(int) (imap.MessageID, []byte, []string)) []imap.MessageID {
	var messageIDs []imap.MessageID

	messages := make([]imap.Message, 0, count)
	literals := make([][]byte, 0, count)
	mailboxes := make([][]imap.MailboxID, 0, count)

	for i := 0; i < count; i++ {
		messageID, literal, flags := createMessage(i)

		messages = append(messages, imap.Message{
			ID:    messageID,
			Flags: imap.NewFlagSetFromSlice(flags),
			Date:  time.Now(),
		})

		literals = append(literals, literal)

		mailboxes = append(mailboxes, []imap.MailboxID{mailboxID})

		messageIDs = append(messageIDs, messageID)
	}

	require.NoError(s.tb, s.conns[s.userIDs[user]].MessagesCreated(messages, literals, mailboxes))

	s.conns[s.userIDs[user]].Flush()

	return messageIDs
}

func (s *testSession) messageCreatedFromFile(user string, mailboxID imap.MailboxID, path string, flags ...string) imap.MessageID {
	literal, err := os.ReadFile(path)
	require.NoError(s.tb, err)

	return s.messageCreated(user, mailboxID, literal, time.Now(), flags...)
}

func (s *testSession) messagesCreatedFromMBox(user string, mailboxID imap.MailboxID, path string, flags ...string) {
	f, err := os.Open(path)
	require.NoError(s.tb, err)

	require.NoError(s.tb, forMessageInMBox(f, func(messageDelimiter, literal []byte) {
		// If possible use mbox delimiter time as internal date to able to
		// test cases where header and internal date are different.
		internalDate, err := parseDateFromDelimiter(string(messageDelimiter))
		if err != nil {
			internalDate = time.Now()
		}

		s.messageCreated(user, mailboxID, literal, internalDate, flags...)
	}))

	require.NoError(s.tb, f.Close())
}

func (s *testSession) messageAdded(user string, messageID imap.MessageID, mailboxID imap.MailboxID) {
	require.NoError(s.tb, s.conns[s.userIDs[user]].MessageAdded(messageID, mailboxID))

	s.conns[s.userIDs[user]].Flush()
}

func (s *testSession) messageRemoved(user string, messageID imap.MessageID, mailboxID imap.MailboxID) {
	require.NoError(s.tb, s.conns[s.userIDs[user]].MessageRemoved(messageID, mailboxID))

	s.conns[s.userIDs[user]].Flush()
}

func (s *testSession) messageDeleted(user string, messageID imap.MessageID) {
	require.NoError(s.tb, s.conns[s.userIDs[user]].MessageDeleted(messageID))

	s.conns[s.userIDs[user]].Flush()
}

func (s *testSession) messageSeen(user string, messageID imap.MessageID, seen bool) {
	require.NoError(s.tb, s.conns[s.userIDs[user]].MessageSeen(messageID, seen))

	s.conns[s.userIDs[user]].Flush()
}

func (s *testSession) messageFlagged(user string, messageID imap.MessageID, flagged bool) {
	require.NoError(s.tb, s.conns[s.userIDs[user]].MessageFlagged(messageID, flagged))

	s.conns[s.userIDs[user]].Flush()
}

func (s *testSession) uidValidityBumped(user string) {
	s.conns[s.userIDs[user]].UIDValidityBumped()
}

func (s *testSession) flush(user string) {
	s.conns[s.userIDs[user]].Flush()
}

func (s *testSession) setUpdatesAllowedToFail(user string, value bool) {
	s.conns[s.userIDs[user]].SetUpdatesAllowedToFail(value)
}

func (s *testSession) removeAccount(t testing.TB, user string) string {
	userID := s.userIDs[user]

	s.conns[userID].Flush()
	require.NoError(t, s.server.RemoveUser(context.Background(), userID, true))

	delete(s.conns, userID)
	delete(s.userIDs, user)

	return userID
}

func forMessageInMBox(rr io.Reader, fn func(messageDelimiter, literal []byte)) error {
	mr := mbox.NewReader(rr)

	var (
		r   io.Reader
		err error
	)

	for r, err = mr.NextMessage(); err == nil; r, err = mr.NextMessage() {
		literal, err := io.ReadAll(r)
		if err != nil {
			return err
		}

		fn(mr.GetMessageDelimiter(), literal)
	}

	if !errors.Is(err, io.EOF) {
		return err
	}

	return nil
}

func parseDateFromDelimiter(messageDelimiter string) (t time.Time, err error) {
	split := strings.Split(messageDelimiter, " ")
	if len(split) <= 3 {
		return t, errors.New("not enough arguments in delimiter")
	}

	return time.Parse("Mon Jan _2 15:04:05 2006", strings.TrimSpace(strings.Join(split[2:], " ")))
}

func TestTooManyInvalidCommands(t *testing.T) {
	runOneToOneTestWithAuth(t, defaultServerOptions(t), func(c *testConnection, _ *testSession) {
		for i := 1; i <= 19; i++ {
			c.Cf("%d FOO", i).BAD(fmt.Sprintf("%d", i))
		}

		// The next command should fail; the server should disconnect the client.
		c.Cf("100 FOO").BAD("100")

		// The client should be disconnected.
		_, err := c.conn.Read(make([]byte, 1))
		require.Error(t, err)
	})
}

func TestResetTooManyInvalidCommands(t *testing.T) {
	runOneToOneTestWithAuth(t, defaultServerOptions(t), func(c *testConnection, _ *testSession) {
		for i := 1; i <= 19; i++ {
			c.Cf("%d FOO", i).BAD(fmt.Sprintf("%d", i))
		}

		// The next command should succeed; the counter should be reset.
		c.C("100 NOOP").OK("100")

		for i := 1; i <= 19; i++ {
			c.Cf("%d FOO", i).BAD(fmt.Sprintf("%d", i))
		}

		// The next command should fail; the server should disconnect the client.
		c.Cf("100 FOO").BAD("100")

		// The client should be disconnected.
		_, err := c.conn.Read(make([]byte, 1))
		require.Error(t, err)
	})
}