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
|
/****************************************************************************
**
** Copyright (C) 2017 The Qt Company Ltd.
** Contact: https://www.qt.io/licensing/
**
** This file is part of the test suite of the Qt Toolkit.
**
** $QT_BEGIN_LICENSE:GPL-EXCEPT$
** Commercial License Usage
** Licensees holding valid commercial Qt licenses may use this file in
** accordance with the commercial license agreement provided with the
** Software or, alternatively, in accordance with the terms contained in
** a written agreement between you and The Qt Company. For licensing terms
** and conditions see https://www.qt.io/terms-conditions. For further
** information use the contact form at https://www.qt.io/contact-us.
**
** GNU General Public License Usage
** Alternatively, this file may be used under the terms of the GNU
** General Public License version 3 as published by the Free Software
** Foundation with exceptions as appearing in the file LICENSE.GPL3-EXCEPT
** included in the packaging of this file. Please review the following
** information to ensure the GNU General Public License requirements will
** be met: https://www.gnu.org/licenses/gpl-3.0.html.
**
** $QT_END_LICENSE$
**
****************************************************************************/
//TESTED_COMPONENT=src/versit
#include "tst_qvcard30writer.h"
#ifdef QT_BUILD_INTERNAL
#include <QtVersit/private/qvcard30writer_p.h>
#endif
#include <QtVersit/qversitdocument.h>
#include <QtVersit/qversitproperty.h>
#include <QtTest/QtTest>
#include <QByteArray>
#include <QVariant>
// This says "NOKIA" in Katakana encoded with UTF-8
const QString KATAKANA_NOKIA(QString::fromUtf8("\xe3\x83\x8e\xe3\x82\xad\xe3\x82\xa2"));
QTVERSIT_USE_NAMESPACE
Q_DECLARE_METATYPE(QVersitProperty)
#ifdef QT_BUILD_INTERNAL
void tst_QVCard30Writer::init()
{
mWriter = new QVCard30Writer(QVersitDocument::VCard30Type);
mWriter->setCodec(QTextCodec::codecForName("UTF-8"));
}
void tst_QVCard30Writer::cleanup()
{
delete mWriter;
}
void tst_QVCard30Writer::testEncodeVersitProperty()
{
QFETCH(QVersitProperty, property);
QFETCH(QByteArray, expectedResult);
QByteArray encodedProperty;
QBuffer buffer(&encodedProperty);
mWriter->setDevice(&buffer);
buffer.open(QIODevice::WriteOnly);
mWriter->encodeVersitProperty(property);
QCOMPARE(encodedProperty, expectedResult);
}
void tst_QVCard30Writer::testEncodeVersitProperty_data()
{
QTest::addColumn<QVersitProperty>("property");
QTest::addColumn<QByteArray>("expectedResult");
QVersitProperty property;
QByteArray expectedResult;
// No parameters
expectedResult = "FN:John Citizen\r\n";
property.setName(QString::fromLatin1("FN"));
property.setValue(QString::fromLatin1("John Citizen"));
QTest::newRow("No parameters") << property << expectedResult;
// With parameter(s)
expectedResult = "TEL;TYPE=HOME:123\r\n";
property.setName(QString::fromLatin1("TEL"));
property.setValue(QString::fromLatin1("123"));
property.insertParameter(QString::fromLatin1("TYPE"),QString::fromLatin1("HOME"));
QTest::newRow("With parameters, plain value") << property << expectedResult;
// normal FN property is backslash escaped
property.clear();
property.setName(QStringLiteral("FN"));
property.setValue(QStringLiteral(";,:\\"));
// semicolons, commas and backslashes are escaped (not colons, as per RFC2426)
expectedResult = "FN:\\;\\,:\\\\\r\n";
QTest::newRow("FN property") << property << expectedResult;
// Structured N
property.setName(QStringLiteral("N"));
property.setValue(QStringList()
<< QStringLiteral("La;st") // needs to be backslash escaped
<< QStringLiteral("Fi,rst")
<< QStringLiteral("Mi:ddle")
<< QStringLiteral("Pr\\efix") // needs to be QP encoded
<< QStringLiteral("Suffix"));
property.setValueType(QVersitProperty::CompoundType);
expectedResult = "N:La\\;st;Fi\\,rst;Mi:ddle;Pr\\\\efix;Suffix\r\n";
QTest::newRow("N property") << property << expectedResult;
// Structured CATEGORIES
property.setName(QStringLiteral("CATEGORIES"));
property.setValue(QStringList()
<< QStringLiteral("re;d")
<< QStringLiteral("gr,een")
<< QStringLiteral("bl:ue")
<< QStringLiteral("ye\\llow"));
property.setValueType(QVersitProperty::ListType);
expectedResult = "CATEGORIES:re\\;d,gr\\,een,bl:ue,ye\\\\llow\r\n";
QTest::newRow("CATEGORIES property") << property << expectedResult;
// Convert X-NICKNAME to NICKNAME
expectedResult = "NICKNAME:Jack\r\n";
property.setParameters(QMultiHash<QString,QString>());
property.setName(QString::fromLatin1("X-NICKNAME"));
property.setValue(QString::fromLatin1("Jack"));
QTest::newRow("NICKNAME property") << property << expectedResult;
// Convert X-IMPP to IMPP;
expectedResult = "IMPP:msn:msn-address\r\n";
property.setParameters(QMultiHash<QString,QString>());
property.setName(QString::fromLatin1("X-IMPP"));
property.setValue(QString::fromLatin1("msn:msn-address"));
QTest::newRow("IMPP property") << property << expectedResult;
// AGENT property
expectedResult = "AGENT:BEGIN:VCARD\\nVERSION:3.0\\nFN:Secret Agent\\nEND:VCARD\\n\r\n";
property.setName(QString::fromLatin1("AGENT"));
property.setValue(QString());
QVersitDocument document(QVersitDocument::VCard30Type);
document.setComponentType(QStringLiteral("VCARD"));
QVersitProperty embeddedProperty;
embeddedProperty.setName(QString(QString::fromLatin1("FN")));
embeddedProperty.setValue(QString::fromLatin1("Secret Agent"));
document.addProperty(embeddedProperty);
property.setValue(QVariant::fromValue(document));
QTest::newRow("AGENT property") << property << expectedResult;
// Value is base64 encoded.
QByteArray value("value");
expectedResult = "Springfield.HOUSE.PHOTO;ENCODING=b:" + value.toBase64() + "\r\n";
QStringList groups(QString::fromLatin1("Springfield"));
groups.append(QString::fromLatin1("HOUSE"));
property.setGroups(groups);
property.setParameters(QMultiHash<QString,QString>());
property.setName(QString::fromLatin1("PHOTO"));
property.setValue(value);
QTest::newRow("base64 encoded") << property << expectedResult;
// Characters other than ASCII:
expectedResult = "ORG:" + KATAKANA_NOKIA.toUtf8() + "\r\n";
property = QVersitProperty();
property.setName(QStringLiteral("ORG"));
property.setValue(KATAKANA_NOKIA);
QTest::newRow("non-ASCII") << property << expectedResult;
// No CHARSET and QUOTED-PRINTABLE parameters
expectedResult = "EMAIL:john@" + KATAKANA_NOKIA.toUtf8() + ".com\r\n";
property = QVersitProperty();
property.setName(QStringLiteral("EMAIL"));
property.setValue(QString::fromLatin1("john@%1.com").arg(KATAKANA_NOKIA));
QTest::newRow("special chars") << property << expectedResult;
}
void tst_QVCard30Writer::testEncodeParameters()
{
QByteArray encodedParameters;
QBuffer buffer(&encodedParameters);
mWriter->setDevice(&buffer);
buffer.open(QIODevice::WriteOnly);
QString typeParameterName(QString::fromLatin1("TYPE"));
QString encodingParameterName(QString::fromLatin1("ENCODING"));
// No parameters
QMultiHash<QString,QString> parameters;
mWriter->encodeParameters(parameters);
QCOMPARE(encodedParameters, QByteArray(""));
// One TYPE parameter
parameters.insert(typeParameterName,QString::fromLatin1("HOME"));
mWriter->writeCrlf(); // so it doesn't start folding
buffer.close();
encodedParameters.clear();
buffer.open(QIODevice::WriteOnly);
mWriter->encodeParameters(parameters);
QCOMPARE(encodedParameters, QByteArray(";TYPE=HOME"));
// Two TYPE parameters
parameters.insert(typeParameterName,QString::fromLatin1("VOICE"));
mWriter->writeCrlf(); // so it doesn't start folding
buffer.close();
encodedParameters.clear();
buffer.open(QIODevice::WriteOnly);
mWriter->encodeParameters(parameters);
QCOMPARE(encodedParameters, QByteArray(";TYPE=VOICE,HOME"));
// One ENCODING parameter
parameters.clear();
parameters.insert(encodingParameterName,QString::fromLatin1("8BIT"));
mWriter->writeCrlf(); // so it doesn't start folding
buffer.close();
encodedParameters.clear();
buffer.open(QIODevice::WriteOnly);
mWriter->encodeParameters(parameters);
QCOMPARE(encodedParameters, QByteArray(";ENCODING=8BIT"));
// Two parameters
parameters.insert(QString::fromLatin1("X-PARAM"),QString::fromLatin1("VALUE"));
mWriter->writeCrlf(); // so it doesn't start folding
buffer.close();
encodedParameters.clear();
buffer.open(QIODevice::WriteOnly);
mWriter->encodeParameters(parameters);
QCOMPARE(encodedParameters, QByteArray(";ENCODING=8BIT;X-PARAM=VALUE"));
// Parameter with characters that require backslash escaping
parameters.clear();
parameters.insert(QString::fromLatin1("X-P;ARAM"),QString::fromLatin1("VA,LUE"));
mWriter->writeCrlf(); // so it doesn't start folding
buffer.close();
encodedParameters.clear();
buffer.open(QIODevice::WriteOnly);
mWriter->encodeParameters(parameters);
QCOMPARE(encodedParameters, QByteArray(";X-P\\;ARAM=VA\\,LUE"));
}
void tst_QVCard30Writer::testBackSlashEscape()
{
// Empty string
QString input;
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString());
// Nothing to escape in the string
input = QString::fromLatin1("Nothing to escape");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString::fromLatin1("Nothing to escape"));
// Line break in the beginning
input = QString::fromLatin1("\r\n input");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString::fromLatin1("\\n input"));
// Line break in the end
input = QString::fromLatin1("input\r\n");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString::fromLatin1("input\\n"));
// Semicolon in the beginning
input = QString::fromLatin1(";input");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString::fromLatin1("\\;input"));
// Semicolon in the end
input = QString::fromLatin1("input;");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString::fromLatin1("input\\;"));
// Comma in the beginning
input = QString::fromLatin1(",input");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString::fromLatin1("\\,input"));
// Comma in the end
input = QString::fromLatin1("input,");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString::fromLatin1("input\\,"));
// Backslash in the beginning
input = QString::fromLatin1("\\input");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString::fromLatin1("\\\\input"));
// Backslash in the end
input = QString::fromLatin1("input\\");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input,QString::fromLatin1("input\\\\"));
// Line break, semicolon, backslash and comma in the middle of the string
input = QString::fromLatin1("Escape these \r\n ; , \\ ");
QVCard30Writer::backSlashEscape(&input);
QCOMPARE(input, QString::fromLatin1("Escape these \\n \\; \\, \\\\ "));
}
#endif
QTEST_MAIN(tst_QVCard30Writer)
|