OpenVPN
customer_database_test.c
Go to the documentation of this file.
1 /*
2  * Copyright 2008 Google Inc.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 #include <stdarg.h>
17 #include <stddef.h>
18 #include <setjmp.h>
19 #include <cmocka.h>
20 #include <database.h>
21 
23 extern unsigned int get_customer_id_by_name(
24  DatabaseConnection * const connection, const char * const customer_name);
25 
26 /* Mock query database function. */
27 static unsigned int mock_query_database(DatabaseConnection* const connection,
28  const char * const query_string,
29  void *** const results) {
30  (void) connection; /* unused */
31  (void) query_string; /* unused */
32 
33  *results = (void **)mock_ptr_type(int *);
34  return mock_ptr_type(int);
35 }
36 
37 /* Mock of the connect to database function. */
38 DatabaseConnection* connect_to_database(const char * const database_url,
39  const unsigned int port) {
40  (void) database_url; /* unused */
41  (void) port; /* unused */
42 
43  return (DatabaseConnection*)((size_t)mock());
44 }
45 
46 static void test_connect_to_customer_database(void **state) {
47  (void) state; /* unused */
48 
49  will_return(connect_to_database, 0x0DA7ABA53);
50 
51  assert_int_equal((size_t)connect_to_customer_database(), 0x0DA7ABA53);
52 }
53 
54 /* This test fails as the mock function connect_to_database() will have no
55  * value to return. */
56 #if 0
57 static void fail_connect_to_customer_database(void **state) {
58  (void) state; /* unused */
59 
61  (DatabaseConnection*)0x0DA7ABA53);
62 }
63 #endif
64 
65 static void test_get_customer_id_by_name(void **state) {
66  DatabaseConnection connection = {
67  "somedatabase.somewhere.com", 12345678, mock_query_database
68  };
69  /* Return a single customer ID when mock_query_database() is called. */
70  int customer_ids = 543;
71  int rc;
72 
73  (void) state; /* unused */
74 
76  cast_ptr_to_largest_integral_type(&customer_ids));
78 
79  rc = get_customer_id_by_name(&connection, "john doe");
80  assert_int_equal(rc, 543);
81 }
82 
83 int main(void) {
84  const struct CMUnitTest tests[] = {
87  };
88  return cmocka_run_group_tests(tests, NULL, NULL);
89 }
int main(void)
#define assert_true(c)
Definition: cmocka.h:1045
unsigned int get_customer_id_by_name(DatabaseConnection *const connection, const char *const customer_name)
#define cmocka_unit_test(f)
Initializes a CMUnitTest structure.
Definition: cmocka.h:1653
static void test_connect_to_customer_database(void **state)
static void test_get_customer_id_by_name(void **state)
DatabaseConnection * connect_to_database(const char *const database_url, const unsigned int port)
#define will_return(function, value)
Definition: cmocka.h:294
#define cast_ptr_to_largest_integral_type(value)
Definition: cmocka.h:133
static unsigned int mock_query_database(DatabaseConnection *const connection, const char *const query_string, void ***const results)
#define mock()
Definition: cmocka.h:210
#define cmocka_run_group_tests(group_tests, group_setup, group_teardown)
Definition: cmocka.h:1749
DatabaseConnection * connect_to_customer_database()
#define mock_ptr_type(type)
Definition: cmocka.h:263
#define assert_int_equal(a, b)
Definition: cmocka.h:1174