OLD | NEW |
(Empty) | |
| 1 // Copyright 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import <Foundation/Foundation.h> |
| 6 |
| 7 #include "base/mac/scoped_nsobject.h" |
| 8 #include "base/strings/sys_string_conversions.h" |
| 9 #include "testing/gtest/include/gtest/gtest.h" |
| 10 #include "testing/gtest_mac.h" |
| 11 #include "url/gurl.h" |
| 12 #import "url/mac/url_conversions.h" |
| 13 |
| 14 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 15 #include "base/mac/mac_util.h" |
| 16 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 17 |
| 18 namespace { |
| 19 |
| 20 class URLConversionTest : public ::testing::Test { |
| 21 protected: |
| 22 virtual void SetUp() { |
| 23 testData_.reset( |
| 24 [[NSArray alloc] initWithObjects: |
| 25 // Simple URL with protocol |
| 26 @"https://www.google.com/", |
| 27 @"https://www.google.com/", |
| 28 |
| 29 // Simple URL with protocol and query string |
| 30 @"https://www.google.com/search?q=gtest", |
| 31 @"https://www.google.com/search?q=gtest", |
| 32 |
| 33 // Simple URL with protocol and query string multiple params |
| 34 @"https://www.google.com/search?hl=en&q=gtest", |
| 35 @"https://www.google.com/search?hl=en&q=gtest", |
| 36 |
| 37 // Simple URL with protocol and query string and fragment |
| 38 @"https://www.google.com/search?q=gtest#123", |
| 39 @"https://www.google.com/search?q=gtest#123", |
| 40 |
| 41 // URL with ~ |
| 42 @"http://www.mysite.com/~user", |
| 43 @"http://www.mysite.com/~user", |
| 44 |
| 45 // URL with # |
| 46 @"http://www.mysite.com/#123456", |
| 47 @"http://www.mysite.com/#123456", |
| 48 |
| 49 // URL with # before ? |
| 50 @"http://www.mysite.com/test#test?test", |
| 51 @"http://www.mysite.com/test#test?test", |
| 52 |
| 53 // URL with ? before # |
| 54 @"http://www.mysite.com/test?test#test", |
| 55 @"http://www.mysite.com/test?test#test", |
| 56 |
| 57 // URL with two #s |
| 58 @"http://www.mysite.com/test#test#test", |
| 59 @"http://www.mysite.com/test#test%23test", |
| 60 |
| 61 // URL with two ?s |
| 62 @"http://www.mysite.com/test?test?test", |
| 63 @"http://www.mysite.com/test?test?test", |
| 64 |
| 65 // URL with pattern ? # ? |
| 66 @"http://www.mysite.com/test?test#test?test", |
| 67 @"http://www.mysite.com/test?test#test?test", |
| 68 |
| 69 // URL with pattern # ? # |
| 70 @"http://www.mysite.com/test#test?test#test", |
| 71 @"http://www.mysite.com/test#test?test%23test", |
| 72 |
| 73 // URL with % |
| 74 @"http://www.mysite.com/%123", |
| 75 @"http://www.mysite.com/%123", |
| 76 |
| 77 // URL with [ |
| 78 @"http://www.mysite.com/[123", |
| 79 @"http://www.mysite.com/%5B123", |
| 80 |
| 81 // URL with ] |
| 82 @"http://www.mysite.com/]123", |
| 83 @"http://www.mysite.com/%5D123", |
| 84 |
| 85 // URL with ` |
| 86 @"http://www.mysite.com/`123", |
| 87 @"http://www.mysite.com/%60123", |
| 88 |
| 89 // URL with ^ |
| 90 @"http://www.mysite.com/^123", |
| 91 @"http://www.mysite.com/%5E123", |
| 92 |
| 93 // URL with backslash (GURL canonicallizes unescaped \ to /) |
| 94 @"http://www.mysite.com/\\123", |
| 95 @"http://www.mysite.com//123", |
| 96 |
| 97 // URL with space |
| 98 @"http://www.mysite.com/~user name", |
| 99 @"http://www.mysite.com/~user%20name", |
| 100 |
| 101 // URL with < |
| 102 @"http://www.mysite.com/123<456", |
| 103 @"http://www.mysite.com/123%3C456", |
| 104 |
| 105 // URL with > |
| 106 @"http://www.mysite.com/456>123", |
| 107 @"http://www.mysite.com/456%3E123", |
| 108 |
| 109 // URL with | |
| 110 @"http://www.mysite.com/|123", |
| 111 @"http://www.mysite.com/%7C123", |
| 112 |
| 113 // URL with ! |
| 114 @"http://www.mysite.com/!123", |
| 115 @"http://www.mysite.com/!123", |
| 116 |
| 117 // URL with ~ |
| 118 @"http://www.mysite.com/~user", |
| 119 @"http://www.mysite.com/~user", |
| 120 |
| 121 // URL with & (no ?) |
| 122 @"http://www.mysite.com/&123", |
| 123 @"http://www.mysite.com/&123", |
| 124 |
| 125 // URL with ' |
| 126 @"http://www.mysite.com/'user", |
| 127 @"http://www.mysite.com/'user", |
| 128 |
| 129 // URL with " |
| 130 @"http://www.mysite.com/\"user", |
| 131 @"http://www.mysite.com/%22user", |
| 132 |
| 133 // URL with ( |
| 134 @"http://www.mysite.com/(123", |
| 135 @"http://www.mysite.com/(123", |
| 136 |
| 137 // URL with ) |
| 138 @"http://www.mysite.com/)123", |
| 139 @"http://www.mysite.com/)123", |
| 140 |
| 141 // URL with + |
| 142 @"http://www.mysite.com/+123", |
| 143 @"http://www.mysite.com/+123", |
| 144 |
| 145 // URL with * |
| 146 @"http://www.mysite.com/*123", |
| 147 @"http://www.mysite.com/*123", |
| 148 |
| 149 // URL with space |
| 150 @"http://www.mysite.com/user name", |
| 151 @"http://www.mysite.com/user%20name", |
| 152 |
| 153 // URL with unescaped European accented characters |
| 154 @"http://fr.news.yahoo.com/bactérie-e-coli-ajouter-vinaigre-leau-" |
| 155 "rinçage-légumes-061425535.html", |
| 156 @"http://fr.news.yahoo.com/bact%C3%A9rie-e-coli-ajouter-vinaigre-" |
| 157 "leau-rin%C3%A7age-l%C3%A9gumes-061425535.html", |
| 158 |
| 159 // URL with mix of unescaped European accented characters |
| 160 @"http://fr.news.yahoo.com/bactérie-e-coli-ajouter-vinaigre-leau-" |
| 161 "rinçage-l%C3%A9gumes-061425535.html", |
| 162 @"http://fr.news.yahoo.com/bact%C3%A9rie-e-coli-ajouter-vinaigre-" |
| 163 "leau-rin%C3%A7age-l%C3%A9gumes-061425535.html", |
| 164 |
| 165 // URL with unescaped Asian unicode characters |
| 166 @"http://www.baidu.com/s?cl=3&fr=tb01000&wd=鍜嬩箞鍠傞", |
| 167 @"http://www.baidu.com/s?cl=3&fr=tb01000&wd=" |
| 168 "%E9%8D%9C%E5%AC%A9%E7%AE%9E%E9%8D%A0%E5%82%9E", |
| 169 |
| 170 // URL containing every character in the range 20->7F |
| 171 @"http://google.com/ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM" |
| 172 "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~", |
| 173 @"http://google.com/%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCD" |
| 174 "EFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" |
| 175 "%7B%7C%7D~", |
| 176 |
| 177 // URL containing every accented character from the range 80->FF |
| 178 @"http://google.com/¿ÀÁÂÃÄÅÆÇÈÉÊËÌÍÎÏÐÑÒÓÔÕÖרÙÚÛÜÝÞßàáâãäåæçèéêëìí" |
| 179 "îïðñòóôõö÷øùúûüýþÿ", |
| 180 @"http://google.com/%C2%BF%C3%80%C3%81%C3%82%C3%83%C3%84%C3%85%C3" |
| 181 "%86%C3%87%C3%88%C3%89%C3%8A%C3%8B%C3%8C%C3%8D%C3%8E%C3%8F%C3%90" |
| 182 "%C3%91%C3%92%C3%93%C3%94%C3%95%C3%96%C3%97%C3%98%C3%99%C3%9A%C3" |
| 183 "%9B%C3%9C%C3%9D%C3%9E%C3%9F%C3%A0%C3%A1%C3%A2%C3%A3%C3%A4%C3%A5" |
| 184 "%C3%A6%C3%A7%C3%A8%C3%A9%C3%AA%C3%AB%C3%AC%C3%AD%C3%AE%C3%AF%C3" |
| 185 "%B0%C3%B1%C3%B2%C3%B3%C3%B4%C3%B5%C3%B6%C3%B7%C3%B8%C3%B9%C3%BA" |
| 186 "%C3%BB%C3%BC%C3%BD%C3%BE%C3%BF", |
| 187 |
| 188 // URL containing every character in the range 20->7F repeated twice |
| 189 @"http://google.com/ !\"#$%&'()*+,-./0123456789:;<=>?@ABCDEFGHIJKLM" |
| 190 "NOPQRSTUVWXYZ[\\]^_`abcdefghijklmnopqrstuvwxyz{|}~ !\"#$%&'()*+,-" |
| 191 "./0123456789:;<=>?@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_`abcdefghijklm" |
| 192 "nopqrstuvwxyz{|}~", |
| 193 @"http://google.com/%20!%22#$%25&'()*+,-./0123456789:;%3C=%3E?@ABCD" |
| 194 "EFGHIJKLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz" |
| 195 "%7B%7C%7D~%20!%22%23$%25&'()*+,-./0123456789:;%3C=%3E?@ABCDEFGHIJ" |
| 196 "KLMNOPQRSTUVWXYZ%5B%5C%5D%5E_%60abcdefghijklmnopqrstuvwxyz%7B%7C" |
| 197 "%7D~", |
| 198 |
| 199 // Special case 7F. the weird out of place control character for DEL |
| 200 @"data:,\177", |
| 201 @"data:,%7F", |
| 202 |
| 203 // URL with some common control characters. |
| 204 // GURL simply removes most control characters. |
| 205 @"data:,\a\b\t\r\n", |
| 206 @"data:,", |
| 207 |
| 208 // All control characters but \000. |
| 209 @"data:,\001\002\003\004\005\006\007\010\011\012\013\014\015\016" |
| 210 "\017\020\021\022\023\024\025\026\027\030\031\032\033\034\035\036" |
| 211 "\037", |
| 212 @"data:,", |
| 213 |
| 214 nil]); |
| 215 } |
| 216 |
| 217 // NSArray of NSString pairs used for running the tests. |
| 218 // Each pair is in the form <input value, expected result>. |
| 219 base::scoped_nsobject<NSArray> testData_; |
| 220 }; |
| 221 |
| 222 TEST_F(URLConversionTest, TestNSURLCreationFromStrings) { |
| 223 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 224 if (!base::mac::IsOSLionOrLater()) |
| 225 return; |
| 226 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 227 |
| 228 for (NSUInteger i = 0; i < [testData_ count]; i += 2) { |
| 229 NSString* inputStr = [testData_ objectAtIndex:i]; |
| 230 NSString* expected = [testData_ objectAtIndex:(i + 1)]; |
| 231 NSURL* url = NSURLWithGURL(GURL(base::SysNSStringToUTF8(inputStr))); |
| 232 EXPECT_NSEQ(expected, [url absoluteString]); |
| 233 } |
| 234 } |
| 235 |
| 236 TEST_F(URLConversionTest, TestURLWithStringDoesNotModifyAlreadyEscapedURLs) { |
| 237 #if defined(OS_MACOSX) && !defined(OS_IOS) |
| 238 if (!base::mac::IsOSLionOrLater()) |
| 239 return; |
| 240 #endif // defined(OS_MACOSX) && !defined(OS_IOS) |
| 241 |
| 242 for (NSUInteger i = 0; i < [testData_ count]; i += 2) { |
| 243 NSString* inputStr = [testData_ objectAtIndex:i + 1]; |
| 244 NSURL* url = NSURLWithGURL(GURL(base::SysNSStringToUTF8(inputStr))); |
| 245 NSString* expected = [testData_ objectAtIndex:i + 1]; |
| 246 // Test the expected URL is created. |
| 247 EXPECT_NSEQ(expected, [url absoluteString]); |
| 248 } |
| 249 } |
| 250 |
| 251 } // anonymous namespace |
OLD | NEW |