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