diff options
author | David Benjamin <davidben@chromium.org> | 2015-01-26 03:41:26 +0300 |
---|---|---|
committer | Adam Langley <agl@google.com> | 2015-01-27 00:24:50 +0300 |
commit | cd5c892a871389dfa077bda6eec64ffa62dac722 (patch) | |
tree | a00658698dcb7858fa579b2e15a97d9678959be6 /crypto/conf | |
parent | fd4eb604979bbe934468dfb6f4bbba6805a1291f (diff) |
Add a test for CONF_parse_list.
Change-Id: I7af0753cac07756a75d03a4820a16f3f8b18d7ba
Reviewed-on: https://boringssl-review.googlesource.com/3026
Reviewed-by: Adam Langley <agl@google.com>
Diffstat (limited to 'crypto/conf')
-rw-r--r-- | crypto/conf/CMakeLists.txt | 8 | ||||
-rw-r--r-- | crypto/conf/conf_test.c | 129 |
2 files changed, 137 insertions, 0 deletions
diff --git a/crypto/conf/CMakeLists.txt b/crypto/conf/CMakeLists.txt index 05439d54..3d842cb7 100644 --- a/crypto/conf/CMakeLists.txt +++ b/crypto/conf/CMakeLists.txt @@ -8,3 +8,11 @@ add_library( conf.c conf_error.c ) + +add_executable( + conf_test + + conf_test.c +) + +target_link_libraries(conf_test crypto) diff --git a/crypto/conf/conf_test.c b/crypto/conf/conf_test.c new file mode 100644 index 00000000..57e95f17 --- /dev/null +++ b/crypto/conf/conf_test.c @@ -0,0 +1,129 @@ +/* Copyright (c) 2014, Google Inc. + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY + * SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN ACTION + * OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF OR IN + * CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. */ + +#include <stdio.h> +#include <string.h> + +#include <openssl/conf.h> +#include <openssl/crypto.h> + + +static int stop_cb(const char *elem, int len, void *data) { + int *count = (int *)data; + (*count)++; + return -42; +} + +static int test_stop_iteration(void) { + int count = 0; + if (CONF_parse_list("foo|bar", '|', 0, stop_cb, &count) != -42) { + fprintf(stderr, "CONF_parse_list returned incorrect value.\n"); + return 0; + } + + if (count != 1) { + fprintf(stderr, "stop_cb called incorrect number of times.\n"); + return 0; + } + + return 1; +} + +static int test_cb(const char *elem, int len, void *data) { + const char ***ptr = data; + const char **next = *ptr; + if (*next == NULL) { + /* The callback hit the trailing NULL early. */ + fprintf(stderr, "test_cb called too many times.\n"); + return 0; + } + if (len < 0 || (size_t)len != strlen(*next) || + strncmp(elem, *next, len) != 0) { + fprintf(stderr, "test_cb called on '%.*s', wanted '%s'\n", len, elem, *next); + return 0; + } + /* Advance to the next expectation. */ + *ptr = next + 1; + return 1; +} + +static int test_parse_list(const char *list, int remove_whitespace, + const char **expected) { + const char **next = expected; + if (!CONF_parse_list(list, ',', remove_whitespace, test_cb, &next)) { + return 0; + } + if (*next != NULL) { + fprintf(stderr, "test_cb called too few times.\n"); + return 0; + } + return 1; +} + +/* Test basic parsing. Whitespace is not trimmed, empty entries are + * preserved. */ +static const char kList1[] = " foo ,, bar , baz "; +static const int kRemoveWhitespace1 = 0; +static const char *kExpected1[] = { + " foo ", + "", + " bar ", + " baz ", + NULL, +}; + +/* Test that a trailing separator gives an empty entry. */ +static const char kList2[] = "foo,bar,baz,"; +static const int kRemoveWhitespace2 = 0; +static const char *kExpected2[] = { + "foo", + "bar", + "baz", + "", + NULL, +}; + +/* Test whitespace removal. */ +static const char kList3[] = " foo ,\n,bar\t, baz "; +static const int kRemoveWhitespace3 = 1; +static const char *kExpected3[] = { + "foo", + "", + "bar", + "baz", + NULL, +}; + +/* Test empty string behavior. */ +static const char kList4[] = ""; +static const int kRemoveWhitespace4 = 0; +static const char *kExpected4[] = { + "", + NULL, +}; + +int main(void) { + CRYPTO_library_init(); + + if (!test_stop_iteration() || + !test_parse_list(kList1, kRemoveWhitespace1, kExpected1) || + !test_parse_list(kList2, kRemoveWhitespace2, kExpected2) || + !test_parse_list(kList3, kRemoveWhitespace3, kExpected3) || + !test_parse_list(kList4, kRemoveWhitespace4, kExpected4)) { + return 1; + } + + printf("PASS\n"); + return 0; +} |