| 1 |
// SPDX-License-Identifier: GPL-3.0-or-later |
| 2 |
#include "tests_clipboarddataguard.h" |
| 3 |
|
| 4 |
#include "common/clipboarddataguard.h" |
| 5 |
|
| 6 |
#include <QMimeData> |
| 7 |
#include <QSet> |
| 8 |
#include <QTest> |
| 9 |
#include <QUrl> |
| 10 |
|
| 11 |
#include <new> |
| 12 |
|
| 13 |
ClipboardDataGuardTests::ClipboardDataGuardTests(const TestInterfacePtr &, QObject *parent) |
| 14 |
: QObject(parent) |
| 15 |
{ |
| 16 |
setProperty("CopyQ_test_id", QStringLiteral("clipboarddataguard")); |
| 17 |
} |
| 18 |
|
| 19 |
class ThrowingMimeData : public QMimeData { |
| 20 |
public: |
| 21 |
void setThrowFor(const QString &mime) { m_throwMimes.insert(mime); } |
| 22 |
protected: |
| 23 |
QVariant retrieveData(const QString &mime, QMetaType type) const override { |
| 24 |
if (m_throwMimes.contains(mime)) |
| 25 |
throw std::bad_alloc(); |
| 26 |
return QMimeData::retrieveData(mime, type); |
| 27 |
} |
| 28 |
private: |
| 29 |
QSet<QString> m_throwMimes; |
| 30 |
}; |
| 31 |
|
| 32 |
void ClipboardDataGuardTests::badAllocData() |
| 33 |
{ |
| 34 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", ".*:-1"); |
| 35 |
ThrowingMimeData md; |
| 36 |
md.setData("text/plain", "hello"); |
| 37 |
md.setThrowFor("text/plain"); |
| 38 |
ClipboardDataGuard guard(&md); |
| 39 |
QVERIFY(guard.data("text/plain").isEmpty()); |
| 40 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 41 |
} |
| 42 |
|
| 43 |
void ClipboardDataGuardTests::badAllocUrls() |
| 44 |
{ |
| 45 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", ".*:-1"); |
| 46 |
ThrowingMimeData md; |
| 47 |
md.setThrowFor("text/uri-list"); |
| 48 |
ClipboardDataGuard guard(&md); |
| 49 |
QVERIFY(guard.urls().isEmpty()); |
| 50 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 51 |
} |
| 52 |
|
| 53 |
void ClipboardDataGuardTests::badAllocText() |
| 54 |
{ |
| 55 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", ".*:-1"); |
| 56 |
ThrowingMimeData md; |
| 57 |
md.setThrowFor("text/plain"); |
| 58 |
ClipboardDataGuard guard(&md); |
| 59 |
QVERIFY(guard.text().isEmpty()); |
| 60 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 61 |
} |
| 62 |
|
| 63 |
void ClipboardDataGuardTests::badAllocImageData() |
| 64 |
{ |
| 65 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", ".*:-1"); |
| 66 |
ThrowingMimeData md; |
| 67 |
md.setThrowFor("application/x-qt-image"); |
| 68 |
ClipboardDataGuard guard(&md); |
| 69 |
QVERIFY(guard.getImageData().isNull()); |
| 70 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 71 |
} |
| 72 |
|
| 73 |
void ClipboardDataGuardTests::badAllocGetUtf8Data() |
| 74 |
{ |
| 75 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", ".*:-1"); |
| 76 |
ThrowingMimeData md; |
| 77 |
md.setThrowFor("text/html"); |
| 78 |
ClipboardDataGuard guard(&md); |
| 79 |
QVERIFY(guard.getUtf8Data("text/html").isEmpty()); |
| 80 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 81 |
} |
| 82 |
|
| 83 |
void ClipboardDataGuardTests::normalDataStillWorks() |
| 84 |
{ |
| 85 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", ".*:-1"); |
| 86 |
ThrowingMimeData md; |
| 87 |
md.setData("application/octet-stream", "binary"); |
| 88 |
md.setThrowFor("text/plain"); // only text/plain throws |
| 89 |
ClipboardDataGuard guard(&md); |
| 90 |
QCOMPARE(guard.data("application/octet-stream"), QByteArray("binary")); |
| 91 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 92 |
} |
| 93 |
|
| 94 |
static void bumpConfigGeneration() |
| 95 |
{ |
| 96 |
const int gen = qApp->property("CopyQ_config_generation").toInt(); |
| 97 |
qApp->setProperty("CopyQ_config_generation", gen + 1); |
| 98 |
} |
| 99 |
|
| 100 |
void ClipboardDataGuardTests::overflowTreatedAsNoLimit() |
| 101 |
{ |
| 102 |
// 9999999999G overflows qint64 (9999999999 * 1073741824 > INT64_MAX). |
| 103 |
// parseByteSize must return -1 (no limit), so data passes through. |
| 104 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", ".*:9999999999G"); |
| 105 |
bumpConfigGeneration(); |
| 106 |
QMimeData md; |
| 107 |
md.setData("text/plain", "overflow-test"); |
| 108 |
ClipboardDataGuard guard(&md); |
| 109 |
QCOMPARE(guard.data("text/plain"), QByteArray("overflow-test")); |
| 110 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 111 |
bumpConfigGeneration(); |
| 112 |
} |
| 113 |
|
| 114 |
void ClipboardDataGuardTests::emptyRulesIgnored() |
| 115 |
{ |
| 116 |
// Empty entries from leading/trailing/consecutive semicolons are skipped. |
| 117 |
// The valid rule text/plain:5 must still apply. |
| 118 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", ";text/plain:5;;"); |
| 119 |
bumpConfigGeneration(); |
| 120 |
QMimeData md; |
| 121 |
md.setData("text/plain", "hi"); |
| 122 |
ClipboardDataGuard guard(&md); |
| 123 |
QCOMPARE(guard.data("text/plain"), QByteArray("hi")); |
| 124 |
|
| 125 |
// Oversized for the 5-byte limit. |
| 126 |
QMimeData md2; |
| 127 |
md2.setData("text/plain", "this is way over five bytes"); |
| 128 |
ClipboardDataGuard guard2(&md2); |
| 129 |
QVERIFY(guard2.data("text/plain").isEmpty()); |
| 130 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 131 |
bumpConfigGeneration(); |
| 132 |
} |
| 133 |
|
| 134 |
|
| 135 |
void ClipboardDataGuardTests::negativeValueMeansNoLimit() |
| 136 |
{ |
| 137 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", "text/plain:-1"); |
| 138 |
bumpConfigGeneration(); |
| 139 |
QMimeData md; |
| 140 |
md.setData("text/plain", "anything"); |
| 141 |
ClipboardDataGuard guard(&md); |
| 142 |
QCOMPARE(guard.data("text/plain"), QByteArray("anything")); |
| 143 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 144 |
bumpConfigGeneration(); |
| 145 |
} |
| 146 |
|
| 147 |
void ClipboardDataGuardTests::invalidRulesFallBackToDefault() |
| 148 |
{ |
| 149 |
// .*:abc is not a valid size — falls back to built-in default (.*:100M). |
| 150 |
// Small data still passes. |
| 151 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", ".*:abc"); |
| 152 |
bumpConfigGeneration(); |
| 153 |
QMimeData md; |
| 154 |
md.setData("text/plain", "small"); |
| 155 |
ClipboardDataGuard guard(&md); |
| 156 |
QCOMPARE(guard.data("text/plain"), QByteArray("small")); |
| 157 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 158 |
bumpConfigGeneration(); |
| 159 |
} |
| 160 |
|
| 161 |
void ClipboardDataGuardTests::suffixParsing() |
| 162 |
{ |
| 163 |
// K suffix: 1K = 1024 bytes. |
| 164 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", "text/plain:1K"); |
| 165 |
bumpConfigGeneration(); |
| 166 |
QMimeData md; |
| 167 |
md.setData("text/plain", "short"); |
| 168 |
ClipboardDataGuard guard(&md); |
| 169 |
QCOMPARE(guard.data("text/plain"), QByteArray("short")); |
| 170 |
|
| 171 |
// G suffix: 1G = huge limit, small data passes. |
| 172 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", "text/plain:1G"); |
| 173 |
bumpConfigGeneration(); |
| 174 |
QMimeData md2; |
| 175 |
md2.setData("text/plain", "gtest"); |
| 176 |
ClipboardDataGuard guard2(&md2); |
| 177 |
QCOMPARE(guard2.data("text/plain"), QByteArray("gtest")); |
| 178 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 179 |
bumpConfigGeneration(); |
| 180 |
} |
| 181 |
|
| 182 |
void ClipboardDataGuardTests::firstMatchWins() |
| 183 |
{ |
| 184 |
// First rule blocks text/plain, second allows all. |
| 185 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", "text/plain:0;.*:100M"); |
| 186 |
bumpConfigGeneration(); |
| 187 |
QMimeData md; |
| 188 |
md.setData("text/plain", "blocked"); |
| 189 |
md.setData("application/octet-stream", "allowed"); |
| 190 |
ClipboardDataGuard guard(&md); |
| 191 |
QVERIFY(guard.data("text/plain").isEmpty()); |
| 192 |
QCOMPARE(guard.data("application/octet-stream"), QByteArray("allowed")); |
| 193 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 194 |
bumpConfigGeneration(); |
| 195 |
} |
| 196 |
|
| 197 |
void ClipboardDataGuardTests::emptySizeMeansNoLimit() |
| 198 |
{ |
| 199 |
// "text/plain:" (empty size after colon) means no limit. |
| 200 |
qputenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT", "text/plain:"); |
| 201 |
bumpConfigGeneration(); |
| 202 |
QMimeData md; |
| 203 |
md.setData("text/plain", "any data at all"); |
| 204 |
ClipboardDataGuard guard(&md); |
| 205 |
QCOMPARE(guard.data("text/plain"), QByteArray("any data at all")); |
| 206 |
qunsetenv("COPYQ_CLIPBOARD_MIME_SIZE_LIMIT"); |
| 207 |
bumpConfigGeneration(); |
| 208 |
} |