OpenVPN
key_value_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 <string.h>
20 #include <cmocka.h>
21 
22 #include "key_value.h"
23 
24 static KeyValue key_values[] = {
25  { 10, "this" },
26  { 52, "test" },
27  { 20, "a" },
28  { 13, "is" },
29 };
30 
31 static int create_key_values(void **state) {
32  KeyValue * const items = (KeyValue*)test_malloc(sizeof(key_values));
33  memcpy(items, key_values, sizeof(key_values));
34  *state = (void*)items;
35  set_key_values(items, sizeof(key_values) / sizeof(key_values[0]));
36 
37  return 0;
38 }
39 
40 static int destroy_key_values(void **state) {
41  test_free(*state);
42  set_key_values(NULL, 0);
43 
44  return 0;
45 }
46 
47 static void test_find_item_by_value(void **state) {
48  unsigned int i;
49 
50  (void) state; /* unused */
51 
52  for (i = 0; i < sizeof(key_values) / sizeof(key_values[0]); i++) {
53  KeyValue * const found = find_item_by_value(key_values[i].value);
54  assert_true(found != NULL);
55  assert_int_equal(found->key, key_values[i].key);
56  assert_string_equal(found->value, key_values[i].value);
57  }
58 }
59 
60 static void test_sort_items_by_key(void **state) {
61  unsigned int i;
62  KeyValue * const kv = *state;
64  for (i = 1; i < sizeof(key_values) / sizeof(key_values[0]); i++) {
65  assert_true(kv[i - 1].key < kv[i].key);
66  }
67 }
68 
69 int main(void) {
70  const struct CMUnitTest tests[] = {
75  };
76  return cmocka_run_group_tests(tests, NULL, NULL);
77 }
#define assert_true(c)
Definition: cmocka.h:1045
unsigned int key
Definition: key_value.h:18
int main(void)
static void test_find_item_by_value(void **state)
void set_key_values(KeyValue *const new_key_values, const unsigned int new_number_of_key_values)
Definition: key_value.c:25
#define test_free(ptr)
Definition: cmocka.h:1920
static KeyValue key_values[]
#define assert_string_equal(a, b)
Definition: cmocka.h:1214
static int create_key_values(void **state)
KeyValue * find_item_by_value(const char *const value)
Definition: key_value.c:37
void sort_items_by_key(void)
Definition: key_value.c:48
#define cmocka_unit_test_setup_teardown(f, setup, teardown)
Initialize an array of CMUnitTest structures with a setup function for a test and a teardown function...
Definition: cmocka.h:1665
static void test_sort_items_by_key(void **state)
#define cmocka_run_group_tests(group_tests, group_setup, group_teardown)
Definition: cmocka.h:1749
#define test_malloc(size)
Definition: cmocka.h:1872
static int destroy_key_values(void **state)
#define assert_int_equal(a, b)
Definition: cmocka.h:1174
const char * value
Definition: key_value.h:19
Container for unidirectional cipher and HMAC key material.
Definition: crypto.h:151