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/arraysize.h"
12#include "webrtc/base/common.h"
13#include "webrtc/base/gunit.h"
14#include "webrtc/base/thread.h"
15#include "webrtc/base/urlencode.h"
16
17using rtc::UrlEncode;
18
19TEST(Urlencode, SourceTooLong) {
20  char source[] = "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
21      "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^";
22  char dest[1];
23  ASSERT_EQ(0, UrlEncode(source, dest, arraysize(dest)));
24  ASSERT_EQ('\0', dest[0]);
25
26  dest[0] = 'a';
27  ASSERT_EQ(0, UrlEncode(source, dest, 0));
28  ASSERT_EQ('a', dest[0]);
29}
30
31TEST(Urlencode, OneCharacterConversion) {
32  char source[] = "^";
33  char dest[4];
34  ASSERT_EQ(3, UrlEncode(source, dest, arraysize(dest)));
35  ASSERT_STREQ("%5E", dest);
36}
37
38TEST(Urlencode, ShortDestinationNoEncoding) {
39  // In this case we have a destination that would not be
40  // big enough to hold an encoding but is big enough to
41  // hold the text given.
42  char source[] = "aa";
43  char dest[3];
44  ASSERT_EQ(2, UrlEncode(source, dest, arraysize(dest)));
45  ASSERT_STREQ("aa", dest);
46}
47
48TEST(Urlencode, ShortDestinationEncoding) {
49  // In this case we have a destination that is not
50  // big enough to hold the encoding.
51  char source[] = "&";
52  char dest[3];
53  ASSERT_EQ(0, UrlEncode(source, dest, arraysize(dest)));
54  ASSERT_EQ('\0', dest[0]);
55}
56
57TEST(Urlencode, Encoding1) {
58  char source[] = "A^ ";
59  char dest[8];
60  ASSERT_EQ(5, UrlEncode(source, dest, arraysize(dest)));
61  ASSERT_STREQ("A%5E+", dest);
62}
63
64TEST(Urlencode, Encoding2) {
65  char source[] = "A^ ";
66  char dest[8];
67  ASSERT_EQ(7, rtc::UrlEncodeWithoutEncodingSpaceAsPlus(source, dest,
68                                                        arraysize(dest)));
69  ASSERT_STREQ("A%5E%20", dest);
70}
71
72TEST(Urldecode, Decoding1) {
73  char source[] = "A%5E+";
74  char dest[8];
75  ASSERT_EQ(3, rtc::UrlDecode(source, dest));
76  ASSERT_STREQ("A^ ", dest);
77}
78
79TEST(Urldecode, Decoding2) {
80  char source[] = "A%5E+";
81  char dest[8];
82  ASSERT_EQ(3, rtc::UrlDecodeWithoutEncodingSpaceAsPlus(source, dest));
83  ASSERT_STREQ("A^+", dest);
84}
85