[network-manager-openvpn/th/export-bgo764132: 1/23] shared: reimport shared files



commit 95339651a5768efd62c20749b63f86d43cbe56bb
Author: Thomas Haller <thaller redhat com>
Date:   Wed Mar 23 17:17:28 2016 +0100

    shared: reimport shared files

 shared/nm-glib.h            |    7 ++
 shared/nm-macros-internal.h |  133 +++++++++++++++++++++++++++++++++++++++++--
 shared/nm-test-utils.h      |  113 +++++++++++++++++++++++++++++-------
 3 files changed, 227 insertions(+), 26 deletions(-)
---
diff --git a/shared/nm-glib.h b/shared/nm-glib.h
index 201b6d3..c92d6f0 100644
--- a/shared/nm-glib.h
+++ b/shared/nm-glib.h
@@ -367,4 +367,11 @@ _nm_g_hash_table_get_keys_as_array (GHashTable *hash_table,
        })
 #endif
 
+#ifndef g_info
+/* g_info was only added with 2.39.2 */
+#define g_info(...)     g_log (G_LOG_DOMAIN,         \
+                               G_LOG_LEVEL_INFO,     \
+                               __VA_ARGS__)
+#endif
+
 #endif  /* __NM_GLIB_H__ */
diff --git a/shared/nm-macros-internal.h b/shared/nm-macros-internal.h
index 63f5138..4176231 100644
--- a/shared/nm-macros-internal.h
+++ b/shared/nm-macros-internal.h
@@ -22,7 +22,26 @@
 #ifndef __NM_MACROS_INTERNAL_H__
 #define __NM_MACROS_INTERNAL_H__
 
-#include "nm-default.h"
+#include <stdlib.h>
+
+/********************************************************/
+
+#define nm_auto(fcn) __attribute ((cleanup(fcn)))
+
+/**
+ * nm_auto_free:
+ *
+ * Call free() on a variable location when it goes out of scope.
+ */
+#define nm_auto_free nm_auto(_nm_auto_free_impl)
+GS_DEFINE_CLEANUP_FUNCTION(void*, _nm_auto_free_impl, free)
+
+static inline void
+_nm_auto_unset_gvalue_impl (GValue *v)
+{
+       g_value_unset (v);
+}
+#define nm_auto_unset_gvalue nm_auto(_nm_auto_unset_gvalue_impl)
 
 /********************************************************/
 
@@ -106,6 +125,26 @@
 
 /********************************************************/
 
+/**
+ * NM_G_ERROR_MSG:
+ * @error: (allow none): the #GError instance
+ *
+ * All functions must follow the convention that when they
+ * return a failure, they must also set the GError to a valid
+ * message. For external API however, we want to be extra
+ * careful before accessing the error instance. Use NM_G_ERROR_MSG()
+ * which is safe to use on NULL.
+ *
+ * Returns: the error message.
+ **/
+static inline const char *
+NM_G_ERROR_MSG (GError *error)
+{
+       return error ? (error->message ? : "(null)") : "(no-error)"; \
+}
+
+/********************************************************/
+
 /* macro to return strlen() of a compile time string. */
 #define NM_STRLEN(str)     ( sizeof ("" str) - 1 )
 
@@ -257,19 +296,24 @@ _NM_IN_STRSET_streq (const char *x, const char *s)
 
 #if NM_MORE_ASSERTS
 #define nm_assert(cond) G_STMT_START { g_assert (cond); } G_STMT_END
+#define nm_assert_not_reached() G_STMT_START { g_assert_not_reached (); } G_STMT_END
 #else
 #define nm_assert(cond) G_STMT_START { if (FALSE) { if (cond) { } } } G_STMT_END
+#define nm_assert_not_reached() G_STMT_START { ; } G_STMT_END
 #endif
 
 /*****************************************************************************/
 
-#define NM_GOBJECT_PROPERTIES_DEFINE(obj_type, ...) \
+#define NM_GOBJECT_PROPERTIES_DEFINE_BASE(...) \
 typedef enum { \
        _PROPERTY_ENUMS_0, \
        __VA_ARGS__ \
        _PROPERTY_ENUMS_LAST, \
 } _PropertyEnums; \
