// Copyright (C) 2015 Internet Systems Consortium, Inc. ("ISC") // // This Source Code Form is subject to the terms of the Mozilla Public // License, v. 2.0. If a copy of the MPL was not distributed with this // file, You can obtain one at http://mozilla.org/MPL/2.0/. #include #include #include #include #include // This holds statements to create and destroy the schema. #include "schema_mysql_copy.h" using namespace std; namespace isc { namespace dhcp { namespace test { // Connection strings. // Database: keatest // Host: localhost // Username: keatest // Password: keatest const char* VALID_TYPE = "type=mysql"; const char* INVALID_TYPE = "type=unknown"; const char* VALID_NAME = "name=keatest"; const char* INVALID_NAME = "name=invalidname"; const char* VALID_HOST = "host=localhost"; const char* INVALID_HOST = "host=invalidhost"; const char* VALID_USER = "user=keatest"; const char* INVALID_USER = "user=invaliduser"; const char* VALID_PASSWORD = "password=keatest"; const char* INVALID_PASSWORD = "password=invalid"; string connectionString(const char* type, const char* name, const char* host, const char* user, const char* password) { const string space = " "; string result = ""; if (type != NULL) { result += string(type); } if (name != NULL) { if (! result.empty()) { result += space; } result += string(name); } if (host != NULL) { if (! result.empty()) { result += space; } result += string(host); } if (user != NULL) { if (! result.empty()) { result += space; } result += string(user); } if (password != NULL) { if (! result.empty()) { result += space; } result += string(password); } return (result); } // Return valid connection string string validMySQLConnectionString() { return (connectionString(VALID_TYPE, VALID_NAME, VALID_HOST, VALID_USER, VALID_PASSWORD)); } // @brief Clear everything from the database // // There is no error checking in this code: if something fails, one of the // tests will (should) fall over. void destroyMySQLSchema() { MySqlHolder mysql; // Open database (void) mysql_real_connect(mysql, "localhost", "keatest", "keatest", "keatest", 0, NULL, 0); // Get rid of everything in it. for (int i = 0; destroy_statement[i] != NULL; ++i) { (void) mysql_query(mysql, destroy_statement[i]); } } // @brief Create the Schema // // Creates all the tables in what is assumed to be an empty database. // // There is no error checking in this code: if it fails, one of the tests // will fall over. void createMySQLSchema() { MySqlHolder mysql; // Open database (void) mysql_real_connect(mysql, "localhost", "keatest", "keatest", "keatest", 0, NULL, 0); // Execute creation statements. for (int i = 0; create_statement[i] != NULL; ++i) { ASSERT_EQ(0, mysql_query(mysql, create_statement[i])) << "Failed on statement " << i << ": " << create_statement[i] << " error: " << mysql_error(mysql) << " (error code " << mysql_errno(mysql) << ")"; } } }; }; };