blob: 52169132e2252c86755abeb29de4cf387c044ff3 [file] [log] [blame]
henrike@webrtc.orgf0488722014-05-13 18:00:26 +00001/*
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
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000016using rtc::UrlEncode;
17
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000018TEST(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
30TEST(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
37TEST(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
47TEST(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
56TEST(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
63TEST(Urlencode, Encoding2) {
64 char source[] = "A^ ";
65 char dest[8];
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000066 ASSERT_EQ(7, rtc::UrlEncodeWithoutEncodingSpaceAsPlus(source, dest,
67 ARRAY_SIZE(dest)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000068 ASSERT_STREQ("A%5E%20", dest);
69}
70
71TEST(Urldecode, Decoding1) {
72 char source[] = "A%5E+";
73 char dest[8];
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000074 ASSERT_EQ(3, rtc::UrlDecode(source, dest));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000075 ASSERT_STREQ("A^ ", dest);
76}
77
78TEST(Urldecode, Decoding2) {
79 char source[] = "A%5E+";
80 char dest[8];
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000081 ASSERT_EQ(3, rtc::UrlDecodeWithoutEncodingSpaceAsPlus(source, dest));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000082 ASSERT_STREQ("A^+", dest);
83}