]> granicus.if.org Git - libevent/commitdiff
Unit test for disabling events with EVENT_NO*, and for EVENT_BASE_FLAG_IGNORE_ENV.
authorNick Mathewson <nickm@torproject.org>
Tue, 28 Apr 2009 19:08:27 +0000 (19:08 +0000)
committerNick Mathewson <nickm@torproject.org>
Tue, 28 Apr 2009 19:08:27 +0000 (19:08 +0000)
svn:r1247

test/regress.c

index d7161ec3d686de7bc18399655a29cf08ab636924..1e2ff6666af45b66e75e8e05cdf234be7033accd 100644 (file)
@@ -54,6 +54,7 @@
 #include <string.h>
 #include <errno.h>
 #include <assert.h>
+#include <ctype.h>
 
 #include "event2/event.h"
 #include "event2/event_struct.h"
@@ -1437,6 +1438,68 @@ end:
                event_config_free(cfg);
 }
 
+static void
+methodname_to_envvar(const char *mname, char *buf, size_t buflen)
+{
+       char *cp;
+       evutil_snprintf(buf, buflen, "EVENT_NO%s", mname);
+       for (cp = buf; *cp; ++cp) {
+               *cp = toupper(*cp);
+       }
+}
+
+static void
+test_base_environ(void *arg)
+{
+       const char **basenames;
+       char varbuf[128];
+       int i, n_methods=0;
+       struct event_base *base = NULL;
+       struct event_config *cfg = NULL;
+       const char *defaultname;
+
+       basenames = event_get_supported_methods();
+       for (i = 0; basenames[i]; ++i) {
+               methodname_to_envvar(basenames[i], varbuf, sizeof(varbuf));
+               unsetenv(varbuf);
+               ++n_methods;
+       }
+
+       base = event_base_new();
+       tt_assert(base);
+
+       defaultname = event_base_get_method(base);
+       event_base_free(base);
+       base = NULL;
+
+       /* Can we disable the method with EVENT_NOfoo ? */
+       methodname_to_envvar(defaultname, varbuf, sizeof(varbuf));
+       setenv(varbuf, "1", 1);
+
+       base = event_base_new();
+       if (n_methods == 1) {
+               tt_assert(!base);
+       } else {
+               tt_assert(base);
+               tt_str_op(defaultname, !=, event_base_get_method(base));
+               event_base_free(base);
+               base = NULL;
+       }
+
+       /* Can we disable looking at the environment with IGNORE_ENV ? */
+       cfg = event_config_new();
+       event_config_set_flag(cfg, EVENT_BASE_FLAG_IGNORE_ENV);
+       base = event_base_new_with_config(cfg);
+       tt_assert(base);
+       tt_str_op(defaultname, ==, event_base_get_method(base));
+
+end:
+       if (base)
+               event_base_free(base);
+       if (cfg)
+               event_config_free(cfg);
+}
+
 static void
 read_called_once_cb(int fd, short event, void *arg)
 {
@@ -1592,6 +1655,7 @@ struct testcase_t main_testcases[] = {
         { "methods", test_methods, TT_FORK, NULL, NULL },
        { "version", test_version, 0, NULL, NULL },
        { "base_features", test_base_features, TT_FORK, NULL, NULL },
+       { "base_environ", test_base_environ, TT_FORK, NULL, NULL },
 
         /* These are still using the old API */
         LEGACY(persistent_timeout, TT_FORK|TT_NEED_BASE),