| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/net_util.h" | 5 #include "net/base/net_util.h" |
| 6 | 6 |
| 7 #include <string.h> | 7 #include <string.h> |
| 8 | 8 |
| 9 #include <algorithm> | 9 #include <algorithm> |
| 10 | 10 |
| (...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 43 const char* expected; | 43 const char* expected; |
| 44 }; | 44 }; |
| 45 | 45 |
| 46 struct HeaderParamCase { | 46 struct HeaderParamCase { |
| 47 const char* header_name; | 47 const char* header_name; |
| 48 const char* param_name; | 48 const char* param_name; |
| 49 const char* expected; | 49 const char* expected; |
| 50 }; | 50 }; |
| 51 | 51 |
| 52 const char* kLanguages[] = { | 52 const char* kLanguages[] = { |
| 53 "", "en", "zh-CN", "ja", "ko", | 53 "", "en", "zh-CN", "ja", "ko", "he", "ar", |
| 54 "he", "ar", "ru", "el", "fr", | 54 "ru", "el", "fr", "de", "pt", "sv", "th", |
| 55 "de", "pt", "sv", "th", "hi", | 55 "hi", "de,en", "el,en", "zh-TW,en", "ko,ja", "he,ru,en", "zh,ru,en"}; |
| 56 "de,en", "el,en", "zh-TW,en", "ko,ja", "he,ru,en", | |
| 57 "zh,ru,en" | |
| 58 }; | |
| 59 | 56 |
| 60 struct IDNTestCase { | 57 struct IDNTestCase { |
| 61 const char* input; | 58 const char* input; |
| 62 const wchar_t* unicode_output; | 59 const wchar_t* unicode_output; |
| 63 const bool unicode_allowed[arraysize(kLanguages)]; | 60 const bool unicode_allowed[arraysize(kLanguages)]; |
| 64 }; | 61 }; |
| 65 | 62 |
| 66 // TODO(jungshik) This is just a random sample of languages and is far | 63 // TODO(jungshik) This is just a random sample of languages and is far |
| 67 // from exhaustive. We may have to generate all the combinations | 64 // from exhaustive. We may have to generate all the combinations |
| 68 // of languages (powerset of a set of all the languages). | 65 // of languages (powerset of a set of all the languages). |
| 69 const IDNTestCase idn_cases[] = { | 66 const IDNTestCase idn_cases[] = { |
| 70 // No IDN | 67 // No IDN |
| 71 {"www.google.com", L"www.google.com", | 68 {"www.google.com", |
| 72 {true, true, true, true, true, | 69 L"www.google.com", |
| 73 true, true, true, true, true, | 70 {true, true, true, true, true, true, true, true, true, true, true, true, |
| 74 true, true, true, true, true, | 71 true, true, true, true, true, true, true, true, true}}, |
| 75 true, true, true, true, true, | 72 {"www.google.com.", |
| 76 true}}, | 73 L"www.google.com.", |
| 77 {"www.google.com.", L"www.google.com.", | 74 {true, true, true, true, true, true, true, true, true, true, true, true, |
| 78 {true, true, true, true, true, | 75 true, true, true, true, true, true, true, true, true}}, |
| 79 true, true, true, true, true, | 76 {".", |
| 80 true, true, true, true, true, | 77 L".", |
| 81 true, true, true, true, true, | 78 {true, true, true, true, true, true, true, true, true, true, true, true, |
| 82 true}}, | 79 true, true, true, true, true, true, true, true, true}}, |
| 83 {".", L".", | 80 {"", |
| 84 {true, true, true, true, true, | 81 L"", |
| 85 true, true, true, true, true, | 82 {true, true, true, true, true, true, true, true, true, true, true, true, |
| 86 true, true, true, true, true, | 83 true, true, true, true, true, true, true, true, true}}, |
| 87 true, true, true, true, true, | 84 // IDN |
| 88 true}}, | 85 // Hanzi (Traditional Chinese) |
| 89 {"", L"", | 86 {"xn--1lq90ic7f1rc.cn", |
| 90 {true, true, true, true, true, | 87 L"\x5317\x4eac\x5927\x5b78.cn", |
| 91 true, true, true, true, true, | 88 {true, false, true, true, false, false, false, false, false, false, false, |
| 92 true, true, true, true, true, | 89 false, false, false, false, false, false, true, true, false, true}}, |
| 93 true, true, true, true, true, | 90 // Hanzi ('video' in Simplified Chinese : will pass only in zh-CN,zh) |
| 94 true}}, | 91 {"xn--cy2a840a.com", |
| 95 // IDN | 92 L"\x89c6\x9891.com", |
| 96 // Hanzi (Traditional Chinese) | 93 {true, false, true, false, false, false, false, false, false, false, false, |
| 97 {"xn--1lq90ic7f1rc.cn", L"\x5317\x4eac\x5927\x5b78.cn", | 94 false, false, false, false, false, false, false, false, false, true}}, |
| 98 {true, false, true, true, false, | 95 // Hanzi + '123' |
| 99 false, false, false, false, false, | 96 {"www.xn--123-p18d.com", |
| 100 false, false, false, false, false, | 97 L"www.\x4e00" |
| 101 false, false, true, true, false, | 98 L"123.com", |
| 102 true}}, | 99 {true, false, true, true, false, false, false, false, false, false, false, |
| 103 // Hanzi ('video' in Simplified Chinese : will pass only in zh-CN,zh) | 100 false, false, false, false, false, false, true, true, false, true}}, |
| 104 {"xn--cy2a840a.com", L"\x89c6\x9891.com", | 101 // Hanzi + Latin : U+56FD is simplified and is regarded |
| 105 {true, false, true, false, false, | 102 // as not supported in zh-TW. |
| 106 false, false, false, false, false, | 103 {"www.xn--hello-9n1hm04c.com", |
| 107 false, false, false, false, false, | 104 L"www.hello\x4e2d\x56fd.com", |
| 108 false, false, false, false, false, | 105 {false, false, true, true, false, false, false, false, false, false, false, |
| 109 true}}, | 106 false, false, false, false, false, false, false, true, false, true}}, |
| 110 // Hanzi + '123' | 107 // Kanji + Kana (Japanese) |
| 111 {"www.xn--123-p18d.com", L"www.\x4e00" L"123.com", | 108 {"xn--l8jvb1ey91xtjb.jp", |
| 112 {true, false, true, true, false, | 109 L"\x671d\x65e5\x3042\x3055\x3072.jp", |
| 113 false, false, false, false, false, | 110 {true, false, false, true, false, false, false, false, false, false, false, |
| 114 false, false, false, false, false, | 111 false, false, false, false, false, false, false, true, false, false}}, |
| 115 false, false, true, true, false, | 112 // Katakana including U+30FC |
| 116 true}}, | 113 {"xn--tckm4i2e.jp", |
| 117 // Hanzi + Latin : U+56FD is simplified and is regarded | 114 L"\x30b3\x30de\x30fc\x30b9.jp", |
| 118 // as not supported in zh-TW. | 115 { |
| 119 {"www.xn--hello-9n1hm04c.com", L"www.hello\x4e2d\x56fd.com", | 116 true, false, false, true, false, false, false, false, false, false, false, |
| 120 {false, false, true, true, false, | 117 false, false, false, false, false, false, false, true, false, |
| 121 false, false, false, false, false, | 118 }}, |
| 122 false, false, false, false, false, | 119 {"xn--3ck7a7g.jp", |
| 123 false, false, false, true, false, | 120 L"\u30ce\u30f3\u30bd.jp", |
| 124 true}}, | 121 { |
| 125 // Kanji + Kana (Japanese) | 122 true, false, false, true, false, false, false, false, false, false, false, |
| 126 {"xn--l8jvb1ey91xtjb.jp", L"\x671d\x65e5\x3042\x3055\x3072.jp", | 123 false, false, false, false, false, false, false, true, false, |
| 127 {true, false, false, true, false, | 124 }}, |
| 128 false, false, false, false, false, | 125 // Katakana + Latin (Japanese) |
| 129 false, false, false, false, false, | 126 // TODO(jungshik): Change 'false' in the first element to 'true' |
| 130 false, false, false, true, false, | 127 // after upgrading to ICU 4.2.1 to use new uspoof_* APIs instead |
| 131 false}}, | 128 // of our IsIDNComponentInSingleScript(). |
| 132 // Katakana including U+30FC | 129 {"xn--e-efusa1mzf.jp", |
| 133 {"xn--tckm4i2e.jp", L"\x30b3\x30de\x30fc\x30b9.jp", | 130 L"e\x30b3\x30de\x30fc\x30b9.jp", |
| 134 {true, false, false, true, false, | 131 { |
| 135 false, false, false, false, false, | 132 false, false, false, true, false, false, false, false, false, false, |
| 136 false, false, false, false, false, | 133 false, false, false, false, false, false, false, false, true, false, |
| 137 false, false, false, true, false, | 134 }}, |
| 138 }}, | 135 {"xn--3bkxe.jp", |
| 139 {"xn--3ck7a7g.jp", L"\u30ce\u30f3\u30bd.jp", | 136 L"\x30c8\x309a.jp", |
| 140 {true, false, false, true, false, | 137 { |
| 141 false, false, false, false, false, | 138 false, false, false, true, false, false, false, false, false, false, |
| 142 false, false, false, false, false, | 139 false, false, false, false, false, false, false, false, true, false, |
| 143 false, false, false, true, false, | 140 }}, |
| 144 }}, | 141 // Hangul (Korean) |
| 145 // Katakana + Latin (Japanese) | 142 {"www.xn--or3b17p6jjc.kr", |
| 146 // TODO(jungshik): Change 'false' in the first element to 'true' | 143 L"www.\xc804\xc790\xc815\xbd80.kr", |
| 147 // after upgrading to ICU 4.2.1 to use new uspoof_* APIs instead | 144 {true, false, false, false, true, false, false, false, false, false, false, |
| 148 // of our IsIDNComponentInSingleScript(). | 145 false, false, false, false, false, false, false, true, false, false}}, |
| 149 {"xn--e-efusa1mzf.jp", L"e\x30b3\x30de\x30fc\x30b9.jp", | 146 // b<u-umlaut>cher (German) |
| 150 {false, false, false, true, false, | 147 {"xn--bcher-kva.de", |
| 151 false, false, false, false, false, | 148 L"b\x00fc" |
| 152 false, false, false, false, false, | 149 L"cher.de", |
| 153 false, false, false, true, false, | 150 {true, false, false, false, false, false, false, false, false, true, true, |
| 154 }}, | 151 false, false, false, false, true, false, false, false, false, false}}, |
| 155 {"xn--3bkxe.jp", L"\x30c8\x309a.jp", | 152 // a with diaeresis |
| 156 {false, false, false, true, false, | 153 {"www.xn--frgbolaget-q5a.se", |
| 157 false, false, false, false, false, | 154 L"www.f\x00e4rgbolaget.se", |
| 158 false, false, false, false, false, | 155 {true, false, false, false, false, false, false, false, false, false, true, |
| 159 false, false, false, true, false, | 156 false, true, false, false, true, false, false, false, false, false}}, |
| 160 }}, | 157 // c-cedilla (French) |
| 161 // Hangul (Korean) | 158 {"www.xn--alliancefranaise-npb.fr", |
| 162 {"www.xn--or3b17p6jjc.kr", L"www.\xc804\xc790\xc815\xbd80.kr", | 159 L"www.alliancefran\x00e7" |
| 163 {true, false, false, false, true, | 160 L"aise.fr", |
| 164 false, false, false, false, false, | 161 {true, false, false, false, false, false, false, false, false, true, false, |
| 165 false, false, false, false, false, | 162 true, false, false, false, false, false, false, false, false, false}}, |
| 166 false, false, false, true, false, | 163 // caf'e with acute accent' (French) |
| 167 false}}, | 164 {"xn--caf-dma.fr", |
| 168 // b<u-umlaut>cher (German) | 165 L"caf\x00e9.fr", |
| 169 {"xn--bcher-kva.de", L"b\x00fc" L"cher.de", | 166 {true, false, false, false, false, false, false, false, false, true, false, |
| 170 {true, false, false, false, false, | 167 true, true, false, false, false, false, false, false, false, false}}, |
| 171 false, false, false, false, true, | 168 // c-cedillla and a with tilde (Portuguese) |
| 172 true, false, false, false, false, | 169 {"xn--poema-9qae5a.com.br", |
| 173 true, false, false, false, false, | 170 L"p\x00e3oema\x00e7\x00e3.com.br", |
| 174 false}}, | 171 {true, false, false, false, false, false, false, false, false, false, |
| 175 // a with diaeresis | 172 false, true, false, false, false, false, false, false, false, false, |
| 176 {"www.xn--frgbolaget-q5a.se", L"www.f\x00e4rgbolaget.se", | 173 false}}, |
| 177 {true, false, false, false, false, | 174 // s with caron |
| 178 false, false, false, false, false, | 175 {"xn--achy-f6a.com", |
| 179 true, false, true, false, false, | 176 L"\x0161" |
| 180 true, false, false, false, false, | 177 L"achy.com", |
| 181 false}}, | 178 {true, false, false, false, false, false, false, false, false, false, |
| 182 // c-cedilla (French) | 179 false, false, false, false, false, false, false, false, false, false, |
| 183 {"www.xn--alliancefranaise-npb.fr", L"www.alliancefran\x00e7" L"aise.fr", | 180 false}}, |
| 184 {true, false, false, false, false, | 181 // TODO(jungshik) : Add examples with Cyrillic letters |
| 185 false, false, false, false, true, | 182 // only used in some languages written in Cyrillic. |
| 186 false, true, false, false, false, | 183 // Eutopia (Greek) |
| 187 false, false, false, false, false, | 184 {"xn--kxae4bafwg.gr", |
| 188 false}}, | 185 L"\x03bf\x03c5\x03c4\x03bf\x03c0\x03af\x03b1.gr", |
| 189 // caf'e with acute accent' (French) | 186 {true, false, false, false, false, false, false, false, true, false, false, |
| 190 {"xn--caf-dma.fr", L"caf\x00e9.fr", | 187 false, false, false, false, false, true, false, false, false, false}}, |
| 191 {true, false, false, false, false, | 188 // Eutopia + 123 (Greek) |
| 192 false, false, false, false, true, | 189 {"xn---123-pldm0haj2bk.gr", |
| 193 false, true, true, false, false, | 190 L"\x03bf\x03c5\x03c4\x03bf\x03c0\x03af\x03b1-123.gr", |
| 194 false, false, false, false, false, | 191 {true, false, false, false, false, false, false, false, true, false, false, |
| 195 false}}, | 192 false, false, false, false, false, true, false, false, false, false}}, |
| 196 // c-cedillla and a with tilde (Portuguese) | 193 // Cyrillic (Russian) |
| 197 {"xn--poema-9qae5a.com.br", L"p\x00e3oema\x00e7\x00e3.com.br", | 194 {"xn--n1aeec9b.ru", |
| 198 {true, false, false, false, false, | 195 L"\x0442\x043e\x0440\x0442\x044b.ru", |
| 199 false, false, false, false, false, | 196 {true, false, false, false, false, false, false, true, false, false, false, |
| 200 false, true, false, false, false, | 197 false, false, false, false, false, false, false, false, true, true}}, |
| 201 false, false, false, false, false, | 198 // Cyrillic + 123 (Russian) |
| 202 false}}, | 199 {"xn---123-45dmmc5f.ru", |
| 203 // s with caron | 200 L"\x0442\x043e\x0440\x0442\x044b-123.ru", |
| 204 {"xn--achy-f6a.com", L"\x0161" L"achy.com", | 201 {true, false, false, false, false, false, false, true, false, false, false, |
| 205 {true, false, false, false, false, | 202 false, false, false, false, false, false, false, false, true, true}}, |
| 206 false, false, false, false, false, | 203 // Arabic |
| 207 false, false, false, false, false, | 204 {"xn--mgba1fmg.ar", |
| 208 false, false, false, false, false, | 205 L"\x0627\x0641\x0644\x0627\x0645.ar", |
| 209 false}}, | 206 {true, false, false, false, false, false, true, false, false, false, false, |
| 210 // TODO(jungshik) : Add examples with Cyrillic letters | 207 false, false, false, false, false, false, false, false, false, false}}, |
| 211 // only used in some languages written in Cyrillic. | 208 // Hebrew |
| 212 // Eutopia (Greek) | 209 {"xn--4dbib.he", |
| 213 {"xn--kxae4bafwg.gr", L"\x03bf\x03c5\x03c4\x03bf\x03c0\x03af\x03b1.gr", | 210 L"\x05d5\x05d0\x05d4.he", |
| 214 {true, false, false, false, false, | 211 {true, false, false, false, false, true, false, false, false, false, false, |
| 215 false, false, false, true, false, | 212 false, false, false, false, false, false, false, false, true, false}}, |
| 216 false, false, false, false, false, | 213 // Thai |
| 217 false, true, false, false, false, | 214 {"xn--12c2cc4ag3b4ccu.th", |
| 218 false}}, | 215 L"\x0e2a\x0e32\x0e22\x0e01\x0e32\x0e23\x0e1a\x0e34\x0e19.th", |
| 219 // Eutopia + 123 (Greek) | 216 {true, false, false, false, false, false, false, false, false, false, |
| 220 {"xn---123-pldm0haj2bk.gr", | 217 false, false, false, true, false, false, false, false, false, false, |
| 221 L"\x03bf\x03c5\x03c4\x03bf\x03c0\x03af\x03b1-123.gr", | 218 false}}, |
| 222 {true, false, false, false, false, | 219 // Devangari (Hindi) |
| 223 false, false, false, true, false, | 220 {"www.xn--l1b6a9e1b7c.in", |
| 224 false, false, false, false, false, | 221 L"www.\x0905\x0915\x094b\x0932\x093e.in", |
| 225 false, true, false, false, false, | 222 {true, false, false, false, false, false, false, false, false, false, |
| 226 false}}, | 223 false, false, false, false, true, false, false, false, false, false, |
| 227 // Cyrillic (Russian) | 224 false}}, |
| 228 {"xn--n1aeec9b.ru", L"\x0442\x043e\x0440\x0442\x044b.ru", | 225 // Invalid IDN |
| 229 {true, false, false, false, false, | 226 {"xn--hello?world.com", |
| 230 false, false, true, false, false, | 227 NULL, |
| 231 false, false, false, false, false, | 228 {false, false, false, false, false, false, false, false, false, false, |
| 232 false, false, false, false, true, | 229 false, false, false, false, false, false, false, false, false, false, |
| 233 true}}, | 230 false}}, |
| 234 // Cyrillic + 123 (Russian) | 231 // Unsafe IDNs |
| 235 {"xn---123-45dmmc5f.ru", L"\x0442\x043e\x0440\x0442\x044b-123.ru", | 232 // "payp<alpha>l.com" |
| 236 {true, false, false, false, false, | 233 {"www.xn--paypl-g9d.com", |
| 237 false, false, true, false, false, | 234 L"payp\x03b1l.com", |
| 238 false, false, false, false, false, | 235 {false, false, false, false, false, false, false, false, false, false, |
| 239 false, false, false, false, true, | 236 false, false, false, false, false, false, false, false, false, false, |
| 240 true}}, | 237 false}}, |
| 241 // Arabic | 238 // google.gr with Greek omicron and epsilon |
| 242 {"xn--mgba1fmg.ar", L"\x0627\x0641\x0644\x0627\x0645.ar", | 239 {"xn--ggl-6xc1ca.gr", |
| 243 {true, false, false, false, false, | 240 L"g\x03bf\x03bfgl\x03b5.gr", |
| 244 false, true, false, false, false, | 241 {false, false, false, false, false, false, false, false, false, false, |
| 245 false, false, false, false, false, | 242 false, false, false, false, false, false, false, false, false, false, |
| 246 false, false, false, false, false, | 243 false}}, |
| 247 false}}, | 244 // google.ru with Cyrillic o |
| 248 // Hebrew | 245 {"xn--ggl-tdd6ba.ru", |
| 249 {"xn--4dbib.he", L"\x05d5\x05d0\x05d4.he", | 246 L"g\x043e\x043egl\x0435.ru", |
| 250 {true, false, false, false, false, | 247 {false, false, false, false, false, false, false, false, false, false, |
| 251 true, false, false, false, false, | 248 false, false, false, false, false, false, false, false, false, false, |
| 252 false, false, false, false, false, | 249 false}}, |
| 253 false, false, false, false, true, | 250 // h<e with acute>llo<China in Han>.cn |
| 254 false}}, | 251 {"xn--hllo-bpa7979ih5m.cn", |
| 255 // Thai | 252 L"h\x00e9llo\x4e2d\x56fd.cn", |
| 256 {"xn--12c2cc4ag3b4ccu.th", | 253 {false, false, false, false, false, false, false, false, false, false, |
| 257 L"\x0e2a\x0e32\x0e22\x0e01\x0e32\x0e23\x0e1a\x0e34\x0e19.th", | 254 false, false, false, false, false, false, false, false, false, false, |
| 258 {true, false, false, false, false, | 255 false}}, |
| 259 false, false, false, false, false, | 256 // <Greek rho><Cyrillic a><Cyrillic u>.ru |
| 260 false, false, false, true, false, | 257 {"xn--2xa6t2b.ru", |
| 261 false, false, false, false, false, | 258 L"\x03c1\x0430\x0443.ru", |
| 262 false}}, | 259 {false, false, false, false, false, false, false, false, false, false, |
| 263 // Devangari (Hindi) | 260 false, false, false, false, false, false, false, false, false, false, |
| 264 {"www.xn--l1b6a9e1b7c.in", L"www.\x0905\x0915\x094b\x0932\x093e.in", | 261 false}}, |
| 265 {true, false, false, false, false, | 262 // One that's really long that will force a buffer realloc |
| 266 false, false, false, false, false, | 263 {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 267 false, false, false, false, true, | 264 "aaaaaaa", |
| 268 false, false, false, false, false, | 265 L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" |
| 269 false}}, | 266 L"aaaaaaaa", |
| 270 // Invalid IDN | 267 {true, true, true, true, true, true, true, true, true, true, true, true, |
| 271 {"xn--hello?world.com", NULL, | 268 true, true, true, true, true, true, true, true, true}}, |
| 272 {false, false, false, false, false, | 269 // Test cases for characters we blacklisted although allowed in IDN. |
| 273 false, false, false, false, false, | 270 // Embedded spaces will be turned to %20 in the display. |
| 274 false, false, false, false, false, | 271 // TODO(jungshik): We need to have more cases. This is a typical |
| 275 false, false, false, false, false, | 272 // data-driven trap. The following test cases need to be separated |
| 276 false}}, | 273 // and tested only for a couple of languages. |
| 277 // Unsafe IDNs | 274 {"xn--osd3820f24c.kr", |
| 278 // "payp<alpha>l.com" | 275 L"\xac00\xb098\x115f.kr", |
| 279 {"www.xn--paypl-g9d.com", L"payp\x03b1l.com", | 276 {false, false, false, false, false, false, false, false, false, false, |
| 280 {false, false, false, false, false, | 277 false, false, false, false, false, false, false, false, false, false, |
| 281 false, false, false, false, false, | 278 false}}, |
| 282 false, false, false, false, false, | 279 {"www.xn--google-ho0coa.com", |
| 283 false, false, false, false, false, | 280 L"www.\x2039google\x203a.com", |
| 284 false}}, | 281 { |
| 285 // google.gr with Greek omicron and epsilon | 282 false, false, false, false, false, false, false, false, false, false, |
| 286 {"xn--ggl-6xc1ca.gr", L"g\x03bf\x03bfgl\x03b5.gr", | 283 false, false, false, false, false, false, false, false, false, false, |
| 287 {false, false, false, false, false, | 284 }}, |
| 288 false, false, false, false, false, | 285 {"google.xn--comabc-k8d", |
| 289 false, false, false, false, false, | 286 L"google.com\x0338" |
| 290 false, false, false, false, false, | 287 L"abc", |
| 291 false}}, | 288 { |
| 292 // google.ru with Cyrillic o | 289 false, false, false, false, false, false, false, false, false, false, |
| 293 {"xn--ggl-tdd6ba.ru", L"g\x043e\x043egl\x0435.ru", | 290 false, false, false, false, false, false, false, false, false, false, |
| 294 {false, false, false, false, false, | 291 }}, |
| 295 false, false, false, false, false, | 292 {"google.xn--com-oh4ba.evil.jp", |
| 296 false, false, false, false, false, | 293 L"google.com\x309a\x309a.evil.jp", |
| 297 false, false, false, false, false, | 294 { |
| 298 false}}, | 295 false, false, false, false, false, false, false, false, false, false, |
| 299 // h<e with acute>llo<China in Han>.cn | 296 false, false, false, false, false, false, false, false, false, false, |
| 300 {"xn--hllo-bpa7979ih5m.cn", L"h\x00e9llo\x4e2d\x56fd.cn", | 297 }}, |
| 301 {false, false, false, false, false, | 298 {"google.xn--comevil-v04f.jp", |
| 302 false, false, false, false, false, | 299 L"google.com\x30ce" |
| 303 false, false, false, false, false, | 300 L"evil.jp", |
| 304 false, false, false, false, false, | 301 { |
| 305 false}}, | 302 false, false, false, false, false, false, false, false, false, false, |
| 306 // <Greek rho><Cyrillic a><Cyrillic u>.ru | 303 false, false, false, false, false, false, false, false, false, false, |
| 307 {"xn--2xa6t2b.ru", L"\x03c1\x0430\x0443.ru", | 304 }}, |
| 308 {false, false, false, false, false, | |
| 309 false, false, false, false, false, | |
| 310 false, false, false, false, false, | |
| 311 false, false, false, false, false, | |
| 312 false}}, | |
| 313 // One that's really long that will force a buffer realloc | |
| 314 {"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | |
| 315 "aaaaaaa", | |
| 316 L"aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa" | |
| 317 L"aaaaaaaa", | |
| 318 {true, true, true, true, true, | |
| 319 true, true, true, true, true, | |
| 320 true, true, true, true, true, | |
| 321 true, true, true, true, true, | |
| 322 true}}, | |
| 323 // Test cases for characters we blacklisted although allowed in IDN. | |
| 324 // Embedded spaces will be turned to %20 in the display. | |
| 325 // TODO(jungshik): We need to have more cases. This is a typical | |
| 326 // data-driven trap. The following test cases need to be separated | |
| 327 // and tested only for a couple of languages. | |
| 328 {"xn--osd3820f24c.kr", L"\xac00\xb098\x115f.kr", | |
| 329 {false, false, false, false, false, | |
| 330 false, false, false, false, false, | |
| 331 false, false, false, false, false, | |
| 332 false, false, false, false, false, | |
| 333 false}}, | |
| 334 {"www.xn--google-ho0coa.com", L"www.\x2039google\x203a.com", | |
| 335 {false, false, false, false, false, | |
| 336 false, false, false, false, false, | |
| 337 false, false, false, false, false, | |
| 338 false, false, false, false, false, | |
| 339 }}, | |
| 340 {"google.xn--comabc-k8d", L"google.com\x0338" L"abc", | |
| 341 {false, false, false, false, false, | |
| 342 false, false, false, false, false, | |
| 343 false, false, false, false, false, | |
| 344 false, false, false, false, false, | |
| 345 }}, | |
| 346 {"google.xn--com-oh4ba.evil.jp", L"google.com\x309a\x309a.evil.jp", | |
| 347 {false, false, false, false, false, | |
| 348 false, false, false, false, false, | |
| 349 false, false, false, false, false, | |
| 350 false, false, false, false, false, | |
| 351 }}, | |
| 352 {"google.xn--comevil-v04f.jp", L"google.com\x30ce" L"evil.jp", | |
| 353 {false, false, false, false, false, | |
| 354 false, false, false, false, false, | |
| 355 false, false, false, false, false, | |
| 356 false, false, false, false, false, | |
| 357 }}, | |
| 358 #if 0 | 305 #if 0 |
| 359 // These two cases are special. We need a separate test. | 306 // These two cases are special. We need a separate test. |
| 360 // U+3000 and U+3002 are normalized to ASCII space and dot. | 307 // U+3000 and U+3002 are normalized to ASCII space and dot. |
| 361 {"xn-- -kq6ay5z.cn", L"\x4e2d\x56fd\x3000.cn", | 308 {"xn-- -kq6ay5z.cn", L"\x4e2d\x56fd\x3000.cn", |
| 362 {false, false, true, false, false, | 309 {false, false, true, false, false, |
| 363 false, false, false, false, false, | 310 false, false, false, false, false, |
| 364 false, false, false, false, false, | 311 false, false, false, false, false, |
| 365 false, false, true, false, false, | 312 false, false, true, false, false, |
| 366 true}}, | 313 true}}, |
| 367 {"xn--fiqs8s.cn", L"\x4e2d\x56fd\x3002" L"cn", | 314 {"xn--fiqs8s.cn", L"\x4e2d\x56fd\x3002" L"cn", |
| (...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 427 actual->append(to_append); | 374 actual->append(to_append); |
| 428 } | 375 } |
| 429 | 376 |
| 430 // A pair of helpers for the FormatUrlWithOffsets() test. | 377 // A pair of helpers for the FormatUrlWithOffsets() test. |
| 431 void VerboseExpect(size_t expected, | 378 void VerboseExpect(size_t expected, |
| 432 size_t actual, | 379 size_t actual, |
| 433 const std::string& original_url, | 380 const std::string& original_url, |
| 434 size_t position, | 381 size_t position, |
| 435 const base::string16& formatted_url) { | 382 const base::string16& formatted_url) { |
| 436 EXPECT_EQ(expected, actual) << "Original URL: " << original_url | 383 EXPECT_EQ(expected, actual) << "Original URL: " << original_url |
| 437 << " (at char " << position << ")\nFormatted URL: " << formatted_url; | 384 << " (at char " << position |
| 385 << ")\nFormatted URL: " << formatted_url; |
| 438 } | 386 } |
| 439 | 387 |
| 440 void CheckAdjustedOffsets(const std::string& url_string, | 388 void CheckAdjustedOffsets(const std::string& url_string, |
| 441 const std::string& languages, | 389 const std::string& languages, |
| 442 FormatUrlTypes format_types, | 390 FormatUrlTypes format_types, |
| 443 UnescapeRule::Type unescape_rules, | 391 UnescapeRule::Type unescape_rules, |
| 444 const size_t* output_offsets) { | 392 const size_t* output_offsets) { |
| 445 GURL url(url_string); | 393 GURL url(url_string); |
| 446 size_t url_length = url_string.length(); | 394 size_t url_length = url_string.length(); |
| 447 std::vector<size_t> offsets; | 395 std::vector<size_t> offsets; |
| 448 for (size_t i = 0; i <= url_length + 1; ++i) | 396 for (size_t i = 0; i <= url_length + 1; ++i) |
| 449 offsets.push_back(i); | 397 offsets.push_back(i); |
| 450 offsets.push_back(500000); // Something larger than any input length. | 398 offsets.push_back(500000); // Something larger than any input length. |
| 451 offsets.push_back(std::string::npos); | 399 offsets.push_back(std::string::npos); |
| 452 base::string16 formatted_url = FormatUrlWithOffsets(url, languages, | 400 base::string16 formatted_url = FormatUrlWithOffsets( |
| 453 format_types, unescape_rules, NULL, NULL, &offsets); | 401 url, languages, format_types, unescape_rules, NULL, NULL, &offsets); |
| 454 for (size_t i = 0; i < url_length; ++i) | 402 for (size_t i = 0; i < url_length; ++i) |
| 455 VerboseExpect(output_offsets[i], offsets[i], url_string, i, formatted_url); | 403 VerboseExpect(output_offsets[i], offsets[i], url_string, i, formatted_url); |
| 456 VerboseExpect(formatted_url.length(), offsets[url_length], url_string, | 404 VerboseExpect(formatted_url.length(), |
| 457 url_length, formatted_url); | 405 offsets[url_length], |
| 458 VerboseExpect(base::string16::npos, offsets[url_length + 1], url_string, | 406 url_string, |
| 459 500000, formatted_url); | 407 url_length, |
| 460 VerboseExpect(base::string16::npos, offsets[url_length + 2], url_string, | 408 formatted_url); |
| 461 std::string::npos, formatted_url); | 409 VerboseExpect(base::string16::npos, |
| 410 offsets[url_length + 1], |
| 411 url_string, |
| 412 500000, |
| 413 formatted_url); |
| 414 VerboseExpect(base::string16::npos, |
| 415 offsets[url_length + 2], |
| 416 url_string, |
| 417 std::string::npos, |
| 418 formatted_url); |
| 462 } | 419 } |
| 463 | 420 |
| 464 // Helper to strignize an IP number (used to define expectations). | 421 // Helper to strignize an IP number (used to define expectations). |
| 465 std::string DumpIPNumber(const IPAddressNumber& v) { | 422 std::string DumpIPNumber(const IPAddressNumber& v) { |
| 466 std::string out; | 423 std::string out; |
| 467 for (size_t i = 0; i < v.size(); ++i) { | 424 for (size_t i = 0; i < v.size(); ++i) { |
| 468 if (i != 0) | 425 if (i != 0) |
| 469 out.append(","); | 426 out.append(","); |
| 470 out.append(base::IntToString(static_cast<int>(v[i]))); | 427 out.append(base::IntToString(static_cast<int>(v[i]))); |
| 471 } | 428 } |
| 472 return out; | 429 return out; |
| 473 } | 430 } |
| 474 | 431 |
| 475 } // anonymous namespace | 432 } // anonymous namespace |
| 476 | 433 |
| 477 TEST(NetUtilTest, GetIdentityFromURL) { | 434 TEST(NetUtilTest, GetIdentityFromURL) { |
| 478 struct { | 435 struct { |
| 479 const char* input_url; | 436 const char* input_url; |
| 480 const char* expected_username; | 437 const char* expected_username; |
| 481 const char* expected_password; | 438 const char* expected_password; |
| 482 } tests[] = { | 439 } tests[] = { |
| 483 { | 440 { |
| 484 "http://username:password@google.com", | 441 "http://username:password@google.com", "username", "password", |
| 485 "username", | 442 }, |
| 486 "password", | 443 {// Test for http://crbug.com/19200 |
| 487 }, | 444 "http://username:p@ssword@google.com", "username", "p@ssword", |
| 488 { // Test for http://crbug.com/19200 | 445 }, |
| 489 "http://username:p@ssword@google.com", | 446 {// Special URL characters should be unescaped. |
| 490 "username", | 447 "http://username:p%3fa%26s%2fs%23@google.com", "username", "p?a&s/s#", |
| 491 "p@ssword", | 448 }, |
| 492 }, | 449 {// Username contains %20. |
| 493 { // Special URL characters should be unescaped. | 450 "http://use rname:password@google.com", "use rname", "password", |
| 494 "http://username:p%3fa%26s%2fs%23@google.com", | 451 }, |
| 495 "username", | 452 {// Keep %00 as is. |
| 496 "p?a&s/s#", | 453 "http://use%00rname:password@google.com", "use%00rname", "password", |
| 497 }, | 454 }, |
| 498 { // Username contains %20. | 455 {// Use a '+' in the username. |
| 499 "http://use rname:password@google.com", | 456 "http://use+rname:password@google.com", "use+rname", "password", |
| 500 "use rname", | 457 }, |
| 501 "password", | 458 {// Use a '&' in the password. |
| 502 }, | 459 "http://username:p&ssword@google.com", "username", "p&ssword", |
| 503 { // Keep %00 as is. | 460 }, |
| 504 "http://use%00rname:password@google.com", | 461 }; |
| 505 "use%00rname", | |
| 506 "password", | |
| 507 }, | |
| 508 { // Use a '+' in the username. | |
| 509 "http://use+rname:password@google.com", | |
| 510 "use+rname", | |
| 511 "password", | |
| 512 }, | |
| 513 { // Use a '&' in the password. | |
| 514 "http://username:p&ssword@google.com", | |
| 515 "username", | |
| 516 "p&ssword", | |
| 517 }, | |
| 518 }; | |
| 519 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 462 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 520 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, | 463 SCOPED_TRACE( |
| 521 tests[i].input_url)); | 464 base::StringPrintf("Test[%" PRIuS "]: %s", i, tests[i].input_url)); |
| 522 GURL url(tests[i].input_url); | 465 GURL url(tests[i].input_url); |
| 523 | 466 |
| 524 base::string16 username, password; | 467 base::string16 username, password; |
| 525 GetIdentityFromURL(url, &username, &password); | 468 GetIdentityFromURL(url, &username, &password); |
| 526 | 469 |
| 527 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); | 470 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_username), username); |
| 528 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); | 471 EXPECT_EQ(ASCIIToUTF16(tests[i].expected_password), password); |
| 529 } | 472 } |
| 530 } | 473 } |
| 531 | 474 |
| (...skipping 19 matching lines...) Expand all Loading... |
| 551 "Content-TYPE: text/html; charset=utf-8\n" | 494 "Content-TYPE: text/html; charset=utf-8\n" |
| 552 "Content-disposition: attachment; filename=\"download.pdf\"\n" | 495 "Content-disposition: attachment; filename=\"download.pdf\"\n" |
| 553 "Content-Length: 378557\n" | 496 "Content-Length: 378557\n" |
| 554 "X-Google-Google1: 314159265\n" | 497 "X-Google-Google1: 314159265\n" |
| 555 "X-Google-Google2: aaaa2:7783,bbb21:9441\n" | 498 "X-Google-Google2: aaaa2:7783,bbb21:9441\n" |
| 556 "X-Google-Google4: home\n" | 499 "X-Google-Google4: home\n" |
| 557 "Transfer-Encoding: chunked\n" | 500 "Transfer-Encoding: chunked\n" |
| 558 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n" | 501 "Set-Cookie: HEHE_AT=6666x66beef666x6-66xx6666x66; Path=/mail\n" |
| 559 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n" | 502 "Set-Cookie: HEHE_HELP=owned:0;Path=/\n" |
| 560 "Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee" | 503 "Set-Cookie: S=gmail=Xxx-beefbeefbeef_beefb:gmail_yj=beefbeef000beefbee" |
| 561 "fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n" | 504 "fbee:gmproxy=bee-fbeefbe; Domain=.google.com; Path=/\n" |
| 562 "X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n" | 505 "X-Google-Google2: /one/two/three/four/five/six/seven-height/nine:9411\n" |
| 563 "Server: GFE/1.3\n" | 506 "Server: GFE/1.3\n" |
| 564 "Transfer-Encoding: chunked\n" | 507 "Transfer-Encoding: chunked\n" |
| 565 "Date: Mon, 13 Nov 2006 21:38:09 GMT\n" | 508 "Date: Mon, 13 Nov 2006 21:38:09 GMT\n" |
| 566 "Expires: Tue, 14 Nov 2006 19:23:58 GMT\n" | 509 "Expires: Tue, 14 Nov 2006 19:23:58 GMT\n" |
| 567 "X-Malformed: bla; arg=test\"\n" | 510 "X-Malformed: bla; arg=test\"\n" |
| 568 "X-Malformed2: bla; arg=\n" | 511 "X-Malformed2: bla; arg=\n" |
| 569 "X-Test: bla; arg1=val1; arg2=val2"; | 512 "X-Test: bla; arg1=val1; arg2=val2"; |
| 570 | 513 |
| 571 TEST(NetUtilTest, GetSpecificHeader) { | 514 TEST(NetUtilTest, GetSpecificHeader) { |
| 572 const HeaderCase tests[] = { | 515 const HeaderCase tests[] = { |
| 573 {"content-type", "text/html; charset=utf-8"}, | 516 {"content-type", "text/html; charset=utf-8"}, |
| 574 {"CONTENT-LENGTH", "378557"}, | 517 {"CONTENT-LENGTH", "378557"}, |
| 575 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"}, | 518 {"Date", "Mon, 13 Nov 2006 21:38:09 GMT"}, |
| 576 {"Bad-Header", ""}, | 519 {"Bad-Header", ""}, |
| 577 {"", ""}, | 520 {"", ""}, |
| 578 }; | 521 }; |
| 579 | 522 |
| 580 // Test first with google_headers. | 523 // Test first with google_headers. |
| 581 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 524 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 582 std::string result = | 525 std::string result = |
| 583 GetSpecificHeader(google_headers, tests[i].header_name); | 526 GetSpecificHeader(google_headers, tests[i].header_name); |
| 584 EXPECT_EQ(result, tests[i].expected); | 527 EXPECT_EQ(result, tests[i].expected); |
| 585 } | 528 } |
| 586 | 529 |
| 587 // Test again with empty headers. | 530 // Test again with empty headers. |
| 588 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 531 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 589 std::string result = GetSpecificHeader(std::string(), tests[i].header_name); | 532 std::string result = GetSpecificHeader(std::string(), tests[i].header_name); |
| 590 EXPECT_EQ(result, std::string()); | 533 EXPECT_EQ(result, std::string()); |
| 591 } | 534 } |
| 592 } | 535 } |
| 593 | 536 |
| 594 TEST(NetUtilTest, IDNToUnicodeFast) { | 537 TEST(NetUtilTest, IDNToUnicodeFast) { |
| 595 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { | 538 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { |
| 596 for (size_t j = 0; j < arraysize(kLanguages); j++) { | 539 for (size_t j = 0; j < arraysize(kLanguages); j++) { |
| 597 // ja || zh-TW,en || ko,ja -> IDNToUnicodeSlow | 540 // ja || zh-TW,en || ko,ja -> IDNToUnicodeSlow |
| 598 if (j == 3 || j == 17 || j == 18) | 541 if (j == 3 || j == 17 || j == 18) |
| 599 continue; | 542 continue; |
| 600 base::string16 output(IDNToUnicode(idn_cases[i].input, kLanguages[j])); | 543 base::string16 output(IDNToUnicode(idn_cases[i].input, kLanguages[j])); |
| 601 base::string16 expected(idn_cases[i].unicode_allowed[j] ? | 544 base::string16 expected(idn_cases[i].unicode_allowed[j] |
| 602 WideToUTF16(idn_cases[i].unicode_output) : | 545 ? WideToUTF16(idn_cases[i].unicode_output) |
| 603 ASCIIToUTF16(idn_cases[i].input)); | 546 : ASCIIToUTF16(idn_cases[i].input)); |
| 604 AppendLanguagesToOutputs(kLanguages[j], &expected, &output); | 547 AppendLanguagesToOutputs(kLanguages[j], &expected, &output); |
| 605 EXPECT_EQ(expected, output); | 548 EXPECT_EQ(expected, output); |
| 606 } | 549 } |
| 607 } | 550 } |
| 608 } | 551 } |
| 609 | 552 |
| 610 TEST(NetUtilTest, IDNToUnicodeSlow) { | 553 TEST(NetUtilTest, IDNToUnicodeSlow) { |
| 611 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { | 554 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(idn_cases); i++) { |
| 612 for (size_t j = 0; j < arraysize(kLanguages); j++) { | 555 for (size_t j = 0; j < arraysize(kLanguages); j++) { |
| 613 // !(ja || zh-TW,en || ko,ja) -> IDNToUnicodeFast | 556 // !(ja || zh-TW,en || ko,ja) -> IDNToUnicodeFast |
| 614 if (!(j == 3 || j == 17 || j == 18)) | 557 if (!(j == 3 || j == 17 || j == 18)) |
| 615 continue; | 558 continue; |
| 616 base::string16 output(IDNToUnicode(idn_cases[i].input, kLanguages[j])); | 559 base::string16 output(IDNToUnicode(idn_cases[i].input, kLanguages[j])); |
| 617 base::string16 expected(idn_cases[i].unicode_allowed[j] ? | 560 base::string16 expected(idn_cases[i].unicode_allowed[j] |
| 618 WideToUTF16(idn_cases[i].unicode_output) : | 561 ? WideToUTF16(idn_cases[i].unicode_output) |
| 619 ASCIIToUTF16(idn_cases[i].input)); | 562 : ASCIIToUTF16(idn_cases[i].input)); |
| 620 AppendLanguagesToOutputs(kLanguages[j], &expected, &output); | 563 AppendLanguagesToOutputs(kLanguages[j], &expected, &output); |
| 621 EXPECT_EQ(expected, output); | 564 EXPECT_EQ(expected, output); |
| 622 } | 565 } |
| 623 } | 566 } |
| 624 } | 567 } |
| 625 | 568 |
| 626 TEST(NetUtilTest, CompliantHost) { | 569 TEST(NetUtilTest, CompliantHost) { |
| 627 const CompliantHostCase compliant_host_cases[] = { | 570 const CompliantHostCase compliant_host_cases[] = { |
| 628 {"", "", false}, | 571 {"", "", false}, |
| 629 {"a", "", true}, | 572 {"a", "", true}, |
| 630 {"-", "", false}, | 573 {"-", "", false}, |
| 631 {".", "", false}, | 574 {".", "", false}, |
| 632 {"9", "", true}, | 575 {"9", "", true}, |
| 633 {"9a", "", true}, | 576 {"9a", "", true}, |
| 634 {"a.", "", true}, | 577 {"a.", "", true}, |
| 635 {"a.a", "", true}, | 578 {"a.a", "", true}, |
| 636 {"9.a", "", true}, | 579 {"9.a", "", true}, |
| 637 {"a.9", "", true}, | 580 {"a.9", "", true}, |
| 638 {"_9a", "", false}, | 581 {"_9a", "", false}, |
| 639 {"-9a", "", false}, | 582 {"-9a", "", false}, |
| 640 {"-9a", "a", true}, | 583 {"-9a", "a", true}, |
| 641 {"a.a9", "", true}, | 584 {"a.a9", "", true}, |
| 642 {"a.-a9", "", false}, | 585 {"a.-a9", "", false}, |
| 643 {"a+9a", "", false}, | 586 {"a+9a", "", false}, |
| 644 {"-a.a9", "", true}, | 587 {"-a.a9", "", true}, |
| 645 {"1-.a-b", "", true}, | 588 {"1-.a-b", "", true}, |
| 646 {"1_.a-b", "", false}, | 589 {"1_.a-b", "", false}, |
| 647 {"1-2.a_b", "", true}, | 590 {"1-2.a_b", "", true}, |
| 648 {"a.b.c.d.e", "", true}, | 591 {"a.b.c.d.e", "", true}, |
| 649 {"1.2.3.4.5", "", true}, | 592 {"1.2.3.4.5", "", true}, |
| 650 {"1.2.3.4.5.", "", true}, | 593 {"1.2.3.4.5.", "", true}, |
| 651 }; | 594 }; |
| 652 | 595 |
| 653 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(compliant_host_cases); ++i) { | 596 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(compliant_host_cases); ++i) { |
| 654 EXPECT_EQ(compliant_host_cases[i].expected_output, | 597 EXPECT_EQ( |
| 598 compliant_host_cases[i].expected_output, |
| 655 IsCanonicalizedHostCompliant(compliant_host_cases[i].host, | 599 IsCanonicalizedHostCompliant(compliant_host_cases[i].host, |
| 656 compliant_host_cases[i].desired_tld)); | 600 compliant_host_cases[i].desired_tld)); |
| 657 } | 601 } |
| 658 } | 602 } |
| 659 | 603 |
| 660 TEST(NetUtilTest, StripWWW) { | 604 TEST(NetUtilTest, StripWWW) { |
| 661 EXPECT_EQ(base::string16(), StripWWW(base::string16())); | 605 EXPECT_EQ(base::string16(), StripWWW(base::string16())); |
| 662 EXPECT_EQ(base::string16(), StripWWW(ASCIIToUTF16("www."))); | 606 EXPECT_EQ(base::string16(), StripWWW(ASCIIToUTF16("www."))); |
| 663 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("www.blah"))); | 607 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("www.blah"))); |
| 664 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("blah"))); | 608 EXPECT_EQ(ASCIIToUTF16("blah"), StripWWW(ASCIIToUTF16("blah"))); |
| 665 } | 609 } |
| 666 | 610 |
| 667 // This is currently a windows specific function. | 611 // This is currently a windows specific function. |
| 668 #if defined(OS_WIN) | 612 #if defined(OS_WIN) |
| 669 namespace { | 613 namespace { |
| 670 | 614 |
| 671 struct GetDirectoryListingEntryCase { | 615 struct GetDirectoryListingEntryCase { |
| 672 const wchar_t* name; | 616 const wchar_t* name; |
| 673 const char* raw_bytes; | 617 const char* raw_bytes; |
| 674 bool is_dir; | 618 bool is_dir; |
| 675 int64 filesize; | 619 int64 filesize; |
| 676 base::Time time; | 620 base::Time time; |
| 677 const char* expected; | 621 const char* expected; |
| 678 }; | 622 }; |
| 679 | 623 |
| 680 } // namespace | 624 } // namespace |
| 681 TEST(NetUtilTest, GetDirectoryListingEntry) { | 625 TEST(NetUtilTest, GetDirectoryListingEntry) { |
| 682 const GetDirectoryListingEntryCase test_cases[] = { | 626 const GetDirectoryListingEntryCase test_cases[] = { |
| 683 {L"Foo", | 627 {L"Foo", "", false, 10000, base::Time(), |
| 684 "", | 628 "<script>addRow(\"Foo\",\"Foo\",0,\"9.8 kB\",\"\");</script>\n"}, |
| 685 false, | 629 {L"quo\"tes", "", false, 10000, base::Time(), |
| 686 10000, | 630 "<script>addRow(\"quo\\\"tes\",\"quo%22tes\",0,\"9.8 " |
| 687 base::Time(), | 631 "kB\",\"\");</script>" |
| 688 "<script>addRow(\"Foo\",\"Foo\",0,\"9.8 kB\",\"\");</script>\n"}, | 632 "\n"}, |
| 689 {L"quo\"tes", | 633 {L"quo\"tes", "quo\"tes", false, 10000, base::Time(), |
| 690 "", | 634 "<script>addRow(\"quo\\\"tes\",\"quo%22tes\",0,\"9.8 " |
| 691 false, | 635 "kB\",\"\");</script>" |
| 692 10000, | 636 "\n"}, |
| 693 base::Time(), | 637 // U+D55C0 U+AE00. raw_bytes is empty (either a local file with |
| 694 "<script>addRow(\"quo\\\"tes\",\"quo%22tes\",0,\"9.8 kB\",\"\");</script>" | 638 // UTF-8/UTF-16 encoding or a remote file on an ftp server using UTF-8 |
| 695 "\n"}, | 639 {L"\xD55C\xAE00.txt", "", false, 10000, base::Time(), |
| 696 {L"quo\"tes", | 640 "<script>addRow(\"\xED\x95\x9C\xEA\xB8\x80.txt\"," |
| 697 "quo\"tes", | 641 "\"%ED%95%9C%EA%B8%80.txt\",0,\"9.8 kB\",\"\");</script>\n"}, |
| 698 false, | 642 // U+D55C0 U+AE00. raw_bytes is the corresponding EUC-KR sequence: |
| 699 10000, | 643 // a local or remote file in EUC-KR. |
| 700 base::Time(), | 644 {L"\xD55C\xAE00.txt", "\xC7\xD1\xB1\xDB.txt", false, 10000, base::Time(), |
| 701 "<script>addRow(\"quo\\\"tes\",\"quo%22tes\",0,\"9.8 kB\",\"\");</script>" | 645 "<script>addRow(\"\xED\x95\x9C\xEA\xB8\x80.txt\",\"%C7%D1%B1%DB.txt\"" |
| 702 "\n"}, | 646 ",0,\"9.8 kB\",\"\");</script>\n"}, |
| 703 // U+D55C0 U+AE00. raw_bytes is empty (either a local file with | |
| 704 // UTF-8/UTF-16 encoding or a remote file on an ftp server using UTF-8 | |
| 705 {L"\xD55C\xAE00.txt", | |
| 706 "", | |
| 707 false, | |
| 708 10000, | |
| 709 base::Time(), | |
| 710 "<script>addRow(\"\xED\x95\x9C\xEA\xB8\x80.txt\"," | |
| 711 "\"%ED%95%9C%EA%B8%80.txt\",0,\"9.8 kB\",\"\");</script>\n"}, | |
| 712 // U+D55C0 U+AE00. raw_bytes is the corresponding EUC-KR sequence: | |
| 713 // a local or remote file in EUC-KR. | |
| 714 {L"\xD55C\xAE00.txt", | |
| 715 "\xC7\xD1\xB1\xDB.txt", | |
| 716 false, | |
| 717 10000, | |
| 718 base::Time(), | |
| 719 "<script>addRow(\"\xED\x95\x9C\xEA\xB8\x80.txt\",\"%C7%D1%B1%DB.txt\"" | |
| 720 ",0,\"9.8 kB\",\"\");</script>\n"}, | |
| 721 }; | 647 }; |
| 722 | 648 |
| 723 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { | 649 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(test_cases); ++i) { |
| 724 const std::string results = GetDirectoryListingEntry( | 650 const std::string results = |
| 725 WideToUTF16(test_cases[i].name), | 651 GetDirectoryListingEntry(WideToUTF16(test_cases[i].name), |
| 726 test_cases[i].raw_bytes, | 652 test_cases[i].raw_bytes, |
| 727 test_cases[i].is_dir, | 653 test_cases[i].is_dir, |
| 728 test_cases[i].filesize, | 654 test_cases[i].filesize, |
| 729 test_cases[i].time); | 655 test_cases[i].time); |
| 730 EXPECT_EQ(test_cases[i].expected, results); | 656 EXPECT_EQ(test_cases[i].expected, results); |
| 731 } | 657 } |
| 732 } | 658 } |
| 733 | 659 |
| 734 #endif | 660 #endif |
| 735 | 661 |
| 736 TEST(NetUtilTest, ParseHostAndPort) { | 662 TEST(NetUtilTest, ParseHostAndPort) { |
| 737 const struct { | 663 const struct { |
| 738 const char* input; | 664 const char* input; |
| 739 bool success; | 665 bool success; |
| 740 const char* expected_host; | 666 const char* expected_host; |
| 741 int expected_port; | 667 int expected_port; |
| 742 } tests[] = { | 668 } tests[] = {// Valid inputs: |
| 743 // Valid inputs: | 669 {"foo:10", true, "foo", 10}, |
| 744 {"foo:10", true, "foo", 10}, | 670 {"foo", true, "foo", -1}, |
| 745 {"foo", true, "foo", -1}, | 671 { |
| 746 { | 672 "[1080:0:0:0:8:800:200C:4171]:11", true, |
| 747 "[1080:0:0:0:8:800:200C:4171]:11", | 673 "[1080:0:0:0:8:800:200C:4171]", 11, |
| 748 true, | 674 }, |
| 749 "[1080:0:0:0:8:800:200C:4171]", | 675 // Invalid inputs: |
| 750 11, | 676 {"foo:bar", false, "", -1}, |
| 751 }, | 677 {"foo:", false, "", -1}, |
| 752 // Invalid inputs: | 678 {":", false, "", -1}, |
| 753 {"foo:bar", false, "", -1}, | 679 {":80", false, "", -1}, |
| 754 {"foo:", false, "", -1}, | 680 {"", false, "", -1}, |
| 755 {":", false, "", -1}, | 681 {"porttoolong:300000", false, "", -1}, |
| 756 {":80", false, "", -1}, | 682 {"usrname@host", false, "", -1}, |
| 757 {"", false, "", -1}, | 683 {"usrname:password@host", false, "", -1}, |
| 758 {"porttoolong:300000", false, "", -1}, | 684 {":password@host", false, "", -1}, |
| 759 {"usrname@host", false, "", -1}, | 685 {":password@host:80", false, "", -1}, |
| 760 {"usrname:password@host", false, "", -1}, | 686 {":password@host", false, "", -1}, |
| 761 {":password@host", false, "", -1}, | 687 {"@host", false, "", -1}, |
| 762 {":password@host:80", false, "", -1}, | 688 }; |
| 763 {":password@host", false, "", -1}, | |
| 764 {"@host", false, "", -1}, | |
| 765 }; | |
| 766 | 689 |
| 767 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 690 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 768 std::string host; | 691 std::string host; |
| 769 int port; | 692 int port; |
| 770 bool ok = ParseHostAndPort(tests[i].input, &host, &port); | 693 bool ok = ParseHostAndPort(tests[i].input, &host, &port); |
| 771 | 694 |
| 772 EXPECT_EQ(tests[i].success, ok); | 695 EXPECT_EQ(tests[i].success, ok); |
| 773 | 696 |
| 774 if (tests[i].success) { | 697 if (tests[i].success) { |
| 775 EXPECT_EQ(tests[i].expected_host, host); | 698 EXPECT_EQ(tests[i].expected_host, host); |
| 776 EXPECT_EQ(tests[i].expected_port, port); | 699 EXPECT_EQ(tests[i].expected_port, port); |
| 777 } | 700 } |
| 778 } | 701 } |
| 779 } | 702 } |
| 780 | 703 |
| 781 TEST(NetUtilTest, GetHostAndPort) { | 704 TEST(NetUtilTest, GetHostAndPort) { |
| 782 const struct { | 705 const struct { |
| 783 GURL url; | 706 GURL url; |
| 784 const char* expected_host_and_port; | 707 const char* expected_host_and_port; |
| 785 } tests[] = { | 708 } tests[] = { |
| 786 { GURL("http://www.foo.com/x"), "www.foo.com:80"}, | 709 {GURL("http://www.foo.com/x"), "www.foo.com:80"}, |
| 787 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, | 710 {GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, |
| 788 | 711 |
| 789 // For IPv6 literals should always include the brackets. | 712 // For IPv6 literals should always include the brackets. |
| 790 { GURL("http://[1::2]/x"), "[1::2]:80"}, | 713 {GURL("http://[1::2]/x"), "[1::2]:80"}, |
| 791 { GURL("http://[::a]:33/x"), "[::a]:33"}, | 714 {GURL("http://[::a]:33/x"), "[::a]:33"}, |
| 792 }; | 715 }; |
| 793 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 716 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 794 std::string host_and_port = GetHostAndPort(tests[i].url); | 717 std::string host_and_port = GetHostAndPort(tests[i].url); |
| 795 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); | 718 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); |
| 796 } | 719 } |
| 797 } | 720 } |
| 798 | 721 |
| 799 TEST(NetUtilTest, GetHostAndOptionalPort) { | 722 TEST(NetUtilTest, GetHostAndOptionalPort) { |
| 800 const struct { | 723 const struct { |
| 801 GURL url; | 724 GURL url; |
| 802 const char* expected_host_and_port; | 725 const char* expected_host_and_port; |
| 803 } tests[] = { | 726 } tests[] = { |
| 804 { GURL("http://www.foo.com/x"), "www.foo.com"}, | 727 {GURL("http://www.foo.com/x"), "www.foo.com"}, |
| 805 { GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, | 728 {GURL("http://www.foo.com:21/x"), "www.foo.com:21"}, |
| 806 | 729 |
| 807 // For IPv6 literals should always include the brackets. | 730 // For IPv6 literals should always include the brackets. |
| 808 { GURL("http://[1::2]/x"), "[1::2]"}, | 731 {GURL("http://[1::2]/x"), "[1::2]"}, |
| 809 { GURL("http://[::a]:33/x"), "[::a]:33"}, | 732 {GURL("http://[::a]:33/x"), "[::a]:33"}, |
| 810 }; | 733 }; |
| 811 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 734 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 812 std::string host_and_port = GetHostAndOptionalPort(tests[i].url); | 735 std::string host_and_port = GetHostAndOptionalPort(tests[i].url); |
| 813 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); | 736 EXPECT_EQ(std::string(tests[i].expected_host_and_port), host_and_port); |
| 814 } | 737 } |
| 815 } | 738 } |
| 816 | 739 |
| 817 TEST(NetUtilTest, IPAddressToString) { | 740 TEST(NetUtilTest, IPAddressToString) { |
| 818 uint8 addr1[4] = {0, 0, 0, 0}; | 741 uint8 addr1[4] = {0, 0, 0, 0}; |
| 819 EXPECT_EQ("0.0.0.0", IPAddressToString(addr1, sizeof(addr1))); | 742 EXPECT_EQ("0.0.0.0", IPAddressToString(addr1, sizeof(addr1))); |
| 820 | 743 |
| (...skipping 15 matching lines...) Expand all Loading... |
| 836 uint8 addr3[16] = {0xFE, 0xDC, 0xBA, 0x98}; | 759 uint8 addr3[16] = {0xFE, 0xDC, 0xBA, 0x98}; |
| 837 EXPECT_EQ("[fedc:ba98::]:8080", | 760 EXPECT_EQ("[fedc:ba98::]:8080", |
| 838 IPAddressToStringWithPort(addr3, sizeof(addr3), 8080)); | 761 IPAddressToStringWithPort(addr3, sizeof(addr3), 8080)); |
| 839 } | 762 } |
| 840 | 763 |
| 841 TEST(NetUtilTest, NetAddressToString_IPv4) { | 764 TEST(NetUtilTest, NetAddressToString_IPv4) { |
| 842 const struct { | 765 const struct { |
| 843 uint8 addr[4]; | 766 uint8 addr[4]; |
| 844 const char* result; | 767 const char* result; |
| 845 } tests[] = { | 768 } tests[] = { |
| 846 {{0, 0, 0, 0}, "0.0.0.0"}, | 769 {{0, 0, 0, 0}, "0.0.0.0"}, |
| 847 {{127, 0, 0, 1}, "127.0.0.1"}, | 770 {{127, 0, 0, 1}, "127.0.0.1"}, |
| 848 {{192, 168, 0, 1}, "192.168.0.1"}, | 771 {{192, 168, 0, 1}, "192.168.0.1"}, |
| 849 }; | 772 }; |
| 850 | 773 |
| 851 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 774 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 852 SockaddrStorage storage; | 775 SockaddrStorage storage; |
| 853 MakeIPv4Address(tests[i].addr, 80, &storage); | 776 MakeIPv4Address(tests[i].addr, 80, &storage); |
| 854 std::string result = NetAddressToString(storage.addr, storage.addr_len); | 777 std::string result = NetAddressToString(storage.addr, storage.addr_len); |
| 855 EXPECT_EQ(std::string(tests[i].result), result); | 778 EXPECT_EQ(std::string(tests[i].result), result); |
| 856 } | 779 } |
| 857 } | 780 } |
| 858 | 781 |
| 859 TEST(NetUtilTest, NetAddressToString_IPv6) { | 782 TEST(NetUtilTest, NetAddressToString_IPv6) { |
| 860 const struct { | 783 const struct { |
| 861 uint8 addr[16]; | 784 uint8 addr[16]; |
| 862 const char* result; | 785 const char* result; |
| 863 } tests[] = { | 786 } tests[] = { |
| 864 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, | 787 {{0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, |
| 865 0x98, 0x76, 0x54, 0x32, 0x10}, | 788 0x98, 0x76, 0x54, 0x32, 0x10}, |
| 866 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, | 789 "fedc:ba98:7654:3210:fedc:ba98:7654:3210"}, |
| 867 }; | 790 }; |
| 868 | 791 |
| 869 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 792 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 870 SockaddrStorage storage; | 793 SockaddrStorage storage; |
| 871 MakeIPv6Address(tests[i].addr, 80, &storage); | 794 MakeIPv6Address(tests[i].addr, 80, &storage); |
| 872 EXPECT_EQ(std::string(tests[i].result), | 795 EXPECT_EQ(std::string(tests[i].result), |
| 873 NetAddressToString(storage.addr, storage.addr_len)); | 796 NetAddressToString(storage.addr, storage.addr_len)); |
| 874 } | 797 } |
| 875 } | 798 } |
| 876 | 799 |
| 877 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) { | 800 TEST(NetUtilTest, NetAddressToStringWithPort_IPv4) { |
| 878 uint8 addr[] = {127, 0, 0, 1}; | 801 uint8 addr[] = {127, 0, 0, 1}; |
| 879 SockaddrStorage storage; | 802 SockaddrStorage storage; |
| 880 MakeIPv4Address(addr, 166, &storage); | 803 MakeIPv4Address(addr, 166, &storage); |
| 881 std::string result = NetAddressToStringWithPort(storage.addr, | 804 std::string result = |
| 882 storage.addr_len); | 805 NetAddressToStringWithPort(storage.addr, storage.addr_len); |
| 883 EXPECT_EQ("127.0.0.1:166", result); | 806 EXPECT_EQ("127.0.0.1:166", result); |
| 884 } | 807 } |
| 885 | 808 |
| 886 TEST(NetUtilTest, NetAddressToStringWithPort_IPv6) { | 809 TEST(NetUtilTest, NetAddressToStringWithPort_IPv6) { |
| 887 uint8 addr[] = { | 810 uint8 addr[] = {0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, |
| 888 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10, 0xFE, 0xDC, 0xBA, | 811 0xFE, 0xDC, 0xBA, 0x98, 0x76, 0x54, 0x32, 0x10}; |
| 889 0x98, 0x76, 0x54, 0x32, 0x10 | |
| 890 }; | |
| 891 SockaddrStorage storage; | 812 SockaddrStorage storage; |
| 892 MakeIPv6Address(addr, 361, &storage); | 813 MakeIPv6Address(addr, 361, &storage); |
| 893 std::string result = NetAddressToStringWithPort(storage.addr, | 814 std::string result = |
| 894 storage.addr_len); | 815 NetAddressToStringWithPort(storage.addr, storage.addr_len); |
| 895 | 816 |
| 896 // May fail on systems that don't support IPv6. | 817 // May fail on systems that don't support IPv6. |
| 897 if (!result.empty()) | 818 if (!result.empty()) |
| 898 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result); | 819 EXPECT_EQ("[fedc:ba98:7654:3210:fedc:ba98:7654:3210]:361", result); |
| 899 } | 820 } |
| 900 | 821 |
| 901 TEST(NetUtilTest, GetHostName) { | 822 TEST(NetUtilTest, GetHostName) { |
| 902 // We can't check the result of GetHostName() directly, since the result | 823 // We can't check the result of GetHostName() directly, since the result |
| 903 // will differ across machines. Our goal here is to simply exercise the | 824 // will differ across machines. Our goal here is to simply exercise the |
| 904 // code path, and check that things "look about right". | 825 // code path, and check that things "look about right". |
| 905 std::string hostname = GetHostName(); | 826 std::string hostname = GetHostName(); |
| 906 EXPECT_FALSE(hostname.empty()); | 827 EXPECT_FALSE(hostname.empty()); |
| 907 } | 828 } |
| 908 | 829 |
| 909 TEST(NetUtilTest, FormatUrl) { | 830 TEST(NetUtilTest, FormatUrl) { |
| 910 FormatUrlTypes default_format_type = kFormatUrlOmitUsernamePassword; | 831 FormatUrlTypes default_format_type = kFormatUrlOmitUsernamePassword; |
| 911 const UrlTestData tests[] = { | 832 const UrlTestData tests[] = { |
| 912 {"Empty URL", "", "", default_format_type, UnescapeRule::NORMAL, L"", 0}, | 833 {"Empty URL", "", "", default_format_type, UnescapeRule::NORMAL, L"", 0}, |
| 834 {"Simple URL", "http://www.google.com/", "", default_format_type, |
| 835 UnescapeRule::NORMAL, L"http://www.google.com/", 7}, |
| 836 {"With a port number and a reference", |
| 837 "http://www.google.com:8080/#\xE3\x82\xB0", "", default_format_type, |
| 838 UnescapeRule::NORMAL, L"http://www.google.com:8080/#\x30B0", 7}, |
| 913 | 839 |
| 914 {"Simple URL", | 840 // -------- IDN tests -------- |
| 915 "http://www.google.com/", "", default_format_type, UnescapeRule::NORMAL, | 841 {"Japanese IDN with ja", "http://xn--l8jvb1ey91xtjb.jp", "ja", |
| 916 L"http://www.google.com/", 7}, | 842 default_format_type, UnescapeRule::NORMAL, |
| 843 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, |
| 844 {"Japanese IDN with en", "http://xn--l8jvb1ey91xtjb.jp", "en", |
| 845 default_format_type, UnescapeRule::NORMAL, |
| 846 L"http://xn--l8jvb1ey91xtjb.jp/", 7}, |
| 847 {"Japanese IDN without any languages", "http://xn--l8jvb1ey91xtjb.jp", "", |
| 848 default_format_type, UnescapeRule::NORMAL, |
| 849 // Single script is safe for empty languages. |
| 850 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, |
| 851 {"mailto: with Japanese IDN", "mailto:foo@xn--l8jvb1ey91xtjb.jp", "ja", |
| 852 default_format_type, UnescapeRule::NORMAL, |
| 853 // GURL doesn't assume an email address's domain part as a host name. |
| 854 L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7}, |
| 855 {"file: with Japanese IDN", "file://xn--l8jvb1ey91xtjb.jp/config.sys", |
| 856 "ja", default_format_type, UnescapeRule::NORMAL, |
| 857 L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7}, |
| 858 {"ftp: with Japanese IDN", "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", "ja", |
| 859 default_format_type, UnescapeRule::NORMAL, |
| 860 L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6}, |
| 917 | 861 |
| 918 {"With a port number and a reference", | 862 // -------- omit_username_password flag tests -------- |
| 919 "http://www.google.com:8080/#\xE3\x82\xB0", "", default_format_type, | 863 {"With username and password, omit_username_password=false", |
| 920 UnescapeRule::NORMAL, | 864 "http://user:passwd@example.com/foo", "", kFormatUrlOmitNothing, |
| 921 L"http://www.google.com:8080/#\x30B0", 7}, | 865 UnescapeRule::NORMAL, L"http://user:passwd@example.com/foo", 19}, |
| 866 {"With username and password, omit_username_password=true", |
| 867 "http://user:passwd@example.com/foo", "", default_format_type, |
| 868 UnescapeRule::NORMAL, L"http://example.com/foo", 7}, |
| 869 {"With username and no password", "http://user@example.com/foo", "", |
| 870 default_format_type, UnescapeRule::NORMAL, L"http://example.com/foo", 7}, |
| 871 {"Just '@' without username and password", "http://@example.com/foo", "", |
| 872 default_format_type, UnescapeRule::NORMAL, L"http://example.com/foo", 7}, |
| 922 | 873 |
| 923 // -------- IDN tests -------- | 874 // GURL doesn't think local-part of an email address is username for URL. |
| 924 {"Japanese IDN with ja", | 875 {"mailto:, omit_username_password=true", "mailto:foo@example.com", "", |
| 925 "http://xn--l8jvb1ey91xtjb.jp", "ja", default_format_type, | 876 default_format_type, UnescapeRule::NORMAL, L"mailto:foo@example.com", 7}, |
| 926 UnescapeRule::NORMAL, L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, | |
| 927 | 877 |
| 928 {"Japanese IDN with en", | 878 // -------- unescape flag tests -------- |
| 929 "http://xn--l8jvb1ey91xtjb.jp", "en", default_format_type, | 879 {"Do not unescape", |
| 930 UnescapeRule::NORMAL, L"http://xn--l8jvb1ey91xtjb.jp/", 7}, | 880 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" |
| 881 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" |
| 882 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", |
| 883 "en", default_format_type, UnescapeRule::NONE, |
| 884 // GURL parses %-encoded hostnames into Punycode. |
| 885 L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" |
| 886 L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", |
| 887 7}, |
| 888 {"Unescape normally", |
| 889 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" |
| 890 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" |
| 891 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", |
| 892 "en", default_format_type, UnescapeRule::NORMAL, |
| 893 L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB" |
| 894 L"?q=\x30B0\x30FC\x30B0\x30EB", |
| 895 7}, |
| 896 {"Unescape normally with BiDi control character", |
| 897 "http://example.com/%E2%80%AEabc?q=%E2%80%8Fxy", "en", |
| 898 default_format_type, UnescapeRule::NORMAL, |
| 899 L"http://example.com/%E2%80%AEabc?q=%E2%80%8Fxy", 7}, |
| 900 {"Unescape normally including unescape spaces", |
| 901 "http://www.google.com/search?q=Hello%20World", "en", |
| 902 default_format_type, UnescapeRule::SPACES, |
| 903 L"http://www.google.com/search?q=Hello World", 7}, |
| 931 | 904 |
| 932 {"Japanese IDN without any languages", | 905 /* |
| 933 "http://xn--l8jvb1ey91xtjb.jp", "", default_format_type, | 906 {"unescape=true with some special characters", |
| 934 UnescapeRule::NORMAL, | 907 "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", "", |
| 935 // Single script is safe for empty languages. | 908 kFormatUrlOmitNothing, UnescapeRule::NORMAL, |
| 936 L"http://\x671d\x65e5\x3042\x3055\x3072.jp/", 7}, | 909 L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25}, |
| 910 */ |
| 911 // Disabled: the resultant URL becomes "...user%253A:%2540passwd...". |
| 937 | 912 |
| 938 {"mailto: with Japanese IDN", | 913 // -------- omit http: -------- |
| 939 "mailto:foo@xn--l8jvb1ey91xtjb.jp", "ja", default_format_type, | 914 {"omit http with user name", "http://user@example.com/foo", "", |
| 940 UnescapeRule::NORMAL, | 915 kFormatUrlOmitAll, UnescapeRule::NORMAL, L"example.com/foo", 0}, |
| 941 // GURL doesn't assume an email address's domain part as a host name. | 916 {"omit http", "http://www.google.com/", "en", kFormatUrlOmitHTTP, |
| 942 L"mailto:foo@xn--l8jvb1ey91xtjb.jp", 7}, | 917 UnescapeRule::NORMAL, L"www.google.com/", 0}, |
| 918 {"omit http with https", "https://www.google.com/", "en", |
| 919 kFormatUrlOmitHTTP, UnescapeRule::NORMAL, L"https://www.google.com/", 8}, |
| 920 {"omit http starts with ftp.", "http://ftp.google.com/", "en", |
| 921 kFormatUrlOmitHTTP, UnescapeRule::NORMAL, L"http://ftp.google.com/", 7}, |
| 943 | 922 |
| 944 {"file: with Japanese IDN", | 923 // -------- omit trailing slash on bare hostname -------- |
| 945 "file://xn--l8jvb1ey91xtjb.jp/config.sys", "ja", default_format_type, | 924 {"omit slash when it's the entire path", "http://www.google.com/", "en", |
| 946 UnescapeRule::NORMAL, | 925 kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, |
| 947 L"file://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 7}, | 926 L"http://www.google.com", 7}, |
| 927 {"omit slash when there's a ref", "http://www.google.com/#ref", "en", |
| 928 kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, |
| 929 L"http://www.google.com/#ref", 7}, |
| 930 {"omit slash when there's a query", "http://www.google.com/?", "en", |
| 931 kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, |
| 932 L"http://www.google.com/?", 7}, |
| 933 {"omit slash when it's not the entire path", "http://www.google.com/foo", |
| 934 "en", kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, |
| 935 L"http://www.google.com/foo", 7}, |
| 936 {"omit slash for nonstandard URLs", "data:/", "en", |
| 937 kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, |
| 938 L"data:/", 5}, |
| 939 {"omit slash for file URLs", "file:///", "en", |
| 940 kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, |
| 941 L"file:///", 7}, |
| 948 | 942 |
| 949 {"ftp: with Japanese IDN", | 943 // -------- view-source: -------- |
| 950 "ftp://xn--l8jvb1ey91xtjb.jp/config.sys", "ja", default_format_type, | 944 {"view-source", "view-source:http://xn--qcka1pmc.jp/", "ja", |
| 951 UnescapeRule::NORMAL, | 945 default_format_type, UnescapeRule::NORMAL, |
| 952 L"ftp://\x671d\x65e5\x3042\x3055\x3072.jp/config.sys", 6}, | 946 L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", 19}, |
| 947 {"view-source of view-source", |
| 948 "view-source:view-source:http://xn--qcka1pmc.jp/", "ja", |
| 949 default_format_type, UnescapeRule::NORMAL, |
| 950 L"view-source:view-source:http://xn--qcka1pmc.jp/", 12}, |
| 953 | 951 |
| 954 // -------- omit_username_password flag tests -------- | 952 // view-source should omit http and trailing slash where non-view-source |
| 955 {"With username and password, omit_username_password=false", | 953 // would. |
| 956 "http://user:passwd@example.com/foo", "", | 954 {"view-source omit http", "view-source:http://a.b/c", "en", |
| 957 kFormatUrlOmitNothing, UnescapeRule::NORMAL, | 955 kFormatUrlOmitAll, UnescapeRule::NORMAL, L"view-source:a.b/c", 12}, |
| 958 L"http://user:passwd@example.com/foo", 19}, | 956 {"view-source omit http starts with ftp.", "view-source:http://ftp.b/c", |
| 959 | 957 "en", kFormatUrlOmitAll, UnescapeRule::NORMAL, |
| 960 {"With username and password, omit_username_password=true", | 958 L"view-source:http://ftp.b/c", 19}, |
| 961 "http://user:passwd@example.com/foo", "", default_format_type, | 959 {"view-source omit slash when it's the entire path", |
| 962 UnescapeRule::NORMAL, L"http://example.com/foo", 7}, | 960 "view-source:http://a.b/", "en", kFormatUrlOmitAll, UnescapeRule::NORMAL, |
| 963 | 961 L"view-source:a.b", 12}, |
| 964 {"With username and no password", | |
| 965 "http://user@example.com/foo", "", default_format_type, | |
| 966 UnescapeRule::NORMAL, L"http://example.com/foo", 7}, | |
| 967 | |
| 968 {"Just '@' without username and password", | |
| 969 "http://@example.com/foo", "", default_format_type, UnescapeRule::NORMAL, | |
| 970 L"http://example.com/foo", 7}, | |
| 971 | |
| 972 // GURL doesn't think local-part of an email address is username for URL. | |
| 973 {"mailto:, omit_username_password=true", | |
| 974 "mailto:foo@example.com", "", default_format_type, UnescapeRule::NORMAL, | |
| 975 L"mailto:foo@example.com", 7}, | |
| 976 | |
| 977 // -------- unescape flag tests -------- | |
| 978 {"Do not unescape", | |
| 979 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" | |
| 980 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" | |
| 981 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", "en", default_format_type, | |
| 982 UnescapeRule::NONE, | |
| 983 // GURL parses %-encoded hostnames into Punycode. | |
| 984 L"http://xn--qcka1pmc.jp/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" | |
| 985 L"?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", 7}, | |
| 986 | |
| 987 {"Unescape normally", | |
| 988 "http://%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB.jp/" | |
| 989 "%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB" | |
| 990 "?q=%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", "en", default_format_type, | |
| 991 UnescapeRule::NORMAL, | |
| 992 L"http://xn--qcka1pmc.jp/\x30B0\x30FC\x30B0\x30EB" | |
| 993 L"?q=\x30B0\x30FC\x30B0\x30EB", 7}, | |
| 994 | |
| 995 {"Unescape normally with BiDi control character", | |
| 996 "http://example.com/%E2%80%AEabc?q=%E2%80%8Fxy", "en", default_format_type, | |
| 997 UnescapeRule::NORMAL, L"http://example.com/%E2%80%AEabc?q=%E2%80%8Fxy", 7}, | |
| 998 | |
| 999 {"Unescape normally including unescape spaces", | |
| 1000 "http://www.google.com/search?q=Hello%20World", "en", default_format_type, | |
| 1001 UnescapeRule::SPACES, L"http://www.google.com/search?q=Hello World", 7}, | |
| 1002 | |
| 1003 /* | |
| 1004 {"unescape=true with some special characters", | |
| 1005 "http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", "", | |
| 1006 kFormatUrlOmitNothing, UnescapeRule::NORMAL, | |
| 1007 L"http://user%3A:%40passwd@example.com/foo%3Fbar?q=b%26z", 25}, | |
| 1008 */ | |
| 1009 // Disabled: the resultant URL becomes "...user%253A:%2540passwd...". | |
| 1010 | |
| 1011 // -------- omit http: -------- | |
| 1012 {"omit http with user name", | |
| 1013 "http://user@example.com/foo", "", kFormatUrlOmitAll, | |
| 1014 UnescapeRule::NORMAL, L"example.com/foo", 0}, | |
| 1015 | |
| 1016 {"omit http", | |
| 1017 "http://www.google.com/", "en", kFormatUrlOmitHTTP, | |
| 1018 UnescapeRule::NORMAL, L"www.google.com/", | |
| 1019 0}, | |
| 1020 | |
| 1021 {"omit http with https", | |
| 1022 "https://www.google.com/", "en", kFormatUrlOmitHTTP, | |
| 1023 UnescapeRule::NORMAL, L"https://www.google.com/", | |
| 1024 8}, | |
| 1025 | |
| 1026 {"omit http starts with ftp.", | |
| 1027 "http://ftp.google.com/", "en", kFormatUrlOmitHTTP, | |
| 1028 UnescapeRule::NORMAL, L"http://ftp.google.com/", | |
| 1029 7}, | |
| 1030 | |
| 1031 // -------- omit trailing slash on bare hostname -------- | |
| 1032 {"omit slash when it's the entire path", | |
| 1033 "http://www.google.com/", "en", | |
| 1034 kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, | |
| 1035 L"http://www.google.com", 7}, | |
| 1036 {"omit slash when there's a ref", | |
| 1037 "http://www.google.com/#ref", "en", | |
| 1038 kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, | |
| 1039 L"http://www.google.com/#ref", 7}, | |
| 1040 {"omit slash when there's a query", | |
| 1041 "http://www.google.com/?", "en", | |
| 1042 kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, | |
| 1043 L"http://www.google.com/?", 7}, | |
| 1044 {"omit slash when it's not the entire path", | |
| 1045 "http://www.google.com/foo", "en", | |
| 1046 kFormatUrlOmitTrailingSlashOnBareHostname, UnescapeRule::NORMAL, | |
| 1047 L"http://www.google.com/foo", 7}, | |
| 1048 {"omit slash for nonstandard URLs", | |
| 1049 "data:/", "en", kFormatUrlOmitTrailingSlashOnBareHostname, | |
| 1050 UnescapeRule::NORMAL, L"data:/", 5}, | |
| 1051 {"omit slash for file URLs", | |
| 1052 "file:///", "en", kFormatUrlOmitTrailingSlashOnBareHostname, | |
| 1053 UnescapeRule::NORMAL, L"file:///", 7}, | |
| 1054 | |
| 1055 // -------- view-source: -------- | |
| 1056 {"view-source", | |
| 1057 "view-source:http://xn--qcka1pmc.jp/", "ja", default_format_type, | |
| 1058 UnescapeRule::NORMAL, L"view-source:http://\x30B0\x30FC\x30B0\x30EB.jp/", | |
| 1059 19}, | |
| 1060 | |
| 1061 {"view-source of view-source", | |
| 1062 "view-source:view-source:http://xn--qcka1pmc.jp/", "ja", | |
| 1063 default_format_type, UnescapeRule::NORMAL, | |
| 1064 L"view-source:view-source:http://xn--qcka1pmc.jp/", 12}, | |
| 1065 | |
| 1066 // view-source should omit http and trailing slash where non-view-source | |
| 1067 // would. | |
| 1068 {"view-source omit http", | |
| 1069 "view-source:http://a.b/c", "en", kFormatUrlOmitAll, | |
| 1070 UnescapeRule::NORMAL, L"view-source:a.b/c", | |
| 1071 12}, | |
| 1072 {"view-source omit http starts with ftp.", | |
| 1073 "view-source:http://ftp.b/c", "en", kFormatUrlOmitAll, | |
| 1074 UnescapeRule::NORMAL, L"view-source:http://ftp.b/c", | |
| 1075 19}, | |
| 1076 {"view-source omit slash when it's the entire path", | |
| 1077 "view-source:http://a.b/", "en", kFormatUrlOmitAll, | |
| 1078 UnescapeRule::NORMAL, L"view-source:a.b", | |
| 1079 12}, | |
| 1080 }; | 962 }; |
| 1081 | 963 |
| 1082 for (size_t i = 0; i < arraysize(tests); ++i) { | 964 for (size_t i = 0; i < arraysize(tests); ++i) { |
| 1083 size_t prefix_len; | 965 size_t prefix_len; |
| 1084 base::string16 formatted = FormatUrl( | 966 base::string16 formatted = FormatUrl(GURL(tests[i].input), |
| 1085 GURL(tests[i].input), tests[i].languages, tests[i].format_types, | 967 tests[i].languages, |
| 1086 tests[i].escape_rules, NULL, &prefix_len, NULL); | 968 tests[i].format_types, |
| 969 tests[i].escape_rules, |
| 970 NULL, |
| 971 &prefix_len, |
| 972 NULL); |
| 1087 EXPECT_EQ(WideToUTF16(tests[i].output), formatted) << tests[i].description; | 973 EXPECT_EQ(WideToUTF16(tests[i].output), formatted) << tests[i].description; |
| 1088 EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description; | 974 EXPECT_EQ(tests[i].prefix_len, prefix_len) << tests[i].description; |
| 1089 } | 975 } |
| 1090 } | 976 } |
| 1091 | 977 |
| 1092 TEST(NetUtilTest, FormatUrlParsed) { | 978 TEST(NetUtilTest, FormatUrlParsed) { |
| 1093 // No unescape case. | 979 // No unescape case. |
| 1094 url::Parsed parsed; | 980 url::Parsed parsed; |
| 1095 base::string16 formatted = FormatUrl( | 981 base::string16 formatted = |
| 1096 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" | 982 FormatUrl(GURL( |
| 1097 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), | 983 "http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" |
| 1098 "ja", kFormatUrlOmitNothing, UnescapeRule::NONE, &parsed, NULL, | 984 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), |
| 1099 NULL); | 985 "ja", |
| 986 kFormatUrlOmitNothing, |
| 987 UnescapeRule::NONE, |
| 988 &parsed, |
| 989 NULL, |
| 990 NULL); |
| 1100 EXPECT_EQ(WideToUTF16( | 991 EXPECT_EQ(WideToUTF16( |
| 1101 L"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080" | 992 L"http://%E3%82%B0:%E3%83%BC@\x30B0\x30FC\x30B0\x30EB.jp:8080" |
| 1102 L"/%E3%82%B0/?q=%E3%82%B0#\x30B0"), formatted); | 993 L"/%E3%82%B0/?q=%E3%82%B0#\x30B0"), |
| 994 formatted); |
| 1103 EXPECT_EQ(WideToUTF16(L"%E3%82%B0"), | 995 EXPECT_EQ(WideToUTF16(L"%E3%82%B0"), |
| 1104 formatted.substr(parsed.username.begin, parsed.username.len)); | 996 formatted.substr(parsed.username.begin, parsed.username.len)); |
| 1105 EXPECT_EQ(WideToUTF16(L"%E3%83%BC"), | 997 EXPECT_EQ(WideToUTF16(L"%E3%83%BC"), |
| 1106 formatted.substr(parsed.password.begin, parsed.password.len)); | 998 formatted.substr(parsed.password.begin, parsed.password.len)); |
| 1107 EXPECT_EQ(WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB.jp"), | 999 EXPECT_EQ(WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB.jp"), |
| 1108 formatted.substr(parsed.host.begin, parsed.host.len)); | 1000 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1109 EXPECT_EQ(WideToUTF16(L"8080"), | 1001 EXPECT_EQ(WideToUTF16(L"8080"), |
| 1110 formatted.substr(parsed.port.begin, parsed.port.len)); | 1002 formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1111 EXPECT_EQ(WideToUTF16(L"/%E3%82%B0/"), | 1003 EXPECT_EQ(WideToUTF16(L"/%E3%82%B0/"), |
| 1112 formatted.substr(parsed.path.begin, parsed.path.len)); | 1004 formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1113 EXPECT_EQ(WideToUTF16(L"q=%E3%82%B0"), | 1005 EXPECT_EQ(WideToUTF16(L"q=%E3%82%B0"), |
| 1114 formatted.substr(parsed.query.begin, parsed.query.len)); | 1006 formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1115 EXPECT_EQ(WideToUTF16(L"\x30B0"), | 1007 EXPECT_EQ(WideToUTF16(L"\x30B0"), |
| 1116 formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1008 formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1117 | 1009 |
| 1118 // Unescape case. | 1010 // Unescape case. |
| 1119 formatted = FormatUrl( | 1011 formatted = |
| 1120 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" | 1012 FormatUrl(GURL( |
| 1121 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), | 1013 "http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" |
| 1122 "ja", kFormatUrlOmitNothing, UnescapeRule::NORMAL, &parsed, NULL, | 1014 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), |
| 1123 NULL); | 1015 "ja", |
| 1124 EXPECT_EQ(WideToUTF16(L"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080" | 1016 kFormatUrlOmitNothing, |
| 1125 L"/\x30B0/?q=\x30B0#\x30B0"), formatted); | 1017 UnescapeRule::NORMAL, |
| 1018 &parsed, |
| 1019 NULL, |
| 1020 NULL); |
| 1021 EXPECT_EQ(WideToUTF16( |
| 1022 L"http://\x30B0:\x30FC@\x30B0\x30FC\x30B0\x30EB.jp:8080" |
| 1023 L"/\x30B0/?q=\x30B0#\x30B0"), |
| 1024 formatted); |
| 1126 EXPECT_EQ(WideToUTF16(L"\x30B0"), | 1025 EXPECT_EQ(WideToUTF16(L"\x30B0"), |
| 1127 formatted.substr(parsed.username.begin, parsed.username.len)); | 1026 formatted.substr(parsed.username.begin, parsed.username.len)); |
| 1128 EXPECT_EQ(WideToUTF16(L"\x30FC"), | 1027 EXPECT_EQ(WideToUTF16(L"\x30FC"), |
| 1129 formatted.substr(parsed.password.begin, parsed.password.len)); | 1028 formatted.substr(parsed.password.begin, parsed.password.len)); |
| 1130 EXPECT_EQ(WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB.jp"), | 1029 EXPECT_EQ(WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB.jp"), |
| 1131 formatted.substr(parsed.host.begin, parsed.host.len)); | 1030 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1132 EXPECT_EQ(WideToUTF16(L"8080"), | 1031 EXPECT_EQ(WideToUTF16(L"8080"), |
| 1133 formatted.substr(parsed.port.begin, parsed.port.len)); | 1032 formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1134 EXPECT_EQ(WideToUTF16(L"/\x30B0/"), | 1033 EXPECT_EQ(WideToUTF16(L"/\x30B0/"), |
| 1135 formatted.substr(parsed.path.begin, parsed.path.len)); | 1034 formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1136 EXPECT_EQ(WideToUTF16(L"q=\x30B0"), | 1035 EXPECT_EQ(WideToUTF16(L"q=\x30B0"), |
| 1137 formatted.substr(parsed.query.begin, parsed.query.len)); | 1036 formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1138 EXPECT_EQ(WideToUTF16(L"\x30B0"), | 1037 EXPECT_EQ(WideToUTF16(L"\x30B0"), |
| 1139 formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1038 formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1140 | 1039 |
| 1141 // Omit_username_password + unescape case. | 1040 // Omit_username_password + unescape case. |
| 1142 formatted = FormatUrl( | 1041 formatted = |
| 1143 GURL("http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" | 1042 FormatUrl(GURL( |
| 1144 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), | 1043 "http://\xE3\x82\xB0:\xE3\x83\xBC@xn--qcka1pmc.jp:8080/" |
| 1145 "ja", kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, &parsed, | 1044 "%E3%82%B0/?q=%E3%82%B0#\xE3\x82\xB0"), |
| 1146 NULL, NULL); | 1045 "ja", |
| 1147 EXPECT_EQ(WideToUTF16(L"http://\x30B0\x30FC\x30B0\x30EB.jp:8080" | 1046 kFormatUrlOmitUsernamePassword, |
| 1148 L"/\x30B0/?q=\x30B0#\x30B0"), formatted); | 1047 UnescapeRule::NORMAL, |
| 1048 &parsed, |
| 1049 NULL, |
| 1050 NULL); |
| 1051 EXPECT_EQ(WideToUTF16( |
| 1052 L"http://\x30B0\x30FC\x30B0\x30EB.jp:8080" |
| 1053 L"/\x30B0/?q=\x30B0#\x30B0"), |
| 1054 formatted); |
| 1149 EXPECT_FALSE(parsed.username.is_valid()); | 1055 EXPECT_FALSE(parsed.username.is_valid()); |
| 1150 EXPECT_FALSE(parsed.password.is_valid()); | 1056 EXPECT_FALSE(parsed.password.is_valid()); |
| 1151 EXPECT_EQ(WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB.jp"), | 1057 EXPECT_EQ(WideToUTF16(L"\x30B0\x30FC\x30B0\x30EB.jp"), |
| 1152 formatted.substr(parsed.host.begin, parsed.host.len)); | 1058 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1153 EXPECT_EQ(WideToUTF16(L"8080"), | 1059 EXPECT_EQ(WideToUTF16(L"8080"), |
| 1154 formatted.substr(parsed.port.begin, parsed.port.len)); | 1060 formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1155 EXPECT_EQ(WideToUTF16(L"/\x30B0/"), | 1061 EXPECT_EQ(WideToUTF16(L"/\x30B0/"), |
| 1156 formatted.substr(parsed.path.begin, parsed.path.len)); | 1062 formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1157 EXPECT_EQ(WideToUTF16(L"q=\x30B0"), | 1063 EXPECT_EQ(WideToUTF16(L"q=\x30B0"), |
| 1158 formatted.substr(parsed.query.begin, parsed.query.len)); | 1064 formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1159 EXPECT_EQ(WideToUTF16(L"\x30B0"), | 1065 EXPECT_EQ(WideToUTF16(L"\x30B0"), |
| 1160 formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1066 formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1161 | 1067 |
| 1162 // View-source case. | 1068 // View-source case. |
| 1163 formatted = | 1069 formatted = |
| 1164 FormatUrl(GURL("view-source:http://user:passwd@host:81/path?query#ref"), | 1070 FormatUrl(GURL("view-source:http://user:passwd@host:81/path?query#ref"), |
| 1165 std::string(), | 1071 std::string(), |
| 1166 kFormatUrlOmitUsernamePassword, | 1072 kFormatUrlOmitUsernamePassword, |
| 1167 UnescapeRule::NORMAL, | 1073 UnescapeRule::NORMAL, |
| 1168 &parsed, | 1074 &parsed, |
| 1169 NULL, | 1075 NULL, |
| 1170 NULL); | 1076 NULL); |
| 1171 EXPECT_EQ(WideToUTF16(L"view-source:http://host:81/path?query#ref"), | 1077 EXPECT_EQ(WideToUTF16(L"view-source:http://host:81/path?query#ref"), |
| 1172 formatted); | 1078 formatted); |
| 1173 EXPECT_EQ(WideToUTF16(L"view-source:http"), | 1079 EXPECT_EQ(WideToUTF16(L"view-source:http"), |
| 1174 formatted.substr(parsed.scheme.begin, parsed.scheme.len)); | 1080 formatted.substr(parsed.scheme.begin, parsed.scheme.len)); |
| 1175 EXPECT_FALSE(parsed.username.is_valid()); | 1081 EXPECT_FALSE(parsed.username.is_valid()); |
| 1176 EXPECT_FALSE(parsed.password.is_valid()); | 1082 EXPECT_FALSE(parsed.password.is_valid()); |
| 1177 EXPECT_EQ(WideToUTF16(L"host"), | 1083 EXPECT_EQ(WideToUTF16(L"host"), |
| 1178 formatted.substr(parsed.host.begin, parsed.host.len)); | 1084 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1179 EXPECT_EQ(WideToUTF16(L"81"), | 1085 EXPECT_EQ(WideToUTF16(L"81"), |
| 1180 formatted.substr(parsed.port.begin, parsed.port.len)); | 1086 formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1181 EXPECT_EQ(WideToUTF16(L"/path"), | 1087 EXPECT_EQ(WideToUTF16(L"/path"), |
| 1182 formatted.substr(parsed.path.begin, parsed.path.len)); | 1088 formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1183 EXPECT_EQ(WideToUTF16(L"query"), | 1089 EXPECT_EQ(WideToUTF16(L"query"), |
| 1184 formatted.substr(parsed.query.begin, parsed.query.len)); | 1090 formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1185 EXPECT_EQ(WideToUTF16(L"ref"), | 1091 EXPECT_EQ(WideToUTF16(L"ref"), |
| 1186 formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1092 formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1187 | 1093 |
| 1188 // omit http case. | 1094 // omit http case. |
| 1189 formatted = FormatUrl(GURL("http://host:8000/a?b=c#d"), | 1095 formatted = FormatUrl(GURL("http://host:8000/a?b=c#d"), |
| 1190 std::string(), | 1096 std::string(), |
| 1191 kFormatUrlOmitHTTP, | 1097 kFormatUrlOmitHTTP, |
| 1192 UnescapeRule::NORMAL, | 1098 UnescapeRule::NORMAL, |
| 1193 &parsed, | 1099 &parsed, |
| 1194 NULL, | 1100 NULL, |
| 1195 NULL); | 1101 NULL); |
| 1196 EXPECT_EQ(WideToUTF16(L"host:8000/a?b=c#d"), formatted); | 1102 EXPECT_EQ(WideToUTF16(L"host:8000/a?b=c#d"), formatted); |
| 1197 EXPECT_FALSE(parsed.scheme.is_valid()); | 1103 EXPECT_FALSE(parsed.scheme.is_valid()); |
| 1198 EXPECT_FALSE(parsed.username.is_valid()); | 1104 EXPECT_FALSE(parsed.username.is_valid()); |
| 1199 EXPECT_FALSE(parsed.password.is_valid()); | 1105 EXPECT_FALSE(parsed.password.is_valid()); |
| 1200 EXPECT_EQ(WideToUTF16(L"host"), | 1106 EXPECT_EQ(WideToUTF16(L"host"), |
| 1201 formatted.substr(parsed.host.begin, parsed.host.len)); | 1107 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1202 EXPECT_EQ(WideToUTF16(L"8000"), | 1108 EXPECT_EQ(WideToUTF16(L"8000"), |
| 1203 formatted.substr(parsed.port.begin, parsed.port.len)); | 1109 formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1204 EXPECT_EQ(WideToUTF16(L"/a"), | 1110 EXPECT_EQ(WideToUTF16(L"/a"), |
| 1205 formatted.substr(parsed.path.begin, parsed.path.len)); | 1111 formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1206 EXPECT_EQ(WideToUTF16(L"b=c"), | 1112 EXPECT_EQ(WideToUTF16(L"b=c"), |
| 1207 formatted.substr(parsed.query.begin, parsed.query.len)); | 1113 formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1208 EXPECT_EQ(WideToUTF16(L"d"), | 1114 EXPECT_EQ(WideToUTF16(L"d"), |
| 1209 formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1115 formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1210 | 1116 |
| 1211 // omit http starts with ftp case. | 1117 // omit http starts with ftp case. |
| 1212 formatted = FormatUrl(GURL("http://ftp.host:8000/a?b=c#d"), | 1118 formatted = FormatUrl(GURL("http://ftp.host:8000/a?b=c#d"), |
| 1213 std::string(), | 1119 std::string(), |
| 1214 kFormatUrlOmitHTTP, | 1120 kFormatUrlOmitHTTP, |
| 1215 UnescapeRule::NORMAL, | 1121 UnescapeRule::NORMAL, |
| 1216 &parsed, | 1122 &parsed, |
| 1217 NULL, | 1123 NULL, |
| 1218 NULL); | 1124 NULL); |
| 1219 EXPECT_EQ(WideToUTF16(L"http://ftp.host:8000/a?b=c#d"), formatted); | 1125 EXPECT_EQ(WideToUTF16(L"http://ftp.host:8000/a?b=c#d"), formatted); |
| 1220 EXPECT_TRUE(parsed.scheme.is_valid()); | 1126 EXPECT_TRUE(parsed.scheme.is_valid()); |
| 1221 EXPECT_FALSE(parsed.username.is_valid()); | 1127 EXPECT_FALSE(parsed.username.is_valid()); |
| 1222 EXPECT_FALSE(parsed.password.is_valid()); | 1128 EXPECT_FALSE(parsed.password.is_valid()); |
| 1223 EXPECT_EQ(WideToUTF16(L"http"), | 1129 EXPECT_EQ(WideToUTF16(L"http"), |
| 1224 formatted.substr(parsed.scheme.begin, parsed.scheme.len)); | 1130 formatted.substr(parsed.scheme.begin, parsed.scheme.len)); |
| 1225 EXPECT_EQ(WideToUTF16(L"ftp.host"), | 1131 EXPECT_EQ(WideToUTF16(L"ftp.host"), |
| 1226 formatted.substr(parsed.host.begin, parsed.host.len)); | 1132 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1227 EXPECT_EQ(WideToUTF16(L"8000"), | 1133 EXPECT_EQ(WideToUTF16(L"8000"), |
| 1228 formatted.substr(parsed.port.begin, parsed.port.len)); | 1134 formatted.substr(parsed.port.begin, parsed.port.len)); |
| 1229 EXPECT_EQ(WideToUTF16(L"/a"), | 1135 EXPECT_EQ(WideToUTF16(L"/a"), |
| 1230 formatted.substr(parsed.path.begin, parsed.path.len)); | 1136 formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1231 EXPECT_EQ(WideToUTF16(L"b=c"), | 1137 EXPECT_EQ(WideToUTF16(L"b=c"), |
| 1232 formatted.substr(parsed.query.begin, parsed.query.len)); | 1138 formatted.substr(parsed.query.begin, parsed.query.len)); |
| 1233 EXPECT_EQ(WideToUTF16(L"d"), | 1139 EXPECT_EQ(WideToUTF16(L"d"), |
| 1234 formatted.substr(parsed.ref.begin, parsed.ref.len)); | 1140 formatted.substr(parsed.ref.begin, parsed.ref.len)); |
| 1235 | 1141 |
| 1236 // omit http starts with 'f' case. | 1142 // omit http starts with 'f' case. |
| 1237 formatted = FormatUrl(GURL("http://f/"), | 1143 formatted = FormatUrl(GURL("http://f/"), |
| 1238 std::string(), | 1144 std::string(), |
| 1239 kFormatUrlOmitHTTP, | 1145 kFormatUrlOmitHTTP, |
| 1240 UnescapeRule::NORMAL, | 1146 UnescapeRule::NORMAL, |
| 1241 &parsed, | 1147 &parsed, |
| 1242 NULL, | 1148 NULL, |
| 1243 NULL); | 1149 NULL); |
| 1244 EXPECT_EQ(WideToUTF16(L"f/"), formatted); | 1150 EXPECT_EQ(WideToUTF16(L"f/"), formatted); |
| 1245 EXPECT_FALSE(parsed.scheme.is_valid()); | 1151 EXPECT_FALSE(parsed.scheme.is_valid()); |
| 1246 EXPECT_FALSE(parsed.username.is_valid()); | 1152 EXPECT_FALSE(parsed.username.is_valid()); |
| 1247 EXPECT_FALSE(parsed.password.is_valid()); | 1153 EXPECT_FALSE(parsed.password.is_valid()); |
| 1248 EXPECT_FALSE(parsed.port.is_valid()); | 1154 EXPECT_FALSE(parsed.port.is_valid()); |
| 1249 EXPECT_TRUE(parsed.path.is_valid()); | 1155 EXPECT_TRUE(parsed.path.is_valid()); |
| 1250 EXPECT_FALSE(parsed.query.is_valid()); | 1156 EXPECT_FALSE(parsed.query.is_valid()); |
| 1251 EXPECT_FALSE(parsed.ref.is_valid()); | 1157 EXPECT_FALSE(parsed.ref.is_valid()); |
| 1252 EXPECT_EQ(WideToUTF16(L"f"), | 1158 EXPECT_EQ(WideToUTF16(L"f"), |
| 1253 formatted.substr(parsed.host.begin, parsed.host.len)); | 1159 formatted.substr(parsed.host.begin, parsed.host.len)); |
| 1254 EXPECT_EQ(WideToUTF16(L"/"), | 1160 EXPECT_EQ(WideToUTF16(L"/"), |
| 1255 formatted.substr(parsed.path.begin, parsed.path.len)); | 1161 formatted.substr(parsed.path.begin, parsed.path.len)); |
| 1256 } | 1162 } |
| 1257 | 1163 |
| 1258 // Make sure that calling FormatUrl on a GURL and then converting back to a GURL | 1164 // Make sure that calling FormatUrl on a GURL and then converting back to a GURL |
| 1259 // results in the original GURL, for each ASCII character in the path. | 1165 // results in the original GURL, for each ASCII character in the path. |
| 1260 TEST(NetUtilTest, FormatUrlRoundTripPathASCII) { | 1166 TEST(NetUtilTest, FormatUrlRoundTripPathASCII) { |
| 1261 for (unsigned char test_char = 32; test_char < 128; ++test_char) { | 1167 for (unsigned char test_char = 32; test_char < 128; ++test_char) { |
| 1262 GURL url(std::string("http://www.google.com/") + | 1168 GURL url(std::string("http://www.google.com/") + |
| 1263 static_cast<char>(test_char)); | 1169 static_cast<char>(test_char)); |
| 1264 size_t prefix_len; | 1170 size_t prefix_len; |
| 1265 base::string16 formatted = FormatUrl(url, | 1171 base::string16 formatted = FormatUrl(url, |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1337 if (test_char && | 1243 if (test_char && |
| 1338 strchr(kUnescapedCharacters, static_cast<char>(test_char))) { | 1244 strchr(kUnescapedCharacters, static_cast<char>(test_char))) { |
| 1339 EXPECT_NE(url.spec(), GURL(formatted).spec()); | 1245 EXPECT_NE(url.spec(), GURL(formatted).spec()); |
| 1340 } else { | 1246 } else { |
| 1341 EXPECT_EQ(url.spec(), GURL(formatted).spec()); | 1247 EXPECT_EQ(url.spec(), GURL(formatted).spec()); |
| 1342 } | 1248 } |
| 1343 } | 1249 } |
| 1344 } | 1250 } |
| 1345 | 1251 |
| 1346 TEST(NetUtilTest, FormatUrlWithOffsets) { | 1252 TEST(NetUtilTest, FormatUrlWithOffsets) { |
| 1347 CheckAdjustedOffsets(std::string(), "en", kFormatUrlOmitNothing, | 1253 CheckAdjustedOffsets( |
| 1348 UnescapeRule::NORMAL, NULL); | 1254 std::string(), "en", kFormatUrlOmitNothing, UnescapeRule::NORMAL, NULL); |
| 1349 | 1255 |
| 1350 const size_t basic_offsets[] = { | 1256 const size_t basic_offsets[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, |
| 1351 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, | 1257 9, 10, 11, 12, 13, 14, 15, 16, 17, |
| 1352 21, 22, 23, 24, 25 | 1258 18, 19, 20, 21, 22, 23, 24, 25}; |
| 1353 }; | 1259 CheckAdjustedOffsets("http://www.google.com/foo/", |
| 1354 CheckAdjustedOffsets("http://www.google.com/foo/", "en", | 1260 "en", |
| 1355 kFormatUrlOmitNothing, UnescapeRule::NORMAL, | 1261 kFormatUrlOmitNothing, |
| 1262 UnescapeRule::NORMAL, |
| 1356 basic_offsets); | 1263 basic_offsets); |
| 1357 | 1264 |
| 1358 const size_t omit_auth_offsets_1[] = { | 1265 const size_t omit_auth_offsets_1[] = { |
| 1359 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 7, | 1266 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, |
| 1360 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 | 1267 kNpos, kNpos, kNpos, kNpos, kNpos, 7, 8, 9, 10, 11, |
| 1361 }; | 1268 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}; |
| 1362 CheckAdjustedOffsets("http://foo:bar@www.google.com/", "en", | 1269 CheckAdjustedOffsets("http://foo:bar@www.google.com/", |
| 1363 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, | 1270 "en", |
| 1271 kFormatUrlOmitUsernamePassword, |
| 1272 UnescapeRule::NORMAL, |
| 1364 omit_auth_offsets_1); | 1273 omit_auth_offsets_1); |
| 1365 | 1274 |
| 1366 const size_t omit_auth_offsets_2[] = { | 1275 const size_t omit_auth_offsets_2[] = { |
| 1367 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, kNpos, 7, 8, 9, 10, 11, 12, 13, 14, | 1276 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, kNpos, 7, 8, |
| 1368 15, 16, 17, 18, 19, 20, 21 | 1277 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}; |
| 1369 }; | 1278 CheckAdjustedOffsets("http://foo@www.google.com/", |
| 1370 CheckAdjustedOffsets("http://foo@www.google.com/", "en", | 1279 "en", |
| 1371 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, | 1280 kFormatUrlOmitUsernamePassword, |
| 1281 UnescapeRule::NORMAL, |
| 1372 omit_auth_offsets_2); | 1282 omit_auth_offsets_2); |
| 1373 | 1283 |
| 1374 const size_t dont_omit_auth_offsets[] = { | 1284 const size_t dont_omit_auth_offsets[] = { |
| 1375 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, | 1285 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 1376 kNpos, kNpos, 11, 12, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, | 1286 10, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 11, |
| 1377 kNpos, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, | 1287 12, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 13, |
| 1378 30, 31 | 1288 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, |
| 1379 }; | 1289 24, 25, 26, 27, 28, 29, 30, 31}; |
| 1380 // Unescape to "http://foo\x30B0:\x30B0bar@www.google.com". | 1290 // Unescape to "http://foo\x30B0:\x30B0bar@www.google.com". |
| 1381 CheckAdjustedOffsets("http://foo%E3%82%B0:%E3%82%B0bar@www.google.com/", "en", | 1291 CheckAdjustedOffsets("http://foo%E3%82%B0:%E3%82%B0bar@www.google.com/", |
| 1382 kFormatUrlOmitNothing, UnescapeRule::NORMAL, | 1292 "en", |
| 1293 kFormatUrlOmitNothing, |
| 1294 UnescapeRule::NORMAL, |
| 1383 dont_omit_auth_offsets); | 1295 dont_omit_auth_offsets); |
| 1384 | 1296 |
| 1385 const size_t view_source_offsets[] = { | 1297 const size_t view_source_offsets[] = { |
| 1386 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, kNpos, | 1298 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
| 1387 kNpos, kNpos, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33 | 1299 13, 14, 15, 16, 17, 18, 19, kNpos, kNpos, kNpos, 19, 20, 21, |
| 1388 }; | 1300 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32, 33}; |
| 1389 CheckAdjustedOffsets("view-source:http://foo@www.google.com/", "en", | 1301 CheckAdjustedOffsets("view-source:http://foo@www.google.com/", |
| 1390 kFormatUrlOmitUsernamePassword, UnescapeRule::NORMAL, | 1302 "en", |
| 1303 kFormatUrlOmitUsernamePassword, |
| 1304 UnescapeRule::NORMAL, |
| 1391 view_source_offsets); | 1305 view_source_offsets); |
| 1392 | 1306 |
| 1393 const size_t idn_hostname_offsets_1[] = { | 1307 const size_t idn_hostname_offsets_1[] = { |
| 1394 0, 1, 2, 3, 4, 5, 6, 7, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, | 1308 0, 1, 2, 3, 4, 5, 6, 7, kNpos, |
| 1395 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 12, | 1309 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, |
| 1396 13, 14, 15, 16, 17, 18, 19 | 1310 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 12, 13, |
| 1397 }; | 1311 14, 15, 16, 17, 18, 19}; |
| 1398 // Convert punycode to "http://\x671d\x65e5\x3042\x3055\x3072.jp/foo/". | 1312 // Convert punycode to "http://\x671d\x65e5\x3042\x3055\x3072.jp/foo/". |
| 1399 CheckAdjustedOffsets("http://xn--l8jvb1ey91xtjb.jp/foo/", "ja", | 1313 CheckAdjustedOffsets("http://xn--l8jvb1ey91xtjb.jp/foo/", |
| 1400 kFormatUrlOmitNothing, UnescapeRule::NORMAL, | 1314 "ja", |
| 1315 kFormatUrlOmitNothing, |
| 1316 UnescapeRule::NORMAL, |
| 1401 idn_hostname_offsets_1); | 1317 idn_hostname_offsets_1); |
| 1402 | 1318 |
| 1403 const size_t idn_hostname_offsets_2[] = { | 1319 const size_t idn_hostname_offsets_2[] = { |
| 1404 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, kNpos, kNpos, kNpos, kNpos, kNpos, | 1320 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 1405 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 14, 15, kNpos, kNpos, kNpos, | 1321 10, 11, 12, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, |
| 1406 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, | 1322 kNpos, kNpos, kNpos, kNpos, 14, 15, kNpos, kNpos, kNpos, kNpos, |
| 1407 kNpos, 19, 20, 21, 22, 23, 24 | 1323 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, |
| 1408 }; | 1324 kNpos, 19, 20, 21, 22, 23, 24}; |
| 1409 // Convert punycode to | 1325 // Convert punycode to |
| 1410 // "http://test.\x89c6\x9891.\x5317\x4eac\x5927\x5b78.test/". | 1326 // "http://test.\x89c6\x9891.\x5317\x4eac\x5927\x5b78.test/". |
| 1411 CheckAdjustedOffsets("http://test.xn--cy2a840a.xn--1lq90ic7f1rc.test/", | 1327 CheckAdjustedOffsets("http://test.xn--cy2a840a.xn--1lq90ic7f1rc.test/", |
| 1412 "zh-CN", kFormatUrlOmitNothing, UnescapeRule::NORMAL, | 1328 "zh-CN", |
| 1329 kFormatUrlOmitNothing, |
| 1330 UnescapeRule::NORMAL, |
| 1413 idn_hostname_offsets_2); | 1331 idn_hostname_offsets_2); |
| 1414 | 1332 |
| 1415 const size_t unescape_offsets[] = { | 1333 const size_t unescape_offsets[] = { |
| 1416 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, | 1334 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, |
| 1417 21, 22, 23, 24, 25, kNpos, kNpos, 26, 27, 28, 29, 30, kNpos, kNpos, kNpos, | 1335 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, |
| 1418 kNpos, kNpos, kNpos, kNpos, kNpos, 31, kNpos, kNpos, kNpos, kNpos, kNpos, | 1336 20, 21, 22, 23, 24, 25, kNpos, kNpos, 26, 27, |
| 1419 kNpos, kNpos, kNpos, 32, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, | 1337 28, 29, 30, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, |
| 1420 kNpos, 33, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos | 1338 kNpos, 31, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, |
| 1421 }; | 1339 32, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 33, |
| 1340 kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos}; |
| 1422 // Unescape to "http://www.google.com/foo bar/\x30B0\x30FC\x30B0\x30EB". | 1341 // Unescape to "http://www.google.com/foo bar/\x30B0\x30FC\x30B0\x30EB". |
| 1423 CheckAdjustedOffsets( | 1342 CheckAdjustedOffsets( |
| 1424 "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", | 1343 "http://www.google.com/foo%20bar/%E3%82%B0%E3%83%BC%E3%82%B0%E3%83%AB", |
| 1425 "en", kFormatUrlOmitNothing, UnescapeRule::SPACES, unescape_offsets); | 1344 "en", |
| 1345 kFormatUrlOmitNothing, |
| 1346 UnescapeRule::SPACES, |
| 1347 unescape_offsets); |
| 1426 | 1348 |
| 1427 const size_t ref_offsets[] = { | 1349 const size_t ref_offsets[] = { |
| 1428 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, | 1350 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, |
| 1429 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, kNpos, kNpos, 32, kNpos, kNpos, | 1351 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, |
| 1430 33 | 1352 26, 27, 28, 29, 30, 31, kNpos, kNpos, 32, kNpos, kNpos, 33}; |
| 1431 }; | |
| 1432 // Unescape to "http://www.google.com/foo.html#\x30B0\x30B0z". | 1353 // Unescape to "http://www.google.com/foo.html#\x30B0\x30B0z". |
| 1433 CheckAdjustedOffsets( | 1354 CheckAdjustedOffsets( |
| 1434 "http://www.google.com/foo.html#\xE3\x82\xB0\xE3\x82\xB0z", "en", | 1355 "http://www.google.com/foo.html#\xE3\x82\xB0\xE3\x82\xB0z", |
| 1435 kFormatUrlOmitNothing, UnescapeRule::NORMAL, ref_offsets); | 1356 "en", |
| 1357 kFormatUrlOmitNothing, |
| 1358 UnescapeRule::NORMAL, |
| 1359 ref_offsets); |
| 1436 | 1360 |
| 1437 const size_t omit_http_offsets[] = { | 1361 const size_t omit_http_offsets[] = { |
| 1438 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, | 1362 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, 1, 2, 3, |
| 1439 10, 11, 12, 13, 14 | 1363 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14}; |
| 1440 }; | 1364 CheckAdjustedOffsets("http://www.google.com/", |
| 1441 CheckAdjustedOffsets("http://www.google.com/", "en", kFormatUrlOmitHTTP, | 1365 "en", |
| 1442 UnescapeRule::NORMAL, omit_http_offsets); | 1366 kFormatUrlOmitHTTP, |
| 1367 UnescapeRule::NORMAL, |
| 1368 omit_http_offsets); |
| 1443 | 1369 |
| 1444 const size_t omit_http_start_with_ftp_offsets[] = { | 1370 const size_t omit_http_start_with_ftp_offsets[] = { |
| 1445 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21 | 1371 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, |
| 1446 }; | 1372 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21}; |
| 1447 CheckAdjustedOffsets("http://ftp.google.com/", "en", kFormatUrlOmitHTTP, | 1373 CheckAdjustedOffsets("http://ftp.google.com/", |
| 1448 UnescapeRule::NORMAL, omit_http_start_with_ftp_offsets); | 1374 "en", |
| 1375 kFormatUrlOmitHTTP, |
| 1376 UnescapeRule::NORMAL, |
| 1377 omit_http_start_with_ftp_offsets); |
| 1449 | 1378 |
| 1450 const size_t omit_all_offsets[] = { | 1379 const size_t omit_all_offsets[] = { |
| 1451 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, kNpos, kNpos, kNpos, kNpos, | 1380 0, kNpos, kNpos, kNpos, kNpos, kNpos, kNpos, 0, kNpos, kNpos, |
| 1452 0, 1, 2, 3, 4, 5, 6, 7 | 1381 kNpos, kNpos, 0, 1, 2, 3, 4, 5, 6, 7}; |
| 1453 }; | 1382 CheckAdjustedOffsets("http://user@foo.com/", |
| 1454 CheckAdjustedOffsets("http://user@foo.com/", "en", kFormatUrlOmitAll, | 1383 "en", |
| 1455 UnescapeRule::NORMAL, omit_all_offsets); | 1384 kFormatUrlOmitAll, |
| 1385 UnescapeRule::NORMAL, |
| 1386 omit_all_offsets); |
| 1456 } | 1387 } |
| 1457 | 1388 |
| 1458 TEST(NetUtilTest, SimplifyUrlForRequest) { | 1389 TEST(NetUtilTest, SimplifyUrlForRequest) { |
| 1459 struct { | 1390 struct { |
| 1460 const char* input_url; | 1391 const char* input_url; |
| 1461 const char* expected_simplified_url; | 1392 const char* expected_simplified_url; |
| 1462 } tests[] = { | 1393 } tests[] = { |
| 1463 { | 1394 {// Reference section should be stripped. |
| 1464 // Reference section should be stripped. | 1395 "http://www.google.com:78/foobar?query=1#hash", |
| 1465 "http://www.google.com:78/foobar?query=1#hash", | 1396 "http://www.google.com:78/foobar?query=1", |
| 1466 "http://www.google.com:78/foobar?query=1", | 1397 }, |
| 1467 }, | 1398 {// Reference section can itself contain #. |
| 1468 { | 1399 "http://192.168.0.1?query=1#hash#10#11#13#14", |
| 1469 // Reference section can itself contain #. | 1400 "http://192.168.0.1?query=1", |
| 1470 "http://192.168.0.1?query=1#hash#10#11#13#14", | 1401 }, |
| 1471 "http://192.168.0.1?query=1", | 1402 {// Strip username/password. |
| 1472 }, | 1403 "http://user:pass@google.com", "http://google.com/", |
| 1473 { // Strip username/password. | 1404 }, |
| 1474 "http://user:pass@google.com", | 1405 {// Strip both the reference and the username/password. |
| 1475 "http://google.com/", | 1406 "http://user:pass@google.com:80/sup?yo#X#X", |
| 1476 }, | 1407 "http://google.com/sup?yo", |
| 1477 { // Strip both the reference and the username/password. | 1408 }, |
| 1478 "http://user:pass@google.com:80/sup?yo#X#X", | 1409 {// Try an HTTPS URL -- strip both the reference and the |
| 1479 "http://google.com/sup?yo", | 1410 // username/password. |
| 1480 }, | 1411 "https://user:pass@google.com:80/sup?yo#X#X", |
| 1481 { // Try an HTTPS URL -- strip both the reference and the username/password. | 1412 "https://google.com:80/sup?yo", |
| 1482 "https://user:pass@google.com:80/sup?yo#X#X", | 1413 }, |
| 1483 "https://google.com:80/sup?yo", | 1414 {// Try an FTP URL -- strip both the reference and the |
| 1484 }, | 1415 // username/password. |
| 1485 { // Try an FTP URL -- strip both the reference and the username/password. | 1416 "ftp://user:pass@google.com:80/sup?yo#X#X", |
| 1486 "ftp://user:pass@google.com:80/sup?yo#X#X", | 1417 "ftp://google.com:80/sup?yo", |
| 1487 "ftp://google.com:80/sup?yo", | 1418 }, |
| 1488 }, | 1419 {// Try a nonstandard URL |
| 1489 { // Try a nonstandard URL | 1420 "foobar://user:pass@google.com:80/sup?yo#X#X", |
| 1490 "foobar://user:pass@google.com:80/sup?yo#X#X", | 1421 "foobar://user:pass@google.com:80/sup?yo", |
| 1491 "foobar://user:pass@google.com:80/sup?yo", | 1422 }, |
| 1492 }, | 1423 }; |
| 1493 }; | |
| 1494 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 1424 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 1495 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s", i, | 1425 SCOPED_TRACE( |
| 1496 tests[i].input_url)); | 1426 base::StringPrintf("Test[%" PRIuS "]: %s", i, tests[i].input_url)); |
| 1497 GURL input_url(GURL(tests[i].input_url)); | 1427 GURL input_url(GURL(tests[i].input_url)); |
| 1498 GURL expected_url(GURL(tests[i].expected_simplified_url)); | 1428 GURL expected_url(GURL(tests[i].expected_simplified_url)); |
| 1499 EXPECT_EQ(expected_url, SimplifyUrlForRequest(input_url)); | 1429 EXPECT_EQ(expected_url, SimplifyUrlForRequest(input_url)); |
| 1500 } | 1430 } |
| 1501 } | 1431 } |
| 1502 | 1432 |
| 1503 TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) { | 1433 TEST(NetUtilTest, SetExplicitlyAllowedPortsTest) { |
| 1504 std::string invalid[] = { "1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12" }; | 1434 std::string invalid[] = {"1,2,a", "'1','2'", "1, 2, 3", "1 0,11,12"}; |
| 1505 std::string valid[] = { "", "1", "1,2", "1,2,3", "10,11,12,13" }; | 1435 std::string valid[] = {"", "1", "1,2", "1,2,3", "10,11,12,13"}; |
| 1506 | 1436 |
| 1507 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(invalid); ++i) { | 1437 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(invalid); ++i) { |
| 1508 SetExplicitlyAllowedPorts(invalid[i]); | 1438 SetExplicitlyAllowedPorts(invalid[i]); |
| 1509 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts())); | 1439 EXPECT_EQ(0, static_cast<int>(GetCountOfExplicitlyAllowedPorts())); |
| 1510 } | 1440 } |
| 1511 | 1441 |
| 1512 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) { | 1442 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(valid); ++i) { |
| 1513 SetExplicitlyAllowedPorts(valid[i]); | 1443 SetExplicitlyAllowedPorts(valid[i]); |
| 1514 EXPECT_EQ(i, GetCountOfExplicitlyAllowedPorts()); | 1444 EXPECT_EQ(i, GetCountOfExplicitlyAllowedPorts()); |
| 1515 } | 1445 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1558 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number)); | 1488 EXPECT_TRUE(ParseIPLiteralToNumber("1:abcd::3:4:ff", &number)); |
| 1559 EXPECT_EQ("0,1,171,205,0,0,0,0,0,0,0,3,0,4,0,255", DumpIPNumber(number)); | 1489 EXPECT_EQ("0,1,171,205,0,0,0,0,0,0,0,3,0,4,0,255", DumpIPNumber(number)); |
| 1560 EXPECT_EQ("1:abcd::3:4:ff", IPAddressToString(number)); | 1490 EXPECT_EQ("1:abcd::3:4:ff", IPAddressToString(number)); |
| 1561 } | 1491 } |
| 1562 | 1492 |
| 1563 // Test mapping an IPv4 address to an IPv6 address. | 1493 // Test mapping an IPv4 address to an IPv6 address. |
| 1564 TEST(NetUtilTest, ConvertIPv4NumberToIPv6Number) { | 1494 TEST(NetUtilTest, ConvertIPv4NumberToIPv6Number) { |
| 1565 IPAddressNumber ipv4_number; | 1495 IPAddressNumber ipv4_number; |
| 1566 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number)); | 1496 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number)); |
| 1567 | 1497 |
| 1568 IPAddressNumber ipv6_number = | 1498 IPAddressNumber ipv6_number = ConvertIPv4NumberToIPv6Number(ipv4_number); |
| 1569 ConvertIPv4NumberToIPv6Number(ipv4_number); | |
| 1570 | 1499 |
| 1571 // ::ffff:192.168.0.1 | 1500 // ::ffff:192.168.0.1 |
| 1572 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1", | 1501 EXPECT_EQ("0,0,0,0,0,0,0,0,0,0,255,255,192,168,0,1", |
| 1573 DumpIPNumber(ipv6_number)); | 1502 DumpIPNumber(ipv6_number)); |
| 1574 EXPECT_EQ("::ffff:c0a8:1", IPAddressToString(ipv6_number)); | 1503 EXPECT_EQ("::ffff:c0a8:1", IPAddressToString(ipv6_number)); |
| 1575 } | 1504 } |
| 1576 | 1505 |
| 1577 TEST(NetUtilTest, IsIPv4Mapped) { | 1506 TEST(NetUtilTest, IsIPv4Mapped) { |
| 1578 IPAddressNumber ipv4_number; | 1507 IPAddressNumber ipv4_number; |
| 1579 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number)); | 1508 EXPECT_TRUE(ParseIPLiteralToNumber("192.168.0.1", &ipv4_number)); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 1593 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number)); | 1522 EXPECT_TRUE(ParseIPLiteralToNumber("::ffff:0101:1", &ipv4mapped_number)); |
| 1594 IPAddressNumber expected; | 1523 IPAddressNumber expected; |
| 1595 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected)); | 1524 EXPECT_TRUE(ParseIPLiteralToNumber("1.1.0.1", &expected)); |
| 1596 IPAddressNumber result = ConvertIPv4MappedToIPv4(ipv4mapped_number); | 1525 IPAddressNumber result = ConvertIPv4MappedToIPv4(ipv4mapped_number); |
| 1597 EXPECT_EQ(expected, result); | 1526 EXPECT_EQ(expected, result); |
| 1598 } | 1527 } |
| 1599 | 1528 |
| 1600 // Test parsing invalid CIDR notation literals. | 1529 // Test parsing invalid CIDR notation literals. |
| 1601 TEST(NetUtilTest, ParseCIDRBlock_Invalid) { | 1530 TEST(NetUtilTest, ParseCIDRBlock_Invalid) { |
| 1602 const char* bad_literals[] = { | 1531 const char* bad_literals[] = { |
| 1603 "foobar", | 1532 "foobar", "", "192.168.0.1", "::1", "/", |
| 1604 "", | 1533 "/1", "1", "192.168.1.1/-1", "192.168.1.1/33", "::1/-3", |
| 1605 "192.168.0.1", | 1534 "a::3/129", "::1/x", "192.168.0.1//11"}; |
| 1606 "::1", | |
| 1607 "/", | |
| 1608 "/1", | |
| 1609 "1", | |
| 1610 "192.168.1.1/-1", | |
| 1611 "192.168.1.1/33", | |
| 1612 "::1/-3", | |
| 1613 "a::3/129", | |
| 1614 "::1/x", | |
| 1615 "192.168.0.1//11" | |
| 1616 }; | |
| 1617 | 1535 |
| 1618 for (size_t i = 0; i < arraysize(bad_literals); ++i) { | 1536 for (size_t i = 0; i < arraysize(bad_literals); ++i) { |
| 1619 IPAddressNumber ip_number; | 1537 IPAddressNumber ip_number; |
| 1620 size_t prefix_length_in_bits; | 1538 size_t prefix_length_in_bits; |
| 1621 | 1539 |
| 1622 EXPECT_FALSE(ParseCIDRBlock(bad_literals[i], | 1540 EXPECT_FALSE( |
| 1623 &ip_number, | 1541 ParseCIDRBlock(bad_literals[i], &ip_number, &prefix_length_in_bits)); |
| 1624 &prefix_length_in_bits)); | |
| 1625 } | 1542 } |
| 1626 } | 1543 } |
| 1627 | 1544 |
| 1628 // Test parsing a valid CIDR notation literal. | 1545 // Test parsing a valid CIDR notation literal. |
| 1629 TEST(NetUtilTest, ParseCIDRBlock_Valid) { | 1546 TEST(NetUtilTest, ParseCIDRBlock_Valid) { |
| 1630 IPAddressNumber ip_number; | 1547 IPAddressNumber ip_number; |
| 1631 size_t prefix_length_in_bits; | 1548 size_t prefix_length_in_bits; |
| 1632 | 1549 |
| 1633 EXPECT_TRUE(ParseCIDRBlock("192.168.0.1/11", | 1550 EXPECT_TRUE( |
| 1634 &ip_number, | 1551 ParseCIDRBlock("192.168.0.1/11", &ip_number, &prefix_length_in_bits)); |
| 1635 &prefix_length_in_bits)); | |
| 1636 | 1552 |
| 1637 EXPECT_EQ("192,168,0,1", DumpIPNumber(ip_number)); | 1553 EXPECT_EQ("192,168,0,1", DumpIPNumber(ip_number)); |
| 1638 EXPECT_EQ(11u, prefix_length_in_bits); | 1554 EXPECT_EQ(11u, prefix_length_in_bits); |
| 1639 } | 1555 } |
| 1640 | 1556 |
| 1641 TEST(NetUtilTest, IPNumberMatchesPrefix) { | 1557 TEST(NetUtilTest, IPNumberMatchesPrefix) { |
| 1642 struct { | 1558 struct { |
| 1643 const char* cidr_literal; | 1559 const char* cidr_literal; |
| 1644 const char* ip_literal; | 1560 const char* ip_literal; |
| 1645 bool expected_to_match; | 1561 bool expected_to_match; |
| 1646 } tests[] = { | 1562 } tests[] = {// IPv4 prefix with IPv4 inputs. |
| 1647 // IPv4 prefix with IPv4 inputs. | 1563 {"10.10.1.32/27", "10.10.1.44", true}, |
| 1648 { | 1564 {"10.10.1.32/27", "10.10.1.90", false}, |
| 1649 "10.10.1.32/27", | 1565 {"10.10.1.32/27", "10.10.1.90", false}, |
| 1650 "10.10.1.44", | |
| 1651 true | |
| 1652 }, | |
| 1653 { | |
| 1654 "10.10.1.32/27", | |
| 1655 "10.10.1.90", | |
| 1656 false | |
| 1657 }, | |
| 1658 { | |
| 1659 "10.10.1.32/27", | |
| 1660 "10.10.1.90", | |
| 1661 false | |
| 1662 }, | |
| 1663 | 1566 |
| 1664 // IPv6 prefix with IPv6 inputs. | 1567 // IPv6 prefix with IPv6 inputs. |
| 1665 { | 1568 {"2001:db8::/32", "2001:DB8:3:4::5", true}, |
| 1666 "2001:db8::/32", | 1569 {"2001:db8::/32", "2001:c8::", false}, |
| 1667 "2001:DB8:3:4::5", | |
| 1668 true | |
| 1669 }, | |
| 1670 { | |
| 1671 "2001:db8::/32", | |
| 1672 "2001:c8::", | |
| 1673 false | |
| 1674 }, | |
| 1675 | 1570 |
| 1676 // IPv6 prefix with IPv4 inputs. | 1571 // IPv6 prefix with IPv4 inputs. |
| 1677 { | 1572 {"2001:db8::/33", "192.168.0.1", false}, |
| 1678 "2001:db8::/33", | 1573 {"::ffff:192.168.0.1/112", "192.168.33.77", true}, |
| 1679 "192.168.0.1", | |
| 1680 false | |
| 1681 }, | |
| 1682 { | |
| 1683 "::ffff:192.168.0.1/112", | |
| 1684 "192.168.33.77", | |
| 1685 true | |
| 1686 }, | |
| 1687 | 1574 |
| 1688 // IPv4 prefix with IPv6 inputs. | 1575 // IPv4 prefix with IPv6 inputs. |
| 1689 { | 1576 {"10.11.33.44/16", "::ffff:0a0b:89", true}, |
| 1690 "10.11.33.44/16", | 1577 {"10.11.33.44/16", "::ffff:10.12.33.44", false}, |
| 1691 "::ffff:0a0b:89", | 1578 }; |
| 1692 true | |
| 1693 }, | |
| 1694 { | |
| 1695 "10.11.33.44/16", | |
| 1696 "::ffff:10.12.33.44", | |
| 1697 false | |
| 1698 }, | |
| 1699 }; | |
| 1700 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 1579 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 1701 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s, %s", i, | 1580 SCOPED_TRACE(base::StringPrintf("Test[%" PRIuS "]: %s, %s", |
| 1581 i, |
| 1702 tests[i].cidr_literal, | 1582 tests[i].cidr_literal, |
| 1703 tests[i].ip_literal)); | 1583 tests[i].ip_literal)); |
| 1704 | 1584 |
| 1705 IPAddressNumber ip_number; | 1585 IPAddressNumber ip_number; |
| 1706 EXPECT_TRUE(ParseIPLiteralToNumber(tests[i].ip_literal, &ip_number)); | 1586 EXPECT_TRUE(ParseIPLiteralToNumber(tests[i].ip_literal, &ip_number)); |
| 1707 | 1587 |
| 1708 IPAddressNumber ip_prefix; | 1588 IPAddressNumber ip_prefix; |
| 1709 size_t prefix_length_in_bits; | 1589 size_t prefix_length_in_bits; |
| 1710 | 1590 |
| 1711 EXPECT_TRUE(ParseCIDRBlock(tests[i].cidr_literal, | 1591 EXPECT_TRUE(ParseCIDRBlock( |
| 1712 &ip_prefix, | 1592 tests[i].cidr_literal, &ip_prefix, &prefix_length_in_bits)); |
| 1713 &prefix_length_in_bits)); | |
| 1714 | 1593 |
| 1715 EXPECT_EQ(tests[i].expected_to_match, | 1594 EXPECT_EQ( |
| 1716 IPNumberMatchesPrefix(ip_number, | 1595 tests[i].expected_to_match, |
| 1717 ip_prefix, | 1596 IPNumberMatchesPrefix(ip_number, ip_prefix, prefix_length_in_bits)); |
| 1718 prefix_length_in_bits)); | |
| 1719 } | 1597 } |
| 1720 } | 1598 } |
| 1721 | 1599 |
| 1722 TEST(NetUtilTest, IsLocalhost) { | 1600 TEST(NetUtilTest, IsLocalhost) { |
| 1723 EXPECT_TRUE(net::IsLocalhost("localhost")); | 1601 EXPECT_TRUE(net::IsLocalhost("localhost")); |
| 1724 EXPECT_TRUE(net::IsLocalhost("localhost.localdomain")); | 1602 EXPECT_TRUE(net::IsLocalhost("localhost.localdomain")); |
| 1725 EXPECT_TRUE(net::IsLocalhost("localhost6")); | 1603 EXPECT_TRUE(net::IsLocalhost("localhost6")); |
| 1726 EXPECT_TRUE(net::IsLocalhost("localhost6.localdomain6")); | 1604 EXPECT_TRUE(net::IsLocalhost("localhost6.localdomain6")); |
| 1727 EXPECT_TRUE(net::IsLocalhost("127.0.0.1")); | 1605 EXPECT_TRUE(net::IsLocalhost("127.0.0.1")); |
| 1728 EXPECT_TRUE(net::IsLocalhost("127.0.1.0")); | 1606 EXPECT_TRUE(net::IsLocalhost("127.0.1.0")); |
| (...skipping 15 matching lines...) Expand all Loading... |
| 1744 EXPECT_FALSE(net::IsLocalhost("::1:1")); | 1622 EXPECT_FALSE(net::IsLocalhost("::1:1")); |
| 1745 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:1:0:0:1")); | 1623 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:1:0:0:1")); |
| 1746 EXPECT_FALSE(net::IsLocalhost("::1:1")); | 1624 EXPECT_FALSE(net::IsLocalhost("::1:1")); |
| 1747 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:0:0:0:0:1")); | 1625 EXPECT_FALSE(net::IsLocalhost("0:0:0:0:0:0:0:0:1")); |
| 1748 } | 1626 } |
| 1749 | 1627 |
| 1750 // Verify GetNetworkList(). | 1628 // Verify GetNetworkList(). |
| 1751 TEST(NetUtilTest, GetNetworkList) { | 1629 TEST(NetUtilTest, GetNetworkList) { |
| 1752 NetworkInterfaceList list; | 1630 NetworkInterfaceList list; |
| 1753 ASSERT_TRUE(GetNetworkList(&list, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)); | 1631 ASSERT_TRUE(GetNetworkList(&list, INCLUDE_HOST_SCOPE_VIRTUAL_INTERFACES)); |
| 1754 for (NetworkInterfaceList::iterator it = list.begin(); | 1632 for (NetworkInterfaceList::iterator it = list.begin(); it != list.end(); |
| 1755 it != list.end(); ++it) { | 1633 ++it) { |
| 1756 // Verify that the names are not empty. | 1634 // Verify that the names are not empty. |
| 1757 EXPECT_FALSE(it->name.empty()); | 1635 EXPECT_FALSE(it->name.empty()); |
| 1758 EXPECT_FALSE(it->friendly_name.empty()); | 1636 EXPECT_FALSE(it->friendly_name.empty()); |
| 1759 | 1637 |
| 1760 // Verify that the address is correct. | 1638 // Verify that the address is correct. |
| 1761 EXPECT_TRUE(it->address.size() == kIPv4AddressSize || | 1639 EXPECT_TRUE(it->address.size() == kIPv4AddressSize || |
| 1762 it->address.size() == kIPv6AddressSize) | 1640 it->address.size() == kIPv6AddressSize) |
| 1763 << "Invalid address of size " << it->address.size(); | 1641 << "Invalid address of size " << it->address.size(); |
| 1764 bool all_zeroes = true; | 1642 bool all_zeroes = true; |
| 1765 for (size_t i = 0; i < it->address.size(); ++i) { | 1643 for (size_t i = 0; i < it->address.size(); ++i) { |
| 1766 if (it->address[i] != 0) { | 1644 if (it->address[i] != 0) { |
| 1767 all_zeroes = false; | 1645 all_zeroes = false; |
| 1768 break; | 1646 break; |
| 1769 } | 1647 } |
| 1770 } | 1648 } |
| 1771 EXPECT_FALSE(all_zeroes); | 1649 EXPECT_FALSE(all_zeroes); |
| 1772 EXPECT_GT(it->network_prefix, 1u); | 1650 EXPECT_GT(it->network_prefix, 1u); |
| 1773 EXPECT_LE(it->network_prefix, it->address.size() * 8); | 1651 EXPECT_LE(it->network_prefix, it->address.size() * 8); |
| 1774 | 1652 |
| 1775 #if defined(OS_WIN) | 1653 #if defined(OS_WIN) |
| 1776 // On Windows |name| is NET_LUID. | 1654 // On Windows |name| is NET_LUID. |
| 1777 base::ScopedNativeLibrary phlpapi_lib( | 1655 base::ScopedNativeLibrary phlpapi_lib( |
| 1778 base::FilePath(FILE_PATH_LITERAL("iphlpapi.dll"))); | 1656 base::FilePath(FILE_PATH_LITERAL("iphlpapi.dll"))); |
| 1779 ASSERT_TRUE(phlpapi_lib.is_valid()); | 1657 ASSERT_TRUE(phlpapi_lib.is_valid()); |
| 1780 typedef NETIO_STATUS (WINAPI* ConvertInterfaceIndexToLuid)(NET_IFINDEX, | 1658 typedef NETIO_STATUS(WINAPI * ConvertInterfaceIndexToLuid)(NET_IFINDEX, |
| 1781 PNET_LUID); | 1659 PNET_LUID); |
| 1782 ConvertInterfaceIndexToLuid interface_to_luid = | 1660 ConvertInterfaceIndexToLuid interface_to_luid = |
| 1783 reinterpret_cast<ConvertInterfaceIndexToLuid>( | 1661 reinterpret_cast<ConvertInterfaceIndexToLuid>( |
| 1784 phlpapi_lib.GetFunctionPointer("ConvertInterfaceIndexToLuid")); | 1662 phlpapi_lib.GetFunctionPointer("ConvertInterfaceIndexToLuid")); |
| 1785 | 1663 |
| 1786 typedef NETIO_STATUS (WINAPI* ConvertInterfaceLuidToGuid)(NET_LUID*, | 1664 typedef NETIO_STATUS(WINAPI * ConvertInterfaceLuidToGuid)(NET_LUID*, GUID*); |
| 1787 GUID*); | |
| 1788 ConvertInterfaceLuidToGuid luid_to_guid = | 1665 ConvertInterfaceLuidToGuid luid_to_guid = |
| 1789 reinterpret_cast<ConvertInterfaceLuidToGuid>( | 1666 reinterpret_cast<ConvertInterfaceLuidToGuid>( |
| 1790 phlpapi_lib.GetFunctionPointer("ConvertInterfaceLuidToGuid")); | 1667 phlpapi_lib.GetFunctionPointer("ConvertInterfaceLuidToGuid")); |
| 1791 | 1668 |
| 1792 if (interface_to_luid && luid_to_guid) { | 1669 if (interface_to_luid && luid_to_guid) { |
| 1793 NET_LUID luid; | 1670 NET_LUID luid; |
| 1794 EXPECT_EQ(interface_to_luid(it->interface_index, &luid), NO_ERROR); | 1671 EXPECT_EQ(interface_to_luid(it->interface_index, &luid), NO_ERROR); |
| 1795 GUID guid; | 1672 GUID guid; |
| 1796 EXPECT_EQ(luid_to_guid(&luid, &guid), NO_ERROR); | 1673 EXPECT_EQ(luid_to_guid(&luid, &guid), NO_ERROR); |
| 1797 LPOLESTR name; | 1674 LPOLESTR name; |
| 1798 StringFromCLSID(guid, &name); | 1675 StringFromCLSID(guid, &name); |
| 1799 EXPECT_STREQ(base::UTF8ToWide(it->name).c_str(), name); | 1676 EXPECT_STREQ(base::UTF8ToWide(it->name).c_str(), name); |
| 1800 CoTaskMemFree(name); | 1677 CoTaskMemFree(name); |
| 1801 continue; | 1678 continue; |
| 1802 } else { | 1679 } else { |
| 1803 EXPECT_LT(base::win::GetVersion(), base::win::VERSION_VISTA); | 1680 EXPECT_LT(base::win::GetVersion(), base::win::VERSION_VISTA); |
| 1804 EXPECT_LT(it->interface_index, 1u << 24u); // Must fit 0.x.x.x. | 1681 EXPECT_LT(it->interface_index, 1u << 24u); // Must fit 0.x.x.x. |
| 1805 EXPECT_NE(it->interface_index, 0u); // 0 means to use default. | 1682 EXPECT_NE(it->interface_index, 0u); // 0 means to use default. |
| 1806 } | 1683 } |
| 1807 if (it->type == NetworkChangeNotifier::CONNECTION_WIFI) { | 1684 if (it->type == NetworkChangeNotifier::CONNECTION_WIFI) { |
| 1808 EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol()); | 1685 EXPECT_NE(WIFI_PHY_LAYER_PROTOCOL_NONE, GetWifiPHYLayerProtocol()); |
| 1809 } | 1686 } |
| 1810 #elif !defined(OS_ANDROID) | 1687 #elif !defined(OS_ANDROID) |
| 1811 char name[IF_NAMESIZE]; | 1688 char name[IF_NAMESIZE]; |
| 1812 EXPECT_TRUE(if_indextoname(it->interface_index, name)); | 1689 EXPECT_TRUE(if_indextoname(it->interface_index, name)); |
| 1813 EXPECT_STREQ(it->name.c_str(), name); | 1690 EXPECT_STREQ(it->name.c_str(), name); |
| 1814 #endif | 1691 #endif |
| 1815 } | 1692 } |
| 1816 } | 1693 } |
| 1817 | 1694 |
| 1818 struct NonUniqueNameTestData { | 1695 struct NonUniqueNameTestData { |
| 1819 bool is_unique; | 1696 bool is_unique; |
| 1820 const char* hostname; | 1697 const char* hostname; |
| 1821 }; | 1698 }; |
| 1822 | 1699 |
| 1823 // Google Test pretty-printer. | 1700 // Google Test pretty-printer. |
| 1824 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { | 1701 void PrintTo(const NonUniqueNameTestData& data, std::ostream* os) { |
| 1825 ASSERT_TRUE(data.hostname); | 1702 ASSERT_TRUE(data.hostname); |
| 1826 *os << " hostname: " << testing::PrintToString(data.hostname) | 1703 *os << " hostname: " << testing::PrintToString(data.hostname) |
| 1827 << "; is_unique: " << testing::PrintToString(data.is_unique); | 1704 << "; is_unique: " << testing::PrintToString(data.is_unique); |
| 1828 } | 1705 } |
| 1829 | 1706 |
| 1830 const NonUniqueNameTestData kNonUniqueNameTestData[] = { | 1707 const NonUniqueNameTestData kNonUniqueNameTestData[] = { |
| 1831 // Domains under ICANN-assigned domains. | 1708 // Domains under ICANN-assigned domains. |
| 1832 { true, "google.com" }, | 1709 {true, "google.com"}, |
| 1833 { true, "google.co.uk" }, | 1710 {true, "google.co.uk"}, |
| 1834 // Domains under private registries. | 1711 // Domains under private registries. |
| 1835 { true, "appspot.com" }, | 1712 {true, "appspot.com"}, |
| 1836 { true, "test.appspot.com" }, | 1713 {true, "test.appspot.com"}, |
| 1837 // Unreserved IPv4 addresses (in various forms). | 1714 // Unreserved IPv4 addresses (in various forms). |
| 1838 { true, "8.8.8.8" }, | 1715 {true, "8.8.8.8"}, |
| 1839 { true, "99.64.0.0" }, | 1716 {true, "99.64.0.0"}, |
| 1840 { true, "212.15.0.0" }, | 1717 {true, "212.15.0.0"}, |
| 1841 { true, "212.15" }, | 1718 {true, "212.15"}, |
| 1842 { true, "212.15.0" }, | 1719 {true, "212.15.0"}, |
| 1843 { true, "3557752832" }, | 1720 {true, "3557752832"}, |
| 1844 // Reserved IPv4 addresses (in various forms). | 1721 // Reserved IPv4 addresses (in various forms). |
| 1845 { false, "192.168.0.0" }, | 1722 {false, "192.168.0.0"}, |
| 1846 { false, "192.168.0.6" }, | 1723 {false, "192.168.0.6"}, |
| 1847 { false, "10.0.0.5" }, | 1724 {false, "10.0.0.5"}, |
| 1848 { false, "10.0" }, | 1725 {false, "10.0"}, |
| 1849 { false, "10.0.0" }, | 1726 {false, "10.0.0"}, |
| 1850 { false, "3232235526" }, | 1727 {false, "3232235526"}, |
| 1851 // Unreserved IPv6 addresses. | 1728 // Unreserved IPv6 addresses. |
| 1852 { true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, | 1729 {true, "FFC0:ba98:7654:3210:FEDC:BA98:7654:3210"}, |
| 1853 { true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210" }, | 1730 {true, "2000:ba98:7654:2301:EFCD:BA98:7654:3210"}, |
| 1854 // Reserved IPv6 addresses. | 1731 // Reserved IPv6 addresses. |
| 1855 { false, "::192.9.5.5" }, | 1732 {false, "::192.9.5.5"}, |
| 1856 { false, "FEED::BEEF" }, | 1733 {false, "FEED::BEEF"}, |
| 1857 { false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210" }, | 1734 {false, "FEC0:ba98:7654:3210:FEDC:BA98:7654:3210"}, |
| 1858 // 'internal'/non-IANA assigned domains. | 1735 // 'internal'/non-IANA assigned domains. |
| 1859 { false, "intranet" }, | 1736 {false, "intranet"}, |
| 1860 { false, "intranet." }, | 1737 {false, "intranet."}, |
| 1861 { false, "intranet.example" }, | 1738 {false, "intranet.example"}, |
| 1862 { false, "host.intranet.example" }, | 1739 {false, "host.intranet.example"}, |
| 1863 // gTLDs under discussion, but not yet assigned. | 1740 // gTLDs under discussion, but not yet assigned. |
| 1864 { false, "intranet.corp" }, | 1741 {false, "intranet.corp"}, |
| 1865 { false, "example.tech" }, | 1742 {false, "example.tech"}, |
| 1866 { false, "intranet.internal" }, | 1743 {false, "intranet.internal"}, |
| 1867 // Invalid host names are treated as unique - but expected to be | 1744 // Invalid host names are treated as unique - but expected to be |
| 1868 // filtered out before then. | 1745 // filtered out before then. |
| 1869 { true, "junk)(£)$*!@~#" }, | 1746 {true, "junk)(£)$*!@~#"}, |
| 1870 { true, "w$w.example.com" }, | 1747 {true, "w$w.example.com"}, |
| 1871 { true, "nocolonsallowed:example" }, | 1748 {true, "nocolonsallowed:example"}, |
| 1872 { true, "[::4.5.6.9]" }, | 1749 {true, "[::4.5.6.9]"}, |
| 1873 }; | 1750 }; |
| 1874 | 1751 |
| 1875 class NetUtilNonUniqueNameTest | 1752 class NetUtilNonUniqueNameTest |
| 1876 : public testing::TestWithParam<NonUniqueNameTestData> { | 1753 : public testing::TestWithParam<NonUniqueNameTestData> { |
| 1877 public: | 1754 public: |
| 1878 virtual ~NetUtilNonUniqueNameTest() {} | 1755 virtual ~NetUtilNonUniqueNameTest() {} |
| 1879 | 1756 |
| 1880 protected: | 1757 protected: |
| 1881 bool IsUnique(const std::string& hostname) { | 1758 bool IsUnique(const std::string& hostname) { |
| 1882 return !IsHostnameNonUnique(hostname); | 1759 return !IsHostnameNonUnique(hostname); |
| 1883 } | 1760 } |
| 1884 }; | 1761 }; |
| 1885 | 1762 |
| 1886 // Test that internal/non-unique names are properly identified as such, but | 1763 // Test that internal/non-unique names are properly identified as such, but |
| 1887 // that IP addresses and hosts beneath registry-controlled domains are flagged | 1764 // that IP addresses and hosts beneath registry-controlled domains are flagged |
| 1888 // as unique names. | 1765 // as unique names. |
| 1889 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { | 1766 TEST_P(NetUtilNonUniqueNameTest, IsHostnameNonUnique) { |
| 1890 const NonUniqueNameTestData& test_data = GetParam(); | 1767 const NonUniqueNameTestData& test_data = GetParam(); |
| 1891 | 1768 |
| 1892 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); | 1769 EXPECT_EQ(test_data.is_unique, IsUnique(test_data.hostname)); |
| 1893 } | 1770 } |
| 1894 | 1771 |
| 1895 INSTANTIATE_TEST_CASE_P(, NetUtilNonUniqueNameTest, | 1772 INSTANTIATE_TEST_CASE_P(, |
| 1773 NetUtilNonUniqueNameTest, |
| 1896 testing::ValuesIn(kNonUniqueNameTestData)); | 1774 testing::ValuesIn(kNonUniqueNameTestData)); |
| 1897 | 1775 |
| 1898 } // namespace net | 1776 } // namespace net |
| OLD | NEW |