OpenDNSSEC-enforcer  1.4.10
test_routines_database.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2008-2009 Nominet UK. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  * 1. Redistributions of source code must retain the above copyright
8  * notice, this list of conditions and the following disclaimer.
9  * 2. Redistributions in binary form must reproduce the above copyright
10  * notice, this list of conditions and the following disclaimer in the
11  * documentation and/or other materials provided with the distribution.
12  *
13  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
14  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
15  * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
16  * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY
17  * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
18  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
19  * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
20  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER
21  * IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
22  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN
23  * IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
24  *
25  */
26 
27 /*+
28  * test_routines_database.c - Database Test Routines
29  *
30  * Description:
31  * A set of routines to help with the tests that access the database.
32 -*/
33 
34 #include <stdlib.h>
35 #include <stdio.h>
36 #include <string.h>
37 
38 #include "ksm/database.h"
39 #include "test_routines.h"
40 
41 
42 /*+
43  * TdbUsername - Return database username
44  * TdbPassword - Return database password
45  * TdbHost - Return database host
46  * TdbPort - Return database port
47  * TdbName - Return database name
48  *
49  * Description:
50  * Translates the environment variables:
51  *
52  * DB_USERNAME
53  * DB_PASSWORD
54  * DB_HOST
55  * DB_PORT
56  * DB_NAME
57  *
58  * ... and returns the value.
59  *
60  * Arguments:
61  * None.
62  *
63  * Returns:
64  * const char*
65  * Pointer to the appropriate value. This may be NULL if the value
66  * is not defined.
67  *
68  * The string should not be modified by the caller - it points to
69  * internal storage.
70 -*/
71 
72 const char* TdbUsername(void)
73 {
74  return getenv("DB_USERNAME");
75 }
76 
77 const char* TdbPassword(void)
78 {
79  return getenv("DB_PASSWORD");
80 }
81 
82 const char* TdbHost(void)
83 {
84  return getenv("DB_HOST");
85 }
86 
87 const char* TdbName(void)
88 {
89  return getenv("DB_NAME");
90 }
91 
92 const char* TdbPort(void)
93 {
94  return getenv("DB_PORT");
95 }
96 
97 /*+
98  * TdbSetup - Set Up Database
99  * TdbTeardown - Teardown Database
100  *
101  * Description:
102  * Sets up a database and connects to it/tears down a database and
103  * disconnects from it.
104  *
105  * Arguments:
106  * None.
107  *
108  * Returns:
109  * int
110  * 0 Success
111  * Other Some failure
112 -*/
113 
114 int TdbSetup(void)
115 {
116  DB_HANDLE handle; /* database handle (unused) */
117  int status; /* Status return from connection */
118  const char* name = TdbName();
119  const char* host = TdbHost();
120  const char* port = TdbPort();
121  const char* user = TdbUsername();
122  const char* pass = TdbPassword();
123 
124  if (name && !strlen(name)) name=NULL;
125  if (host && !strlen(host)) host=NULL;
126  if (port && !strlen(port)) port=NULL;
127  if (user && !strlen(user)) user=NULL;
128  if (pass && !strlen(pass)) pass=NULL;
129 
130 #ifdef USE_MYSQL
131  if (!name || !pass || !user)
132  {
133  printf("Please run ./configure with --with-dbname, --with-dbuser, and --with-dbpass. "
134  "(--with-dbhost and --with-dbport are optional)\n");
135  exit(1);
136  }
137 
138  (void) system("sh ./database_setup_mysql.sh setup");
139 #else
140  if (!name) {
141  printf("Please run ./configure with --with-dbname to indicate the location of a test database.\n");
142  exit(1);
143  }
144 
145  (void) system("sh ./database_setup_sqlite3.sh setup");
146 #endif
147 
148  DbInit();
149 
150  status = DbConnect(&handle, name, host, pass, user, port);
151 
152  return status;
153 }
154 
155 int TdbTeardown(void)
156 {
157  /* Ignore errors - teardown failure does not imply test failure */
158 
159  (void) DbDisconnect(DbHandle());
160 
161  DbRundown();
162 
163 #ifdef USE_MYSQL
164  (void) system("sh ./database_setup_mysql.sh teardown");
165 #else
166  (void) system("sh ./database_setup_sqlite3.sh teardown");
167 #endif
168 
169  return 0;
170 }
sqlite3 * DB_HANDLE
Definition: database.h:77
const char * TdbName(void)
int TdbTeardown(void)
const char * TdbUsername(void)
void DbInit(void)
const char * TdbHost(void)
DB_HANDLE DbHandle(void)
int DbDisconnect(DB_HANDLE dbhandle)
void DbRundown(void)
int TdbSetup(void)
const char * TdbPassword(void)
const char * TdbPort(void)
int DbConnect(DB_HANDLE *dbhandle, const char *database,...)