From 2fe5ccfdf7d028f45eff508a65e14758963a35d9 Mon Sep 17 00:00:00 2001 From: Olly Betts Date: Wed, 18 Oct 2017 22:51:15 +1300 Subject: [PATCH] Improve stub file handling of disabled backends Disabling a backend used to mean that lines for that backend would throw DatabaseError with message "Bad line" - now they they throw FeatureUnavailableError. --- xapian-core/backends/dbfactory.cc | 38 +++++++++++++++++++++++++------------- xapian-core/tests/api_db.cc | 31 ++++++++++++++++++++++++------- 2 files changed, 49 insertions(+), 20 deletions(-) diff --git a/xapian-core/backends/dbfactory.cc b/xapian-core/backends/dbfactory.cc index c5d55a413..bb66530d8 100644 --- a/xapian-core/backends/dbfactory.cc +++ b/xapian-core/backends/dbfactory.cc @@ -1,7 +1,7 @@ /** @file dbfactory.cc * @brief Database factories for non-remote databases. */ -/* Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2011,2012,2013,2014,2015,2016 Olly Betts +/* Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2011,2012,2013,2014,2015,2016,2017 Olly Betts * Copyright 2008 Lemur Consulting Ltd * * This program is free software; you can redistribute it and/or @@ -125,16 +125,18 @@ open_stub(Database &db, const string &file) continue; } -#ifdef XAPIAN_HAS_GLASS_BACKEND if (type == "glass") { +#ifdef XAPIAN_HAS_GLASS_BACKEND resolve_relative_path(line, file); db.add_database(Database(new GlassDatabase(line))); continue; - } +#else + throw FeatureUnavailableError("Glass backend disabled"); #endif + } -#ifdef XAPIAN_HAS_REMOTE_BACKEND if (type == "remote" && !line.empty()) { +#ifdef XAPIAN_HAS_REMOTE_BACKEND if (line[0] == ':') { // prog // FIXME: timeouts @@ -168,15 +170,19 @@ open_stub(Database &db, const string &file) continue; } } - } +#else + throw FeatureUnavailableError("Remote backend disabled"); #endif + } -#ifdef XAPIAN_HAS_INMEMORY_BACKEND if (type == "inmemory" && line.empty()) { +#ifdef XAPIAN_HAS_INMEMORY_BACKEND db.add_database(Database(string(), DB_BACKEND_INMEMORY)); continue; - } +#else + throw FeatureUnavailableError("Inmemory backend disabled"); #endif + } if (type == "chert") { throw FeatureUnavailableError("Chert backend no longer supported"); @@ -240,16 +246,18 @@ open_stub(WritableDatabase &db, const string &file, int flags) continue; } -#ifdef XAPIAN_HAS_GLASS_BACKEND if (type == "glass") { +#ifdef XAPIAN_HAS_GLASS_BACKEND resolve_relative_path(line, file); db.add_database(WritableDatabase(line, flags|DB_BACKEND_GLASS)); continue; - } +#else + throw FeatureUnavailableError("Glass backend disabled"); #endif + } + if (type == "remote" && !line.empty()) { #ifdef XAPIAN_HAS_REMOTE_BACKEND - if (type == "remote") { if (line[0] == ':') { // prog // FIXME: timeouts @@ -283,15 +291,19 @@ open_stub(WritableDatabase &db, const string &file, int flags) continue; } } - } +#else + throw FeatureUnavailableError("Remote backend disabled"); #endif + } -#ifdef XAPIAN_HAS_INMEMORY_BACKEND if (type == "inmemory" && line.empty()) { +#ifdef XAPIAN_HAS_INMEMORY_BACKEND db.add_database(WritableDatabase(string(), DB_BACKEND_INMEMORY)); continue; - } +#else + throw FeatureUnavailableError("Inmemory backend disabled"); #endif + } if (type == "chert") { throw FeatureUnavailableError("Chert backend no longer supported"); diff --git a/xapian-core/tests/api_db.cc b/xapian-core/tests/api_db.cc index c5175839f..cee793645 100644 --- a/xapian-core/tests/api_db.cc +++ b/xapian-core/tests/api_db.cc @@ -2,7 +2,7 @@ * * Copyright 1999,2000,2001 BrightStation PLC * Copyright 2002 Ananova Ltd - * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2011,2012,2013,2015,2016 Olly Betts + * Copyright 2002,2003,2004,2005,2006,2007,2008,2009,2011,2012,2013,2015,2016,2017 Olly Betts * Copyright 2006,2007,2008,2009 Lemur Consulting Ltd * * This program is free software; you can redistribute it and/or @@ -106,17 +106,26 @@ DEFINE_TESTCASE(stubdb2, backend && !inmemory && !remote) { << ' ' << get_database_path("apitest_simpledata") << endl; out.close(); - { + try { Xapian::Database db(dbpath, Xapian::DB_BACKEND_STUB); Xapian::Enquire enquire(db); enquire.set_query(Xapian::Query("word")); enquire.get_mset(0, 10); + } catch (Xapian::FeatureUnavailableError&) { +#ifdef XAPIAN_HAS_REMOTE_BACKEND + throw; +#endif } - { + + try { Xapian::Database db(dbpath); Xapian::Enquire enquire(db); enquire.set_query(Xapian::Query("word")); enquire.get_mset(0, 10); + } catch (Xapian::FeatureUnavailableError&) { +#ifdef XAPIAN_HAS_REMOTE_BACKEND + throw; +#endif } out.open(dbpath); @@ -134,21 +143,28 @@ DEFINE_TESTCASE(stubdb2, backend && !inmemory && !remote) { Xapian::WritableDatabase db(dbpath, Xapian::DB_BACKEND_STUB) ); +#ifdef XAPIAN_HAS_REMOTE_BACKEND +# define EXPECTED_EXCEPTION Xapian::DatabaseOpeningError +#else +# define EXPECTED_EXCEPTION Xapian::FeatureUnavailableError +#endif + out.open(dbpath); TEST(out.is_open()); out << "remote foo" << endl; out.close(); // Quietly ignored prior to 1.4.1. - TEST_EXCEPTION(Xapian::DatabaseOpeningError, + TEST_EXCEPTION(EXPECTED_EXCEPTION, Xapian::Database db(dbpath, Xapian::DB_BACKEND_STUB) ); // Quietly ignored prior to 1.4.1. - TEST_EXCEPTION(Xapian::DatabaseOpeningError, + TEST_EXCEPTION(EXPECTED_EXCEPTION, Xapian::WritableDatabase db(dbpath, Xapian::DB_BACKEND_STUB) ); +#ifdef XAPIAN_HAS_REMOTE_BACKEND out.open(dbpath); TEST(out.is_open()); out << "remote [::1]:80" << endl; @@ -179,6 +195,7 @@ DEFINE_TESTCASE(stubdb2, backend && !inmemory && !remote) { // So we test the message instead of the error string for portability. TEST(e.get_msg().find("host ::1") != string::npos); } +#endif out.open(dbpath); TEST(out.is_open()); @@ -188,13 +205,13 @@ DEFINE_TESTCASE(stubdb2, backend && !inmemory && !remote) { // 1.4.0 threw: // NetworkError: Couldn't resolve host [ (context: remote:tcp([:0)) (No address associated with hostname) - TEST_EXCEPTION(Xapian::DatabaseOpeningError, + TEST_EXCEPTION(EXPECTED_EXCEPTION, Xapian::Database db(dbpath, Xapian::DB_BACKEND_STUB); ); // 1.4.0 threw: // NetworkError: Couldn't resolve host [ (context: remote:tcp([:0)) (No address associated with hostname) - TEST_EXCEPTION(Xapian::DatabaseOpeningError, + TEST_EXCEPTION(EXPECTED_EXCEPTION, Xapian::WritableDatabase db(dbpath, Xapian::DB_BACKEND_STUB); ); -- 2.11.4.GIT