henrike@webrtc.org | f7795df | 2014-05-13 18:00:26 +0000 | [diff] [blame^] | 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 | TEST(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 | |
| 28 | TEST(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 | |
| 35 | TEST(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 | |
| 45 | TEST(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 | |
| 54 | TEST(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 | |
| 61 | TEST(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 | |
| 69 | TEST(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 | |
| 76 | TEST(Urldecode, Decoding2) { |
| 77 | char source[] = "A%5E+"; |
| 78 | char dest[8]; |
| 79 | ASSERT_EQ(3, UrlDecodeWithoutEncodingSpaceAsPlus(source, dest)); |
| 80 | ASSERT_STREQ("A^+", dest); |
| 81 | } |