Skip to content

Commit 2f5ba01

Browse files
committed
Test: IMAP idle interruption COR-173
1 parent 575a9c0 commit 2f5ba01

2 files changed

Lines changed: 373 additions & 1 deletion

File tree

‎Package.swift‎

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -265,7 +265,7 @@ var targets: [Target] = [
265265
"unittest.cpp",
266266
"unittest.mm"
267267
],
268-
sources: ["CertificateUtilsTests.swift", "IMAPInterruptCurrentCommandTests.swift", "LibetpanHelperTests.swift", "unittest.swift"],
268+
sources: ["CertificateUtilsTests.swift", "IMAPIdleCancellationTests.swift", "IMAPInterruptCurrentCommandTests.swift", "LibetpanHelperTests.swift", "unittest.swift"],
269269
resources: [
270270
.copy("data")
271271
]
Lines changed: 372 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,372 @@
1+
//
2+
// IMAPIdleCancellationTests.swift
3+
// mailcore2
4+
//
5+
// Swift port of the "cancel wakes IDLE" half of upstream MailCore's tests/test-imap-idle.cpp
6+
// (MailCore/mailcore2 commit ab53363b). The other half - disconnect() racing a running idle() on
7+
// the core IMAPSession - needs the synchronous C++ session, which the Swift layer does not expose.
8+
//
9+
10+
// Darwin only: the tests need a POSIX listening socket, and the Android job builds the test target
11+
// without running it. Nothing here is platform-specific beyond that socket.
12+
#if canImport(Darwin)
13+
14+
import Darwin
15+
import Dispatch
16+
import Foundation
17+
import XCTest
18+
19+
#if SWIFT_PACKAGE
20+
import CMailCore
21+
#endif
22+
23+
@testable import MailCore
24+
25+
/// The least IMAP server that can take a MailCore session into IDLE: greeting, CAPABILITY with IDLE,
26+
/// LOGIN, LIST for the delimiter, SELECT, then "+ idling" and waiting for DONE. Everything else is answered with a tagged OK.
27+
private final class FakeIdleIMAPServer {
28+
29+
private let listeningSocket: Int32
30+
private let serverQueue = DispatchQueue(label: "FakeIdleIMAPServer")
31+
private let condition = NSCondition()
32+
private var clientSocket: Int32 = -1
33+
private var idleEntered = false
34+
private var doneReceived = false
35+
private var connectionClosed = false
36+
private var isStopped = false
37+
private var transcriptLines: [String] = []
38+
39+
/// Every line the client sent, for diagnostics.
40+
var transcript: String {
41+
condition.lock()
42+
defer { condition.unlock() }
43+
return transcriptLines.joined(separator: "\n")
44+
}
45+
46+
let port: UInt16
47+
48+
init() throws {
49+
let fileDescriptor = socket(AF_INET, SOCK_STREAM, 0)
50+
guard fileDescriptor >= 0 else {
51+
throw NSError(domain: "FakeIdleIMAPServer", code: Int(errno), userInfo: nil)
52+
}
53+
54+
var reuse: Int32 = 1
55+
setsockopt(fileDescriptor, SOL_SOCKET, SO_REUSEADDR, &reuse, socklen_t(MemoryLayout<Int32>.size))
56+
57+
var address = sockaddr_in()
58+
address.sin_family = sa_family_t(AF_INET)
59+
address.sin_port = 0 // any free port
60+
address.sin_addr = in_addr(s_addr: inet_addr("127.0.0.1"))
61+
62+
let bound = withUnsafePointer(to: &address) { pointer -> Int32 in
63+
return pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { sockaddrPointer in
64+
return bind(fileDescriptor, sockaddrPointer, socklen_t(MemoryLayout<sockaddr_in>.size))
65+
}
66+
}
67+
68+
guard bound == 0, listen(fileDescriptor, 1) == 0 else {
69+
close(fileDescriptor)
70+
throw NSError(domain: "FakeIdleIMAPServer", code: Int(errno), userInfo: nil)
71+
}
72+
73+
var boundAddress = sockaddr_in()
74+
var length = socklen_t(MemoryLayout<sockaddr_in>.size)
75+
let named = withUnsafeMutablePointer(to: &boundAddress) { pointer -> Int32 in
76+
return pointer.withMemoryRebound(to: sockaddr.self, capacity: 1) { sockaddrPointer in
77+
return getsockname(fileDescriptor, sockaddrPointer, &length)
78+
}
79+
}
80+
81+
guard named == 0 else {
82+
close(fileDescriptor)
83+
throw NSError(domain: "FakeIdleIMAPServer", code: Int(errno), userInfo: nil)
84+
}
85+
86+
listeningSocket = fileDescriptor
87+
port = UInt16(bigEndian: boundAddress.sin_port)
88+
89+
serverQueue.async { [weak self] in
90+
self?.serve()
91+
}
92+
}
93+
94+
// MARK: Observation
95+
96+
/// True once the client has sent IDLE and been answered with "+ idling".
97+
func waitForIdleEntered(timeout: TimeInterval) -> Bool {
98+
return wait(timeout: timeout) { self.idleEntered }
99+
}
100+
101+
/// True once the client either sent DONE or closed the connection - the two ways a running IDLE
102+
/// can legitimately end.
103+
func waitForDoneOrClose(timeout: TimeInterval) -> Bool {
104+
return wait(timeout: timeout) { self.doneReceived || self.connectionClosed }
105+
}
106+
107+
private func wait(timeout: TimeInterval, until predicate: () -> Bool) -> Bool {
108+
let deadline = Date(timeIntervalSinceNow: timeout)
109+
condition.lock()
110+
defer { condition.unlock() }
111+
while !predicate() {
112+
if !condition.wait(until: deadline) {
113+
return predicate()
114+
}
115+
}
116+
return true
117+
}
118+
119+
private func mark(_ change: () -> Void) {
120+
condition.lock()
121+
change()
122+
condition.broadcast()
123+
condition.unlock()
124+
}
125+
126+
func stop() {
127+
condition.lock()
128+
guard !isStopped else {
129+
condition.unlock()
130+
return
131+
}
132+
isStopped = true
133+
let client = clientSocket
134+
condition.unlock()
135+
136+
shutdown(listeningSocket, SHUT_RDWR)
137+
Darwin.close(listeningSocket)
138+
if client >= 0 {
139+
shutdown(client, SHUT_RDWR)
140+
Darwin.close(client)
141+
}
142+
}
143+
144+
// MARK: Protocol
145+
146+
private func serve() {
147+
let accepted = accept(listeningSocket, nil, nil)
148+
guard accepted >= 0 else {
149+
return // stopped before a client arrived
150+
}
151+
mark { clientSocket = accepted }
152+
153+
guard send("* OK fake imap ready\r\n") else {
154+
mark { connectionClosed = true }
155+
return
156+
}
157+
158+
var idleTag = ""
159+
while let line = readLine() {
160+
mark { transcriptLines.append(line) }
161+
if line == "DONE" {
162+
mark { doneReceived = true }
163+
if !idleTag.isEmpty {
164+
_ = send("\(idleTag) OK IDLE done\r\n")
165+
}
166+
continue
167+
}
168+
169+
let tag = String(line.prefix { $0 != " " })
170+
let upper = line.uppercased()
171+
let ok: Bool
172+
if upper.contains(" CAPABILITY") {
173+
ok = send("* CAPABILITY IMAP4rev1 IDLE\r\n\(tag) OK CAPABILITY done\r\n")
174+
}
175+
else if upper.contains(" LOGIN") {
176+
ok = send("\(tag) OK LOGIN done\r\n")
177+
}
178+
else if upper.contains(" LIST") {
179+
// Readdle's fork asks for the hierarchy delimiter before selecting anything.
180+
ok = send("* LIST (\\Noselect) \"/\" \"\"\r\n\(tag) OK LIST done\r\n")
181+
}
182+
else if upper.contains(" SELECT") {
183+
ok = send("* FLAGS (\\Seen)\r\n"
184+
+ "* 0 EXISTS\r\n"
185+
+ "* 0 RECENT\r\n"
186+
+ "* OK [UIDVALIDITY 1]\r\n"
187+
+ "* OK [UIDNEXT 1]\r\n"
188+
+ "* OK [PERMANENTFLAGS (\\Seen)]\r\n"
189+
+ "\(tag) OK [READ-WRITE] SELECT done\r\n")
190+
}
191+
else if upper.contains(" IDLE") {
192+
idleTag = tag
193+
ok = send("+ idling\r\n")
194+
if ok {
195+
mark { idleEntered = true }
196+
}
197+
}
198+
else if upper.contains(" LOGOUT") {
199+
_ = send("* BYE logging out\r\n\(tag) OK LOGOUT done\r\n")
200+
mark { connectionClosed = true }
201+
return
202+
}
203+
else {
204+
ok = send("\(tag) OK ignored\r\n")
205+
}
206+
207+
if !ok {
208+
mark { connectionClosed = true }
209+
return
210+
}
211+
}
212+
mark { connectionClosed = true }
213+
}
214+
215+
private func send(_ text: String) -> Bool {
216+
var bytes = Array(text.utf8)
217+
var remaining = bytes.count
218+
var offset = 0
219+
while remaining > 0 {
220+
let written = bytes.withUnsafeMutableBytes { buffer -> Int in
221+
return Darwin.send(clientSocket, buffer.baseAddress! + offset, remaining, 0)
222+
}
223+
guard written > 0 else {
224+
return false
225+
}
226+
offset += written
227+
remaining -= written
228+
}
229+
return true
230+
}
231+
232+
private func readLine() -> String? {
233+
var line: [UInt8] = []
234+
var byte: UInt8 = 0
235+
while true {
236+
let count = recv(clientSocket, &byte, 1, 0)
237+
if count == 0 {
238+
return nil
239+
}
240+
if count < 0 {
241+
if errno == EINTR {
242+
continue
243+
}
244+
return nil
245+
}
246+
if byte == UInt8(ascii: "\n") {
247+
if line.last == UInt8(ascii: "\r") {
248+
line.removeLast()
249+
}
250+
return String(decoding: line, as: UTF8.self)
251+
}
252+
line.append(byte)
253+
}
254+
}
255+
}
256+
257+
final class IMAPIdleCancellationTests: XCTestCase {
258+
259+
/// Well above every wait below: an IDLE left alone must not be able to end on its own and pass a
260+
/// test that is about being woken up. (IMAPSession itself caps IDLE at 28 minutes.)
261+
private let sessionTimeout: TimeInterval = 60
262+
263+
private func makeSession(port: UInt16) -> MCOIMAPSession {
264+
let session = MCOIMAPSession()
265+
session.hostname = "127.0.0.1"
266+
session.port = UInt32(port)
267+
session.connectionType = ConnectionTypeClear
268+
session.username = "user"
269+
session.password = "password"
270+
session.timeout = sessionTimeout
271+
session.maximumConnections = 1
272+
return session
273+
}
274+
275+
/// Runs the test body off the main thread while the main thread keeps spinning its run loop.
276+
/// mailcore hands parts of an operation's lifecycle to the main queue and waits for them, so a
277+
/// test that blocks the main thread never gets its operation started in the first place.
278+
private func runOffMainThread(timeout: TimeInterval, _ body: @escaping () -> Void) {
279+
let finished = expectation(description: "test body")
280+
281+
DispatchQueue.global(qos: .userInitiated).async {
282+
body()
283+
finished.fulfill()
284+
}
285+
286+
waitForExpectations(timeout: timeout)
287+
}
288+
289+
private func startIdle(_ session: MCOIMAPSession) -> (MCOIMAPIdleOperation, DispatchSemaphore) {
290+
let finished = DispatchSemaphore(value: 0)
291+
let operation = session.idleOperation(folder: "INBOX", lastKnownUID: 0)
292+
operation.start { error in
293+
if let error = error {
294+
print("IMAPIdleCancellationTests: idle finished with error \(error)")
295+
}
296+
finished.signal()
297+
}
298+
return (operation, finished)
299+
}
300+
301+
private func waitUntilQueueStopped(_ session: MCOIMAPSession, timeout: TimeInterval) -> Bool {
302+
let deadline = Date(timeIntervalSinceNow: timeout)
303+
while session.isOperationQueueRunning {
304+
if Date() >= deadline {
305+
return false
306+
}
307+
Thread.sleep(forTimeInterval: 0.01)
308+
}
309+
return true
310+
}
311+
312+
/// Control case: `interruptIdle()` is the documented way to end an IDLE early, and it must work
313+
/// against the fake server before the cancel test below means anything.
314+
func testInterruptIdleEndsRunningIdle() throws {
315+
let server = try FakeIdleIMAPServer()
316+
defer { server.stop() }
317+
318+
let session = makeSession(port: server.port)
319+
320+
runOffMainThread(timeout: 30) {
321+
let (operation, finished) = self.startIdle(session)
322+
323+
XCTAssertTrue(server.waitForIdleEntered(timeout: 5), "The session was expected to enter IDLE. Client sent:\n\(server.transcript)")
324+
XCTAssertEqual(finished.wait(timeout: .now() + 1), .timedOut, "IDLE was expected to still be running")
325+
326+
operation.interruptIdle()
327+
328+
XCTAssertEqual(finished.wait(timeout: .now() + 5), .success, "interruptIdle() did not end the IDLE")
329+
XCTAssertTrue(server.waitForDoneOrClose(timeout: 5), "The server saw neither DONE nor a close")
330+
XCTAssertTrue(self.waitUntilQueueStopped(session, timeout: 5), "The operation queue did not stop")
331+
}
332+
}
333+
334+
/// Upstream's `testCancelWakesIdleIteration`: cancelling every operation on the session has to
335+
/// wake a running IDLE. Upstream made this true by having IMAPIdleOperation::cancel() call
336+
/// interruptIdle(); without that, cancel only flips a flag the blocked IDLE never looks at, and
337+
/// everything queued behind it - a disconnect included - waits for the IDLE to time out.
338+
func testCancelAllOperationsWakesRunningIdle() throws {
339+
let server = try FakeIdleIMAPServer()
340+
defer { server.stop() }
341+
342+
let session = makeSession(port: server.port)
343+
344+
runOffMainThread(timeout: 30) {
345+
let (operation, finished) = self.startIdle(session)
346+
347+
XCTAssertTrue(server.waitForIdleEntered(timeout: 5), "The session was expected to enter IDLE. Client sent:\n\(server.transcript)")
348+
XCTAssertEqual(finished.wait(timeout: .now() + 1), .timedOut, "IDLE was expected to still be running")
349+
350+
session.cancelAllOperations()
351+
352+
let stopped = self.waitUntilQueueStopped(session, timeout: 5)
353+
// Known gap until upstream 03a19472 ("Fix IMAP IDLE teardown races") is merged: the
354+
// expectation is strict, so the test fails the day the fix lands and this block must go.
355+
// Spark does not depend on it today - it never calls cancelAllOperations() on an IMAP
356+
// session and always interruptIdle()s before disconnecting the idle session.
357+
XCTExpectFailure("IMAPIdleOperation::cancel() does not interrupt a running IDLE yet") {
358+
XCTAssertTrue(stopped, "cancelAllOperations() did not wake the running IDLE")
359+
}
360+
if stopped {
361+
XCTAssertTrue(server.waitForDoneOrClose(timeout: 5), "The server saw neither DONE nor a close")
362+
}
363+
else {
364+
// Do not leave the IDLE blocked behind us: end it the way that is known to work.
365+
operation.interruptIdle()
366+
_ = self.waitUntilQueueStopped(session, timeout: 5)
367+
}
368+
}
369+
}
370+
}
371+
372+
#endif

0 commit comments

Comments
 (0)