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
|
/*
* qtmaildir - a Qt6 mail client for notmuch-indexed Maildirs
* Copyright (C) 2026 Danilo M. <danix@danix.xyz>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program 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 this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
*/
#include <QtTest>
#include "launchselectors.h"
/// The command line and the socket payload, over values. No window, no
/// QApplication: this is the half of item 200 that can be asserted exactly,
/// which is why it is its own unit rather than code inside main.cpp.
class TestLaunchSelectors : public QObject
{
Q_OBJECT
private slots:
void anEmptyCommandLineSelectsNothing();
void eachSelectorIsParsed();
void theThreeSelectorsCompose();
void anUnknownOptionIsReportedNotFatal();
void aPayloadRoundTrips();
void anEmptyPayloadRoundTripsToNothing();
void aTruncatedPayloadIsRejected();
void anOversizedPayloadIsRejected();
};
void TestLaunchSelectors::anEmptyCommandLineSelectsNothing()
{
QString error;
const LaunchSelectors selectors =
LaunchSelectors::parse({ QStringLiteral("qtmaildir") }, &error);
QVERIFY(error.isEmpty());
QVERIFY(selectors.isEmpty());
QVERIFY(selectors.account.isEmpty());
QVERIFY(selectors.threadId.isEmpty());
QVERIFY(selectors.messageId.isEmpty());
}
void TestLaunchSelectors::eachSelectorIsParsed()
{
QString error;
const LaunchSelectors account = LaunchSelectors::parse(
{ QStringLiteral("qtmaildir"), QStringLiteral("--account"),
QStringLiteral("work") }, &error);
QVERIFY(error.isEmpty());
QCOMPARE(account.account, QStringLiteral("work"));
QVERIFY(!account.isEmpty());
const LaunchSelectors thread = LaunchSelectors::parse(
{ QStringLiteral("qtmaildir"), QStringLiteral("--thread"),
QStringLiteral("0000000000001a2b") }, &error);
QVERIFY(error.isEmpty());
QCOMPARE(thread.threadId, QStringLiteral("0000000000001a2b"));
const LaunchSelectors message = LaunchSelectors::parse(
{ QStringLiteral("qtmaildir"), QStringLiteral("--message"),
QStringLiteral("<abc@example.org>") }, &error);
QVERIFY(error.isEmpty());
QCOMPARE(message.messageId, QStringLiteral("<abc@example.org>"));
}
void TestLaunchSelectors::theThreeSelectorsCompose()
{
// They are not exclusive: "open this message, in this account's view" is
// one sensible request, and the spec says they compose.
QString error;
const LaunchSelectors selectors = LaunchSelectors::parse(
{ QStringLiteral("qtmaildir"),
QStringLiteral("--account"), QStringLiteral("work"),
QStringLiteral("--thread"), QStringLiteral("00001a2b"),
QStringLiteral("--message"), QStringLiteral("<abc@example.org>") },
&error);
QVERIFY(error.isEmpty());
QCOMPARE(selectors.account, QStringLiteral("work"));
QCOMPARE(selectors.threadId, QStringLiteral("00001a2b"));
QCOMPARE(selectors.messageId, QStringLiteral("<abc@example.org>"));
}
void TestLaunchSelectors::anUnknownOptionIsReportedNotFatal()
{
// Reported so main() can print it, and NOT a crash or a silent ignore.
// Today's strcmp loop ignores everything it does not know, which is how a
// typo currently produces a normal window and no clue.
QString error;
const LaunchSelectors selectors = LaunchSelectors::parse(
{ QStringLiteral("qtmaildir"), QStringLiteral("--nonsense") }, &error);
QVERIFY(!error.isEmpty());
QVERIFY(selectors.isEmpty());
}
void TestLaunchSelectors::aPayloadRoundTrips()
{
// What crosses the socket. A round trip is the whole contract: the values
// that go in are the values that come out, including one with an embedded
// newline, which is what defeats a line-based format.
LaunchSelectors original;
original.account = QStringLiteral("work");
original.threadId = QStringLiteral("00001a2b");
original.messageId = QStringLiteral("<a\nb@example.org>");
QString error;
const LaunchSelectors parsed =
LaunchSelectors::fromPayload(original.toPayload(), &error);
QVERIFY(error.isEmpty());
QCOMPARE(parsed.account, original.account);
QCOMPARE(parsed.threadId, original.threadId);
QCOMPARE(parsed.messageId, original.messageId);
}
void TestLaunchSelectors::anEmptyPayloadRoundTripsToNothing()
{
// A bare `qtmaildir` with a running instance still sends a payload: it
// means "raise yourself", which is a real request and not an error.
QString error;
const LaunchSelectors parsed =
LaunchSelectors::fromPayload(LaunchSelectors().toPayload(), &error);
QVERIFY(error.isEmpty());
QVERIFY(parsed.isEmpty());
}
void TestLaunchSelectors::aTruncatedPayloadIsRejected()
{
// The socket hands over whatever it is given. A half-written payload must
// be refused rather than half-applied.
LaunchSelectors original;
original.account = QStringLiteral("work");
const QByteArray payload = original.toPayload();
QVERIFY(payload.size() > 4);
QString error;
const LaunchSelectors parsed =
LaunchSelectors::fromPayload(payload.left(payload.size() - 2), &error);
QVERIFY(!error.isEmpty());
QVERIFY(parsed.isEmpty());
}
void TestLaunchSelectors::anOversizedPayloadIsRejected()
{
// A cap, because a local socket will hand over as much as the peer sends.
// The peer is the user's own process, so this is not a hostile-input
// defence; it is what stops a confused writer from being read as a
// gigabyte of selector.
QString error;
const LaunchSelectors parsed = LaunchSelectors::fromPayload(
QByteArray(LaunchSelectors::kMaxPayloadBytes + 1, 'x'), &error);
QVERIFY(!error.isEmpty());
QVERIFY(parsed.isEmpty());
}
QTEST_MAIN(TestLaunchSelectors)
#include "test_launchselectors.moc"
|