parser_test.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. /*
  2. *
  3. * Copyright 2015, Google Inc.
  4. * 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
  8. * met:
  9. *
  10. * * Redistributions of source code must retain the above copyright
  11. * notice, this list of conditions and the following disclaimer.
  12. * * Redistributions in binary form must reproduce the above
  13. * copyright notice, this list of conditions and the following disclaimer
  14. * in the documentation and/or other materials provided with the
  15. * distribution.
  16. * * Neither the name of Google Inc. nor the names of its
  17. * contributors may be used to endorse or promote products derived from
  18. * this software without specific prior written permission.
  19. *
  20. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
  21. * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  22. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
  23. * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
  24. * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  25. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
  26. * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
  27. * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
  28. * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  29. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  30. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  31. *
  32. */
  33. #include "src/core/lib/http/parser.h"
  34. #include <stdarg.h>
  35. #include <string.h>
  36. #include <grpc/support/alloc.h>
  37. #include <grpc/support/log.h>
  38. #include <grpc/support/string_util.h>
  39. #include <grpc/support/useful.h>
  40. #include "test/core/util/slice_splitter.h"
  41. #include "test/core/util/test_config.h"
  42. static void test_request_succeeds(grpc_slice_split_mode split_mode,
  43. char *request_text, char *expect_method,
  44. grpc_http_version expect_version,
  45. char *expect_path, char *expect_body, ...) {
  46. grpc_http_parser parser;
  47. gpr_slice input_slice = gpr_slice_from_copied_string(request_text);
  48. size_t num_slices;
  49. size_t i;
  50. gpr_slice *slices;
  51. va_list args;
  52. grpc_http_request request;
  53. memset(&request, 0, sizeof(request));
  54. grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
  55. gpr_slice_unref(input_slice);
  56. grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request);
  57. for (i = 0; i < num_slices; i++) {
  58. GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i]) == GRPC_ERROR_NONE);
  59. gpr_slice_unref(slices[i]);
  60. }
  61. GPR_ASSERT(grpc_http_parser_eof(&parser));
  62. GPR_ASSERT(GRPC_HTTP_REQUEST == parser.type);
  63. GPR_ASSERT(0 == strcmp(expect_method, request.method));
  64. GPR_ASSERT(0 == strcmp(expect_path, request.path));
  65. GPR_ASSERT(expect_version == request.version);
  66. if (expect_body != NULL) {
  67. GPR_ASSERT(strlen(expect_body) == request.body_length);
  68. GPR_ASSERT(0 == memcmp(expect_body, request.body, request.body_length));
  69. } else {
  70. GPR_ASSERT(request.body_length == 0);
  71. }
  72. va_start(args, expect_body);
  73. i = 0;
  74. for (;;) {
  75. char *expect_key;
  76. char *expect_value;
  77. expect_key = va_arg(args, char *);
  78. if (!expect_key) break;
  79. GPR_ASSERT(i < request.hdr_count);
  80. expect_value = va_arg(args, char *);
  81. GPR_ASSERT(expect_value);
  82. GPR_ASSERT(0 == strcmp(expect_key, request.hdrs[i].key));
  83. GPR_ASSERT(0 == strcmp(expect_value, request.hdrs[i].value));
  84. i++;
  85. }
  86. va_end(args);
  87. GPR_ASSERT(i == request.hdr_count);
  88. grpc_http_parser_destroy(&parser);
  89. gpr_free(slices);
  90. }
  91. static void test_succeeds(grpc_slice_split_mode split_mode, char *response_text,
  92. int expect_status, char *expect_body, ...) {
  93. grpc_http_parser parser;
  94. gpr_slice input_slice = gpr_slice_from_copied_string(response_text);
  95. size_t num_slices;
  96. size_t i;
  97. gpr_slice *slices;
  98. va_list args;
  99. grpc_http_response response;
  100. grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
  101. gpr_slice_unref(input_slice);
  102. grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response);
  103. for (i = 0; i < num_slices; i++) {
  104. GPR_ASSERT(grpc_http_parser_parse(&parser, slices[i]) == GRPC_ERROR_NONE);
  105. gpr_slice_unref(slices[i]);
  106. }
  107. GPR_ASSERT(grpc_http_parser_eof(&parser));
  108. GPR_ASSERT(GRPC_HTTP_RESPONSE == parser.type);
  109. GPR_ASSERT(expect_status == response.status);
  110. if (expect_body != NULL) {
  111. GPR_ASSERT(strlen(expect_body) == response.body_length);
  112. GPR_ASSERT(0 == memcmp(expect_body, response.body, response.body_length));
  113. } else {
  114. GPR_ASSERT(response.body_length == 0);
  115. }
  116. va_start(args, expect_body);
  117. i = 0;
  118. for (;;) {
  119. char *expect_key;
  120. char *expect_value;
  121. expect_key = va_arg(args, char *);
  122. if (!expect_key) break;
  123. GPR_ASSERT(i < response.hdr_count);
  124. expect_value = va_arg(args, char *);
  125. GPR_ASSERT(expect_value);
  126. GPR_ASSERT(0 == strcmp(expect_key, response.hdrs[i].key));
  127. GPR_ASSERT(0 == strcmp(expect_value, response.hdrs[i].value));
  128. i++;
  129. }
  130. va_end(args);
  131. GPR_ASSERT(i == response.hdr_count);
  132. grpc_http_parser_destroy(&parser);
  133. gpr_free(slices);
  134. }
  135. static void test_fails(grpc_slice_split_mode split_mode, char *response_text) {
  136. grpc_http_parser parser;
  137. gpr_slice input_slice = gpr_slice_from_copied_string(response_text);
  138. size_t num_slices;
  139. size_t i;
  140. gpr_slice *slices;
  141. grpc_error *error = GRPC_ERROR_NONE;
  142. grpc_http_response response;
  143. memset(&response, 0, sizeof(response));
  144. grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
  145. gpr_slice_unref(input_slice);
  146. grpc_http_parser_init(&parser, GRPC_HTTP_RESPONSE, &response);
  147. for (i = 0; i < num_slices; i++) {
  148. if (GRPC_ERROR_NONE == error) {
  149. error = grpc_http_parser_parse(&parser, slices[i]);
  150. }
  151. gpr_slice_unref(slices[i]);
  152. }
  153. if (GRPC_ERROR_NONE == error) {
  154. error = grpc_http_parser_eof(&parser);
  155. }
  156. GPR_ASSERT(error != GRPC_ERROR_NONE);
  157. GRPC_ERROR_UNREF(error);
  158. grpc_http_response_destroy(&response);
  159. grpc_http_parser_destroy(&parser);
  160. gpr_free(slices);
  161. }
  162. static void test_request_fails(grpc_slice_split_mode split_mode,
  163. char *request_text) {
  164. grpc_http_parser parser;
  165. gpr_slice input_slice = gpr_slice_from_copied_string(request_text);
  166. size_t num_slices;
  167. size_t i;
  168. gpr_slice *slices;
  169. int done = 0;
  170. grpc_http_request request;
  171. memset(&request, 0, sizeof(request));
  172. grpc_split_slices(split_mode, &input_slice, 1, &slices, &num_slices);
  173. gpr_slice_unref(input_slice);
  174. grpc_http_parser_init(&parser, GRPC_HTTP_REQUEST, &request);
  175. for (i = 0; i < num_slices; i++) {
  176. if (!done && !grpc_http_parser_parse(&parser, slices[i])) {
  177. done = 1;
  178. }
  179. gpr_slice_unref(slices[i]);
  180. }
  181. if (!done && !grpc_http_parser_eof(&parser)) {
  182. done = 1;
  183. }
  184. GPR_ASSERT(done);
  185. grpc_http_request_destroy(&request);
  186. grpc_http_parser_destroy(&parser);
  187. gpr_free(slices);
  188. }
  189. int main(int argc, char **argv) {
  190. size_t i;
  191. const grpc_slice_split_mode split_modes[] = {GRPC_SLICE_SPLIT_IDENTITY,
  192. GRPC_SLICE_SPLIT_ONE_BYTE};
  193. char *tmp1, *tmp2;
  194. grpc_test_init(argc, argv);
  195. for (i = 0; i < GPR_ARRAY_SIZE(split_modes); i++) {
  196. test_succeeds(split_modes[i],
  197. "HTTP/1.0 200 OK\r\n"
  198. "xyz: abc\r\n"
  199. "\r\n"
  200. "hello world!",
  201. 200, "hello world!", "xyz", "abc", NULL);
  202. test_succeeds(split_modes[i],
  203. "HTTP/1.0 404 Not Found\r\n"
  204. "\r\n",
  205. 404, NULL, NULL);
  206. test_succeeds(split_modes[i],
  207. "HTTP/1.1 200 OK\r\n"
  208. "xyz: abc\r\n"
  209. "\r\n"
  210. "hello world!",
  211. 200, "hello world!", "xyz", "abc", NULL);
  212. test_succeeds(split_modes[i],
  213. "HTTP/1.1 200 OK\n"
  214. "\n"
  215. "abc",
  216. 200, "abc", NULL);
  217. test_request_succeeds(split_modes[i],
  218. "GET / HTTP/1.0\r\n"
  219. "\r\n",
  220. "GET", GRPC_HTTP_HTTP10, "/", NULL, NULL);
  221. test_request_succeeds(split_modes[i],
  222. "GET / HTTP/1.0\r\n"
  223. "\r\n"
  224. "xyz",
  225. "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL);
  226. test_request_succeeds(split_modes[i],
  227. "GET / HTTP/1.1\r\n"
  228. "\r\n"
  229. "xyz",
  230. "GET", GRPC_HTTP_HTTP11, "/", "xyz", NULL);
  231. test_request_succeeds(split_modes[i],
  232. "GET / HTTP/2.0\r\n"
  233. "\r\n"
  234. "xyz",
  235. "GET", GRPC_HTTP_HTTP20, "/", "xyz", NULL);
  236. test_request_succeeds(split_modes[i],
  237. "GET / HTTP/1.0\r\n"
  238. "xyz: abc\r\n"
  239. "\r\n"
  240. "xyz",
  241. "GET", GRPC_HTTP_HTTP10, "/", "xyz", "xyz", "abc",
  242. NULL);
  243. test_request_succeeds(split_modes[i],
  244. "GET / HTTP/1.0\n"
  245. "\n"
  246. "xyz",
  247. "GET", GRPC_HTTP_HTTP10, "/", "xyz", NULL);
  248. test_fails(split_modes[i], "HTTP/1.0\r\n");
  249. test_fails(split_modes[i], "HTTP/1.2\r\n");
  250. test_fails(split_modes[i], "HTTP/1.0 000 XYX\r\n");
  251. test_fails(split_modes[i], "HTTP/1.0 200 OK\n");
  252. test_fails(split_modes[i], "HTTP/1.0 200 OK\r\n");
  253. test_fails(split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n");
  254. test_fails(split_modes[i],
  255. "HTTP/1.0 200 OK\r\n"
  256. "xyz: abc\r\n"
  257. " def\r\n"
  258. "\r\n"
  259. "hello world!");
  260. test_request_fails(split_modes[i], "GET\r\n");
  261. test_request_fails(split_modes[i], "GET /\r\n");
  262. test_request_fails(split_modes[i], "GET / HTTP/0.0\r\n");
  263. test_request_fails(split_modes[i], "GET / ____/1.0\r\n");
  264. test_request_fails(split_modes[i], "GET / HTTP/1.2\r\n");
  265. test_request_fails(split_modes[i], "GET / HTTP/1.0\n");
  266. tmp1 = gpr_malloc(2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH);
  267. memset(tmp1, 'a', 2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1);
  268. tmp1[2 * GRPC_HTTP_PARSER_MAX_HEADER_LENGTH - 1] = 0;
  269. gpr_asprintf(&tmp2, "HTTP/1.0 200 OK\r\nxyz: %s\r\n\r\n", tmp1);
  270. test_fails(split_modes[i], tmp2);
  271. gpr_free(tmp1);
  272. gpr_free(tmp2);
  273. }
  274. return 0;
  275. }