| 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/http/http_content_disposition.h" | 5 #include "net/http/http_content_disposition.h" |
| 6 | 6 |
| 7 #include "base/utf_string_conversions.h" | 7 #include "base/utf_string_conversions.h" |
| 8 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 9 | 9 |
| 10 namespace net { | 10 namespace net { |
| 11 | 11 |
| 12 namespace { | 12 namespace { |
| 13 | 13 |
| 14 struct FileNameCDCase { | 14 struct FileNameCDCase { |
| 15 const char* header; | 15 const char* header; |
| 16 const char* referrer_charset; | 16 const char* default_charset; |
| 17 const wchar_t* expected; | 17 const wchar_t* expected; |
| 18 }; | 18 }; |
| 19 | 19 |
| 20 } // anonymous namespace | 20 } // anonymous namespace |
| 21 | 21 |
| 22 TEST(HttpContentDispositionTest, Filename) { | 22 TEST(HttpContentDispositionTest, Filename) { |
| 23 const FileNameCDCase tests[] = { | 23 const FileNameCDCase tests[] = { |
| 24 // Test various forms of C-D header fields emitted by web servers. | 24 // Test various forms of C-D header fields emitted by web servers. |
| 25 {"inline; filename=\"abcde.pdf\"", "", L"abcde.pdf"}, | 25 {"inline; filename=\"abcde.pdf\"", "", L"abcde.pdf"}, |
| 26 {"inline; name=\"abcde.pdf\"", "", L"abcde.pdf"}, | 26 {"inline; name=\"abcde.pdf\"", "", L"abcde.pdf"}, |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 63 {"inline; filename=\"=?iso88591?Q?caf=E9_?= .png\"", | 63 {"inline; filename=\"=?iso88591?Q?caf=E9_?= .png\"", |
| 64 "", L"caf\x00e9 .png"}, | 64 "", L"caf\x00e9 .png"}, |
| 65 // Two encoded words with different charsets (not very likely to be emitted | 65 // Two encoded words with different charsets (not very likely to be emitted |
| 66 // by web servers in the wild). Spaces between them are removed. | 66 // by web servers in the wild). Spaces between them are removed. |
| 67 {"inline; filename=\"=?euc-kr?b?v7m8+iAz?=" | 67 {"inline; filename=\"=?euc-kr?b?v7m8+iAz?=" |
| 68 " =?ksc5601?q?=BF=B9=BC=FA=2Epng?=\"", "", | 68 " =?ksc5601?q?=BF=B9=BC=FA=2Epng?=\"", "", |
| 69 L"\xc608\xc220 3\xc608\xc220.png"}, | 69 L"\xc608\xc220 3\xc608\xc220.png"}, |
| 70 {"attachment; filename=\"=?windows-1252?Q?caf=E9?=" | 70 {"attachment; filename=\"=?windows-1252?Q?caf=E9?=" |
| 71 " =?iso-8859-7?b?4eI=?= .png\"", "", L"caf\x00e9\x03b1\x03b2.png"}, | 71 " =?iso-8859-7?b?4eI=?= .png\"", "", L"caf\x00e9\x03b1\x03b2.png"}, |
| 72 // Non-ASCII string is passed through and treated as UTF-8 as long as | 72 // Non-ASCII string is passed through and treated as UTF-8 as long as |
| 73 // it's valid as UTF-8 and regardless of |referrer_charset|. | 73 // it's valid as UTF-8 and regardless of |default_charset|. |
| 74 {"attachment; filename=caf\xc3\xa9.png", | 74 {"attachment; filename=caf\xc3\xa9.png", |
| 75 "iso-8859-1", L"caf\x00e9.png"}, | 75 "iso-8859-1", L"caf\x00e9.png"}, |
| 76 {"attachment; filename=caf\xc3\xa9.png", | 76 {"attachment; filename=caf\xc3\xa9.png", |
| 77 "", L"caf\x00e9.png"}, | 77 "", L"caf\x00e9.png"}, |
| 78 // Non-ASCII/Non-UTF-8 string. Fall back to the referrer charset. | 78 // Non-ASCII/Non-UTF-8 string. Fall back to the referrer charset. |
| 79 {"attachment; filename=caf\xe5.png", | 79 {"attachment; filename=caf\xe5.png", |
| 80 "windows-1253", L"caf\x03b5.png"}, | 80 "windows-1253", L"caf\x03b5.png"}, |
| 81 #if 0 | 81 #if 0 |
| 82 // Non-ASCII/Non-UTF-8 string. Fall back to the native codepage. | 82 // Non-ASCII/Non-UTF-8 string. Fall back to the native codepage. |
| 83 // TODO(jungshik): We need to set the OS default codepage | 83 // TODO(jungshik): We need to set the OS default codepage |
| (...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 184 {"attachment; filename=\"foo-ae.html\"; " | 184 {"attachment; filename=\"foo-ae.html\"; " |
| 185 "filename*=UTF-8''foo-%c3%a4.html", "", L"foo-\xe4.html"}, | 185 "filename*=UTF-8''foo-%c3%a4.html", "", L"foo-\xe4.html"}, |
| 186 // attfnboth2 | 186 // attfnboth2 |
| 187 {"attachment; filename*=UTF-8''foo-%c3%a4.html; " | 187 {"attachment; filename*=UTF-8''foo-%c3%a4.html; " |
| 188 "filename=\"foo-ae.html\"", "", L"foo-\xe4.html"}, | 188 "filename=\"foo-ae.html\"", "", L"foo-\xe4.html"}, |
| 189 // attnewandfn | 189 // attnewandfn |
| 190 {"attachment; foobar=x; filename=\"foo.html\"", "", | 190 {"attachment; foobar=x; filename=\"foo.html\"", "", |
| 191 L"foo.html"}, | 191 L"foo.html"}, |
| 192 }; | 192 }; |
| 193 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 193 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 194 HttpContentDisposition header(tests[i].header, tests[i].referrer_charset); | 194 HttpContentDisposition header(tests[i].header, tests[i].default_charset); |
| 195 EXPECT_EQ(tests[i].expected, | 195 EXPECT_EQ(tests[i].expected, |
| 196 UTF8ToWide(header.filename())) | 196 UTF8ToWide(header.filename())) |
| 197 << "Failed on input: " << tests[i].header; | 197 << "Failed on input: " << tests[i].header; |
| 198 } | 198 } |
| 199 } | 199 } |
| 200 | 200 |
| 201 // Test cases from http://greenbytes.de/tech/tc2231/ | 201 // Test cases from http://greenbytes.de/tech/tc2231/ |
| 202 TEST(HttpContentDispositionTest, tc2231) { | 202 TEST(HttpContentDispositionTest, tc2231) { |
| 203 const struct FileNameCDCase { | 203 const struct FileNameCDCase { |
| 204 const char* header; | 204 const char* header; |
| (...skipping 296 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 501 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { | 501 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) { |
| 502 HttpContentDisposition header(tests[i].header, std::string()); | 502 HttpContentDisposition header(tests[i].header, std::string()); |
| 503 EXPECT_EQ(tests[i].expected_type, header.type()) | 503 EXPECT_EQ(tests[i].expected_type, header.type()) |
| 504 << "Failed on input: " << tests[i].header; | 504 << "Failed on input: " << tests[i].header; |
| 505 EXPECT_EQ(tests[i].expected_filename, UTF8ToWide(header.filename())) | 505 EXPECT_EQ(tests[i].expected_filename, UTF8ToWide(header.filename())) |
| 506 << "Failed on input: " << tests[i].header; | 506 << "Failed on input: " << tests[i].header; |
| 507 } | 507 } |
| 508 } | 508 } |
| 509 | 509 |
| 510 } // namespace net | 510 } // namespace net |
| OLD | NEW |