2
* Copyright (C) 2010 Intel Corporation
5
#include "WebDAVSource.h"
6
#include "CalDAVSource.h"
7
#include "CardDAVSource.h"
8
#include <syncevo/SyncSource.h>
9
#ifdef ENABLE_UNIT_TESTS
13
#include <boost/bind.hpp>
14
#include <boost/tokenizer.hpp>
16
#include <syncevo/declarations.h>
19
static SyncSource *createSource(const SyncSourceParams ¶ms)
21
SourceType sourceType = SyncSource::getSourceType(params.m_nodes);
24
isMe = sourceType.m_backend == "CalDAV";
26
if (sourceType.m_format == "" ||
27
sourceType.m_format == "text/calendar" ||
28
sourceType.m_format == "text/x-calendar" ||
29
sourceType.m_format == "text/x-vcalendar") {
31
boost::shared_ptr<Neon::Settings> settings;
32
boost::shared_ptr<SubSyncSource> sub(new CalDAVSource(params, settings));
33
return new MapSyncSource(params, sub);
35
return RegisterSyncSource::InactiveSource;
40
isMe = sourceType.m_backend == "CardDAV";
42
if (sourceType.m_format == "" ||
43
sourceType.m_format == "text/x-vcard" ||
44
sourceType.m_format == "text/vcard") {
46
boost::shared_ptr<Neon::Settings> settings;
47
return new CardDAVSource(params, settings);
49
return RegisterSyncSource::InactiveSource;
57
static class RegisterWebDAVSyncSource : public RegisterSyncSource
60
RegisterWebDAVSyncSource() :
61
RegisterSyncSource("DAV",
76
// configure and register our own property;
77
// do this regardless whether the backend is enabled,
78
// so that config migration always includes this property
79
WebDAVCredentialsOkay.setHidden(true);
80
SyncConfig::getRegistry().push_back(&WebDAVCredentialsOkay);
85
#ifdef ENABLE_UNIT_TESTS
87
class WebDAVTest : public CppUnit::TestFixture {
88
CPPUNIT_TEST_SUITE(WebDAVTest);
89
CPPUNIT_TEST(testInstantiate);
90
CPPUNIT_TEST(testHTMLEntities);
91
CPPUNIT_TEST_SUITE_END();
94
void testInstantiate() {
95
boost::shared_ptr<TestingSyncSource> source;
96
source.reset((TestingSyncSource *)SyncSource::createTestingSource("CalDAV", "CalDAV", true));
97
source.reset((TestingSyncSource *)SyncSource::createTestingSource("CalDAV", "CalDAV:text/calendar", true));
98
source.reset((TestingSyncSource *)SyncSource::createTestingSource("CalDAV", "CalDAV:text/x-vcalendar", true));
99
source.reset((TestingSyncSource *)SyncSource::createTestingSource("CardDAV", "CardDAV", true));
100
source.reset((TestingSyncSource *)SyncSource::createTestingSource("CardDAV", "CardDAV:text/vcard", true));
101
source.reset((TestingSyncSource *)SyncSource::createTestingSource("CardDAV", "CardDAV:text/x-vcard", true));
104
std::string decode(const char *item) {
105
std::string buffer = item;
106
CardDAVSource::replaceHTMLEntities(buffer);
110
void testHTMLEntities() {
112
CPPUNIT_ASSERT_EQUAL(std::string("\" & ' < >"),
113
decode("" & ' < >"));
114
// decimal and hex, encoded in different ways
115
CPPUNIT_ASSERT_EQUAL(std::string("\" & ' < >"),
116
decode("" & ' < >"));
117
// no translation needed
118
CPPUNIT_ASSERT_EQUAL(std::string("hello world"),
119
decode("hello world"));
121
CPPUNIT_ASSERT_EQUAL(std::string("< "),
124
CPPUNIT_ASSERT_EQUAL(std::string(" <"),
127
CPPUNIT_ASSERT_EQUAL(std::string("\\"),
128
decode("&#92;"));
129
CPPUNIT_ASSERT_EQUAL(std::string("ampersand entity & less-than entity <"),
130
decode("ampersand entity & less-than entity &lt;"));
133
CPPUNIT_ASSERT_EQUAL(std::string(" &"),
135
CPPUNIT_ASSERT_EQUAL(std::string("&"),
137
CPPUNIT_ASSERT_EQUAL(std::string("& "),
139
CPPUNIT_ASSERT_EQUAL(std::string("&;"),
141
CPPUNIT_ASSERT_EQUAL(std::string("&; "),
143
CPPUNIT_ASSERT_EQUAL(std::string(" &; "),
145
CPPUNIT_ASSERT_EQUAL(std::string(" &;"),
147
CPPUNIT_ASSERT_EQUAL(std::string("&xyz;"),
149
CPPUNIT_ASSERT_EQUAL(std::string("f;"),
151
CPPUNIT_ASSERT_EQUAL(std::string("f;"),
153
CPPUNIT_ASSERT_EQUAL(std::string(" ;"),
155
CPPUNIT_ASSERT_EQUAL(std::string("&#quot ;"),
160
SYNCEVOLUTION_TEST_SUITE_REGISTRATION(WebDAVTest);
162
#endif // ENABLE_UNIT_TESTS
172
* implements one specific source for local testing;
173
* creates "target-config@client-test-<server>" peer config
174
* and <type> source inside it before instantiating the
177
class WebDAVTest : public RegisterSyncSourceTest {
178
std::string m_server;
184
* @param server for example, "yahoo", "google"
185
* @param type "caldav" or "carddav"
186
* @param props sync properties (username, password, syncURL, ...)
187
* or key/value parameters for the testing (testcases)
189
WebDAVTest(const std::string &server,
190
const std::string &type,
191
const ConfigProps &props) :
192
RegisterSyncSourceTest(server + "_" + type, // for example, google_caldav
193
props.get(type + "/testconfig",
194
props.get("testconfig",
195
type == "caldav" ? "eds_event" :
196
type == "carddav" ? "eds_contact" :
203
virtual void updateConfig(ClientTestConfig &config) const
205
config.type = m_type.c_str();
206
config.createSourceA = boost::bind(&WebDAVTest::createSource, this, _3);
207
config.createSourceB = boost::bind(&WebDAVTest::createSource, this, _3);
208
ConfigProps::const_iterator it = m_props.find(m_type + "/testcases");
209
if (it != m_props.end() ||
210
(it = m_props.find("testcases")) != m_props.end()) {
211
config.testcases = it->second.c_str();
215
TestingSyncSource *createSource(bool isSourceA) const
217
boost::shared_ptr<SyncConfig> context(new SyncConfig(string("target-config@client-test-") + m_server));
218
SyncSourceNodes nodes = context->getSyncSourceNodes(m_type,
219
/* string("_") m_clientID + */
220
string("_") + (isSourceA ? "A" : "B"));
222
// always set properties taken from the environment;
223
// TODO: "database" property (currently always uses the default)
224
nodes.getProperties()->setProperty("backend", m_type);
225
BOOST_FOREACH(const StringPair &propval, m_props) {
226
boost::shared_ptr<FilterConfigNode> node = context->getNode(propval.first);
228
node->setProperty(propval.first, propval.second);
229
} else if (!boost::ends_with(propval.first, "testconfig") &&
230
!boost::ends_with(propval.first, "testcases")) {
231
SE_THROW(StringPrintf("invalid property %s=%s set in CLIENT_TEST_WEBDAV for %s %s",
232
propval.first.c_str(), propval.second.c_str(),
233
m_server.c_str(), m_type.c_str()));
238
SyncSourceParams params(m_type,
241
SyncSource *ss = SyncSource::createSource(params);
242
return static_cast<TestingSyncSource *>(ss);
248
* creates WebDAV sources by parsing
249
* CLIENT_TEST_WEBDAV=<server> [caldav] [carddav] <prop>=<val> ...; ...
251
static class WebDAVTestSingleton {
252
list< boost::shared_ptr<WebDAVTest> > m_sources;
255
WebDAVTestSingleton()
257
const char *env = getenv("CLIENT_TEST_WEBDAV");
262
std::string settings(env);
263
boost::char_separator<char> sep1(";");
264
boost::char_separator<char> sep2("\t ");
265
BOOST_FOREACH(const std::string &entry,
266
boost::tokenizer< boost::char_separator<char> >(settings, boost::char_separator<char>(";"))) {
268
bool caldav = false, carddav = false;
270
BOOST_FOREACH(const std::string &token,
271
boost::tokenizer< boost::char_separator<char> >(entry, boost::char_separator<char>("\t "))) {
272
if (server.empty()) {
274
} else if (token == "caldav") {
276
} else if (token == "carddav") {
279
size_t pos = token.find('=');
280
if (pos == token.npos) {
281
SE_THROW(StringPrintf("CLIENT_TEST_WEBDAV: unknown keyword %s", token.c_str()));
283
props[token.substr(0,pos)] = token.substr(pos + 1);
287
boost::shared_ptr<WebDAVTest> ptr(new WebDAVTest(server, "caldav", props));
288
m_sources.push_back(ptr);
291
boost::shared_ptr<WebDAVTest> ptr(new WebDAVTest(server, "carddav", props));
292
m_sources.push_back(ptr);
296
} WebDAVTestSingleton;