settings: Add settings_value_as_uint64() helper function
authorTobias Brunner <tobias@strongswan.org>
Mon, 9 Nov 2015 18:27:20 +0000 (19:27 +0100)
committerTobias Brunner <tobias@strongswan.org>
Wed, 11 Nov 2015 14:39:49 +0000 (15:39 +0100)
src/libstrongswan/settings/settings.c
src/libstrongswan/settings/settings.h
src/libstrongswan/tests/suites/test_settings.c

index 305ebe6..56cc2f1 100644 (file)
@@ -540,6 +540,31 @@ METHOD(settings_t, get_int, int,
 /**
  * Described in header
  */
+inline u_int64_t settings_value_as_uint64(char *value, u_int64_t def)
+{
+       u_int64_t intval;
+       char *end;
+       int base = 10;
+
+       if (value)
+       {
+               errno = 0;
+               if (value[0] == '0' && value[1] == 'x')
+               {       /* manually detect 0x prefix as we want to avoid octal encoding */
+                       base = 16;
+               }
+               intval = strtoull(value, &end, base);
+               if (errno == 0 && *end == 0 && end != value)
+               {
+                       return intval;
+               }
+       }
+       return def;
+}
+
+/**
+ * Described in header
+ */
 inline double settings_value_as_double(char *value, double def)
 {
        double dval;
index 4ef80d0..a133a36 100644 (file)
@@ -51,6 +51,15 @@ bool settings_value_as_bool(char *value, bool def);
 int settings_value_as_int(char *value, int def);
 
 /**
+ * Convert a string value returned by a key/value enumerator to an u_int64_t.
+ *
+ * @see settings_t.create_key_value_enumerator()
+ * @param value                        the string value
+ * @param def                  the default value, if value is NULL or invalid
+ */
+u_int64_t settings_value_as_uint64(char *value, u_int64_t def);
+
+/**
  * Convert a string value returned by a key/value enumerator to a double.
  *
  * @see settings_t.create_key_value_enumerator()
index bead9d7..5ddd0bb 100644 (file)
@@ -317,6 +317,26 @@ START_TEST(test_set_int)
 }
 END_TEST
 
+START_TEST(test_value_as_unit64)
+{
+       test_int_eq(1, settings_value_as_uint64(NULL, 1));
+       test_int_eq(1, settings_value_as_uint64("", 1));
+       test_int_eq(1, settings_value_as_uint64("2a", 1));
+       test_int_eq(1, settings_value_as_uint64("a2", 1));
+       test_int_eq(1, settings_value_as_uint64("2.0", 1));
+
+       test_int_eq(10, settings_value_as_uint64("10", 0));
+       test_int_eq(10, settings_value_as_uint64("010", 0));
+       test_int_eq(16, settings_value_as_uint64("0x010", 0));
+       test_int_eq(0x2a, settings_value_as_uint64("0x2a", 0));
+
+       test_int_eq(0xffffffffffffffffLL, settings_value_as_uint64("0xffffffffffffffff", 0));
+       test_int_eq(0xffffffff00000000LL, settings_value_as_uint64("0xffffffff00000000", 0));
+       test_int_eq(0xffffffff00000000LL, settings_value_as_uint64("18446744069414584320", 0));
+       test_int_eq(0xffffffff00000001LL, settings_value_as_uint64("18446744069414584321", 0));
+}
+END_TEST
+
 START_SETUP(setup_double_config)
 {
        create_settings(chunk_from_str(
@@ -1158,6 +1178,10 @@ Suite *settings_suite_create()
        tcase_add_test(tc, test_set_int);
        suite_add_tcase(s, tc);
 
+       tc = tcase_create("settings_value_as_uint64");
+       tcase_add_test(tc, test_value_as_unit64);
+       suite_add_tcase(s, tc);
+
        tc = tcase_create("get/set_double");
        tcase_add_checked_fixture(tc, setup_double_config, teardown_config);
        tcase_add_test(tc, test_get_double);