CheckSource(): did not handle invalid or missing "type" property (MB #8317)

An invalid type (like inactive backend, or something the user
entered into the file manually) raises an exception when accessing
the source parameters. This exception must be caught and transformed
into the SourceUnusable exception expected by D-Bus clients.

An unset type leads to a NULL SyncSource pointer. This was checked,
but without raising an exception. CheckSource() thus incorrectly
reported success.
This commit is contained in:
Patrick Ohly 2009-12-02 15:05:36 +01:00
parent a175f8e43c
commit b132b94402
2 changed files with 39 additions and 2 deletions

View File

@ -1316,13 +1316,22 @@ void ReadOperations::checkSource(const std::string &sourceName)
if(it == sourceNames.end()) {
SE_THROW_EXCEPTION(NoSuchSource, "'" + m_configName + "' has no '" + sourceName + "' source");
}
SyncSourceParams params(sourceName, config->getSyncSourceNodes(sourceName));
auto_ptr<SyncSource> syncSource(SyncSource::createSource(params, false));
bool checked = false;
try {
// this can already throw exceptions when the config is invalid
SyncSourceParams params(sourceName, config->getSyncSourceNodes(sourceName));
auto_ptr<SyncSource> syncSource(SyncSource::createSource(params, false));
if (syncSource.get()) {
syncSource->open();
// success!
checked = true;
}
} catch (...) {
Exception::handle();
}
if (!checked) {
SE_THROW_EXCEPTION(SourceUnusable, "The source '" + sourceName + "' is not usable");
}
}

View File

@ -863,6 +863,34 @@ class TestSessionAPIsDummy(unittest.TestCase, DBusUtil):
else:
self.fail("no exception thrown")
def testCheckSourceInvalidType(self):
""" test the right error is reported when the type is invalid """
self.setupConfig()
# we cannot set an invalid type here, so pick one which is likely
# to be not supported in syncevo-dbus-server
config = { "source/memo" : { "type" : "apple-contacts"} }
self.session.SetConfig(True, False, config, utf8_strings=True)
try:
self.session.CheckSource("memo", utf8_strings=True)
except dbus.DBusException, ex:
self.failUnlessEqual(str(ex),
"org.syncevolution.SourceUnusable: The source 'memo' is not usable")
else:
self.fail("no exception thrown")
def testCheckSourceNoType(self):
""" test the right error is reported when the type is unset """
self.setupConfig()
config = { "source/memo" : { "type" : "" } }
self.session.SetConfig(True, False, config, utf8_strings=True)
try:
self.session.CheckSource("memo", utf8_strings=True)
except dbus.DBusException, ex:
self.failUnlessEqual(str(ex),
"org.syncevolution.SourceUnusable: The source 'memo' is not usable")
else:
self.fail("no exception thrown")
def testCheckSource(self):
""" test all are right """
self.setupConfig()