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
3 // found in the LICENSE file.
13 #include "base/files/file_path.h"
14 #include "base/files/file_util.h"
15 #include "base/files/scoped_temp_dir.h"
16 #include "base/logging.h"
17 #include "base/strings/string_split.h"
18 #include "content/browser/download/file_metadata_linux.h"
19 #include "testing/gtest/include/gtest/gtest.h"
25 using std::istringstream
;
29 class FileMetadataLinuxTest
: public testing::Test
{
31 FileMetadataLinuxTest()
32 : source_url_("http://www.source.com"),
33 referrer_url_("http://www.referrer.com"),
34 is_xattr_supported_(false) {}
36 const base::FilePath
& test_file() const {
40 const GURL
& source_url() const {
44 const GURL
& referrer_url() const {
48 bool is_xattr_supported() const {
49 return is_xattr_supported_
;
53 void SetUp() override
{
54 ASSERT_TRUE(temp_dir_
.CreateUniqueTempDir());
55 ASSERT_TRUE(base::CreateTemporaryFileInDir(temp_dir_
.path(), &test_file_
));
56 int result
= setxattr(test_file_
.value().c_str(),
57 "user.test", "test", 4, 0);
58 is_xattr_supported_
= (!result
) || (errno
!= ENOTSUP
);
59 if (!is_xattr_supported_
) {
60 DVLOG(0) << "Test will be skipped because extended attributes are not "
61 << "supported on this OS/file system.";
65 void CheckExtendedAttributeValue(const string attr_name
,
66 const string expected_value
) const {
67 ssize_t len
= getxattr(test_file().value().c_str(), attr_name
.c_str(),
69 if (len
<= static_cast<ssize_t
>(0)) {
70 FAIL() << "Attribute '" << attr_name
<< "' does not exist";
72 char* buffer
= new char[len
];
73 len
= getxattr(test_file().value().c_str(), attr_name
.c_str(), buffer
, len
);
74 EXPECT_EQ(expected_value
.size(), static_cast<size_t>(len
));
75 string
real_value(buffer
, len
);
77 EXPECT_EQ(expected_value
, real_value
);
80 void GetExtendedAttributeNames(vector
<string
>* attr_names
) const {
81 ssize_t len
= listxattr(test_file().value().c_str(), NULL
, 0);
82 if (len
<= static_cast<ssize_t
>(0)) return;
83 char* buffer
= new char[len
];
84 len
= listxattr(test_file().value().c_str(), buffer
, len
);
85 *attr_names
= base::SplitString(string(buffer
, len
), std::string(1, '\0'),
86 base::TRIM_WHITESPACE
,
87 base::SPLIT_WANT_ALL
);
91 void VerifyAttributesAreSetCorrectly() const {
92 vector
<string
> attr_names
;
93 GetExtendedAttributeNames(&attr_names
);
95 // Check if the attributes are set on the file
96 vector
<string
>::const_iterator pos
= find(attr_names
.begin(),
97 attr_names
.end(), kSourceURLAttrName
);
98 EXPECT_NE(pos
, attr_names
.end());
99 pos
= find(attr_names
.begin(), attr_names
.end(), kReferrerURLAttrName
);
100 EXPECT_NE(pos
, attr_names
.end());
102 // Check if the attribute values are set correctly
103 CheckExtendedAttributeValue(kSourceURLAttrName
, source_url().spec());
104 CheckExtendedAttributeValue(kReferrerURLAttrName
, referrer_url().spec());
108 base::ScopedTempDir temp_dir_
;
109 base::FilePath test_file_
;
112 bool is_xattr_supported_
;
115 TEST_F(FileMetadataLinuxTest
, CheckMetadataSetCorrectly
) {
116 if (!is_xattr_supported()) return;
117 AddOriginMetadataToFile(test_file(), source_url(), referrer_url());
118 VerifyAttributesAreSetCorrectly();
121 TEST_F(FileMetadataLinuxTest
, SetMetadataMultipleTimes
) {
122 if (!is_xattr_supported()) return;
123 GURL
dummy_url("http://www.dummy.com");
124 AddOriginMetadataToFile(test_file(), dummy_url
, dummy_url
);
125 AddOriginMetadataToFile(test_file(), source_url(), referrer_url());
126 VerifyAttributesAreSetCorrectly();
129 TEST_F(FileMetadataLinuxTest
, InvalidSourceURLTest
) {
130 if (!is_xattr_supported()) return;
132 vector
<string
> attr_names
;
133 AddOriginMetadataToFile(test_file(), invalid_url
, referrer_url());
134 GetExtendedAttributeNames(&attr_names
);
135 EXPECT_EQ(attr_names
.end(), find(attr_names
.begin(), attr_names
.end(),
136 kSourceURLAttrName
));
137 CheckExtendedAttributeValue(kReferrerURLAttrName
, referrer_url().spec());
140 TEST_F(FileMetadataLinuxTest
, InvalidReferrerURLTest
) {
141 if (!is_xattr_supported()) return;
143 vector
<string
> attr_names
;
144 AddOriginMetadataToFile(test_file(), source_url(), invalid_url
);
145 GetExtendedAttributeNames(&attr_names
);
146 EXPECT_EQ(attr_names
.end(), find(attr_names
.begin(), attr_names
.end(),
147 kReferrerURLAttrName
));
148 CheckExtendedAttributeValue(kSourceURLAttrName
, source_url().spec());
151 TEST_F(FileMetadataLinuxTest
, InvalidURLsTest
) {
152 if (!is_xattr_supported()) return;
154 vector
<string
> attr_names
;
155 AddOriginMetadataToFile(test_file(), invalid_url
, invalid_url
);
156 GetExtendedAttributeNames(&attr_names
);
157 EXPECT_EQ(attr_names
.end(), find(attr_names
.begin(), attr_names
.end(),
158 kSourceURLAttrName
));
159 EXPECT_EQ(attr_names
.end(), find(attr_names
.begin(), attr_names
.end(),
160 kReferrerURLAttrName
));
164 } // namespace content