OpenDNSSEC-enforcer
1.3.4
|
00001 /* 00002 * $Id: test_routines_database.c 4645 2011-03-24 14:23:09Z rb $ 00003 * 00004 * Copyright (c) 2008-2009 Nominet UK. All rights reserved. 00005 * 00006 * Redistribution and use in source and binary forms, with or without 00007 * modification, are permitted provided that the following conditions 00008 * are met: 00009 * 1. Redistributions of source code must retain the above copyright 00010 * notice, this list of conditions and the following disclaimer. 00011 * 2. Redistributions in binary form must reproduce the above copyright 00012 * notice, this list of conditions and the following disclaimer in the 00013 * documentation and/or other materials provided with the distribution. 00014 * 00015 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR 00016 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 00017 * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE 00018 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY 00019 * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00020 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE 00021 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS 00022 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER 00023 * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 00024 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN 00025 * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00026 * 00027 */ 00028 00029 /*+ 00030 * test_routines_database.c - Database Test Routines 00031 * 00032 * Description: 00033 * A set of routines to help with the tests that access the database. 00034 -*/ 00035 00036 #include <stdlib.h> 00037 #include <stdio.h> 00038 #include <string.h> 00039 00040 #include "ksm/database.h" 00041 #include "test_routines.h" 00042 00043 00044 /*+ 00045 * TdbUsername - Return database username 00046 * TdbPassword - Return database password 00047 * TdbHost - Return database host 00048 * TdbPort - Return database port 00049 * TdbName - Return database name 00050 * 00051 * Description: 00052 * Translates the environment variables: 00053 * 00054 * DB_USERNAME 00055 * DB_PASSWORD 00056 * DB_HOST 00057 * DB_PORT 00058 * DB_NAME 00059 * 00060 * ... and returns the value. 00061 * 00062 * Arguments: 00063 * None. 00064 * 00065 * Returns: 00066 * const char* 00067 * Pointer to the appropriate value. This may be NULL if the value 00068 * is not defined. 00069 * 00070 * The string should not be modified by the caller - it points to 00071 * internal storage. 00072 -*/ 00073 00074 const char* TdbUsername(void) 00075 { 00076 return getenv("DB_USERNAME"); 00077 } 00078 00079 const char* TdbPassword(void) 00080 { 00081 return getenv("DB_PASSWORD"); 00082 } 00083 00084 const char* TdbHost(void) 00085 { 00086 return getenv("DB_HOST"); 00087 } 00088 00089 const char* TdbName(void) 00090 { 00091 return getenv("DB_NAME"); 00092 } 00093 00094 const char* TdbPort(void) 00095 { 00096 return getenv("DB_PORT"); 00097 } 00098 00099 /*+ 00100 * TdbSetup - Set Up Database 00101 * TdbTeardown - Teardown Database 00102 * 00103 * Description: 00104 * Sets up a database and connects to it/tears down a database and 00105 * disconnects from it. 00106 * 00107 * Arguments: 00108 * None. 00109 * 00110 * Returns: 00111 * int 00112 * 0 Success 00113 * Other Some failure 00114 -*/ 00115 00116 int TdbSetup(void) 00117 { 00118 DB_HANDLE handle; /* database handle (unused) */ 00119 int status; /* Status return from connection */ 00120 const char* name = TdbName(); 00121 const char* host = TdbHost(); 00122 const char* port = TdbPort(); 00123 const char* user = TdbUsername(); 00124 const char* pass = TdbPassword(); 00125 00126 if (name && !strlen(name)) name=NULL; 00127 if (host && !strlen(host)) host=NULL; 00128 if (port && !strlen(port)) port=NULL; 00129 if (user && !strlen(user)) user=NULL; 00130 if (pass && !strlen(pass)) pass=NULL; 00131 00132 #ifdef USE_MYSQL 00133 if (!name || !pass || !user) 00134 { 00135 printf("Please run ./configure with --with-dbname, --with-dbuser, and --with-dbpass. " 00136 "(--with-dbhost and --with-dbport are optional)\n"); 00137 exit(1); 00138 } 00139 00140 (void) system("sh ./database_setup_mysql.sh setup"); 00141 #else 00142 if (!name) { 00143 printf("Please run ./configure with --with-dbname to indicate the location of a test database.\n"); 00144 exit(1); 00145 } 00146 00147 (void) system("sh ./database_setup_sqlite3.sh setup"); 00148 #endif 00149 00150 DbInit(); 00151 00152 status = DbConnect(&handle, name, host, pass, user, port); 00153 00154 return status; 00155 } 00156 00157 int TdbTeardown(void) 00158 { 00159 /* Ignore errors - teardown failure does not imply test failure */ 00160 00161 (void) DbDisconnect(DbHandle()); 00162 00163 DbRundown(); 00164 00165 #ifdef USE_MYSQL 00166 (void) system("sh ./database_setup_mysql.sh teardown"); 00167 #else 00168 (void) system("sh ./database_setup_sqlite3.sh teardown"); 00169 #endif 00170 00171 return 0; 00172 }