From b28c936bd281c4b7ff9ed0f621b840f6d5a4b328 Mon Sep 17 00:00:00 2001 From: Stef Walter Date: Wed, 23 Jan 2013 14:29:25 +0100 Subject: Use the stdbool.h C99 bool type It was getting really wild knowing whether a function returning an int would return -1 on failure or 0 or whether the int return value was actually a number etc.. --- p11-kit/tests/conf-test.c | 19 ++++++++++++++----- p11-kit/tests/mock-module.c | 14 +++++++------- p11-kit/tests/uri-test.c | 41 ++++++++++++++++++++++++++++++++++++----- 3 files changed, 57 insertions(+), 17 deletions(-) (limited to 'p11-kit/tests') diff --git a/p11-kit/tests/conf-test.c b/p11-kit/tests/conf-test.c index ab3b31d..704313d 100644 --- a/p11-kit/tests/conf-test.c +++ b/p11-kit/tests/conf-test.c @@ -107,7 +107,7 @@ test_merge_defaults (CuTest *tc) p11_dict_set (defaults, strdup ("two"), strdup ("default2")); p11_dict_set (defaults, strdup ("three"), strdup ("default3")); - if (_p11_conf_merge_defaults (values, defaults) < 0) + if (!_p11_conf_merge_defaults (values, defaults)) CuFail (tc, "should not be reached"); p11_dict_free (defaults); @@ -247,13 +247,11 @@ test_load_globals_user_sets_invalid (CuTest *tc) p11_dict_free (config); } -static int +static bool assert_msg_contains (const char *msg, const char *text) { - if (msg == NULL) - return 0; - return strstr (msg, text) ? 1 : 0; + return (msg && strstr (msg, text)) ? true : false; } static void @@ -378,6 +376,16 @@ test_load_modules_no_user (CuTest *tc) p11_dict_free (configs); } +static void +test_parse_boolean (CuTest *tc) +{ + p11_message_quiet (); + + CuAssertIntEquals (tc, true, _p11_conf_parse_boolean ("yes", false)); + CuAssertIntEquals (tc, false, _p11_conf_parse_boolean ("no", true)); + CuAssertIntEquals (tc, true, _p11_conf_parse_boolean ("!!!", true)); +} + int main (void) { @@ -402,6 +410,7 @@ main (void) SUITE_ADD_TEST (suite, test_load_modules_no_user); SUITE_ADD_TEST (suite, test_load_modules_user_only); SUITE_ADD_TEST (suite, test_load_modules_user_none); + SUITE_ADD_TEST (suite, test_parse_boolean); p11_kit_be_quiet (); diff --git a/p11-kit/tests/mock-module.c b/p11-kit/tests/mock-module.c index 1a74806..15db600 100644 --- a/p11-kit/tests/mock-module.c +++ b/p11-kit/tests/mock-module.c @@ -53,7 +53,7 @@ static p11_mutex_t init_mutex; /* Whether we've been initialized, and on what process id it happened */ -static int pkcs11_initialized = 0; +static bool pkcs11_initialized = false; static pid_t pkcs11_initialized_pid = 0; /* ----------------------------------------------------------------------------- @@ -141,10 +141,10 @@ mock_C_Initialize (CK_VOID_PTR init_args) done: /* Mark us as officially initialized */ if (ret == CKR_OK) { - pkcs11_initialized = 1; + pkcs11_initialized = true; pkcs11_initialized_pid = pid; } else if (ret != CKR_CRYPTOKI_ALREADY_INITIALIZED) { - pkcs11_initialized = 0; + pkcs11_initialized = false; pkcs11_initialized_pid = 0; } @@ -158,13 +158,13 @@ CK_RV mock_C_Finalize (CK_VOID_PTR reserved) { debug (("C_Finalize: enter")); - return_val_if_fail (pkcs11_initialized != 0, CKR_CRYPTOKI_NOT_INITIALIZED); + return_val_if_fail (pkcs11_initialized, CKR_CRYPTOKI_NOT_INITIALIZED); return_val_if_fail (reserved == NULL, CKR_ARGUMENTS_BAD); p11_mutex_lock (&init_mutex); /* This should stop all other calls in */ - pkcs11_initialized = 0; + pkcs11_initialized = false; pkcs11_initialized_pid = 0; p11_mutex_unlock (&init_mutex); @@ -890,9 +890,9 @@ CK_FUNCTION_LIST mock_module_no_slots = { void mock_module_init (void) { - static int initialized = 0; + static bool initialized = false; if (!initialized) { p11_mutex_init (&init_mutex); - initialized = 1; + initialized = true; } } diff --git a/p11-kit/tests/uri-test.c b/p11-kit/tests/uri-test.c index 1920412..11fdebe 100644 --- a/p11-kit/tests/uri-test.c +++ b/p11-kit/tests/uri-test.c @@ -210,18 +210,18 @@ test_uri_parse_with_bad_hex_encoding (CuTest *tc) p11_kit_uri_free (uri); } -static int +static bool is_space_string (CK_UTF8CHAR_PTR string, CK_ULONG size, const char *check) { size_t i, len = strlen (check); if (len > size) - return 0; + return false; if (memcmp (string, check, len) != 0) - return 0; + return false; for (i = len; i < size; ++i) if (string[i] != ' ') - return 0; - return 1; + return false; + return true; } static void @@ -909,6 +909,36 @@ test_uri_match_module (CuTest *tc) } static void +test_uri_match_version (CuTest *tc) +{ + CK_INFO info; + P11KitUri *uri; + int ret; + + memset (&info, 0, sizeof (info)); + + uri = p11_kit_uri_new (); + CuAssertPtrNotNull (tc, uri); + + ret = p11_kit_uri_parse ("pkcs11:library-version=5.8", P11_KIT_URI_FOR_ANY, uri); + CuAssertIntEquals (tc, P11_KIT_URI_OK, ret); + + info.libraryVersion.major = 5; + info.libraryVersion.minor = 8; + + ret = p11_kit_uri_match_module_info (uri, &info); + CuAssertIntEquals (tc, 1, ret); + + info.libraryVersion.major = 2; + info.libraryVersion.minor = 3; + + ret = p11_kit_uri_match_module_info (uri, &info); + CuAssertIntEquals (tc, 0, ret); + + p11_kit_uri_free (uri); +} + +static void test_uri_match_attributes (CuTest *tc) { CK_ATTRIBUTE attrs[4]; @@ -1208,6 +1238,7 @@ main (void) SUITE_ADD_TEST (suite, test_uri_get_set_unrecognized); SUITE_ADD_TEST (suite, test_uri_match_token); SUITE_ADD_TEST (suite, test_uri_match_module); + SUITE_ADD_TEST (suite, test_uri_match_version); SUITE_ADD_TEST (suite, test_uri_match_attributes); SUITE_ADD_TEST (suite, test_uri_get_set_attribute); SUITE_ADD_TEST (suite, test_uri_get_set_attributes); -- cgit v1.1