percent_encoding_test.c 3.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  1. /*
  2. *
  3. * Copyright 2016, 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/support/percent_encoding.h"
  34. #include <grpc/support/alloc.h>
  35. #include <grpc/support/log.h>
  36. #include "src/core/lib/support/string.h"
  37. #include "test/core/util/test_config.h"
  38. #define TEST_VECTOR(raw, encoded) \
  39. test_vector(raw, sizeof(raw) - 1, encoded, sizeof(encoded) - 1)
  40. static void test_vector(const char *raw, size_t raw_length, const char *encoded,
  41. size_t encoded_length) {
  42. char *raw_msg = gpr_dump(raw, raw_length, GPR_DUMP_HEX | GPR_DUMP_ASCII);
  43. char *encoded_msg =
  44. gpr_dump(encoded, encoded_length, GPR_DUMP_HEX | GPR_DUMP_ASCII);
  45. gpr_log(GPR_DEBUG, "Trial:\nraw = %s\nencoded = %s", raw_msg, encoded_msg);
  46. gpr_free(raw_msg);
  47. gpr_free(encoded_msg);
  48. gpr_slice raw_slice = gpr_slice_from_copied_buffer(raw, raw_length);
  49. gpr_slice encoded_slice =
  50. gpr_slice_from_copied_buffer(encoded, encoded_length);
  51. gpr_slice raw2encoded_slice = gpr_percent_encode_slice(raw_slice);
  52. gpr_slice encoded2raw_slice;
  53. GPR_ASSERT(gpr_percent_decode_slice(encoded_slice, &encoded2raw_slice));
  54. char *raw2encoded_msg =
  55. gpr_dump_slice(raw2encoded_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
  56. char *encoded2raw_msg =
  57. gpr_dump_slice(encoded2raw_slice, GPR_DUMP_HEX | GPR_DUMP_ASCII);
  58. gpr_log(GPR_DEBUG, "Result:\nraw2encoded = %s\nencoded2raw = %s",
  59. raw2encoded_msg, encoded2raw_msg);
  60. gpr_free(raw2encoded_msg);
  61. gpr_free(encoded2raw_msg);
  62. GPR_ASSERT(0 == gpr_slice_cmp(raw_slice, encoded2raw_slice));
  63. GPR_ASSERT(0 == gpr_slice_cmp(encoded_slice, raw2encoded_slice));
  64. gpr_slice_unref(encoded2raw_slice);
  65. gpr_slice_unref(raw2encoded_slice);
  66. gpr_slice_unref(raw_slice);
  67. gpr_slice_unref(encoded_slice);
  68. }
  69. int main(int argc, char **argv) {
  70. grpc_test_init(argc, argv);
  71. TEST_VECTOR(
  72. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~",
  73. "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~");
  74. TEST_VECTOR("\x00", "%00");
  75. TEST_VECTOR("\x01", "%01");
  76. TEST_VECTOR("a b", "a%20b");
  77. TEST_VECTOR(" b", "%20b");
  78. TEST_VECTOR("\x0f", "%0F");
  79. TEST_VECTOR("\xff", "%FF");
  80. TEST_VECTOR("\xee", "%EE");
  81. return 0;
  82. }