| |
1 /* |
| |
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
| |
3 * |
| |
4 * Copyright 2026 Olaf Wintermann. All rights reserved. |
| |
5 * |
| |
6 * Redistribution and use in source and binary forms, with or without |
| |
7 * modification, are permitted provided that the following conditions are met: |
| |
8 * |
| |
9 * 1. Redistributions of source code must retain the above copyright |
| |
10 * notice, this list of conditions and the following disclaimer. |
| |
11 * |
| |
12 * 2. Redistributions in binary form must reproduce the above copyright |
| |
13 * notice, this list of conditions and the following disclaimer in the |
| |
14 * documentation and/or other materials provided with the distribution. |
| |
15 * |
| |
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
| |
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
| |
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
| |
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
| |
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
| |
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
| |
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
| |
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
| |
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
| |
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
| |
26 * POSSIBILITY OF SUCH DAMAGE. |
| |
27 */ |
| |
28 |
| |
29 #include "httpparser.h" |
| |
30 |
| |
31 #include "../daemon/httpparser.h" |
| |
32 |
| |
33 CX_TEST(test_parse_request_line) { |
| |
34 CX_TEST_DO { |
| |
35 HttpParser parser; |
| |
36 memset(&parser, 0, sizeof(HttpParser)); |
| |
37 parser.start_line = cx_mutstr("GET / HTTP/1.1\r\n"); |
| |
38 |
| |
39 int ret1 = parse_request_line(&parser); |
| |
40 CX_TEST_ASSERT(!ret1); |
| |
41 CX_TEST_ASSERT(!cx_strcmp(parser.method, "GET")); |
| |
42 CX_TEST_ASSERT(!cx_strcmp(parser.uri, "/")); |
| |
43 CX_TEST_ASSERT(!cx_strcmp(parser.httpv, "HTTP/1.1")); |
| |
44 |
| |
45 memset(&parser, 0, sizeof(HttpParser)); |
| |
46 parser.start_line = cx_mutstr("POST /longer/url/test/path/ HTTP/1.0\r\n"); |
| |
47 int ret2 = parse_request_line(&parser); |
| |
48 CX_TEST_ASSERT(!ret2); |
| |
49 CX_TEST_ASSERT(!cx_strcmp(parser.method, "POST")); |
| |
50 CX_TEST_ASSERT(!cx_strcmp(parser.uri, "/longer/url/test/path/")); |
| |
51 CX_TEST_ASSERT(!cx_strcmp(parser.httpv, "HTTP/1.0")); |
| |
52 |
| |
53 memset(&parser, 0, sizeof(HttpParser)); |
| |
54 parser.start_line = cx_mutstr("G / v\r\n"); |
| |
55 int ret3 = parse_request_line(&parser); |
| |
56 CX_TEST_ASSERT(!ret3); |
| |
57 CX_TEST_ASSERT(!cx_strcmp(parser.method, "G")); |
| |
58 CX_TEST_ASSERT(!cx_strcmp(parser.uri, "/")); |
| |
59 CX_TEST_ASSERT(!cx_strcmp(parser.httpv, "v")); |
| |
60 |
| |
61 memset(&parser, 0, sizeof(HttpParser)); |
| |
62 parser.start_line = cx_mutstr("HEAD /space HTTP/0.9 \r\n"); |
| |
63 int ret4 = parse_request_line(&parser); |
| |
64 CX_TEST_ASSERT(!ret4); |
| |
65 CX_TEST_ASSERT(!cx_strcmp(parser.method, "HEAD")); |
| |
66 CX_TEST_ASSERT(!cx_strcmp(parser.uri, "/space")); |
| |
67 CX_TEST_ASSERT(!cx_strcmp(parser.httpv, "HTTP/0.9")); |
| |
68 |
| |
69 // unix-style line break tests |
| |
70 memset(&parser, 0, sizeof(HttpParser)); |
| |
71 parser.start_line = cx_mutstr("PROPFIND /collection/ HTTP/1.1\n"); |
| |
72 int uret1 = parse_request_line(&parser); |
| |
73 CX_TEST_ASSERT(!uret1); |
| |
74 CX_TEST_ASSERT(!cx_strcmp(parser.method, "PROPFIND")); |
| |
75 CX_TEST_ASSERT(!cx_strcmp(parser.uri, "/collection/")); |
| |
76 CX_TEST_ASSERT(!cx_strcmp(parser.httpv, "HTTP/1.1")); |
| |
77 |
| |
78 // negative tests |
| |
79 parser.start_line = cx_mutstr("\r\n"); |
| |
80 int nret1 = parse_request_line(&parser); |
| |
81 CX_TEST_ASSERT(nret1); |
| |
82 |
| |
83 parser.start_line = cx_mutstr("GET\r\n"); |
| |
84 int nret2 = parse_request_line(&parser); |
| |
85 CX_TEST_ASSERT(nret2); |
| |
86 |
| |
87 parser.start_line = cx_mutstr("GET /test\r\n"); |
| |
88 int nret3 = parse_request_line(&parser); |
| |
89 CX_TEST_ASSERT(nret3); |
| |
90 |
| |
91 parser.start_line = cx_mutstr("GET /test \r\n"); |
| |
92 int nret4 = parse_request_line(&parser); |
| |
93 CX_TEST_ASSERT(nret4); |
| |
94 |
| |
95 parser.start_line = cx_mutstr("/test HTTP/1.1\r\n"); |
| |
96 int nret5 = parse_request_line(&parser); |
| |
97 CX_TEST_ASSERT(nret5); |
| |
98 |
| |
99 parser.start_line = cx_mutstr("GET /uri HTTP/1.1 test\r\n"); |
| |
100 int nret6 = parse_request_line(&parser); |
| |
101 CX_TEST_ASSERT(nret6); |
| |
102 } |
| |
103 } |
| |
104 |
| |
105 CX_TEST(test_parse_response_line) { |
| |
106 CX_TEST_DO { |
| |
107 HttpParser parser; |
| |
108 memset(&parser, 0, sizeof(HttpParser)); |
| |
109 parser.start_line = cx_mutstr("HTTP/1.1 200 OK\r\n"); |
| |
110 parser.type = 1; |
| |
111 |
| |
112 int ret1 = parse_response_line(&parser); |
| |
113 CX_TEST_ASSERT(!ret1); |
| |
114 CX_TEST_ASSERT(!cx_strcmp(parser.httpv, "HTTP/1.1")); |
| |
115 CX_TEST_ASSERT(!cx_strcmp(parser.msg, "OK")); |
| |
116 CX_TEST_ASSERT(parser.status == 200); |
| |
117 |
| |
118 memset(&parser, 0, sizeof(HttpParser)); |
| |
119 parser.start_line = cx_mutstr("HTTP/1.0 201 Created\r\n"); |
| |
120 parser.type = 1; |
| |
121 int ret2 = parse_response_line(&parser); |
| |
122 CX_TEST_ASSERT(!ret2); |
| |
123 CX_TEST_ASSERT(!cx_strcmp(parser.httpv, "HTTP/1.0")); |
| |
124 CX_TEST_ASSERT(!cx_strcmp(parser.msg, "Created")); |
| |
125 CX_TEST_ASSERT(parser.status == 201); |
| |
126 |
| |
127 memset(&parser, 0, sizeof(HttpParser)); |
| |
128 parser.start_line = cx_mutstr("HTTP/0.9 204 No Content\r\n"); |
| |
129 parser.type = 1; |
| |
130 int ret3 = parse_response_line(&parser); |
| |
131 CX_TEST_ASSERT(!ret3); |
| |
132 CX_TEST_ASSERT(!cx_strcmp(parser.httpv, "HTTP/0.9")); |
| |
133 CX_TEST_ASSERT(!cx_strcmp(parser.msg, "No Content")); |
| |
134 CX_TEST_ASSERT(parser.status == 204); |
| |
135 } |
| |
136 } |