This source file includes following definitions.
- TEST
- TEST
- TEST
#include "net/http/http_content_disposition.h"
#include "base/strings/utf_string_conversions.h"
#include "testing/gtest/include/gtest/gtest.h"
namespace net {
namespace {
struct FileNameCDCase {
const char* header;
const char* referrer_charset;
const wchar_t* expected;
};
}
TEST(HttpContentDispositionTest, Filename) {
const FileNameCDCase tests[] = {
{"inline; filename=\"abcde.pdf\"", "", L"abcde.pdf"},
{"inline; name=\"abcde.pdf\"", "", L"abcde.pdf"},
{"attachment; filename=abcde.pdf", "", L"abcde.pdf"},
{"attachment; name=abcde.pdf", "", L"abcde.pdf"},
{"attachment; filename=abc,de.pdf", "", L"abc,de.pdf"},
{"filename=abcde.pdf", "", L"abcde.pdf"},
{"filename= abcde.pdf", "", L"abcde.pdf"},
{"filename =abcde.pdf", "", L"abcde.pdf"},
{"filename = abcde.pdf", "", L"abcde.pdf"},
{"filename\t=abcde.pdf", "", L"abcde.pdf"},
{"filename \t\t =abcde.pdf", "", L"abcde.pdf"},
{"name=abcde.pdf", "", L"abcde.pdf"},
{"inline; filename=\"abc%20de.pdf\"", "",
L"abc de.pdf"},
{"filename=\"abcdef.pdf", "", L"abcdef.pdf"},
{"inline; filename=\"abc \t\nde.pdf\"", "",
L"abc de.pdf"},
{"attachment; filename=\"%EC%98%88%EC%88%A0%20"
"%EC%98%88%EC%88%A0.jpg\"", "", L"\xc608\xc220 \xc608\xc220.jpg"},
{"attachment; filename=\"%F0%90%8C%B0%F0%90%8C%B1"
"abc.jpg\"", "", L"\U00010330\U00010331abc.jpg"},
{"attachment; filename=\"%EC%98%88%EC%88%A0 \n"
"%EC%98%88%EC%88%A0.jpg\"", "", L"\xc608\xc220 \xc608\xc220.jpg"},
{"attachment; filename=\"=?EUC-JP?Q?=B7=DD=BD="
"D13=2Epng?=\"", "", L"\x82b8\x8853" L"3.png"},
{"attachment; filename==?eUc-Kr?b?v7m8+iAzLnBuZw==?=",
"", L"\xc608\xc220 3.png"},
{"attachment; filename==?utf-8?Q?=E8=8A=B8=E8"
"=A1=93_3=2Epng?=", "", L"\x82b8\x8853 3.png"},
{"attachment; filename==?utf-8?Q?=F0=90=8C=B0"
"_3=2Epng?=", "", L"\U00010330 3.png"},
{"inline; filename=\"=?iso88591?Q?caf=e9_=2epng?=\"",
"", L"caf\x00e9 .png"},
{"inline; filename=\"=?iso88591?Q?caf=E9_?= .png\"",
"", L"caf\x00e9 .png"},
{"inline; filename=\"=?euc-kr?b?v7m8+iAz?="
" =?ksc5601?q?=BF=B9=BC=FA=2Epng?=\"", "",
L"\xc608\xc220 3\xc608\xc220.png"},
{"attachment; filename=\"=?windows-1252?Q?caf=E9?="
" =?iso-8859-7?b?4eI=?= .png\"", "", L"caf\x00e9\x03b1\x03b2.png"},
{"attachment; filename=caf\xc3\xa9.png",
"iso-8859-1", L"caf\x00e9.png"},
{"attachment; filename=caf\xc3\xa9.png",
"", L"caf\x00e9.png"},
{"attachment; filename=caf\xe5.png",
"windows-1253", L"caf\x03b5.png"},
#if 0
{"attachment; filename=\xb0\xa1\xb0\xa2.png",
"", L"\xac00\xac01.png"},
#endif
{"attachment; filename==?iiso88591?Q?caf=EG?=", "",
L""},
{"attachment; filename==?iso88591?Q?caf=E3?", "", L""},
{"attachment; filename==?iso88591?Q?caf=E3?==",
"", L""},
{"attachment; filename==?iso88591?Q?caf=E3?=?",
"", L""},
{"attachment; filename==?iso88591?Q?caf=E3?=?=",
"", L""},
{"attachment; filename==?windows-1252?Q?=63=61=E?=",
"", L""},
{"attachment; filename=%EC%98%88%EC%88%A", "", L""},
{"attachment; filename=%B7%DD%BD%D1.png", "", L""},
{"attachment; filename==?windows-1252?Q?caf=E3?="
"=?iso-8859-7?b?4eIucG5nCg==?=", "", L""},
{"attachment; filename*=foo.html", "", L""},
{"attachment; filename*=foo'.html", "", L""},
{"attachment; filename*=''foo'.html", "", L""},
{"attachment; filename*=''foo.html'", "", L""},
{"attachment; filename*=''f\"oo\".html'", "", L""},
{"attachment; filename*=bogus_charset''foo.html'",
"", L""},
{"attachment; filename*='en'foo.html'", "", L""},
{"attachment; filename*=iso-8859-1'en'foo.html", "",
L"foo.html"},
{"attachment; filename*=utf-8'en'foo.html", "",
L"foo.html"},
{"attachment; filename*='es'f\xfa.html'", "", L""},
{"attachment; filename*=iso-8859-1'es'f\xfa.html", "",
L""},
{"attachment; filename*=utf-8'es'f\xce\xba.html", "",
L""},
{"inline; filename*=iso88591''cafe foo.png", "",
L"cafe foo.png"},
{"attachment; filename*=EUC-JP''%B7%DD%BD%D13%2Epng",
"", L"\x82b8\x8853" L"3.png"},
{"attachment; filename*=utf-8''"
"%E8%8A%B8%E8%A1%93%203%2Epng", "", L"\x82b8\x8853 3.png"},
{"attachment; filename*=utf-8''%F0%90%8C%B0 3.png", "",
L"\U00010330 3.png"},
{"inline; filename*=Euc-Kr'ko'%BF%B9%BC%FA%2Epng", "",
L"\xc608\xc220.png"},
{"attachment; filename*=windows-1252''caf%E9.png", "",
L"caf\x00e9.png"},
{"attachment; name=\"foo\"; filename=\"bar\"", "", L"bar"},
{"attachment; filename=\"bar\"; name=\"foo\"", "", L"bar"},
{"attachment; filename=\"bar\"; filename*=utf-8''baz", "", L"baz"},
{"attachment; filename*=iso-8859-1''foo-%E4.html", "",
L"foo-\xe4.html"},
{"attachment; filename*="
"UTF-8''foo-%c3%a4-%e2%82%ac.html", "", L"foo-\xe4-\x20ac.html"},
{"attachment; filename*=''foo-%c3%a4-%e2%82%ac.html",
"", L""},
{"attachment; filename*=UTF-8''foo-a%cc%88.html", "",
L"foo-\xe4.html"},
#ifdef ICU_SHOULD_FAIL_CONVERSION_ON_INVALID_CHARACTER
{"attachment; filename*="
"iso-8859-1''foo-%c3%a4-%e2%82%ac.html", "", L""},
#endif
{"attachment; filename *=UTF-8''foo-%c3%a4.html", "",
L""},
{"attachment; filename*= UTF-8''foo-%c3%a4.html", "",
L"foo-\xe4.html"},
{"attachment; filename* =UTF-8''foo-%c3%a4.html", "",
L"foo-\xe4.html"},
{"attachment; filename*=\"UTF-8''foo-%c3%a4.html\"",
"", L""},
{"attachment; filename=\"foo-ae.html\"; "
"filename*=UTF-8''foo-%c3%a4.html", "", L"foo-\xe4.html"},
{"attachment; filename*=UTF-8''foo-%c3%a4.html; "
"filename=\"foo-ae.html\"", "", L"foo-\xe4.html"},
{"attachment; foobar=x; filename=\"foo.html\"", "",
L"foo.html"},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
HttpContentDisposition header(tests[i].header, tests[i].referrer_charset);
EXPECT_EQ(tests[i].expected,
base::UTF8ToWide(header.filename()))
<< "Failed on input: " << tests[i].header;
}
}
TEST(HttpContentDispositionTest, tc2231) {
const struct FileNameCDCase {
const char* header;
net::HttpContentDisposition::Type expected_type;
const wchar_t* expected_filename;
} tests[] = {
{ "inline",
net::HttpContentDisposition::INLINE,
L""
},
{ "\"inline\"",
net::HttpContentDisposition::INLINE,
L""
},
{ "inline; filename=\"foo.html\"",
net::HttpContentDisposition::INLINE,
L"foo.html"
},
{ "inline; filename=\"Not an attachment!\"",
net::HttpContentDisposition::INLINE,
L"Not an attachment!"
},
{ "inline; filename=\"foo.pdf\"",
net::HttpContentDisposition::INLINE,
L"foo.pdf"
},
{ "attachment",
net::HttpContentDisposition::ATTACHMENT,
L""
},
{ "\"attachment\"",
net::HttpContentDisposition::INLINE,
L""
},
{ "ATTACHMENT",
net::HttpContentDisposition::ATTACHMENT,
L""
},
{ "attachment; filename=\"foo.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
{ "attachment; filename=\"f\\oo.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
{ "attachment; filename=\"\\\"quoting\\\" tested.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"\"quoting\" tested.html"
},
{ "attachment; filename=\"Here's a semicolon;.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"Here's a semicolon;.html"
},
{ "attachment; foo=\"bar\"; filename=\"foo.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
{ "attachment; foo=\"\\\"\\\\\";filename=\"foo.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
{ "attachment; FILENAME=\"foo.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
{ "attachment; filename=foo.html",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
{ "attachment; filename=foo.html ;",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
{ "attachment; ;filename=foo",
net::HttpContentDisposition::ATTACHMENT,
L"foo"
},
{ "attachment; filename=foo bar.html",
net::HttpContentDisposition::ATTACHMENT,
L"foo bar.html"
},
{ "attachment; filename='foo.bar'",
net::HttpContentDisposition::ATTACHMENT,
L"foo.bar"
},
#ifdef ICU_SHOULD_FAIL_CONVERSION_ON_INVALID_CHARACTER
{ "attachment; filename=\"foo-\xE4html\"",
net::HttpContentDisposition::ATTACHMENT,
L""
},
#endif
{ "attachment; filename=\"foo-\xC3\xA4.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo-\xE4.html"
},
{ "attachment; filename=\"foo-%41.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo-A.html"
},
{ "attachment; filename=\"50%.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"50%.html"
},
{ "attachment; filename=\"foo-%\\41.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo-A.html"
},
{ "attachment; name=\"foo-%41.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo-A.html"
},
#ifdef ICU_SHOULD_FAIL_CONVERSION_ON_INVALID_CHARACTER
{ "attachment; filename=\"\xE4-%41.html\"",
net::HttpContentDisposition::ATTACHMENT,
L""
},
#endif
{ "attachment; filename=\"foo-%c3%a4-%e2%82%ac.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo-\xE4-\u20AC.html"
},
{ "attachment; filename =\"foo.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
{ "attachment; filename=\"foo.html\"; filename=\"bar.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
{ "attachment; filename=foo[1](2).html",
net::HttpContentDisposition::ATTACHMENT,
L"foo[1](2).html"
},
#ifdef ICU_SHOULD_FAIL_CONVERSION_ON_INVALID_CHARACTER
{ "attachment; filename=foo-\xE4.html",
net::HttpContentDisposition::ATTACHMENT,
L""
},
#endif
{ "attachment; filename=foo-\xC3\xA4.html",
net::HttpContentDisposition::ATTACHMENT,
L"foo-\xE4.html"
},
{ "filename=foo.html",
net::HttpContentDisposition::INLINE,
L"foo.html"
},
{ "x=y; filename=foo.html",
net::HttpContentDisposition::INLINE,
L"foo.html"
},
{ "\"foo; filename=bar;baz\"; filename=qux",
net::HttpContentDisposition::INLINE,
L""
},
{ "filename=foo.html, filename=bar.html",
net::HttpContentDisposition::INLINE,
L"foo.html, filename=bar.html"
},
{ "; filename=foo.html",
net::HttpContentDisposition::INLINE,
L"foo.html"
},
{ "inline; attachment; filename=foo.html",
net::HttpContentDisposition::INLINE,
L""
},
{ "attachment; inline; filename=foo.html",
net::HttpContentDisposition::ATTACHMENT,
L""
},
{ "attachment; filename=\"foo.html\".txt",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html\".txt"
},
{ "attachment; filename=\"bar",
net::HttpContentDisposition::ATTACHMENT,
L"bar"
},
{ "attachment; filename=foo\"bar;baz\"qux",
net::HttpContentDisposition::ATTACHMENT,
L"foo\"bar;baz\"qux"
},
{ "attachment; filename=foo.html, attachment; filename=bar.html",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html, attachment"
},
{ "attachment; foo=foo filename=bar",
net::HttpContentDisposition::ATTACHMENT,
L""
},
{ "filename=foo.html; attachment",
net::HttpContentDisposition::INLINE,
L"foo.html"
},
{ "attachment; xfilename=foo.html",
net::HttpContentDisposition::ATTACHMENT,
L""
},
{ "attachment; filename=\"/foo.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"/foo.html"
},
{ "attachment; filename=\"\\\\foo.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"\\foo.html"
},
{ "foobar",
net::HttpContentDisposition::ATTACHMENT,
L""
},
{ "attachment; example=\"filename=example.txt\"",
net::HttpContentDisposition::ATTACHMENT,
L""
},
{ "attachment; foobar=x; filename=\"foo.html\"",
net::HttpContentDisposition::ATTACHMENT,
L"foo.html"
},
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(tests); ++i) {
HttpContentDisposition header(tests[i].header, std::string());
EXPECT_EQ(tests[i].expected_type, header.type())
<< "Failed on input: " << tests[i].header;
EXPECT_EQ(tests[i].expected_filename, base::UTF8ToWide(header.filename()))
<< "Failed on input: " << tests[i].header;
}
}
TEST(HttpContentDispositionTest, ParseResult) {
const struct ParseResultTestCase {
const char* header;
int expected_flags;
} kTestCases[] = {
{ "", HttpContentDisposition::INVALID },
{ "example=x", HttpContentDisposition::INVALID },
{ "attachment; filename=", HttpContentDisposition::HAS_DISPOSITION_TYPE },
{ "attachment; name=", HttpContentDisposition::HAS_DISPOSITION_TYPE },
{ "attachment; filename*=", HttpContentDisposition::HAS_DISPOSITION_TYPE },
{ "attachment; filename==?utf-8?Q?\?=",
HttpContentDisposition::HAS_DISPOSITION_TYPE },
{ "filename=x", HttpContentDisposition::HAS_FILENAME },
{ "example; filename=x",
HttpContentDisposition::HAS_DISPOSITION_TYPE |
HttpContentDisposition::HAS_UNKNOWN_DISPOSITION_TYPE |
HttpContentDisposition::HAS_FILENAME},
{ "attachment; filename=x",
HttpContentDisposition::HAS_DISPOSITION_TYPE |
HttpContentDisposition::HAS_FILENAME },
{ "attachment; filename=x; name=y",
HttpContentDisposition::HAS_DISPOSITION_TYPE |
HttpContentDisposition::HAS_FILENAME |
HttpContentDisposition::HAS_NAME },
{ "attachment; name=y; filename*=utf-8''foo; name=x",
HttpContentDisposition::HAS_DISPOSITION_TYPE |
HttpContentDisposition::HAS_EXT_FILENAME |
HttpContentDisposition::HAS_NAME },
{ "filename=foo\xcc\x88",
HttpContentDisposition::HAS_FILENAME |
HttpContentDisposition::HAS_NON_ASCII_STRINGS },
{ "filename=foo%cc%88",
HttpContentDisposition::HAS_FILENAME |
HttpContentDisposition::HAS_PERCENT_ENCODED_STRINGS },
{ "filename==?utf-8?Q?foo?=",
HttpContentDisposition::HAS_FILENAME |
HttpContentDisposition::HAS_RFC2047_ENCODED_STRINGS },
{ "filename=\"=?utf-8?Q?foo?=\"",
HttpContentDisposition::HAS_FILENAME |
HttpContentDisposition::HAS_RFC2047_ENCODED_STRINGS },
{ "filename==?utf-8?Q?foo?", HttpContentDisposition::INVALID },
{ "name=foo\xcc\x88",
HttpContentDisposition::HAS_NAME },
{ "filename=x; name=foo\xcc\x88",
HttpContentDisposition::HAS_FILENAME |
HttpContentDisposition::HAS_NAME },
{ "filename=foo\xcc\x88 foo%cc%88 =?utf-8?Q?foo?=",
HttpContentDisposition::HAS_FILENAME |
HttpContentDisposition::HAS_NON_ASCII_STRINGS |
HttpContentDisposition::HAS_PERCENT_ENCODED_STRINGS |
HttpContentDisposition::HAS_RFC2047_ENCODED_STRINGS },
{ "filename=foo\xcc\x88 foo%cc%88 =?utf-8?Q?foo?",
HttpContentDisposition::INVALID },
{ "filename=foo\xcc\x88 foo%cc%88 =?utf-8?Q?foo?; name=x",
HttpContentDisposition::HAS_NAME },
};
for (size_t i = 0; i < ARRAYSIZE_UNSAFE(kTestCases); ++i) {
const ParseResultTestCase& test_case = kTestCases[i];
HttpContentDisposition content_disposition(test_case.header, "utf-8");
int result = content_disposition.parse_result_flags();
SCOPED_TRACE(testing::Message() << "Test case " << i
<< " with header " << test_case.header);
EXPECT_EQ(test_case.expected_flags, result);
}
}
}