xref: /lighttpd1.4/src/t/test_http_header.c (revision 71317bc9)
1 #include "first.h"
2 
3 #undef NDEBUG
4 #include <assert.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 
8 #include "http_header.c"
9 
test_http_header_tables(void)10 static void test_http_header_tables (void) {
11     /* verify enum http_header_e presence in http_headers[] */
12     unsigned int u;
13     for (int i = 0; i < 64; ++i) {
14         /* Note: must be kept in sync http_headers[] and http_headers_off[] */
15         /* Note: must be kept in sync with http_header.h enum http_header_e */
16         /* Note: must be kept in sync with http_header.c http_headers[] */
17         /* Note: must be kept in sync h2.c:http_header_lc[] */
18         /* Note: must be kept in sync h2.c:http_header_lshpack_idx[] */
19         /* Note: must be kept in sync h2.c:lshpack_idx_http_header[] */
20         /* switch() statement with each entry in enum http_header_e;
21          * no 'default' case to trigger warning if entry is added */
22         enum http_header_e x = (enum http_header_e)i;
23         switch (x) {
24           case HTTP_HEADER_OTHER:
25           case HTTP_HEADER_ACCEPT:
26           case HTTP_HEADER_ACCEPT_ENCODING:
27           case HTTP_HEADER_ACCEPT_LANGUAGE:
28           case HTTP_HEADER_ACCEPT_RANGES:
29           case HTTP_HEADER_ACCESS_CONTROL_ALLOW_ORIGIN:
30           case HTTP_HEADER_AGE:
31           case HTTP_HEADER_ALLOW:
32           case HTTP_HEADER_ALT_SVC:
33           case HTTP_HEADER_ALT_USED:
34           case HTTP_HEADER_AUTHORIZATION:
35           case HTTP_HEADER_CACHE_CONTROL:
36           case HTTP_HEADER_CONNECTION:
37           case HTTP_HEADER_CONTENT_ENCODING:
38           case HTTP_HEADER_CONTENT_LENGTH:
39           case HTTP_HEADER_CONTENT_LOCATION:
40           case HTTP_HEADER_CONTENT_RANGE:
41           case HTTP_HEADER_CONTENT_SECURITY_POLICY:
42           case HTTP_HEADER_CONTENT_TYPE:
43           case HTTP_HEADER_COOKIE:
44           case HTTP_HEADER_DATE:
45           case HTTP_HEADER_DNT:
46           case HTTP_HEADER_ETAG:
47           case HTTP_HEADER_EXPECT:
48           case HTTP_HEADER_EXPECT_CT:
49           case HTTP_HEADER_EXPIRES:
50           case HTTP_HEADER_FORWARDED:
51           case HTTP_HEADER_HOST:
52           case HTTP_HEADER_HTTP2_SETTINGS:
53           case HTTP_HEADER_IF_MATCH:
54           case HTTP_HEADER_IF_MODIFIED_SINCE:
55           case HTTP_HEADER_IF_NONE_MATCH:
56           case HTTP_HEADER_IF_RANGE:
57           case HTTP_HEADER_IF_UNMODIFIED_SINCE:
58           case HTTP_HEADER_LAST_MODIFIED:
59           case HTTP_HEADER_LINK:
60           case HTTP_HEADER_LOCATION:
61           case HTTP_HEADER_ONION_LOCATION:
62           case HTTP_HEADER_P3P:
63           case HTTP_HEADER_PRAGMA:
64           case HTTP_HEADER_PRIORITY:
65           case HTTP_HEADER_RANGE:
66           case HTTP_HEADER_REFERER:
67           case HTTP_HEADER_REFERRER_POLICY:
68           case HTTP_HEADER_SERVER:
69           case HTTP_HEADER_SET_COOKIE:
70           case HTTP_HEADER_STATUS:
71           case HTTP_HEADER_STRICT_TRANSPORT_SECURITY:
72           case HTTP_HEADER_TE:
73           case HTTP_HEADER_TRANSFER_ENCODING:
74           case HTTP_HEADER_UPGRADE:
75           case HTTP_HEADER_UPGRADE_INSECURE_REQUESTS:
76           case HTTP_HEADER_USER_AGENT:
77           case HTTP_HEADER_VARY:
78           case HTTP_HEADER_WWW_AUTHENTICATE:
79           case HTTP_HEADER_X_CONTENT_TYPE_OPTIONS:
80           case HTTP_HEADER_X_FORWARDED_FOR:
81           case HTTP_HEADER_X_FORWARDED_PROTO:
82           case HTTP_HEADER_X_FRAME_OPTIONS:
83           case HTTP_HEADER_X_XSS_PROTECTION:
84             for (u = 0; u < sizeof(http_headers)/sizeof(*http_headers); ++u) {
85                 if (i == http_headers[u].key) {
86                     assert(x == http_header_hkey_get(http_headers[u].value,
87                                                      http_headers[u].vlen));
88                     assert(x == http_header_hkey_get_lc(http_headers[u].value,
89                                                         http_headers[u].vlen));
90                     break;
91                 }
92             }
93             assert(u < sizeof(http_headers)/sizeof(*http_headers));
94             break;
95         }
96     }
97 
98     /* verify http_headers_off[] */
99     for (u = 0; u < sizeof(http_headers)/sizeof(*http_headers); ++u) {
100         if (http_headers[u].vlen == 0) break;
101         int8_t x = http_headers_off[http_headers[u].vlen];
102         assert((unsigned int)x <= u);
103         assert(http_headers[x].vlen == http_headers[u].vlen);
104     }
105 }
106 
107 void test_http_header (void);
test_http_header(void)108 void test_http_header (void)
109 {
110     test_http_header_tables();
111 }
112