Debian Codegraph @

src/microhttpd/test_http_reasons.c

raw
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 static const char *const r_unknown = "unknown";
33
34 /* Return zero when no error is detected */
35 static int
36 expect_result (unsigned int code, const char *expected)
37 {
38 const char *const reason = MHD_get_reason_phrase_for (code);
39 const size_t len = MHD_get_reason_phrase_len_for (code);
40 size_t exp_len;
41 if (! MHD_str_equal_caseless_ (reason, expected))
42 {
43 fprintf (stderr,
44 "Incorrect reason returned for code %u:\n Returned: \"%s\" \tExpected: \"%s\"\n",
45 code, reason, expected);
46 return 1;
47 }
48 if (r_unknown == expected)
49 exp_len = 0;
50 else
51 exp_len = strlen (expected);
52 if (exp_len != len)
53 {
54 fprintf (stderr,
55 "Incorrect reason length returned for code %u:\n Returned: \"%u\" \tExpected: \"%u\"\n",
56 code, (unsigned) len, (unsigned) exp_len);
57 return 1;
58 }
59 return 0;
60 }
61
62
63 static int
64 expect_absent (unsigned int code)
65 {
66 return expect_result (code, r_unknown);
67 }
68
69
70 static int
71 test_absent_codes (void)
72 {
73 int errcount = 0;
74 errcount += expect_absent (0);
75 errcount += expect_absent (1);
76 errcount += expect_absent (50);
77 errcount += expect_absent (99);
78 errcount += expect_absent (600);
79 errcount += expect_absent (601);
80 errcount += expect_absent (900);
81 errcount += expect_absent (10000);
82 return errcount;
83 }
84
85
86 static int
87 test_1xx (void)
88 {
89 int errcount = 0;
90 errcount += expect_result (MHD_HTTP_CONTINUE, "continue");
91 errcount += expect_result (MHD_HTTP_PROCESSING, "processing");
92 errcount += expect_absent (110);
93 errcount += expect_absent (190);
94 return errcount;
95 }
96
97
98 static int
99 test_2xx (void)
100 {
101 int errcount = 0;
102 errcount += expect_result (MHD_HTTP_OK, "ok");
103 errcount += expect_result (MHD_HTTP_ALREADY_REPORTED, "already reported");
104 errcount += expect_absent (217);
105 errcount += expect_result (MHD_HTTP_IM_USED, "im used");
106 errcount += expect_absent (230);
107 errcount += expect_absent (295);
108 return errcount;
109 }
110
111
112 static int
113 test_3xx (void)
114 {
115 int errcount = 0;
116 errcount += expect_result (MHD_HTTP_MULTIPLE_CHOICES, "multiple choices");
117 errcount += expect_result (MHD_HTTP_SEE_OTHER, "see other");
118 errcount += expect_result (MHD_HTTP_PERMANENT_REDIRECT, "permanent redirect");
119 errcount += expect_absent (311);
120 errcount += expect_absent (399);
121 return errcount;
122 }
123
124
125 static int
126 test_4xx (void)
127 {
128 int errcount = 0;
129 errcount += expect_result (MHD_HTTP_BAD_REQUEST, "bad request");
130 errcount += expect_result (MHD_HTTP_NOT_FOUND, "not found");
131 errcount += expect_result (MHD_HTTP_URI_TOO_LONG, "uri too long");
132 errcount += expect_result (MHD_HTTP_EXPECTATION_FAILED, "expectation failed");
133 errcount += expect_result (MHD_HTTP_REQUEST_HEADER_FIELDS_TOO_LARGE,
134 "request header fields too large");
135 errcount += expect_absent (441);
136 errcount += expect_result (MHD_HTTP_UNAVAILABLE_FOR_LEGAL_REASONS,
137 "unavailable for legal reasons");
138 errcount += expect_absent (470);
139 errcount += expect_absent (493);
140 return errcount;
141 }
142
143
144 static int
145 test_5xx (void)
146 {
147 int errcount = 0;
148 errcount += expect_result (MHD_HTTP_INTERNAL_SERVER_ERROR,
149 "internal server error");
150 errcount += expect_result (MHD_HTTP_BAD_GATEWAY, "bad gateway");
151 errcount += expect_result (MHD_HTTP_HTTP_VERSION_NOT_SUPPORTED,
152 "http version not supported");
153 errcount += expect_result (MHD_HTTP_NETWORK_AUTHENTICATION_REQUIRED,
154 "network authentication required");
155 errcount += expect_absent (520);
156 errcount += expect_absent (597);
157 return errcount;
158 }
159
160
161 int
162 main (int argc, char *argv[])
163 {
164 int errcount = 0;
165 (void) argc; (void) argv; /* Unused. Silent compiler warning. */
166
167 errcount += test_absent_codes ();
168 errcount += test_1xx ();
169 errcount += test_2xx ();
170 errcount += test_3xx ();
171 errcount += test_4xx ();
172 errcount += test_5xx ();
173 return errcount == 0 ? 0 : 1;
174 }