test_http_reasons.c (5168B)
1 /* 2 This file is part of libmicrohttpd 3 Copyright (C) 2017-2021 Karlson2k (Evgeny Grin) 4 5 This test tool is free software; you can redistribute it and/or 6 modify it under the terms of the GNU General Public License as 7 published by the Free Software Foundation; either version 2, or 8 (at your option) any later version. 9 10 This test tool 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 USA 18 */ 19 20 /** 21 * @file microhttpd/test_http_reasons.c 22 * @brief Unit tests for MHD_get_reason_phrase_for() function 23 * @author Karlson2k (Evgeny Grin) 24 */ 25 26 #include "mhd_options.h" 27 #include <stdio.h> 28 #include <string.h> 29 #include "microhttpd.h" 30 #include "mhd_str.h" 31 32 /* Turn any MHD_PANIC() or failing mhd_assert() reached from this 33 test into a marked, classifiable test error (TESTING.md, P5). */ 34 #include "mhd_panic_tripwire.h" 35 36 static const char *const r_unknown = "unknown"; 37 38 /* Return zero when no error is detected */ 39 static int 40 expect_result (unsigned int code, const char *expected) 41 { 42 const char *const reason = MHD_get_reason_phrase_for (code); 43 const size_t len = MHD_get_reason_phrase_len_for (code); 44 size_t exp_len; 45 if (! MHD_str_equal_caseless_ (reason, expected)) 46 { 47 fprintf (stderr, 48 "Incorrect reason returned for code %u:\n Returned: \"%s\" \tExpected: \"%s\"\n", 49 code, reason, expected); 50 return 1; 51 } 52 if (r_unknown == expected) 53 exp_len = 0; 54 else 55 exp_len = strlen (expected); 56 if (exp_len != len) 57 { 58 fprintf (stderr, 59 "Incorrect reason length returned for code %u:\n Returned: \"%u\" \tExpected: \"%u\"\n", 60 code, (unsigned) len, (unsigned) exp_len); 61 return 1; 62 } 63 return 0; 64 } 65 66 67 static int 68 expect_absent (unsigned int code) 69 { 70 return expect_result (code, r_unknown); 71 } 72 73 74 static int 75 test_absent_codes (void) 76 { 77 int errcount = 0; 78 errcount += expect_absent (0); 79 errcount += expect_absent (1); 80 errcount += expect_absent (50); 81 errcount += expect_absent (99); 82 errcount += expect_absent (600); 83 errcount += expect_absent (601); 84 errcount += expect_absent (900); 85 errcount += expect_absent (10000); 86 return errcount; 87 } 88 89 90 static int 91 test_1xx (void) 92 { 93 int errcount = 0; 94 errcount += expect_result (MHD_HTTP_CONTINUE, "continue"); 95 errcount += expect_result (MHD_HTTP_PROCESSING, "processing"); 96 errcount += expect_absent (110); 97 errcount += expect_absent (190); 98 return errcount; 99 } 100 101 102 static int 103 test_2xx (void) 104 { 105 int errcount = 0; 106 errcount += expect_result (MHD_HTTP_OK, "ok"); 107 errcount += expect_result (MHD_HTTP_ALREADY_REPORTED, "already reported"); 108 errcount += expect_absent (217); 109 errcount += expect_result (MHD_HTTP_IM_USED, "im used"); 110 errcount += expect_absent (230); 111 errcount += expect_absent (295); 112 return errcount; 113 } 114 115 116 static int 117 test_3xx (void) 118 { 119 int errcount = 0; 120 errcount += expect_result (MHD_HTTP_MULTIPLE_CHOICES, "multiple choices"); 121 errcount += expect_result (MHD_HTTP_SEE_OTHER, "see other"); 122 errcount += expect_result (MHD_HTTP_PERMANENT_REDIRECT, "permanent redirect"); 123 errcount += expect_absent (311); 124 errcount += expect_absent (399); 125 return errcount; 126 } 127 128 129 static int 130 test_4xx (void) 131 { 132 int errcount = 0; 133 errcount += expect_result (MHD_HTTP_BAD_REQUEST, "bad request"); 134 errcount += expect_result (MHD_HTTP_NOT_FOUND, "not found"); 135 errcount += expect_result (MHD_HTTP_URI_TOO_LONG, "uri too long"); 136 errcount += expect_result (MHD_HTTP_EXPECTATION_FAILED, "expectation failed"); 137 errcount += expect_result (MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE, 138 "request header fields too large"); 139 errcount += expect_absent (441); 140 errcount += expect_result (MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS, 141 "unavailable for legal reasons"); 142 errcount += expect_absent (470); 143 errcount += expect_absent (493); 144 return errcount; 145 } 146 147 148 static int 149 test_5xx (void) 150 { 151 int errcount = 0; 152 errcount += expect_result (MHD_HTTP_INTERNAL_SERVER_ERROR, 153 "internal server error"); 154 errcount += expect_result (MHD_HTTP_BAD_GATEWAY, "bad gateway"); 155 errcount += expect_result (MHD_HTTP_HTTP_VERSION_NOT_SUPPORTED, 156 "http version not supported"); 157 errcount += expect_result (MHD_HTTP_NETWORK_AUTHENTICATION_REQUIRED, 158 "network authentication required"); 159 errcount += expect_absent (520); 160 errcount += expect_absent (597); 161 return errcount; 162 } 163 164 165 int 166 main (int argc, char *argv[]) 167 { 168 int errcount = 0; 169 (void) argc; (void) argv; /* Unused. Silent compiler warning. */ 170 171 errcount += test_absent_codes (); 172 errcount += test_1xx (); 173 errcount += test_2xx (); 174 errcount += test_3xx (); 175 errcount += test_4xx (); 176 errcount += test_5xx (); 177 return errcount == 0 ? 0 : 1; 178 }