blob: 6a61db3ae30fa4cb6102e26c69b7a9b55aeabe8d [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
tfarina5237aaf2015-11-10 23:44:30 -080011#include "webrtc/base/arraysize.h"
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000012#include "webrtc/base/common.h"
13#include "webrtc/base/gunit.h"
14#include "webrtc/base/thread.h"
15#include "webrtc/base/urlencode.h"
16
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000017using rtc::UrlEncode;
18
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000019TEST(Urlencode, SourceTooLong) {
20 char source[] = "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^"
21 "^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^";
22 char dest[1];
tfarina5237aaf2015-11-10 23:44:30 -080023 ASSERT_EQ(0, UrlEncode(source, dest, arraysize(dest)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000024 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];
tfarina5237aaf2015-11-10 23:44:30 -080034 ASSERT_EQ(3, UrlEncode(source, dest, arraysize(dest)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000035 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];
tfarina5237aaf2015-11-10 23:44:30 -080044 ASSERT_EQ(2, UrlEncode(source, dest, arraysize(dest)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000045 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];
tfarina5237aaf2015-11-10 23:44:30 -080053 ASSERT_EQ(0, UrlEncode(source, dest, arraysize(dest)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000054 ASSERT_EQ('\0', dest[0]);
55}
56
57TEST(Urlencode, Encoding1) {
58 char source[] = "A^ ";
59 char dest[8];
tfarina5237aaf2015-11-10 23:44:30 -080060 ASSERT_EQ(5, UrlEncode(source, dest, arraysize(dest)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000061 ASSERT_STREQ("A%5E+", dest);
62}
63
64TEST(Urlencode, Encoding2) {
65 char source[] = "A^ ";
66 char dest[8];
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000067 ASSERT_EQ(7, rtc::UrlEncodeWithoutEncodingSpaceAsPlus(source, dest,
tfarina5237aaf2015-11-10 23:44:30 -080068 arraysize(dest)));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000069 ASSERT_STREQ("A%5E%20", dest);
70}
71
72TEST(Urldecode, Decoding1) {
73 char source[] = "A%5E+";
74 char dest[8];
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000075 ASSERT_EQ(3, rtc::UrlDecode(source, dest));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000076 ASSERT_STREQ("A^ ", dest);
77}
78
79TEST(Urldecode, Decoding2) {
80 char source[] = "A%5E+";
81 char dest[8];
henrike@webrtc.orgc50bf7c2014-05-14 18:24:13 +000082 ASSERT_EQ(3, rtc::UrlDecodeWithoutEncodingSpaceAsPlus(source, dest));
henrike@webrtc.orgf0488722014-05-13 18:00:26 +000083 ASSERT_STREQ("A^+", dest);
84}