parser_test.c 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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/httpcli/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/useful.h>
  39. #include "test/core/util/slice_splitter.h"
  40. #include "test/core/util/test_config.h"
  41. static void
  42. test_succeeds (grpc_slice_split_mode split_mode, char *response, int expect_status, char *expect_body, ...)
  43. {
  44. grpc_httpcli_parser parser;
  45. gpr_slice input_slice = gpr_slice_from_copied_string (response);
  46. size_t num_slices;
  47. size_t i;
  48. gpr_slice *slices;
  49. va_list args;
  50. grpc_split_slices (split_mode, &input_slice, 1, &slices, &num_slices);
  51. gpr_slice_unref (input_slice);
  52. grpc_httpcli_parser_init (&parser);
  53. for (i = 0; i < num_slices; i++)
  54. {
  55. GPR_ASSERT (grpc_httpcli_parser_parse (&parser, slices[i]));
  56. gpr_slice_unref (slices[i]);
  57. }
  58. GPR_ASSERT (grpc_httpcli_parser_eof (&parser));
  59. GPR_ASSERT (expect_status == parser.r.status);
  60. if (expect_body != NULL)
  61. {
  62. GPR_ASSERT (strlen (expect_body) == parser.r.body_length);
  63. GPR_ASSERT (0 == memcmp (expect_body, parser.r.body, parser.r.body_length));
  64. }
  65. else
  66. {
  67. GPR_ASSERT (parser.r.body_length == 0);
  68. }
  69. va_start (args, expect_body);
  70. i = 0;
  71. for (;;)
  72. {
  73. char *expect_key;
  74. char *expect_value;
  75. expect_key = va_arg (args, char *);
  76. if (!expect_key)
  77. break;
  78. GPR_ASSERT (i < parser.r.hdr_count);
  79. expect_value = va_arg (args, char *);
  80. GPR_ASSERT (expect_value);
  81. GPR_ASSERT (0 == strcmp (expect_key, parser.r.hdrs[i].key));
  82. GPR_ASSERT (0 == strcmp (expect_value, parser.r.hdrs[i].value));
  83. i++;
  84. }
  85. va_end (args);
  86. GPR_ASSERT (i == parser.r.hdr_count);
  87. grpc_httpcli_parser_destroy (&parser);
  88. gpr_free (slices);
  89. }
  90. static void
  91. test_fails (grpc_slice_split_mode split_mode, char *response)
  92. {
  93. grpc_httpcli_parser parser;
  94. gpr_slice input_slice = gpr_slice_from_copied_string (response);
  95. size_t num_slices;
  96. size_t i;
  97. gpr_slice *slices;
  98. int done = 0;
  99. grpc_split_slices (split_mode, &input_slice, 1, &slices, &num_slices);
  100. gpr_slice_unref (input_slice);
  101. grpc_httpcli_parser_init (&parser);
  102. for (i = 0; i < num_slices; i++)
  103. {
  104. if (!done && !grpc_httpcli_parser_parse (&parser, slices[i]))
  105. {
  106. done = 1;
  107. }
  108. gpr_slice_unref (slices[i]);
  109. }
  110. if (!done && !grpc_httpcli_parser_eof (&parser))
  111. {
  112. done = 1;
  113. }
  114. GPR_ASSERT (done);
  115. grpc_httpcli_parser_destroy (&parser);
  116. gpr_free (slices);
  117. }
  118. int
  119. main (int argc, char **argv)
  120. {
  121. size_t i;
  122. const grpc_slice_split_mode split_modes[] = { GRPC_SLICE_SPLIT_IDENTITY,
  123. GRPC_SLICE_SPLIT_ONE_BYTE
  124. };
  125. grpc_test_init (argc, argv);
  126. for (i = 0; i < GPR_ARRAY_SIZE (split_modes); i++)
  127. {
  128. test_succeeds (split_modes[i], "HTTP/1.0 200 OK\r\n" "xyz: abc\r\n" "\r\n" "hello world!", 200, "hello world!", "xyz", "abc", NULL);
  129. test_succeeds (split_modes[i], "HTTP/1.0 404 Not Found\r\n" "\r\n", 404, NULL, NULL);
  130. test_succeeds (split_modes[i], "HTTP/1.1 200 OK\r\n" "xyz: abc\r\n" "\r\n" "hello world!", 200, "hello world!", "xyz", "abc", NULL);
  131. test_fails (split_modes[i], "HTTP/1.0\r\n");
  132. test_fails (split_modes[i], "HTTP/1.2\r\n");
  133. test_fails (split_modes[i], "HTTP/1.0 000 XYX\r\n");
  134. test_fails (split_modes[i], "HTTP/1.0 200 OK\n");
  135. test_fails (split_modes[i], "HTTP/1.0 200 OK\r\n");
  136. test_fails (split_modes[i], "HTTP/1.0 200 OK\r\nFoo x\r\n");
  137. }
  138. return 0;
  139. }