1 /*
2  *  Copyright 2004 The WebRTC Project Authors. All rights reserved.
3  *
4  *  Use of this source code is governed by a BSD-style license
5  *  that can be found in the LICENSE file in the root of the source
6  *  tree. An additional intellectual property rights grant can be found
7  *  in the file PATENTS.  All contributing project authors may
8  *  be found in the AUTHORS file in the root of the source tree.
9  */
10 
11 #include "webrtc/base/common.h"
12 #include "webrtc/base/gunit.h"
13 #include "webrtc/base/thread.h"
14 #include "webrtc/base/urlencode.h"
15 
16 using rtc::UrlEncode;
17 
TEST(Urlencode,SourceTooLong)18 TEST(Urlencode, SourceTooLong) {
19   char source[] = "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
20       "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^";
21   char dest[1];
22   ASSERT_EQ(0, UrlEncode(source, dest, ARRAY_SIZE(dest)));
23   ASSERT_EQ('\0', dest[0]);
24 
25   dest[0] = 'a';
26   ASSERT_EQ(0, UrlEncode(source, dest, 0));
27   ASSERT_EQ('a', dest[0]);
28 }
29 
TEST(Urlencode,OneCharacterConversion)30 TEST(Urlencode, OneCharacterConversion) {
31   char source[] = "^";
32   char dest[4];
33   ASSERT_EQ(3, UrlEncode(source, dest, ARRAY_SIZE(dest)));
34   ASSERT_STREQ("%5E", dest);
35 }
36 
TEST(Urlencode,ShortDestinationNoEncoding)37 TEST(Urlencode, ShortDestinationNoEncoding) {
38   // In this case we have a destination that would not be
39   // big enough to hold an encoding but is big enough to
40   // hold the text given.
41   char source[] = "aa";
42   char dest[3];
43   ASSERT_EQ(2, UrlEncode(source, dest, ARRAY_SIZE(dest)));
44   ASSERT_STREQ("aa", dest);
45 }
46 
TEST(Urlencode,ShortDestinationEncoding)47 TEST(Urlencode, ShortDestinationEncoding) {
48   // In this case we have a destination that is not
49   // big enough to hold the encoding.
50   char source[] = "&";
51   char dest[3];
52   ASSERT_EQ(0, UrlEncode(source, dest, ARRAY_SIZE(dest)));
53   ASSERT_EQ('\0', dest[0]);
54 }
55 
TEST(Urlencode,Encoding1)56 TEST(Urlencode, Encoding1) {
57   char source[] = "A^ ";
58   char dest[8];
59   ASSERT_EQ(5, UrlEncode(source, dest, ARRAY_SIZE(dest)));
60   ASSERT_STREQ("A%5E+", dest);
61 }
62 
TEST(Urlencode,Encoding2)63 TEST(Urlencode, Encoding2) {
64   char source[] = "A^ ";
65   char dest[8];
66   ASSERT_EQ(7, rtc::UrlEncodeWithoutEncodingSpaceAsPlus(source, dest,
67                                                         ARRAY_SIZE(dest)));
68   ASSERT_STREQ("A%5E%20", dest);
69 }
70 
TEST(Urldecode,Decoding1)71 TEST(Urldecode, Decoding1) {
72   char source[] = "A%5E+";
73   char dest[8];
74   ASSERT_EQ(3, rtc::UrlDecode(source, dest));
75   ASSERT_STREQ("A^ ", dest);
76 }
77 
TEST(Urldecode,Decoding2)78 TEST(Urldecode, Decoding2) {
79   char source[] = "A%5E+";
80   char dest[8];
81   ASSERT_EQ(3, rtc::UrlDecodeWithoutEncodingSpaceAsPlus(source, dest));
82   ASSERT_STREQ("A^+", dest);
83 }
84