test_client.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (C) 2012 Internet Systems Consortium, Inc. ("ISC")
  2. //
  3. // Permission to use, copy, modify, and/or distribute this software for any
  4. // purpose with or without fee is hereby granted, provided that the above
  5. // copyright notice and this permission notice appear in all copies.
  6. //
  7. // THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES WITH
  8. // REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY
  9. // AND FITNESS. IN NO EVENT SHALL ISC BE LIABLE FOR ANY SPECIAL, DIRECT,
  10. // INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM
  11. // LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE
  12. // OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR
  13. // PERFORMANCE OF THIS SOFTWARE.
  14. #include <exceptions/exceptions.h>
  15. #include <dns/masterload.h>
  16. #include <dns/name.h>
  17. #include <dns/rrclass.h>
  18. #include <datasrc/client.h>
  19. #include <datasrc/zone.h>
  20. #include <datasrc/sqlite3_accessor.h>
  21. #include "test_client.h"
  22. #include <boost/bind.hpp>
  23. #include <boost/shared_ptr.hpp>
  24. #include <istream>
  25. #include <fstream>
  26. using namespace std;
  27. using boost::shared_ptr;
  28. using namespace isc::dns;
  29. namespace isc {
  30. namespace datasrc {
  31. namespace unittest {
  32. namespace {
  33. // A helper subroutine for the SQLite3Client creator.
  34. void
  35. addRRset(ZoneUpdaterPtr updater, ConstRRsetPtr rrset) {
  36. updater->addRRset(*rrset);
  37. }
  38. }
  39. shared_ptr<DataSourceClient>
  40. createSQLite3Client(RRClass zclass, const Name& zname,
  41. const char* const db_file, const char* const zone_file)
  42. {
  43. ifstream ifs(zone_file, ios_base::in);
  44. if (ifs.fail()) {
  45. isc_throw(isc::Unexpected, "Failed to open test zone file: "
  46. << zone_file);
  47. }
  48. return (createSQLite3Client(zclass, zname, db_file, ifs));
  49. }
  50. shared_ptr<DataSourceClient>
  51. createSQLite3Client(RRClass zclass, const Name& zname,
  52. const char* const db_file, istream& rr_stream)
  53. {
  54. // We always begin with an empty template SQLite3 DB file and install
  55. // the zone data from the zone file to ensure both cases have the
  56. // same test data.
  57. const char* const install_cmd_prefix = INSTALL_PROG " " TEST_DATA_COMMONDIR
  58. "/rwtest.sqlite3 ";
  59. const string install_cmd = string(install_cmd_prefix) + db_file;
  60. if (system(install_cmd.c_str()) != 0) {
  61. isc_throw(isc::Unexpected,
  62. "Error setting up; command failed: " << install_cmd);
  63. }
  64. shared_ptr<SQLite3Accessor> accessor(
  65. new SQLite3Accessor(db_file, zclass.toText()));
  66. shared_ptr<DatabaseClient> client(new DatabaseClient(zclass, accessor));
  67. ZoneUpdaterPtr updater = client->getUpdater(zname, true);
  68. masterLoad(rr_stream, zname, zclass, boost::bind(addRRset, updater, _1));
  69. updater->commit();
  70. return (client);
  71. }
  72. }
  73. }
  74. }