| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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 "base/basictypes.h" | 5 #include "base/basictypes.h" |
| 6 #include "base/file_path.h" |
| 6 #include "base/string_util.h" | 7 #include "base/string_util.h" |
| 7 #include "base/time.h" | 8 #include "base/time.h" |
| 8 #include "googleurl/src/gurl.h" | 9 #include "googleurl/src/gurl.h" |
| 9 #include "net/base/net_util.h" | 10 #include "net/base/net_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 #if defined(OS_WIN) | 13 #if defined(OS_WIN) |
| 13 #include <ws2tcpip.h> | 14 #include <ws2tcpip.h> |
| 14 #else | 15 #else |
| 15 #include <netdb.h> | 16 #include <netdb.h> |
| 16 #endif | 17 #endif |
| 17 | 18 |
| 18 namespace { | 19 namespace { |
| 19 | 20 |
| 20 class NetUtilTest : public testing::Test { | 21 class NetUtilTest : public testing::Test { |
| 21 }; | 22 }; |
| 22 | 23 |
| 23 struct FileCase { | 24 struct FileCase { |
| 24 const wchar_t* file; | 25 const wchar_t* file; |
| 25 const wchar_t* url; | 26 const char* url; |
| 26 }; | 27 }; |
| 27 | 28 |
| 28 struct HeaderCase { | 29 struct HeaderCase { |
| 29 const wchar_t* header_name; | 30 const wchar_t* header_name; |
| 30 const wchar_t* expected; | 31 const wchar_t* expected; |
| 31 }; | 32 }; |
| 32 | 33 |
| 33 struct HeaderParamCase { | 34 struct HeaderParamCase { |
| 34 const wchar_t* header_name; | 35 const wchar_t* header_name; |
| 35 const wchar_t* param_name; | 36 const wchar_t* param_name; |
| (...skipping 71 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 107 ai->ai_addr = (sockaddr*)addr6; | 108 ai->ai_addr = (sockaddr*)addr6; |
| 108 return ai; | 109 return ai; |
| 109 } | 110 } |
| 110 | 111 |
| 111 } // anonymous namespace | 112 } // anonymous namespace |
| 112 | 113 |
| 113 TEST(NetUtilTest, FileURLConversion) { | 114 TEST(NetUtilTest, FileURLConversion) { |
| 114 // a list of test file names and the corresponding URLs | 115 // a list of test file names and the corresponding URLs |
| 115 const FileCase round_trip_cases[] = { | 116 const FileCase round_trip_cases[] = { |
| 116 #if defined(OS_WIN) | 117 #if defined(OS_WIN) |
| 117 {L"C:\\foo\\bar.txt", L"file:///C:/foo/bar.txt"}, | 118 {L"C:\\foo\\bar.txt", "file:///C:/foo/bar.txt"}, |
| 118 {L"\\\\some computer\\foo\\bar.txt", | 119 {L"\\\\some computer\\foo\\bar.txt", |
| 119 L"file://some%20computer/foo/bar.txt"}, // UNC | 120 "file://some%20computer/foo/bar.txt"}, // UNC |
| 120 {L"D:\\Name;with%some symbols*#", | 121 {L"D:\\Name;with%some symbols*#", |
| 121 L"file:///D:/Name%3Bwith%25some%20symbols*%23"}, | 122 "file:///D:/Name%3Bwith%25some%20symbols*%23"}, |
| 122 {L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", | 123 {L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", |
| 123 L"file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91" | 124 "file:///D:/Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD%91" |
| 124 L"%E9%A1%B5.doc"}, | 125 "%E9%A1%B5.doc"}, |
| 125 #elif defined(OS_POSIX) | 126 #elif defined(OS_POSIX) |
| 126 {L"/foo/bar.txt", L"file:///foo/bar.txt"}, | 127 {L"/foo/bar.txt", "file:///foo/bar.txt"}, |
| 127 {L"/foo/BAR.txt", L"file:///foo/BAR.txt"}, | 128 {L"/foo/BAR.txt", "file:///foo/BAR.txt"}, |
| 128 {L"/C:/foo/bar.txt", L"file:///C:/foo/bar.txt"}, | 129 {L"/C:/foo/bar.txt", "file:///C:/foo/bar.txt"}, |
| 129 {L"/some computer/foo/bar.txt", L"file:///some%20computer/foo/bar.txt"}, | 130 {L"/some computer/foo/bar.txt", "file:///some%20computer/foo/bar.txt"}, |
| 130 {L"/Name;with%some symbols*#", L"file:///Name%3Bwith%25some%20symbols*%23"}, | 131 {L"/Name;with%some symbols*#", "file:///Name%3Bwith%25some%20symbols*%23"}, |
| 131 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", | 132 {L"/Chinese/\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc", |
| 132 L"file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD" | 133 "file:///Chinese/%E6%89%80%E6%9C%89%E4%B8%AD%E6%96%87%E7%BD" |
| 133 L"%91%E9%A1%B5.doc"}, | 134 "%91%E9%A1%B5.doc"}, |
| 134 #endif | 135 #endif |
| 135 }; | 136 }; |
| 136 | 137 |
| 137 // First, we'll test that we can round-trip all of the above cases of URLs | 138 // First, we'll test that we can round-trip all of the above cases of URLs |
| 138 std::wstring output; | 139 FilePath output; |
| 139 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(round_trip_cases); i++) { | 140 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(round_trip_cases); i++) { |
| 140 // convert to the file URL | 141 // convert to the file URL |
| 141 GURL file_url(net::FilePathToFileURL(round_trip_cases[i].file)); | 142 GURL file_url(net::FilePathToFileURL( |
| 142 EXPECT_EQ(std::wstring(round_trip_cases[i].url), | 143 FilePath::FromWStringHack(round_trip_cases[i].file))); |
| 143 UTF8ToWide(file_url.spec())); | 144 EXPECT_EQ(round_trip_cases[i].url, file_url.spec()); |
| 144 | 145 |
| 145 // Back to the filename. | 146 // Back to the filename. |
| 146 EXPECT_TRUE(net::FileURLToFilePath(file_url, &output)); | 147 EXPECT_TRUE(net::FileURLToFilePath(file_url, &output)); |
| 147 EXPECT_EQ(std::wstring(round_trip_cases[i].file), output); | 148 EXPECT_EQ(round_trip_cases[i].file, output.ToWStringHack()); |
| 148 } | 149 } |
| 149 | 150 |
| 150 // Test that various file: URLs get decoded into the correct file type | 151 // Test that various file: URLs get decoded into the correct file type |
| 151 FileCase url_cases[] = { | 152 FileCase url_cases[] = { |
| 152 #if defined(OS_WIN) | 153 #if defined(OS_WIN) |
| 153 {L"C:\\foo\\bar.txt", L"file:c|/foo\\bar.txt"}, | 154 {L"C:\\foo\\bar.txt", "file:c|/foo\\bar.txt"}, |
| 154 {L"C:\\foo\\bar.txt", L"file:/c:/foo/bar.txt"}, | 155 {L"C:\\foo\\bar.txt", "file:/c:/foo/bar.txt"}, |
| 155 {L"\\\\foo\\bar.txt", L"file://foo\\bar.txt"}, | 156 {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"}, |
| 156 {L"C:\\foo\\bar.txt", L"file:///c:/foo/bar.txt"}, | 157 {L"C:\\foo\\bar.txt", "file:///c:/foo/bar.txt"}, |
| 157 {L"\\\\foo\\bar.txt", L"file:////foo\\bar.txt"}, | 158 {L"\\\\foo\\bar.txt", "file:////foo\\bar.txt"}, |
| 158 {L"\\\\foo\\bar.txt", L"file:/foo/bar.txt"}, | 159 {L"\\\\foo\\bar.txt", "file:/foo/bar.txt"}, |
| 159 {L"\\\\foo\\bar.txt", L"file://foo\\bar.txt"}, | 160 {L"\\\\foo\\bar.txt", "file://foo\\bar.txt"}, |
| 160 {L"C:\\foo\\bar.txt", L"file:\\\\\\c:/foo/bar.txt"}, | 161 {L"C:\\foo\\bar.txt", "file:\\\\\\c:/foo/bar.txt"}, |
| 161 #elif defined(OS_POSIX) | 162 #elif defined(OS_POSIX) |
| 162 {L"/c:/foo/bar.txt", L"file:/c:/foo/bar.txt"}, | 163 {L"/c:/foo/bar.txt", "file:/c:/foo/bar.txt"}, |
| 163 {L"/c:/foo/bar.txt", L"file:///c:/foo/bar.txt"}, | 164 {L"/c:/foo/bar.txt", "file:///c:/foo/bar.txt"}, |
| 164 {L"/foo/bar.txt", L"file:/foo/bar.txt"}, | 165 {L"/foo/bar.txt", "file:/foo/bar.txt"}, |
| 165 {L"/c:/foo/bar.txt", L"file:\\\\\\c:/foo/bar.txt"}, | 166 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, |
| 166 {L"/foo/bar.txt", L"file:foo/bar.txt"}, | 167 {L"/foo/bar.txt", "file:foo/bar.txt"}, |
| 167 {L"/bar.txt", L"file://foo/bar.txt"}, | 168 {L"/bar.txt", "file://foo/bar.txt"}, |
| 168 {L"/foo/bar.txt", L"file:///foo/bar.txt"}, | 169 {L"/foo/bar.txt", "file:///foo/bar.txt"}, |
| 169 {L"/foo/bar.txt", L"file:////foo/bar.txt"}, | 170 {L"/foo/bar.txt", "file:////foo/bar.txt"}, |
| 170 {L"/foo/bar.txt", L"file:////foo//bar.txt"}, | 171 {L"/foo/bar.txt", "file:////foo//bar.txt"}, |
| 171 {L"/foo/bar.txt", L"file:////foo///bar.txt"}, | 172 {L"/foo/bar.txt", "file:////foo///bar.txt"}, |
| 172 {L"/foo/bar.txt", L"file:////foo////bar.txt"}, | 173 {L"/foo/bar.txt", "file:////foo////bar.txt"}, |
| 173 {L"/c:/foo/bar.txt", L"file:\\\\\\c:/foo/bar.txt"}, | 174 {L"/c:/foo/bar.txt", "file:\\\\\\c:/foo/bar.txt"}, |
| 174 {L"/c:/foo/bar.txt", L"file:c:/foo/bar.txt"}, | 175 {L"/c:/foo/bar.txt", "file:c:/foo/bar.txt"}, |
| 175 // We get these wrong because GURL turns back slashes into forward | 176 // We get these wrong because GURL turns back slashes into forward |
| 176 // slashes. | 177 // slashes. |
| 177 //{L"/foo%5Cbar.txt", L"file://foo\\bar.txt"}, | 178 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
| 178 //{L"/c|/foo%5Cbar.txt", L"file:c|/foo\\bar.txt"}, | 179 //{L"/c|/foo%5Cbar.txt", "file:c|/foo\\bar.txt"}, |
| 179 //{L"/foo%5Cbar.txt", L"file://foo\\bar.txt"}, | 180 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
| 180 //{L"/foo%5Cbar.txt", L"file:////foo\\bar.txt"}, | 181 //{L"/foo%5Cbar.txt", "file:////foo\\bar.txt"}, |
| 181 //{L"/foo%5Cbar.txt", L"file://foo\\bar.txt"}, | 182 //{L"/foo%5Cbar.txt", "file://foo\\bar.txt"}, |
| 182 #endif | 183 #endif |
| 183 }; | 184 }; |
| 184 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_cases); i++) { | 185 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(url_cases); i++) { |
| 185 net::FileURLToFilePath(GURL(WideToUTF8(url_cases[i].url)), &output); | 186 net::FileURLToFilePath(GURL(url_cases[i].url), &output); |
| 186 EXPECT_EQ(std::wstring(url_cases[i].file), output); | 187 EXPECT_EQ(url_cases[i].file, output.ToWStringHack()); |
| 187 } | 188 } |
| 188 | 189 |
| 189 // Here, we test that UTF-8 encoded strings get decoded properly, even when | 190 // Here, we test that UTF-8 encoded strings get decoded properly, even when |
| 190 // they might be stored with wide characters. On posix systems, just treat | 191 // they might be stored with wide characters. On posix systems, just treat |
| 191 // this as a stream of bytes. | 192 // this as a stream of bytes. |
| 192 const wchar_t utf8[] = L"file:///d:/Chinese/\xe6\x89\x80\xe6\x9c\x89\xe4\xb8" | 193 const wchar_t utf8[] = L"file:///d:/Chinese/\xe6\x89\x80\xe6\x9c\x89\xe4\xb8" |
| 193 L"\xad\xe6\x96\x87\xe7\xbd\x91\xe9\xa1\xb5.doc"; | 194 L"\xad\xe6\x96\x87\xe7\xbd\x91\xe9\xa1\xb5.doc"; |
| 194 #if defined(OS_WIN) | 195 #if defined(OS_WIN) |
| 195 const wchar_t wide[] = | 196 const wchar_t wide[] = |
| 196 L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc"; | 197 L"D:\\Chinese\\\x6240\x6709\x4e2d\x6587\x7f51\x9875.doc"; |
| 197 #elif defined(OS_POSIX) | 198 #elif defined(OS_POSIX) |
| 198 const wchar_t wide[] = L"/d:/Chinese/\xe6\x89\x80\xe6\x9c\x89\xe4\xb8\xad\xe6" | 199 const wchar_t wide[] = L"/d:/Chinese/\xe6\x89\x80\xe6\x9c\x89\xe4\xb8\xad\xe6" |
| 199 L"\x96\x87\xe7\xbd\x91\xe9\xa1\xb5.doc"; | 200 L"\x96\x87\xe7\xbd\x91\xe9\xa1\xb5.doc"; |
| 200 #endif | 201 #endif |
| 201 EXPECT_TRUE(net::FileURLToFilePath(GURL(WideToUTF8(utf8)), &output)); | 202 EXPECT_TRUE(net::FileURLToFilePath(GURL(WideToUTF8(utf8)), &output)); |
| 202 EXPECT_EQ(std::wstring(wide), output); | 203 EXPECT_EQ(wide, output.ToWStringHack()); |
| 203 | 204 |
| 204 // Unfortunately, UTF8ToWide discards invalid UTF8 input. | 205 // Unfortunately, UTF8ToWide discards invalid UTF8 input. |
| 205 #ifdef BUG_878908_IS_FIXED | 206 #ifdef BUG_878908_IS_FIXED |
| 206 // Test that no conversion happens if the UTF-8 input is invalid, and that | 207 // Test that no conversion happens if the UTF-8 input is invalid, and that |
| 207 // the input is preserved in UTF-8 | 208 // the input is preserved in UTF-8 |
| 208 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; | 209 const char invalid_utf8[] = "file:///d:/Blah/\xff.doc"; |
| 209 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; | 210 const wchar_t invalid_wide[] = L"D:\\Blah\\\xff.doc"; |
| 210 EXPECT_TRUE(net::FileURLToFilePath( | 211 EXPECT_TRUE(net::FileURLToFilePath( |
| 211 GURL(std::string(invalid_utf8)), &output)); | 212 GURL(std::string(invalid_utf8)), &output)); |
| 212 EXPECT_EQ(std::wstring(invalid_wide), output); | 213 EXPECT_EQ(std::wstring(invalid_wide), output); |
| (...skipping 670 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 883 } | 884 } |
| 884 } | 885 } |
| 885 | 886 |
| 886 TEST(NetUtilTest, GetHostName) { | 887 TEST(NetUtilTest, GetHostName) { |
| 887 // We can't check the result of GetHostName() directly, since the result | 888 // We can't check the result of GetHostName() directly, since the result |
| 888 // will differ across machines. Our goal here is to simply exercise the | 889 // will differ across machines. Our goal here is to simply exercise the |
| 889 // code path, and check that things "look about right". | 890 // code path, and check that things "look about right". |
| 890 std::string hostname = net::GetHostName(); | 891 std::string hostname = net::GetHostName(); |
| 891 EXPECT_FALSE(hostname.empty()); | 892 EXPECT_FALSE(hostname.empty()); |
| 892 } | 893 } |
| OLD | NEW |