test_client.cc 2.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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 <cstdlib>
  25. #include <istream>
  26. #include <fstream>
  27. using namespace std;
  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. boost::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. boost::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 " -c " 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. boost::shared_ptr<SQLite3Accessor> accessor(
  65. new SQLite3Accessor(db_file, zclass.toText()));
  66. boost::shared_ptr<DatabaseClient> client(new DatabaseClient(zclass,
  67. accessor));
  68. ZoneUpdaterPtr updater = client->getUpdater(zname, true);
  69. masterLoad(rr_stream, zname, zclass, boost::bind(addRRset, updater, _1));
  70. updater->commit();
  71. return (client);
  72. }
  73. }
  74. }
  75. }