aboutsummaryrefslogtreecommitdiff
path: root/test/unit/key-value-parse.c
blob: e2283989e3b3158d8f4b6d9780e1788965d749cd (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
/* SPDX-License-Identifier: BSD-2 */
/***********************************************************************
 * Copyright (c) 2017-2018, Intel Corporation
 *
 * All rights reserved.
 ***********************************************************************/

#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <setjmp.h>
#include <cmocka.h>

#include "util/key-value-parse.h"

/*
 * Ensure that a simple key / value string is parsed into its component parts.
 */
static void
parse_key_value_simple_test (void **state)
{
    bool ret;
    char test_str[] = "key=value";
    key_value_t key_value = KEY_VALUE_INIT;

    ret = parse_key_value (test_str, &key_value);
    assert_true (ret);
    assert_string_equal (key_value.key, "key");
    assert_string_equal (key_value.value, "value");
}
/*
 * Ensure that a NULL key/value string causes parse_key_value to return false.
 */
static void
parse_key_value_NULL_string_test (void **state)
{
    bool ret;
    key_value_t key_value = KEY_VALUE_INIT;

    ret = parse_key_value (NULL, &key_value);
    assert_false (ret);
}
/*
 * Ensure that a NULL key_value_t parameter causes parse_key_value to return
 * false.
 */
static void
parse_key_value_NULL_key_value_test (void **state)
{
    bool ret;
    char test_str[] = "key=value";

    ret = parse_key_value (test_str, NULL);
    assert_false (ret);
}
/*
 * Ensure that an incomplete key/value string with only the "key=" returns
 * false.
 */
static void
parse_key_value_no_value_test (void **state)
{
    bool ret;
    char test_str[] = "key=";
    key_value_t key_value = KEY_VALUE_INIT;

    ret = parse_key_value (test_str, &key_value);
    assert_false (ret);
}
/*
 * Ensure that a key/value string with only the "=value" part returns false.
 */
static void
parse_key_value_no_key_test (void **state)
{
    bool ret;
    char test_str[] = "=value";
    key_value_t key_value = KEY_VALUE_INIT;

    ret = parse_key_value (test_str, &key_value);
    assert_false (ret);
}
/*
 * Ensure that a key/value string with the separators in the wrong place
 * returns false.
 */
static void
parse_key_value_two_seps_test (void **state)
{
    bool ret;
    char test_str[] = "=foo=";
    key_value_t key_value = KEY_VALUE_INIT;

    ret = parse_key_value (test_str, &key_value);
    assert_false (ret);
}
/*
 * Ensure that a key/value string with all separators returns false.
 */
static void
parse_key_value_all_seps_test (void **state)
{
    bool ret;
    char test_str[] = "====";
    key_value_t key_value = KEY_VALUE_INIT;

    ret = parse_key_value (test_str, &key_value);
    assert_false (ret);
}
/*
 * Ensure that a key/value string that alternates strings and separators
 * will parse the first two and ignore the rest.
 */
static void
parse_key_value_alt_seps_test (void **state)
{
    bool ret;
    char test_str[] = "key=value=key=value";
    key_value_t key_value = KEY_VALUE_INIT;

    ret = parse_key_value (test_str, &key_value);
    assert_true (ret);
    assert_string_equal (key_value.key, "key");
    assert_string_equal (key_value.value, "value");
}
/*
 * This is a simple data structure used to hold values parsed from a string
 * of key/value pairs.
 */
#define TEST_DATA_INIT { \
    .value0 = NULL, \
    .value1 = NULL, \
}
typedef struct {
    char *value0;
    char *value1;
} test_data_t;
/*
 * This is a callback function used to handle extracted key / value pairs.
 */
