|
| 1 | +/* |
| 2 | + This file is part of libhttpserver |
| 3 | + Copyright (C) 2011-2019 Sebastiano Merlino |
| 4 | +
|
| 5 | + This library is free software; you can redistribute it and/or |
| 6 | + modify it under the terms of the GNU Lesser General Public |
| 7 | + License as published by the Free Software Foundation; either |
| 8 | + version 2.1 of the License, or (at your option) any later version. |
| 9 | +
|
| 10 | + This library is distributed in the hope that it will be useful, |
| 11 | + but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 12 | + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 13 | + Lesser General Public License for more details. |
| 14 | +
|
| 15 | + You should have received a copy of the GNU Lesser General Public |
| 16 | + License along with this library; if not, write to the Free Software |
| 17 | + Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 |
| 18 | + USA |
| 19 | +*/ |
| 20 | + |
| 21 | +#include "littletest.hpp" |
| 22 | +#include <curl/curl.h> |
| 23 | +#include <string> |
| 24 | +#include <map> |
| 25 | +#include "httpserver.hpp" |
| 26 | +#include "http_utils.hpp" |
| 27 | + |
| 28 | +using namespace httpserver; |
| 29 | +using namespace std; |
| 30 | +using namespace http; |
| 31 | + |
| 32 | +size_t writefunc(void *ptr, size_t size, size_t nmemb, std::string *s) |
| 33 | +{ |
| 34 | + s->append((char*) ptr, size*nmemb); |
| 35 | + return size*nmemb; |
| 36 | +} |
| 37 | + |
| 38 | +class ok_resource : public http_resource |
| 39 | +{ |
| 40 | + public: |
| 41 | + const http_response render_GET(const http_request& req) |
| 42 | + { |
| 43 | + return http_response_builder("OK", 200, "text/plain").string_response(); |
| 44 | + } |
| 45 | +}; |
| 46 | + |
| 47 | +LT_BEGIN_SUITE(ban_system_suite) |
| 48 | + void set_up() |
| 49 | + { |
| 50 | + } |
| 51 | + |
| 52 | + void tear_down() |
| 53 | + { |
| 54 | + } |
| 55 | +LT_END_SUITE(ban_system_suite) |
| 56 | + |
| 57 | +LT_BEGIN_AUTO_TEST(ban_system_suite, accept_default_ban_blocks) |
| 58 | + webserver ws = create_webserver(8080).default_policy(http_utils::ACCEPT); |
| 59 | + ws.start(false); |
| 60 | + |
| 61 | + ok_resource* resource = new ok_resource(); |
| 62 | + ws.register_resource("base", resource); |
| 63 | + |
| 64 | + { |
| 65 | + curl_global_init(CURL_GLOBAL_ALL); |
| 66 | + std::string s; |
| 67 | + CURL *curl = curl_easy_init(); |
| 68 | + CURLcode res; |
| 69 | + curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); |
| 70 | + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); |
| 71 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); |
| 72 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); |
| 73 | + res = curl_easy_perform(curl); |
| 74 | + LT_ASSERT_EQ(res, 0); |
| 75 | + LT_CHECK_EQ(s, "OK"); |
| 76 | + curl_easy_cleanup(curl); |
| 77 | + } |
| 78 | + |
| 79 | + { |
| 80 | + ws.ban_ip("127.0.0.1"); |
| 81 | + |
| 82 | + curl_global_init(CURL_GLOBAL_ALL); |
| 83 | + std::string s; |
| 84 | + CURL *curl = curl_easy_init(); |
| 85 | + CURLcode res; |
| 86 | + curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); |
| 87 | + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); |
| 88 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); |
| 89 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); |
| 90 | + res = curl_easy_perform(curl); |
| 91 | + LT_ASSERT_NEQ(res, 0); |
| 92 | + curl_easy_cleanup(curl); |
| 93 | + } |
| 94 | + |
| 95 | + { |
| 96 | + ws.unban_ip("127.0.0.1"); |
| 97 | + |
| 98 | + curl_global_init(CURL_GLOBAL_ALL); |
| 99 | + std::string s; |
| 100 | + CURL *curl = curl_easy_init(); |
| 101 | + CURLcode res; |
| 102 | + curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); |
| 103 | + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); |
| 104 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); |
| 105 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); |
| 106 | + res = curl_easy_perform(curl); |
| 107 | + LT_ASSERT_EQ(res, 0); |
| 108 | + LT_CHECK_EQ(s, "OK"); |
| 109 | + curl_easy_cleanup(curl); |
| 110 | + } |
| 111 | + |
| 112 | + ws.stop(); |
| 113 | +LT_END_AUTO_TEST(accept_default_ban_blocks) |
| 114 | + |
| 115 | +LT_BEGIN_AUTO_TEST(ban_system_suite, reject_default_allow_passes) |
| 116 | + webserver ws = create_webserver(8080).default_policy(http_utils::REJECT); |
| 117 | + ws.start(false); |
| 118 | + |
| 119 | + ok_resource* resource = new ok_resource(); |
| 120 | + ws.register_resource("base", resource); |
| 121 | + |
| 122 | + { |
| 123 | + curl_global_init(CURL_GLOBAL_ALL); |
| 124 | + std::string s; |
| 125 | + CURL *curl = curl_easy_init(); |
| 126 | + CURLcode res; |
| 127 | + curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); |
| 128 | + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); |
| 129 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); |
| 130 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); |
| 131 | + res = curl_easy_perform(curl); |
| 132 | + LT_ASSERT_NEQ(res, 0); |
| 133 | + curl_easy_cleanup(curl); |
| 134 | + } |
| 135 | + |
| 136 | + { |
| 137 | + ws.allow_ip("127.0.0.1"); |
| 138 | + |
| 139 | + curl_global_init(CURL_GLOBAL_ALL); |
| 140 | + std::string s; |
| 141 | + CURL *curl = curl_easy_init(); |
| 142 | + CURLcode res; |
| 143 | + curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); |
| 144 | + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); |
| 145 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); |
| 146 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); |
| 147 | + res = curl_easy_perform(curl); |
| 148 | + LT_ASSERT_EQ(res, 0); |
| 149 | + LT_CHECK_EQ(s, "OK"); |
| 150 | + } |
| 151 | + |
| 152 | + { |
| 153 | + ws.disallow_ip("127.0.0.1"); |
| 154 | + |
| 155 | + curl_global_init(CURL_GLOBAL_ALL); |
| 156 | + std::string s; |
| 157 | + CURL *curl = curl_easy_init(); |
| 158 | + CURLcode res; |
| 159 | + curl_easy_setopt(curl, CURLOPT_URL, "localhost:8080/base"); |
| 160 | + curl_easy_setopt(curl, CURLOPT_HTTPGET, 1L); |
| 161 | + curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, writefunc); |
| 162 | + curl_easy_setopt(curl, CURLOPT_WRITEDATA, &s); |
| 163 | + res = curl_easy_perform(curl); |
| 164 | + LT_ASSERT_NEQ(res, 0); |
| 165 | + curl_easy_cleanup(curl); |
| 166 | + } |
| 167 | + |
| 168 | + ws.stop(); |
| 169 | +LT_END_AUTO_TEST(reject_default_allow_passes) |
| 170 | + |
| 171 | +LT_BEGIN_AUTO_TEST_ENV() |
| 172 | + AUTORUN_TESTS() |
| 173 | +LT_END_AUTO_TEST_ENV() |
0 commit comments