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