TSS2_RC
key_value_callback (const key_value_t *key_value,
                    void *user_data)
{
    test_data_t *test_data = (test_data_t*)user_data;

    if (strcmp ("key0", key_value->key) == 0) {
        test_data->value0 = key_value->value;
        return TSS2_RC_SUCCESS;
    } else if (strcmp ("key1", key_value->key) == 0) {
        test_data->value1 = key_value->value;
        return TSS2_RC_SUCCESS;
    } else {
        return 1;
    }
}
/*
 * This tests the typical case for the parsing of a string of key / value
 * pairs.
 */
static void
parse_key_value_string_good_test (void **state)
{
    TSS2_RC rc;
    char test_str[] = "key0=value0,key1=value1";
    test_data_t test_data = TEST_DATA_INIT;

    rc = parse_key_value_string (test_str, key_value_callback, &test_data);
    assert_int_equal (rc, TSS2_RC_SUCCESS);
    assert_string_equal (test_data.value0, "value0");
    assert_string_equal (test_data.value1, "value1");
}
/*
 * This test ensures that he parse_key_value_string function handles a failed
 * call to parse a key/value pair properly.
 */
static void
parse_key_value_string_no_value_test (void **state)
{
    TSS2_RC rc;
    char test_str[] = "key0=,key1=value1";
    test_data_t test_data = TEST_DATA_INIT;

    rc = parse_key_value_string (test_str, key_value_callback, &test_data);
    assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
}
/*
 * This test ensures that the parse_key_value_string function handles a failed
 * call to the user provided callback properly. The return value we get from
 * the parse_key_value_string function is the same value returned by our
 * callback.
 */
static void
parse_key_value_string_unknown_key_test (void **state)
{
    TSS2_RC rc;
    char test_str[] = "key0=foo=bar,baz=qux";
    test_data_t test_data = TEST_DATA_INIT;

    rc = parse_key_value_string (test_str, key_value_callback, &test_data);
    assert_int_equal (rc, 1);
}
/*
 * The following 3 tests ensures that NULL parameters produce an error.
 */
static void
parse_key_value_string_NULL_kv_string_test (void **state)
{
    TSS2_RC rc;
    test_data_t test_data = TEST_DATA_INIT;

    rc = parse_key_value_string (NULL, key_value_callback, &test_data);
    assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
}
static void
parse_key_value_string_NULL_callback_test (void **state)
{
    TSS2_RC rc;
    char test_str[] = "key0=foo=bar,baz=qux";
    test_data_t test_data = TEST_DATA_INIT;

    rc = parse_key_value_string (test_str, NULL, &test_data);
    assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
}
static void
parse_key_value_string_NULL_user_data_test (void **state)
{
    TSS2_RC rc;
    char test_str[] = "key0=foo=bar,baz=qux";

    rc = parse_key_value_string (test_str, key_value_callback, NULL);
    assert_int_equal (rc, TSS2_TCTI_RC_BAD_VALUE);
}

int
main(int argc, char* argv[])
{
    const struct CMUnitTest tests[] = {
        cmocka_unit_test (parse_key_value_simple_test),
        cmocka_unit_test (parse_key_value_NULL_string_test),
        cmocka_unit_test (parse_key_value_NULL_key_value_test),
        cmocka_unit_test (parse_key_value_no_value_test),
        cmocka_unit_test (parse_key_value_no_key_test),
        cmocka_unit_test (parse_key_value_two_seps_test),
        cmocka_unit_test (parse_key_value_all_seps_test),
        cmocka_unit_test (parse_key_value_alt_seps_test),
        cmocka_unit_test (parse_key_value_string_good_test),
        cmocka_unit_test (parse_key_value_string_no_value_test),
        cmocka_unit_test (parse_key_value_string_unknown_key_test),
        cmocka_unit_test (parse_key_value_string_NULL_kv_string_test),
        cmocka_unit_test (parse_key_value_string_NULL_callback_test),
        cmocka_unit_test (parse_key_value_string_NULL_user_data_test),
    };
    return cmocka_run_group_tests (tests, NULL, NULL);
}