You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

98 lines
3.4 KiB

​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
​
  1. /*
  2. * libjingle
  3. * Copyright 2004--2011, Google Inc.
  4. *
  5. * Redistribution and use in source and binary forms, with or without
  6. * modification, are permitted provided that the following conditions are met:
  7. *
  8. * 1. Redistributions of source code must retain the above copyright notice,
  9. * this list of conditions and the following disclaimer.
  10. * 2. Redistributions in binary form must reproduce the above copyright notice,
  11. * this list of conditions and the following disclaimer in the documentation
  12. * and/or other materials provided with the distribution.
  13. * 3. The name of the author may not be used to endorse or promote products
  14. * derived from this software without specific prior written permission.
  15. *
  16. * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED
  17. * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
  18. * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO
  19. * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  20. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
  21. * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
  22. * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
  23. * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
  24. * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
  25. * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  26. */
  27. #include "talk/base/common.h"
  28. #include "talk/base/gunit.h"
  29. #include "talk/base/thread.h"
  30. #include "talk/base/urlencode.h"
  31. TEST(Urlencode, SourceTooLong) {
  32. char source[] = "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
  33. "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^";
  34. char dest[1];
  35. ASSERT_EQ(0, UrlEncode(source, dest, ARRAY_SIZE(dest)));
  36. ASSERT_EQ('\0', dest[0]);
  37. dest[0] = 'a';
  38. ASSERT_EQ(0, UrlEncode(source, dest, 0));
  39. ASSERT_EQ('a', dest[0]);
  40. }
  41. TEST(Urlencode, OneCharacterConversion) {
  42. char source[] = "^";
  43. char dest[4];
  44. ASSERT_EQ(3, UrlEncode(source, dest, ARRAY_SIZE(dest)));
  45. ASSERT_STREQ("%5E", dest);
  46. }
  47. TEST(Urlencode, ShortDestinationNoEncoding) {
  48. // In this case we have a destination that would not be
  49. // big enough to hold an encoding but is big enough to
  50. // hold the text given.
  51. char source[] = "aa";
  52. char dest[3];
  53. ASSERT_EQ(2, UrlEncode(source, dest, ARRAY_SIZE(dest)));
  54. ASSERT_STREQ("aa", dest);
  55. }
  56. TEST(Urlencode, ShortDestinationEncoding) {
  57. // In this case we have a destination that is not
  58. // big enough to hold the encoding.
  59. char source[] = "&";
  60. char dest[3];
  61. ASSERT_EQ(0, UrlEncode(source, dest, ARRAY_SIZE(dest)));
  62. ASSERT_EQ('\0', dest[0]);
  63. }
  64. TEST(Urlencode, Encoding1) {
  65. char source[] = "A^ ";
  66. char dest[8];
  67. ASSERT_EQ(5, UrlEncode(source, dest, ARRAY_SIZE(dest)));
  68. ASSERT_STREQ("A%5E+", dest);
  69. }
  70. TEST(Urlencode, Encoding2) {
  71. char source[] = "A^ ";
  72. char dest[8];
  73. ASSERT_EQ(7, UrlEncodeWithoutEncodingSpaceAsPlus(source, dest,
  74. ARRAY_SIZE(dest)));
  75. ASSERT_STREQ("A%5E%20", dest);
  76. }
  77. TEST(Urldecode, Decoding1) {
  78. char source[] = "A%5E+";
  79. char dest[8];
  80. ASSERT_EQ(3, UrlDecode(source, dest));
  81. ASSERT_STREQ("A^ ", dest);
  82. }
  83. TEST(Urldecode, Decoding2) {
  84. char source[] = "A%5E+";
  85. char dest[8];
  86. ASSERT_EQ(3, UrlDecodeWithoutEncodingSpaceAsPlus(source, dest));
  87. ASSERT_STREQ("A^+", dest);
  88. }