-static GParamSpec *obj_properties[_PROPERTY_ENUMS_LAST] = { NULL, }; \
+static GParamSpec *obj_properties[_PROPERTY_ENUMS_LAST] = { NULL, }
+
+#define NM_GOBJECT_PROPERTIES_DEFINE(obj_type, ...) \
+NM_GOBJECT_PROPERTIES_DEFINE_BASE (__VA_ARGS__); \
 static inline void \
 _notify (obj_type *obj, _PropertyEnums prop) \
 { \
@@ -280,6 +324,38 @@ _notify (obj_type *obj, _PropertyEnums prop) \
 
 /*****************************************************************************/
 
+#define nm_unauto(pp)                                               \
+    ({                                                              \
+        G_STATIC_ASSERT (sizeof *(pp) == sizeof (gpointer));        \
+        gpointer *_pp = (gpointer *) (pp);                          \
+        gpointer _p = *_pp;                                         \
+                                                                    \
+        *_pp = NULL;                                                \
+        _p;                                                         \
+    })
+
+/*****************************************************************************/
+
+static inline gpointer
+nm_g_object_ref (gpointer obj)
+{
+       /* g_object_ref() doesn't accept NULL. */
+       if (obj)
+               g_object_ref (obj);
+       return obj;
+}
+
+static inline void
+nm_g_object_unref (gpointer obj)
+{
+       /* g_object_unref() doesn't accept NULL. Usully, we workaround that
+        * by using g_clear_object(), but sometimes that is not convinient
+        * (for example as as destroy function for a hash table that can contain
+        * NULL values). */
+       if (obj)
+               g_object_unref (obj);
+}
+
 static inline gboolean
 nm_clear_g_source (guint *id)
 {
@@ -421,7 +497,7 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
                 * It disallows a buffer size of sizeof(gpointer) to catch that. */ \
                G_STATIC_ASSERT (G_N_ELEMENTS (buf) == sizeof (buf) && sizeof (buf) != sizeof (char *)); \
                g_snprintf (_buf, sizeof (buf), \
-                           ""format"", __VA_ARGS__); \
+                           ""format"", ##__VA_ARGS__); \
                _buf; \
        })
 
@@ -432,10 +508,57 @@ nm_decode_version (guint version, guint *major, guint *minor, guint *micro) {
                G_STATIC_ASSERT (sizeof (char[MAX ((n_elements), 1)]) == (n_elements)); \
                _buf = g_alloca (n_elements); \
                g_snprintf (_buf, n_elements, \
-                           ""format"", __VA_ARGS__); \
+                           ""format"", ##__VA_ARGS__); \
                _buf; \
        })
 
 /*****************************************************************************/
 
+/**
+ * The boolean type _Bool is C99 while we mostly stick to C89. However, _Bool is too
+ * convinient to miss and is effectively available in gcc and clang. So, just use it.
+ *
+ * Usually, one would include "stdbool.h" to get the "bool" define which aliases
+ * _Bool. We provide this define here, because we want to make use of it anywhere.
+ * (also, stdbool.h is again C99).
+ *
+ * Using _Bool has advantages over gboolean:
+ *
+ * - commonly _Bool is one byte large, instead of gboolean's 4 bytes (because gboolean
+ *   is a typedef for gint). Especially when having boolean fields in a struct, we can
+ *   thereby easily save some space.
+ *
+ * - _Bool type guarantees that two "true" expressions compare equal. E.g. the follwing
+ *   will not work:
+ *        gboolean v1 = 1;
+ *        gboolean v2 = 2;
+ *        g_assert_cmpint (v1, ==, v2); // will fail
+ *   For that, we often to use !! to coerce gboolean values to 0 or 1:
+ *        g_assert_cmpint (!!v2, ==, TRUE);
+ *   With _Bool type, this will be handled properly by the compiler.
+ *
+ * - For structs, we might want to safe even more space and use bitfields:
+ *       struct s1 {
+ *           gboolean v1:1;
+ *       };
+ *   But the problem here is that gboolean is signed, so that
+ *   v1 will be either 0 or -1 (not 1, TRUE). Thus, the following
+ *   fails:
+ *      struct s1 s = { .v1 = TRUE, };
+ *      g_assert_cmpint (s1.v1, ==, TRUE);
+ *   It will however work just fine with bool/_Bool while retaining the
+ *   notion of having a boolean value.
+ *
+ * Also, add the defines for "true" and "false". Those are nicely highlighted by the editor
+ * as special types, contrary to glib's "TRUE"/"FALSE".
+ */
+
+#ifndef bool
+#define bool _Bool
+#define true    1
+#define false   0
+#endif
+
+/*****************************************************************************/
+
 #endif /* __NM_MACROS_INTERNAL_H__ */
