tests: make all tests use the new conf-parsers
[platform/upstream/kmscon.git] / tests / test_include.h
1 /*
2  * kmscon - Common test functions
3  *
4  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <errno.h>
27 #include <signal.h>
28 #include <stdbool.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/signalfd.h>
32 #include "conf.h"
33 #include "eloop.h"
34 #include "log.h"
35
36 #define TEST_HELP \
37         "\t-h, --help                  [off]   Print this help and exit\n" \
38         "\t-v, --verbose               [off]   Print verbose messages\n" \
39         "\t    --debug                 [off]   Enable debug mode\n" \
40         "\t    --silent                [off]   Suppress notices and warnings\n"
41
42 struct {
43         bool help;
44         bool exit;
45         bool verbose;
46         bool debug;
47         bool silent;
48 } test_conf;
49
50 static int aftercheck_debug(struct conf_option *opt, int argc, char **argv,
51                             int idx)
52 {
53         /* --debug implies --verbose */
54         if (test_conf.debug)
55                 test_conf.verbose = 1;
56
57         return 0;
58 }
59
60 static int aftercheck_help(struct conf_option *opt, int argc, char **argv,
61                            int idx)
62 {
63         /* exit after printing --help information */
64         if (test_conf.help) {
65                 print_help();
66                 test_conf.exit = true;
67         }
68
69         return 0;
70 }
71
72 #define TEST_OPTIONS \
73         CONF_OPTION_BOOL('h', "help", aftercheck_help, &test_conf.help, false), \
74         CONF_OPTION_BOOL('v', "verbose", NULL, &test_conf.verbose, false), \
75         CONF_OPTION_BOOL(0, "debug", aftercheck_debug, &test_conf.debug, false), \
76         CONF_OPTION_BOOL(0, "silent", NULL, &test_conf.silent, false)
77
78 static void sig_generic(struct ev_eloop *p, struct signalfd_siginfo *info,
79                         void *data)
80 {
81         struct ev_eloop *eloop = data;
82
83         ev_eloop_exit(eloop);
84         log_info("terminating due to caught signal %d", info->ssi_signo);
85 }
86
87 static int test_prepare(struct conf_option *opts, size_t len,
88                         int argc, char **argv, struct ev_eloop **out)
89 {
90         int ret;
91         struct ev_eloop *eloop;
92
93         ret = conf_parse_argv(opts, len, argc, argv);
94         if (ret)
95                 goto err_out;
96
97         if (test_conf.exit) {
98                 ret = -ECANCELED;
99                 goto err_out;
100         }
101
102         if (!test_conf.debug && !test_conf.verbose && test_conf.silent)
103                 log_set_config(&LOG_CONFIG_WARNING(0, 0, 0, 0));
104         else
105                 log_set_config(&LOG_CONFIG_INFO(test_conf.debug,
106                                                 test_conf.verbose));
107
108         log_print_init(argv[0]);
109
110         ret = ev_eloop_new(&eloop, log_llog);
111         if (ret)
112                 goto err_out;
113
114         ret = ev_eloop_register_signal_cb(eloop, SIGTERM, sig_generic, eloop);
115         if (ret)
116                 goto err_unref;
117
118         ret = ev_eloop_register_signal_cb(eloop, SIGINT, sig_generic, eloop);
119         if (ret) {
120                 ev_eloop_unregister_signal_cb(eloop, SIGTERM,
121                                                 sig_generic, eloop);
122                 goto err_unref;
123         }
124
125         *out = eloop;
126         return 0;
127
128 err_unref:
129         ev_eloop_unref(eloop);
130 err_out:
131         conf_free(opts, len);
132         return ret;
133 }
134
135 static void test_fail(int ret)
136 {
137         if (ret)
138                 log_err("init failed, errno %d: %s", ret, strerror(-ret));
139 }
140
141 static void test_exit(struct conf_option *opts, size_t len,
142                       struct ev_eloop *eloop)
143 {
144         ev_eloop_unregister_signal_cb(eloop, SIGINT, sig_generic, eloop);
145         ev_eloop_unregister_signal_cb(eloop, SIGTERM, sig_generic, eloop);
146         ev_eloop_unref(eloop);
147         conf_free(opts, len);
148 }