[refactor] More post-NSS WebCrypto cleanups (utility functions).
[chromium-blink-merge.git] / content / browser / download / file_metadata_unittest_linux.cc
blobc20e573115ee69d0d29f04ac686b21da893db7fc
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.
5 #include <errno.h>
6 #include <sys/types.h>
7 #include <sys/xattr.h>
9 #include <algorithm>
10 #include <sstream>
11 #include <string>
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"
20 #include "url/gurl.h"
22 namespace content {
23 namespace {
25 using std::istringstream;
26 using std::string;
27 using std::vector;
29 class FileMetadataLinuxTest : public testing::Test {
30 public:
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 {
37 return test_file_;
40 const GURL& source_url() const {
41 return source_url_;
44 const GURL& referrer_url() const {
45 return referrer_url_;
48 bool is_xattr_supported() const {
49 return is_xattr_supported_;
52 protected:
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(),
68 NULL, 0);
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);
76 delete[] buffer;
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);
88 delete[] buffer;
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());
107 private:
108 base::ScopedTempDir temp_dir_;
109 base::FilePath test_file_;
110 GURL source_url_;
111 GURL referrer_url_;
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;
131 GURL invalid_url;
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;
142 GURL invalid_url;
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;
153 GURL invalid_url;
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));
163 } // namespace
164 } // namespace content