diff --git a/shared/nm-test-utils.h b/shared/nm-test-utils.h
index 85a2e2a..2503245 100644
--- a/shared/nm-test-utils.h
+++ b/shared/nm-test-utils.h
@@ -71,6 +71,8 @@
  *
  * "TRACE", this is shorthand for "log-level=TRACE".
  *
+ * "D", this is shorthand for "log-level=TRACE,no-expect-message".
+ *
  * "sudo-cmd=PATH": when running root tests as normal user, the test will execute
  *   itself by invoking sudo at PATH.
  *   For example
@@ -110,23 +112,24 @@
 
 /* general purpose functions that have no dependency on other nmtst functions */
 
-inline static void
-nmtst_assert_error (GError *error,
-                    GQuark expect_error_domain,
-                    gint expect_error_code,
-                    const char *expect_error_pattern)
-{
-       if (expect_error_domain)
-               g_assert_error (error, expect_error_domain, expect_error_code);
-       else
-               g_assert (error);
-       g_assert (error->message);
-       if (   expect_error_pattern
-           && !g_pattern_match_simple (expect_error_pattern, error->message)) {
-               g_error ("error message does not have expected pattern '%s'. Instead it is '%s' (%s, %d)",
-                        expect_error_pattern, error->message, g_quark_to_string (error->domain), 
error->code);
-       }
-}
+#define nmtst_assert_error(error, expect_error_domain, expect_error_code, expect_error_pattern) \
+       G_STMT_START { \
+               GError *_error = (error); \
+               GQuark _expect_error_domain = (expect_error_domain); \
+               const char *_expect_error_pattern = (expect_error_pattern); \
+               \
+               if (_expect_error_domain) \
+                       g_assert_error (_error, _expect_error_domain, (expect_error_code)); \
+               else \
+                       g_assert (_error); \
+               g_assert (_error->message); \
+               if (   _expect_error_pattern \
+                   && !g_pattern_match_simple (_expect_error_pattern, _error->message)) { \
+                       g_error ("%s:%d: error message does not have expected pattern '%s'. Instead it is 
'%s' (%s, %d)", \
+                                __FILE__, __LINE__, \
+                                _expect_error_pattern, _error->message, g_quark_to_string (_error->domain), 
_error->code); \
+               } \
+       } G_STMT_END
 
 #define NMTST_WAIT(max_wait_ms, wait) \
        ({ \
@@ -154,7 +157,7 @@ inline static void
 _nmtst_assert_success (gboolean success, GError *error, const char *file, int line)
 {
        if (!success || error)
-               g_error ("(%s:%d) FAILURE success=%d, error=%s", file, line, success, error && error->message 
? error->message : "(no error)");
+               g_error ("(%s:%d) FAILURE success=%d, error=%s", file, line, success, error ? error->message 
: "(no error)");
 }
 #define nmtst_assert_success(success, error) _nmtst_assert_success ((success), (error), __FILE__, __LINE__)
 
@@ -340,9 +343,14 @@ __nmtst_init (int *argc, char ***argv, gboolean assert_logging, const char *log_
                        } else if (!g_ascii_strncasecmp (debug, "log-level=", strlen ("log-level="))) {
                                g_free (c_log_level);
                                log_level = c_log_level = g_strdup (&debug[strlen ("log-level=")]);
+                       } else if (!g_ascii_strcasecmp (debug, "D")) {
+                               /* shorthand for "log-level=TRACE,no-expect-message" */
+                               g_free (c_log_level);
+                               log_level = c_log_level = g_strdup ("TRACE");
+                               no_expect_message = TRUE;
                        } else if (!g_ascii_strcasecmp (debug, "TRACE")) {
                                g_free (c_log_level);
-                               log_level = c_log_level = g_strdup (debug);
+                               log_level = c_log_level = g_strdup ("TRACE");
                        } else if (!g_ascii_strncasecmp (debug, "log-domains=", strlen ("log-domains="))) {
                                g_free (c_log_domains);
                                log_domains = c_log_domains = g_strdup (&debug[strlen ("log-domains=")]);
@@ -852,6 +860,18 @@ nmtst_main_loop_run (GMainLoop *loop, int timeout_ms)
        return loopx != NULL;
 }
 
+inline static void
+_nmtst_main_loop_quit_on_notify (GObject *object, GParamSpec *pspec, gpointer user_data)
+{
+       GMainLoop *loop = user_data;
+
+       g_assert (G_IS_OBJECT (object));
+       g_assert (loop);
+
+       g_main_loop_quit (loop);
+}
+#define nmtst_main_loop_quit_on_notify ((GCallback) _nmtst_main_loop_quit_on_notify)
+
 /*****************************************************************************/
 
 inline static const char *
@@ -1147,7 +1167,7 @@ nmtst_platform_ip6_address (const char *address, const char *peer_address, guint
 inline static NMPlatformIP6Address *
 nmtst_platform_ip6_address_full (const char *address, const char *peer_address, guint plen,
                                  int ifindex, NMIPConfigSource source, guint32 timestamp,
-                                 guint32 lifetime, guint32 preferred, guint flags)
+                                 guint32 lifetime, guint32 preferred, guint32 flags)
 {
        NMPlatformIP6Address *addr = nmtst_platform_ip6_address (address, peer_address, plen);
 
@@ -1156,7 +1176,7 @@ nmtst_platform_ip6_address_full (const char *address, const char *peer_address,
        addr->timestamp = timestamp;
        addr->lifetime = lifetime;
        addr->preferred = preferred;
-       addr->flags = flags;
+       addr->n_ifa_flags = flags;
 
        return addr;
 }
@@ -1773,6 +1793,55 @@ nmtst_create_connection_from_keyfile (const char *keyfile_str, const char *keyfi
 
 #ifdef __NM_CONNECTION_H__
 
+inline static GVariant *
+_nmtst_variant_new_vardict (int dummy, ...)
+{
+       GVariantBuilder builder;
+       va_list ap;
+       const char *name;
+       GVariant *variant;
+
+       g_variant_builder_init (&builder, G_VARIANT_TYPE_VARDICT);
+
+       va_start (ap, dummy);
+       while ((name = va_arg (ap, const char *))) {
+               variant = va_arg (ap, GVariant *);
+               g_variant_builder_add (&builder, "{sv}", name, variant);
+       }
+       va_end (ap);
+
+       return g_variant_builder_end (&builder);
+}
+#define nmtst_variant_new_vardict(...) _nmtst_variant_new_vardict (0, __VA_ARGS__, NULL)
+
+#define nmtst_assert_variant_is_of_type(variant, type) \
+       G_STMT_START { \
+               GVariant *_variantx = (variant); \
+               \
+               g_assert (_variantx); \
+               g_assert (g_variant_is_of_type (_variantx, (type))); \
+       } G_STMT_END
+
+#define nmtst_assert_variant_uint32(variant, val) \
+       G_STMT_START { \
+               GVariant *_variant = (variant); \
+               \
+               nmtst_assert_variant_is_of_type (_variant, G_VARIANT_TYPE_UINT32); \
+               g_assert_cmpint (g_variant_get_uint32 (_variant), ==, (val)); \
+       } G_STMT_END
+
+#define nmtst_assert_variant_string(variant, str) \
+       G_STMT_START { \
+               gsize _l; \
+               GVariant *_variant = (variant); \
+               const char *_str = (str); \
+               \
+               nmtst_assert_variant_is_of_type (_variant, G_VARIANT_TYPE_STRING); \
+               g_assert (_str); \
+               g_assert_cmpstr (g_variant_get_string (_variant, &_l), ==, _str); \
+               g_assert_cmpint (_l, ==, strlen (_str)); \
+       } G_STMT_END
+
 typedef enum {
        NMTST_VARIANT_EDITOR_CONNECTION,
        NMTST_VARIANT_EDITOR_SETTING,
@@ -1815,6 +1884,8 @@ typedef enum {
                         \
                        if (__cur_setting_name) \
                                g_variant_builder_add (&__connection_builder, "{sa{sv}}", __cur_setting_name, 
&__setting_builder); \
+                       else \
+                               g_variant_builder_clear (&__setting_builder); \
                        g_variant_iter_free (__setting_iter); \
                } \
                 \


[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]