1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
|
/*
* ProFTPD - mod_prometheus API testsuite
* Copyright (c) 2021 TJ Saunders <tj@castaglia.org>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Suite 500, Boston, MA 02110-1335, USA.
*
* As a special exemption, TJ Saunders and other respective copyright holders
* give permission to link this program with OpenSSL, and distribute the
* resulting executable, without including the source code for OpenSSL in the
* source distribution.
*/
/* HTTP API tests. */
#include "tests.h"
#include "prometheus/http.h"
#include "prometheus/registry.h"
static pool *p = NULL;
static void set_up(void) {
if (p == NULL) {
p = permanent_pool = make_sub_pool(NULL);
}
if (getenv("TEST_VERBOSE") != NULL) {
pr_trace_set_levels("prometheus.http", 1, 20);
}
mark_point();
prom_http_init(p);
}
static void tear_down(void) {
prom_http_free();
if (getenv("TEST_VERBOSE") != NULL) {
pr_trace_set_levels("prometheus.http", 0, 0);
}
if (p != NULL) {
destroy_pool(p);
p = permanent_pool = NULL;
}
}
START_TEST (http_init_test) {
int res;
mark_point();
res = prom_http_init(NULL);
fail_unless(res < 0, "Failed to handle null pool");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
res = prom_http_init(p);
fail_unless(res == 0, "Failed to init HTTP API: %s", strerror(errno));
}
END_TEST
START_TEST (http_free_test) {
int res;
mark_point();
res = prom_http_free();
fail_unless(res == 0, "Failed to free HTTP API: %s", strerror(errno));
}
END_TEST
START_TEST (http_stop_test) {
int res;
mark_point();
res = prom_http_stop(NULL, NULL);
fail_unless(res < 0, "Failed to handle null pool");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
res = prom_http_stop(p, NULL);
fail_unless(res < 0, "Failed to handle null http");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
}
END_TEST
START_TEST (http_start_test) {
int res;
pr_netaddr_t *addr;
struct prom_http *http;
struct prom_registry *registry;
mark_point();
http = prom_http_start(NULL, NULL, NULL, NULL, NULL);
fail_unless(http == NULL, "Failed to handle null pool");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
http = prom_http_start(p, NULL, NULL, NULL, NULL);
fail_unless(http == NULL, "Failed to handle null addr");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
addr = pr_netaddr_alloc(p);
pr_netaddr_set_family(addr, AF_INET);
pr_netaddr_set_sockaddr_any(addr);
pr_netaddr_set_port2(addr, 0);
http = prom_http_start(p, addr, NULL, NULL, NULL);
fail_unless(http == NULL, "Failed to handle null registry");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
/* Note: We don't need a real registry here, just a non-null pointer. */
registry = pcalloc(p, 8);
http = prom_http_start(p, addr, registry, NULL, NULL);
fail_unless(http != NULL, "Failed to start http: %s", strerror(errno));
mark_point();
res = prom_http_stop(p, http);
fail_unless(res == 0, "Failed to stop http: %s", strerror(errno));
}
END_TEST
START_TEST (http_run_loop_test) {
int res;
mark_point();
res = prom_http_run_loop(NULL, NULL);
fail_unless(res < 0, "Failed to handle null pool");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
mark_point();
res = prom_http_run_loop(p, NULL);
fail_unless(res < 0, "Failed to handle null http");
fail_unless(errno == EINVAL, "Expected EINVAL (%d), got %s (%d)", EINVAL,
strerror(errno), errno);
}
END_TEST
Suite *tests_get_http_suite(void) {
Suite *suite;
TCase *testcase;
suite = suite_create("http");
testcase = tcase_create("base");
tcase_add_checked_fixture(testcase, set_up, tear_down);
tcase_add_test(testcase, http_init_test);
tcase_add_test(testcase, http_free_test);
tcase_add_test(testcase, http_stop_test);
tcase_add_test(testcase, http_start_test);
tcase_add_test(testcase, http_run_loop_test);
suite_add_tcase(suite, testcase);
return suite;
}
|