/* _ * ___ __ _ __ _ _ _(_) * / __|/ _` |/ _` | | | | | * \__ \ (_| | (_| | |_| | | * |___/\__,_|\__, |\__,_|_| * |___/ * * Cross-platform library which helps to develop web servers or frameworks. * * Copyright (C) 2016-2024 Silvio Clecio * * Sagui library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * Sagui library 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 * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with Sagui library; if not, write to the Free Software * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA */ #define SG_EXTERN #ifndef TEST_HTTPSRV_PORT #define TEST_HTTPSRV_PORT 8080 #endif /* TEST_HTTPSRV_PORT */ #include "sg_assert.h" #include #include "sg_httpuplds.h" #include "sg_httpsrv.c" const char private_key[] = "-----BEGIN PRIVATE KEY-----\n" "MIIJRAIBADANBgkqhkiG9w0BAQEFAASCCS4wggkqAgEAAoICAQDGWTyCaPu8zxLJ\n" "prHIGGbuxC+1egVbwhxUj+78ik1CwmBKOaPou8/GiQJor7CII+ChWnsKBk1k/gkZ\n" "FRTa6uUBK5k5TpuIKFXJIu9r2WNgc//Pb4dsrx6530PrI8tZGuc1hj1l0XxOq86w\n" "W2Zm+K5nc9OmZzLLsybO3HlhCPzpPR3VnZ0SAtiC+o8OiatQF1UXBrDaRk0OTNgG\n" "xH0C1V1h5YQ+LwwjDpa5mqXMvDWIQnQL468OUt02vTagmP46BppN3Bnb2zZqYbbF\n" "K35pgkHYd3QpyglTkppJv24e9rNQn1I+mNSA/UhvOCGoE8oZTxfRAcFeEKVFiEfi\n" "8KlC6Soremmtkkual56h50ZmFGkWUgmzaF7BBPSpnO/mH3AMy50JBcYKs1FqdGF1\n" "XWyW30C1v8cJVyXfjY5tNbjXHm54KSt99me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n" "FLS4PrnirhDjqXzpHZ6vnjNZRkdSSIQT6FypvieltQDvGVUGzKfqbmp9icY8HX+S\n" "u4a/54s0aFWv/tq/T7rztrPpdYVUQQ1csOC8ouqOQfAYgNNYdLGL29kdbotiLUts\n" "rfJrhDJj1gAUuQGEadqnVhZPDCYL50cQAi7Q64WF6jVwINkGWMGTrNC0kaPPNHao\n" "AMylZbByp/QgngjDv7WIHVVZhXYBTByFhgDbx4amep03wZq3gt54y+KYl/phEYea\n" "08osIWBiyHRBEfkApzqY6bmMglddl8zh+kxWj62RUkmxN2oYsYhdBqDO+qKHL8dh\n" "8wixAoIBAQDUa2h9DVBe4TPg+/+UBCH8rGX6A7j07ufHuqWO7kv+A7rW03X+mbaX\n" "XYnvlWOTa9veZXKCbeC+eHFlahYSsa3/i94wLUhqtAZt2AwXoPYQgEXZn71Ntjs7\n" "KjNZ7FC9go72DmgB/j/lkWBCMRVVKXUJg8ZjsRmNkDzfrkwXJ7iBk4PFJn5EUPoq\n" "vHXgEbJLW+SGs3OUkV8x9OcdDey1p7ek7Xyuzh3EoloSxIwiyZzbUapDZ16iHDVK\n" "t67jHRtLrU8CFzMaYIOVHc3E0JsZ4wOlzdfgaoJff2muH9hgag0bfZdLyKtVPcw0\n" "ZXLHRNJloH4aoqAMpMIldWST/VRLB+Pn\n" "-----END PRIVATE KEY-----"; const char certificate[] = "-----BEGIN CERTIFICATE-----\n" "MIIE/jCCAuagAwIBAgIJAKktAyUaaqdMMA0GCSqGSIb3DQEBCwUAMBQxEjAQBgNV\n" "BAMMCWxvY2FsaG9zdDAeFw0xODA3MjkwNTIyMzhaFw0yODA3MjYwNTIyMzhaMBQx\n" "EjAQBgNVBAMMCWxvY2FsaG9zdDCCAiIwDQYJKoZIhvcNAQEBBQADggIPADCCAgoC\n" "ggIBAMZZPIJo+7zPEsmmscgYZu7EL7V6BVvCHFSP7vyKTULCYEo5o+i7z8aJAmiv\n" "sIgj4KFaewoGTWT+CRkVFNrq5QErmTlOm4goVcki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ktoi2e7DKlRw5sl3zSNqqHDd3hMTRnW3foHG/dAki50P/lv2iCrrldUY8\n" "qAzV8Cvy4EyHOOi9GwDrGqqKeItAv0BWPk707rnqUq9bfB/w9gms/DUIL7Xk6C18\n" "7oR12ypWJvrgFlTH+x9fkN+S0sPw7BW3ycK3dBpzCn8lhpOs8ScALBcmPpjZLEXK\n" "4+Q+g01hzFZUT6NJvIq4V4xxkiklrNXmbTNhxuzju5iKJE7C+Vq4pVMGEAfVZjdK\n" "xC7EUyKBXrM9oJQIC5pmRKxauXE1iuQ1YxqtD3LmETH8X7z/UT+1oPawHVWuLL39\n" "rS6424vBDc2LPk3EM2m8pXSU7wm2X9/QS1ba7DeGTS5Z3X90e78nbWgsT6WSFXPc\n" "0TVbmdKpoMAfw8u+1NAmY6DrpHRTUOzJ6bsjwi9pUbP79A==\n" "-----END CERTIFICATE-----"; static void dummy_err_cb(void *cls, const char *err) { strcpy(cls, err); } static void dummy_httpreq_cb(void *cls, struct sg_httpreq *req, struct sg_httpres *res) { (void) cls; (void) req; (void) res; } static void dummy_httpreq_err_cb(void *cls, const char *err) { (void) cls; (void) err; } static void dummy_httpreq_err_cpy_cb(void *cls, const char *err) { strcpy(cls, err); } static bool dummy_httpreq_httpauth_cb(void *cls, struct sg_httpauth *auth, struct sg_httpreq *req, struct sg_httpres *res) { (void) cls; (void) auth; (void) req; (void) res; return true; } static void dummy_httpsrv_cli_cb(void *cls, const void *client, bool *closed) { (void) cls; (void) client; (void) closed; } static int dummy_httpupld_cb(void *cls, void **handle, const char *dir, const char *field, const char *name, const char *mime, const char *encoding) { (void) cls; (void) handle; (void) dir; (void) field; (void) name; (void) mime; (void) encoding; return 0; } static ssize_t dummy_httpupld_write_cb(void *handle, uint64_t offset, const char *buf, size_t size) { (void) handle; (void) offset; (void) buf; (void) size; return 0; } static void dummy_httpupld_free_cb(void *handle) { (void) handle; } static int dummy_httpupld_save_cb(void *handle, bool overwritten) { (void) handle; (void) overwritten; return 0; } static int dummy_httpupld_save_as_cb(void *handle, const char *path, bool overwritten) { (void) handle; (void) path; (void) overwritten; return 0; } static void test__httpsrv_oel(const char *fmt, ...) { struct sg_httpsrv *srv; char err[256]; va_list ap; srv = sg_httpsrv_new2(dummy_httpreq_httpauth_cb, dummy_httpreq_cb, dummy_httpreq_err_cpy_cb, err); ASSERT(srv); va_start(ap, fmt); memset(err, 0, sizeof(err)); sg__httpsrv_oel(srv, fmt, ap); va_end(ap); ASSERT(strcmp(err, "abc123") == 0); sg_httpsrv_free(srv); } static void test__httpsrv_ahc(struct MHD_Connection *con, struct sg_httpsrv *srv) { struct sg_httpreq *req = NULL; size_t size = 0; ASSERT(sg__httpsrv_ahc(srv, con, "abc", "def", "ghi", NULL, &size, (void **) &req) == MHD_NO); } static void test__httpsrv_rcc(struct MHD_Connection *con) { struct sg_httpsrv *srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); struct sg_httpreq *req = sg__httpreq_new(srv, con, NULL, NULL, NULL); sg_httpsrv_free(srv); sg__httpsrv_rcc(NULL, NULL, (void **) &req, MHD_REQUEST_TERMINATED_COMPLETED_OK); ASSERT(!req); } static void test__httpsrv_addopt(void) { struct MHD_OptionItem ops[14]; unsigned char pos = 0; int dummy = 123; memset(ops, 0, sizeof(ops)); sg__httpsrv_addopt(ops, &pos, MHD_OPTION_EXTERNAL_LOGGER, 456, &dummy); ASSERT(ops[0].option == MHD_OPTION_EXTERNAL_LOGGER); ASSERT(ops[0].value == 456); ASSERT(*((int *) ops[0].ptr_value) == 123); ASSERT(pos == 1); dummy = 321; sg__httpsrv_addopt(ops, &pos, MHD_OPTION_NOTIFY_COMPLETED, 789, &dummy); ASSERT(ops[1].option == MHD_OPTION_NOTIFY_COMPLETED); ASSERT(ops[1].value == 789); ASSERT(*((int *) ops[1].ptr_value) == 321); ASSERT(pos == 2); sg__httpsrv_addopt(ops, &pos, MHD_OPTION_END, 0, NULL); ASSERT(ops[2].option == MHD_OPTION_END); ASSERT(ops[2].value == 0); ASSERT(!ops[2].ptr_value); } static void test__httpsrv_eprintf(void) { char err[256], str[256]; struct sg_httpsrv *srv = sg_httpsrv_new2(NULL, dummy_httpreq_cb, dummy_err_cb, err); memset(err, 0, sizeof(err)); sg__httpsrv_eprintf(srv, "%s%d", "abc", 123); memset(str, 0, sizeof(str)); snprintf(str, sizeof(str), "abc123"); ASSERT(strcmp(err, str) == 0); sg_httpsrv_free(srv); } static void test_httpsrv_new2(void) { struct sg_httpsrv *srv; int dummy = 0; char *tmp; errno = 0; ASSERT(!sg_httpsrv_new2(dummy_httpreq_httpauth_cb, NULL, dummy_httpreq_err_cb, &dummy)); ASSERT(errno == EINVAL); errno = 0; ASSERT(!sg_httpsrv_new2(dummy_httpreq_httpauth_cb, dummy_httpreq_cb, NULL, &dummy)); ASSERT(errno == EINVAL); srv = sg_httpsrv_new2(dummy_httpreq_httpauth_cb, dummy_httpreq_cb, dummy_httpreq_err_cb, &dummy); ASSERT(srv); ASSERT(srv->auth_cb == dummy_httpreq_httpauth_cb); ASSERT(srv->req_cb == dummy_httpreq_cb); ASSERT(srv->err_cb == dummy_httpreq_err_cb); ASSERT(srv->cls == &dummy); ASSERT(srv->upld_cb == sg__httpupld_cb); ASSERT(srv->upld_cls == srv); ASSERT(srv->upld_write_cb == sg__httpupld_write_cb); ASSERT(srv->upld_free_cb == sg__httpupld_free_cb); ASSERT(srv->upld_save_cb == sg__httpupld_save_cb); ASSERT(srv->upld_save_as_cb == sg__httpupld_save_as_cb); tmp = sg_tmpdir(); ASSERT(strcmp(srv->uplds_dir, tmp) == 0); sg_free(tmp); #ifdef __arm__ ASSERT(srv->post_buf_size == 1024); ASSERT(srv->payld_limit == 1048576); ASSERT(srv->uplds_limit == 16777216); #else /* __arm__ */ ASSERT(srv->post_buf_size == 4096); ASSERT(srv->payld_limit == 4194304); ASSERT(srv->uplds_limit == 67108864); #endif /* __arm__ */ sg_httpsrv_free(srv); } static void test_httpsrv_new(void) { struct sg_httpsrv *srv; int dummy = 0; errno = 0; ASSERT(!sg_httpsrv_new(NULL, &dummy)); ASSERT(errno == EINVAL); errno = 0; srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); ASSERT(srv); ASSERT(errno == 0); sg_httpsrv_free(srv); errno = 0; srv = sg_httpsrv_new(dummy_httpreq_cb, &dummy); ASSERT(srv); ASSERT(errno == 0); sg_httpsrv_free(srv); } static void test_httpsrv_free(void) { sg_httpsrv_free(NULL); } static void test_httpsrv_listen(struct sg_httpsrv *srv) { struct sg_httpsrv *dummy_srv; errno = 0; ASSERT(!sg_httpsrv_listen(NULL, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_listen(dummy_srv, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_write_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_listen(dummy_srv, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_save_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_listen(dummy_srv, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_save_as_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_listen(dummy_srv, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); sg_free(dummy_srv->uplds_dir); dummy_srv->uplds_dir = NULL; errno = 0; ASSERT(!sg_httpsrv_listen(dummy_srv, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->post_buf_size = 255; errno = 0; ASSERT(!sg_httpsrv_listen(dummy_srv, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); ASSERT(sg_httpsrv_listen(srv, 0, true)); ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(sg_httpsrv_listen(srv, TEST_HTTPSRV_PORT, false)); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_DUAL_STACK); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_ERROR_LOG); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_AUTO_INTERNAL_THREAD); ASSERT(!(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_THREAD_PER_CONNECTION)); ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(sg_httpsrv_listen(srv, TEST_HTTPSRV_PORT, true)); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_DUAL_STACK); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_ERROR_LOG); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_AUTO_INTERNAL_THREAD); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_THREAD_PER_CONNECTION); ASSERT(sg_httpsrv_shutdown(srv) == 0); } static void test_httpsrv_listen2(struct sg_httpsrv *srv) { struct sg_httpsrv *dummy_srv; errno = 0; ASSERT(!sg_httpsrv_listen2(NULL, NULL, TEST_HTTPSRV_PORT, 0, true)); ASSERT(errno == EINVAL); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_listen2(dummy_srv, NULL, TEST_HTTPSRV_PORT, 0, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_write_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_listen2(dummy_srv, NULL, TEST_HTTPSRV_PORT, 0, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_save_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_listen2(dummy_srv, NULL, TEST_HTTPSRV_PORT, 0, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_save_as_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_listen2(dummy_srv, NULL, TEST_HTTPSRV_PORT, 0, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); sg_free(dummy_srv->uplds_dir); dummy_srv->uplds_dir = NULL; errno = 0; ASSERT(!sg_httpsrv_listen2(dummy_srv, NULL, TEST_HTTPSRV_PORT, 0, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->post_buf_size = 255; errno = 0; ASSERT(!sg_httpsrv_listen2(dummy_srv, NULL, TEST_HTTPSRV_PORT, 0, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); ASSERT(sg_httpsrv_listen2(srv, NULL, 0, 0, true)); ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(sg_httpsrv_listen2(srv, NULL, TEST_HTTPSRV_PORT, 0, false)); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_DUAL_STACK); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_ERROR_LOG); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_AUTO_INTERNAL_THREAD); ASSERT(!(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_THREAD_PER_CONNECTION)); ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(sg_httpsrv_listen2(srv, NULL, TEST_HTTPSRV_PORT, 0, true)); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_DUAL_STACK); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_ERROR_LOG); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_AUTO_INTERNAL_THREAD); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_THREAD_PER_CONNECTION); ASSERT(sg_httpsrv_shutdown(srv) == 0); } #ifdef SG_HTTPS_SUPPORT static void test_httpsrv_tls_listen(struct sg_httpsrv *srv) { struct sg_httpsrv *dummy_srv; errno = 0; ASSERT(!sg_httpsrv_tls_listen(NULL, private_key, certificate, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); errno = 0; ASSERT( !sg_httpsrv_tls_listen(srv, NULL, certificate, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); errno = 0; ASSERT( !sg_httpsrv_tls_listen(srv, private_key, NULL, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_tls_listen(dummy_srv, private_key, certificate, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_write_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_tls_listen(dummy_srv, private_key, certificate, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_save_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_tls_listen(dummy_srv, private_key, certificate, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->upld_save_as_cb = NULL; errno = 0; ASSERT(!sg_httpsrv_tls_listen(dummy_srv, private_key, certificate, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); sg_free(dummy_srv->uplds_dir); dummy_srv->uplds_dir = NULL; errno = 0; ASSERT(!sg_httpsrv_tls_listen(dummy_srv, private_key, certificate, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); dummy_srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); dummy_srv->post_buf_size = 255; errno = 0; ASSERT(!sg_httpsrv_tls_listen(dummy_srv, private_key, certificate, TEST_HTTPSRV_PORT, true)); ASSERT(errno == EINVAL); sg_httpsrv_free(dummy_srv); ASSERT(sg_httpsrv_tls_listen(srv, private_key, certificate, 0, true)); ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(sg_httpsrv_tls_listen(srv, private_key, certificate, TEST_HTTPSRV_PORT, false)); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_DUAL_STACK); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_ERROR_LOG); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_AUTO_INTERNAL_THREAD); ASSERT(!(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_THREAD_PER_CONNECTION)); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_TLS); ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(sg_httpsrv_tls_listen(srv, private_key, certificate, TEST_HTTPSRV_PORT, true)); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_DUAL_STACK); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_ERROR_LOG); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_AUTO_INTERNAL_THREAD); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_THREAD_PER_CONNECTION); ASSERT(MHD_get_daemon_info(srv->handle, MHD_DAEMON_INFO_FLAGS)->flags & MHD_USE_TLS); ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(sg_httpsrv_listen(srv, 0, false)); ASSERT(sg_httpsrv_shutdown(srv) == 0); } static void test_httpsrv_tls_listen2(struct sg_httpsrv *srv) { errno = 0; ASSERT(!sg_httpsrv_tls_listen2(srv, NULL, "", "", "", "", TEST_HTTPSRV_PORT, false)); ASSERT(errno == EINVAL); errno = 0; ASSERT(!sg_httpsrv_tls_listen2(srv, "", "", NULL, "", "", TEST_HTTPSRV_PORT, false)); ASSERT(errno == EINVAL); } static void test_httpsrv_tls_listen4(struct sg_httpsrv *srv) { errno = 0; ASSERT(!sg_httpsrv_tls_listen4(srv, NULL, "", "", "", "", "", "", TEST_HTTPSRV_PORT, 0, false)); ASSERT(errno == EINVAL); errno = 0; ASSERT(!sg_httpsrv_tls_listen4(srv, "", "", NULL, "", "", "", "", TEST_HTTPSRV_PORT, 0, false)); ASSERT(errno == EINVAL); } static void test_httpsrv_tls_listen3(struct sg_httpsrv *srv) { errno = 0; ASSERT(!sg_httpsrv_tls_listen3(srv, NULL, "", "", "", "", "", TEST_HTTPSRV_PORT, false)); ASSERT(errno == EINVAL); errno = 0; ASSERT(!sg_httpsrv_tls_listen3(srv, "", "", NULL, "", "", "", TEST_HTTPSRV_PORT, false)); ASSERT(errno == EINVAL); } #endif /* SG_HTTPS_SUPPORT */ static void test_httpsrv_shutdown(struct sg_httpsrv *srv) { ASSERT(sg_httpsrv_shutdown(NULL) == EINVAL); ASSERT(sg_httpsrv_listen(srv, 0, false)); ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(!srv->handle); ASSERT(sg_httpsrv_shutdown(srv) == EALREADY); ASSERT(sg_httpsrv_listen(srv, TEST_HTTPSRV_PORT, true)); } static void test_httpsrv_port(struct sg_httpsrv *srv) { void *saved_handle; errno = 0; ASSERT(sg_httpsrv_port(NULL) == 0); ASSERT(errno == EINVAL); saved_handle = srv->handle; srv->handle = NULL; errno = 0; ASSERT(sg_httpsrv_port(srv) == 0); ASSERT(errno == 0); srv->handle = saved_handle; errno = 0; ASSERT(sg_httpsrv_port(srv) == TEST_HTTPSRV_PORT); ASSERT(errno == 0); } static void test_httpsrv_is_threaded(struct sg_httpsrv *srv) { void *saved_handle; errno = 0; ASSERT(!sg_httpsrv_is_threaded(NULL)); ASSERT(errno == EINVAL); errno = 0; saved_handle = srv->handle; srv->handle = NULL; errno = 0; ASSERT(!sg_httpsrv_is_threaded(srv)); ASSERT(errno == 0); srv->handle = saved_handle; ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(sg_httpsrv_listen(srv, TEST_HTTPSRV_PORT, false)); errno = 0; ASSERT(!sg_httpsrv_is_threaded(srv)); ASSERT(errno == 0); ASSERT(sg_httpsrv_shutdown(srv) == 0); ASSERT(sg_httpsrv_listen(srv, TEST_HTTPSRV_PORT, true)); errno = 0; ASSERT(sg_httpsrv_is_threaded(srv)); ASSERT(errno == 0); } static void test__httpsrv_set_cli_cb(struct sg_httpsrv *srv) { int dummy = 123; ASSERT(sg_httpsrv_set_cli_cb(NULL, dummy_httpsrv_cli_cb, &dummy) == EINVAL); ASSERT(sg_httpsrv_set_cli_cb(srv, NULL, &dummy) == EINVAL); ASSERT(sg_httpsrv_set_cli_cb(srv, dummy_httpsrv_cli_cb, &dummy) == 0); ASSERT(srv->cli_cb == dummy_httpsrv_cli_cb); ASSERT(*((int *) srv->cli_cls) == 123); } static void test__httpsrv_set_upld_cbs(struct sg_httpsrv *srv) { int dummy = 123; ASSERT(sg_httpsrv_set_upld_cbs(NULL, dummy_httpupld_cb, &dummy, dummy_httpupld_write_cb, dummy_httpupld_free_cb, dummy_httpupld_save_cb, dummy_httpupld_save_as_cb) == EINVAL); ASSERT(sg_httpsrv_set_upld_cbs(srv, NULL, &dummy, dummy_httpupld_write_cb, dummy_httpupld_free_cb, dummy_httpupld_save_cb, dummy_httpupld_save_as_cb) == EINVAL); ASSERT(sg_httpsrv_set_upld_cbs(srv, dummy_httpupld_cb, &dummy, NULL, dummy_httpupld_free_cb, dummy_httpupld_save_cb, dummy_httpupld_save_as_cb) == EINVAL); ASSERT(sg_httpsrv_set_upld_cbs( srv, dummy_httpupld_cb, &dummy, dummy_httpupld_write_cb, dummy_httpupld_free_cb, NULL, dummy_httpupld_save_as_cb) == EINVAL); ASSERT(sg_httpsrv_set_upld_cbs( srv, dummy_httpupld_cb, &dummy, dummy_httpupld_write_cb, dummy_httpupld_free_cb, dummy_httpupld_save_cb, NULL) == EINVAL); ASSERT(sg_httpsrv_set_upld_cbs(srv, dummy_httpupld_cb, &dummy, dummy_httpupld_write_cb, dummy_httpupld_free_cb, dummy_httpupld_save_cb, dummy_httpupld_save_as_cb) == 0); ASSERT(srv->upld_cb == dummy_httpupld_cb); ASSERT(srv->upld_write_cb == dummy_httpupld_write_cb); ASSERT(srv->upld_write_cb == dummy_httpupld_write_cb); ASSERT(srv->upld_free_cb == dummy_httpupld_free_cb); ASSERT(srv->upld_save_cb == dummy_httpupld_save_cb); ASSERT(srv->upld_save_as_cb == dummy_httpupld_save_as_cb); ASSERT(*((int *) srv->upld_cls) == 123); } static void test_httpsrv_set_upld_dir(struct sg_httpsrv *srv) { ASSERT(sg_httpsrv_set_upld_dir(NULL, "foo") == EINVAL); ASSERT(sg_httpsrv_set_upld_dir(srv, NULL) == EINVAL); ASSERT(sg_httpsrv_set_upld_dir(srv, "foo") == 0); } static void test_httpsrv_upld_dir(struct sg_httpsrv *srv) { errno = 0; ASSERT(!sg_httpsrv_upld_dir(NULL)); ASSERT(errno == EINVAL); ASSERT(sg_httpsrv_set_upld_dir(srv, "foo") == 0); errno = 0; ASSERT(strcmp(sg_httpsrv_upld_dir(srv), "foo") == 0); ASSERT(errno == 0); } static void test_httpsrv_set_post_buf_size(struct sg_httpsrv *srv) { ASSERT(sg_httpsrv_set_post_buf_size(NULL, 256) == EINVAL); ASSERT(sg_httpsrv_set_post_buf_size(srv, 255) == EINVAL); ASSERT(sg_httpsrv_set_post_buf_size(srv, 256) == 0); } static void test_httpsrv_post_buf_size(struct sg_httpsrv *srv) { errno = 0; ASSERT(sg_httpsrv_post_buf_size(NULL) == 0); ASSERT(errno == EINVAL); ASSERT(sg_httpsrv_set_post_buf_size(srv, 256) == 0); errno = 0; ASSERT(sg_httpsrv_post_buf_size(srv) == 256); ASSERT(errno == 0); } static void test_httpsrv_set_payld_limit(struct sg_httpsrv *srv) { ASSERT(sg_httpsrv_set_payld_limit(NULL, 123) == EINVAL); ASSERT(sg_httpsrv_set_payld_limit(srv, 0) == 0); ASSERT(sg_httpsrv_set_payld_limit(srv, 123) == 0); } static void test_httpsrv_payld_limit(struct sg_httpsrv *srv) { errno = 0; ASSERT(sg_httpsrv_payld_limit(NULL) == 0); ASSERT(errno == EINVAL); ASSERT(sg_httpsrv_set_payld_limit(srv, 123) == 0); errno = 0; ASSERT(sg_httpsrv_payld_limit(srv) == 123); ASSERT(errno == 0); } static void test_httpsrv_set_uplds_limit(struct sg_httpsrv *srv) { ASSERT(sg_httpsrv_set_uplds_limit(NULL, 123) == EINVAL); ASSERT(sg_httpsrv_set_uplds_limit(srv, 0) == 0); ASSERT(sg_httpsrv_set_uplds_limit(srv, 123) == 0); } static void test_httpsrv_uplds_limit(struct sg_httpsrv *srv) { errno = 0; ASSERT(sg_httpsrv_uplds_limit(NULL) == 0); ASSERT(errno == EINVAL); ASSERT(sg_httpsrv_set_uplds_limit(srv, 123) == 0); errno = 0; ASSERT(sg_httpsrv_uplds_limit(srv) == 123); ASSERT(errno == 0); } static void test_httpsrv_set_thr_pool_size(struct sg_httpsrv *srv) { ASSERT(sg_httpsrv_set_thr_pool_size(NULL, 123) == EINVAL); ASSERT(sg_httpsrv_set_thr_pool_size(srv, 0) == 0); ASSERT(sg_httpsrv_set_thr_pool_size(srv, 123) == 0); } static void test_httpsrv_thr_pool_size(struct sg_httpsrv *srv) { errno = 0; ASSERT(sg_httpsrv_thr_pool_size(NULL) == 0); ASSERT(errno == EINVAL); ASSERT(sg_httpsrv_set_thr_pool_size(srv, 123) == 0); errno = 0; ASSERT(sg_httpsrv_thr_pool_size(srv) == 123); ASSERT(errno == 0); } static void test_httpsrv_set_con_timeout(struct sg_httpsrv *srv) { ASSERT(sg_httpsrv_set_con_timeout(NULL, 123) == EINVAL); ASSERT(sg_httpsrv_set_con_timeout(srv, 0) == 0); ASSERT(sg_httpsrv_set_con_timeout(srv, 123) == 0); } static void test_httpsrv_con_timeout(struct sg_httpsrv *srv) { errno = 0; ASSERT(sg_httpsrv_con_timeout(NULL) == 0); ASSERT(errno == EINVAL); ASSERT(sg_httpsrv_set_con_timeout(srv, 123) == 0); errno = 0; ASSERT(sg_httpsrv_con_timeout(srv) == 123); ASSERT(errno == 0); } static void test_httpsrv_set_con_limit(struct sg_httpsrv *srv) { ASSERT(sg_httpsrv_set_con_limit(NULL, 123) == EINVAL); ASSERT(sg_httpsrv_set_con_limit(srv, 0) == 0); ASSERT(sg_httpsrv_set_con_limit(srv, 123) == 0); } static void test_httpsrv_con_limit(struct sg_httpsrv *srv) { errno = 0; ASSERT(sg_httpsrv_con_limit(NULL) == 0); ASSERT(errno == EINVAL); ASSERT(sg_httpsrv_set_con_limit(srv, 123) == 0); errno = 0; ASSERT(sg_httpsrv_con_limit(srv) == 123); ASSERT(errno == 0); } static void test_httpsrv_handle(struct sg_httpsrv *srv) { void *fake_handle = (void *) 123; void *old_handle; errno = 0; ASSERT(sg_httpsrv_handle(NULL) == 0); ASSERT(errno == EINVAL); errno = 0; old_handle = srv->handle; srv->handle = fake_handle; ASSERT(sg_httpsrv_handle(srv) == fake_handle); ASSERT(errno == 0); srv->handle = old_handle; } int main(void) { struct MHD_Connection *con = sg_alloc(256); struct sg_httpsrv *srv = sg_httpsrv_new(dummy_httpreq_cb, NULL); /* test__httperr_cb() */ test__httpsrv_oel("%s%d", "abc", 123); test__httpsrv_ahc(con, srv); test__httpsrv_rcc(con); test__httpsrv_addopt(); test__httpsrv_eprintf(); test_httpsrv_new2(); test_httpsrv_new(); test_httpsrv_free(); test_httpsrv_listen(srv); test_httpsrv_listen2(srv); #ifdef SG_HTTPS_SUPPORT test_httpsrv_tls_listen(srv); test_httpsrv_tls_listen2(srv); test_httpsrv_tls_listen3(srv); test_httpsrv_tls_listen4(srv); #endif /* SG_HTTPS_SUPPORT */ test_httpsrv_shutdown(srv); test_httpsrv_port(srv); test_httpsrv_is_threaded(srv); test__httpsrv_set_cli_cb(srv); test__httpsrv_set_upld_cbs(srv); test_httpsrv_set_upld_dir(srv); test_httpsrv_upld_dir(srv); test_httpsrv_set_post_buf_size(srv); test_httpsrv_post_buf_size(srv); test_httpsrv_set_payld_limit(srv); test_httpsrv_payld_limit(srv); test_httpsrv_set_uplds_limit(srv); test_httpsrv_uplds_limit(srv); test_httpsrv_set_thr_pool_size(srv); test_httpsrv_thr_pool_size(srv); test_httpsrv_set_con_timeout(srv); test_httpsrv_con_timeout(srv); test_httpsrv_set_con_limit(srv); test_httpsrv_con_limit(srv); test_httpsrv_handle(srv); sg_httpsrv_free(srv); return EXIT_SUCCESS; }