OpenVPN
product_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 
24 /* Mock connect to database function.
25  * NOTE: This mock function is very general could be shared between tests
26  * that use the imaginary database.h module. */
27 DatabaseConnection* connect_to_database(const char * const url,
28  const unsigned int port) {
29  check_expected_ptr(url);
30  check_expected(port);
31  return (DatabaseConnection*)((size_t)mock());
32 }
33 
34 static void test_connect_to_product_database(void **state) {
35  (void) state; /* unused */
36 
37  expect_string(connect_to_database, url, "products.abcd.org");
39  will_return(connect_to_database, 0xDA7ABA53);
40  assert_int_equal((size_t)connect_to_product_database(), 0xDA7ABA53);
41 }
42 
43 /* This test will fail since the expected URL is different to the URL that is
44  * passed to connect_to_database() by connect_to_product_database(). */
45 static void test_connect_to_product_database_bad_url(void **state) {
46  (void) state; /* unused */
47 
48  expect_string(connect_to_database, url, "products.abcd.com");
50  will_return(connect_to_database, 0xDA7ABA53);
51  assert_int_equal((size_t)connect_to_product_database(), 0xDA7ABA53);
52 }
53 
54 /* This test will fail since the mock connect_to_database() will attempt to
55  * retrieve a value for the parameter port which isn't specified by this
56  * test function. */
58  (void) state; /* unused */
59 
60  expect_string(connect_to_database, url, "products.abcd.org");
61  will_return(connect_to_database, 0xDA7ABA53);
62  assert_int_equal((size_t)connect_to_product_database(), 0xDA7ABA53);
63 }
64 
65 int main(void) {
66  const struct CMUnitTest tests[] = {
70  };
71  return cmocka_run_group_tests(tests, NULL, NULL);
72 }
#define cmocka_unit_test(f)
Initializes a CMUnitTest structure.
Definition: cmocka.h:1653
DatabaseConnection * connect_to_database(const char *const url, const unsigned int port)
static void test_connect_to_product_database(void **state)
#define expect_value(function, parameter, value)
Definition: cmocka.h:662
static void test_connect_to_product_database_missing_parameter(void **state)
int main(void)
#define will_return(function, value)
Definition: cmocka.h:294
static void test_connect_to_product_database_bad_url(void **state)
DatabaseConnection * connect_to_product_database(void)
#define check_expected(parameter)
Definition: cmocka.h:986
#define mock()
Definition: cmocka.h:210
#define cmocka_run_group_tests(group_tests, group_setup, group_teardown)
Definition: cmocka.h:1749
#define check_expected_ptr(parameter)
Definition: cmocka.h:1004
#define expect_string(function, parameter, string)
Definition: cmocka.h:753
#define assert_int_equal(a, b)
Definition: cmocka.h:1174