/
imap_response.go
220 lines (194 loc) · 6.34 KB
/
imap_response.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
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
// Copyright (c) 2022 Proton AG
//
// This file is part of Proton Mail Bridge.Bridge.
//
// Proton Mail Bridge is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
//
// Proton Mail Bridge is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with Proton Mail Bridge. If not, see <https://www.gnu.org/licenses/>.
package mocks
import (
"bufio"
"fmt"
"io"
"regexp"
"strings"
"time"
"github.com/pkg/errors"
a "github.com/stretchr/testify/assert"
)
type IMAPResponse struct {
t TestingT
err error
result string
sections []string
done bool
}
func (ir *IMAPResponse) sendCommand(reqTag string, reqIndex int, command string, debug *debug, conn io.Writer, response *bufio.Reader) { //nolint:interfacer
defer func() { ir.done = true }()
tstart := time.Now()
commandID := fmt.Sprintf("%sO%0d", reqTag, reqIndex)
command = fmt.Sprintf("%s %s", commandID, command)
debug.printReq(command)
fmt.Fprintf(conn, "%s\r\n", command)
var section string
for {
line, err := response.ReadString('\n')
if err != nil {
ir.err = errors.Wrap(err, "read response failed")
debug.printErr(ir.err.Error() + "\n")
return
}
// Finishing line contains `commandID` following with status (`NO`, `BAD`, ...) and then message itself.
lineWithoutID := strings.Replace(line, commandID+" ", "", 1)
if strings.HasPrefix(line, commandID) && (strings.HasPrefix(lineWithoutID, "NO ") || strings.HasPrefix(lineWithoutID, "BAD ")) {
debug.printErr(line)
err := errors.New(strings.Trim(lineWithoutID, "\r\n"))
ir.err = errors.Wrap(err, "IMAP error")
return
} else if command != "" && len(line) == 0 {
err := errors.New("empty answer")
ir.err = errors.Wrap(err, "IMAP error")
debug.printErr(ir.err.Error() + "\n")
return
}
debug.printRes(line)
if strings.HasPrefix(line, "* ") { //nolint:gocritic
if section != "" {
ir.sections = append(ir.sections, section)
}
section = line
} else if strings.HasPrefix(line, commandID) {
if section != "" {
ir.sections = append(ir.sections, section)
}
ir.result = line
break
} else {
section += line
}
}
debug.printTime(time.Since(tstart))
}
func (ir *IMAPResponse) wait() {
for {
if ir.done {
break
}
time.Sleep(50 * time.Millisecond)
}
}
func (ir *IMAPResponse) AssertOK() *IMAPResponse {
ir.wait()
a.NoError(ir.t, ir.err)
return ir
}
func (ir *IMAPResponse) Sections() []string {
ir.wait()
return ir.sections
}
func (ir *IMAPResponse) AssertResult(wantResult string) *IMAPResponse {
ir.wait()
a.NoError(ir.t, ir.err)
a.Regexp(ir.t, wantResult, ir.result, "Expected result %s but got %s", wantResult, ir.result)
return ir
}
func (ir *IMAPResponse) AssertError(wantErrMsg string) *IMAPResponse {
ir.wait()
if ir.err == nil {
a.Fail(ir.t, "Error is nil", "Expected to have %q", wantErrMsg)
} else {
a.Regexp(ir.t, wantErrMsg, ir.err.Error(), "Expected error %s but got %s", wantErrMsg, ir.err)
}
return ir
}
func (ir *IMAPResponse) AssertSectionsCount(expectedCount int) *IMAPResponse {
ir.wait()
a.Equal(ir.t, expectedCount, len(ir.sections))
return ir
}
// AssertSectionsInOrder checks sections against regular expression in exact order.
// First regexp checks first section, second the second and so on. If there is
// more responses (sections) than expected regexps, that's OK.
func (ir *IMAPResponse) AssertSectionsInOrder(wantRegexps ...string) *IMAPResponse {
ir.wait()
if !a.True(ir.t,
len(ir.sections) >= len(wantRegexps),
"Wrong number of sections, want %v, got %v",
len(wantRegexps),
len(ir.sections),
) {
return ir
}
for idx, wantRegexp := range wantRegexps {
section := ir.sections[idx]
match, err := regexp.MatchString(wantRegexp, section)
if !a.NoError(ir.t, err) {
return ir
}
if !a.True(ir.t, match, "Section does not match given regex", section, wantRegexp) {
return ir
}
}
return ir
}
// AssertSections is similar to AssertSectionsInOrder but is not strict to the order.
// It means it just tries to find all "regexps" in the response.
func (ir *IMAPResponse) AssertSections(wantRegexps ...string) *IMAPResponse {
ir.wait()
for _, wantRegexp := range wantRegexps {
a.NoError(ir.t, ir.hasSectionRegexp(wantRegexp), "regexp %v not found\nSections: %v", wantRegexp, ir.sections)
}
return ir
}
// AssertNotSections is similar to AssertSections but is the opposite.
// It means it just tries to find all "regexps" in the response.
func (ir *IMAPResponse) AssertNotSections(unwantedRegexps ...string) *IMAPResponse {
ir.wait()
for _, unwantedRegexp := range unwantedRegexps {
a.Error(ir.t, ir.hasSectionRegexp(unwantedRegexp), "regexp %v found\nSections: %v", unwantedRegexp, ir.sections)
}
return ir
}
// WaitForSections is the same as AssertSections but waits for `timeout` before giving up.
func (ir *IMAPResponse) WaitForSections(timeout time.Duration, wantRegexps ...string) {
a.Eventually(ir.t, func() bool {
return ir.HasSections(wantRegexps...)
}, timeout, 50*time.Millisecond, "Wanted sections: %v\nSections: %v", wantRegexps, &ir.sections)
}
// WaitForNotSections is the opposite of WaitForSection: waits to not have the response.
func (ir *IMAPResponse) WaitForNotSections(timeout time.Duration, unwantedRegexps ...string) *IMAPResponse {
time.Sleep(timeout)
match := ir.HasSections(unwantedRegexps...)
a.False(ir.t, match, "Unwanted sections: %v\nSections: %v", unwantedRegexps, &ir.sections)
return ir
}
// HasSections is the same as AssertSections but only returns bool (do not uses testingT).
func (ir *IMAPResponse) HasSections(wantRegexps ...string) bool {
for _, wantRegexp := range wantRegexps {
if err := ir.hasSectionRegexp(wantRegexp); err != nil {
return false
}
}
return true
}
func (ir *IMAPResponse) hasSectionRegexp(wantRegexp string) error {
for _, section := range ir.sections {
match, err := regexp.MatchString(wantRegexp, section)
if err != nil {
return err
}
if match {
return nil
}
}
return errors.New("Section matching given regex not found")
}