Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(24)

Side by Side Diff: content/browser/download/file_metadata_unittest_linux.cc

Issue 2123023002: [Downloads] Consolidate MOTW annotation APIs into a single API. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@move-safe-util-to-downloads
Patch Set: [win] Verify that the Zone.Identifier stream has the correct contents. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
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.
4
5 #include <errno.h>
6 #include <stddef.h>
7 #include <sys/types.h>
8 #include <sys/xattr.h>
9
10 #include <algorithm>
11 #include <sstream>
12 #include <string>
13
14 #include "base/files/file_path.h"
15 #include "base/files/file_util.h"
16 #include "base/files/scoped_temp_dir.h"
17 #include "base/logging.h"
18 #include "base/strings/string_split.h"
19 #include "content/browser/download/file_metadata_linux.h"
20 #include "testing/gtest/include/gtest/gtest.h"
21 #include "url/gurl.h"
22
23 namespace content {
24 namespace {
25
26 using std::istringstream;
27 using std::string;
28 using std::vector;
29
30 class FileMetadataLinuxTest : public testing::Test {
31 public:
32 FileMetadataLinuxTest()
33 : source_url_("http://www.source.com"),
34 referrer_url_("http://www.referrer.com"),
35 is_xattr_supported_(false) {}
36
37 const base::FilePath& test_file() const {
38 return test_file_;
39 }
40
41 const GURL& source_url() const {
42 return source_url_;
43 }
44
45 const GURL& referrer_url() const {
46 return referrer_url_;
47 }
48
49 bool is_xattr_supported() const {
50 return is_xattr_supported_;
51 }
52
53 protected:
54 void SetUp() override {
55 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
56 ASSERT_TRUE(
57 base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_file_));
58 int result = setxattr(test_file_.value().c_str(),
59 "user.test", "test", 4, 0);
60 is_xattr_supported_ = (!result) || (errno != ENOTSUP);
61 if (!is_xattr_supported_) {
62 DVLOG(0) << "Test will be skipped because extended attributes are not "
63 << "supported on this OS/file system.";
64 }
65 }
66
67 void CheckExtendedAttributeValue(const string attr_name,
68 const string expected_value) const {
69 ssize_t len = getxattr(test_file().value().c_str(), attr_name.c_str(),
70 NULL, 0);
71 if (len <= static_cast<ssize_t>(0)) {
72 FAIL() << "Attribute '" << attr_name << "' does not exist";
73 }
74 char* buffer = new char[len];
75 len = getxattr(test_file().value().c_str(), attr_name.c_str(), buffer, len);
76 EXPECT_EQ(expected_value.size(), static_cast<size_t>(len));
77 string real_value(buffer, len);
78 delete[] buffer;
79 EXPECT_EQ(expected_value, real_value);
80 }
81
82 void GetExtendedAttributeNames(vector<string>* attr_names) const {
83 ssize_t len = listxattr(test_file().value().c_str(), NULL, 0);
84 if (len <= static_cast<ssize_t>(0)) return;
85 char* buffer = new char[len];
86 len = listxattr(test_file().value().c_str(), buffer, len);
87 *attr_names = base::SplitString(string(buffer, len), std::string(1, '\0'),
88 base::TRIM_WHITESPACE,
89 base::SPLIT_WANT_ALL);
90 delete[] buffer;
91 }
92
93 void VerifyAttributesAreSetCorrectly() const {
94 vector<string> attr_names;
95 GetExtendedAttributeNames(&attr_names);
96
97 // Check if the attributes are set on the file
98 vector<string>::const_iterator pos = find(attr_names.begin(),
99 attr_names.end(), kSourceURLAttrName);
100 EXPECT_NE(pos, attr_names.end());
101 pos = find(attr_names.begin(), attr_names.end(), kReferrerURLAttrName);
102 EXPECT_NE(pos, attr_names.end());
103
104 // Check if the attribute values are set correctly
105 CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec());
106 CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec());
107 }
108
109 private:
110 base::ScopedTempDir temp_dir_;
111 base::FilePath test_file_;
112 GURL source_url_;
113 GURL referrer_url_;
114 bool is_xattr_supported_;
115 };
116
117 TEST_F(FileMetadataLinuxTest, CheckMetadataSetCorrectly) {
118 if (!is_xattr_supported()) return;
119 AddOriginMetadataToFile(test_file(), source_url(), referrer_url());
120 VerifyAttributesAreSetCorrectly();
121 }
122
123 TEST_F(FileMetadataLinuxTest, SetMetadataMultipleTimes) {
124 if (!is_xattr_supported()) return;
125 GURL dummy_url("http://www.dummy.com");
126 AddOriginMetadataToFile(test_file(), dummy_url, dummy_url);
127 AddOriginMetadataToFile(test_file(), source_url(), referrer_url());
128 VerifyAttributesAreSetCorrectly();
129 }
130
131 TEST_F(FileMetadataLinuxTest, InvalidSourceURLTest) {
132 if (!is_xattr_supported()) return;
133 GURL invalid_url;
134 vector<string> attr_names;
135 AddOriginMetadataToFile(test_file(), invalid_url, referrer_url());
136 GetExtendedAttributeNames(&attr_names);
137 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
138 kSourceURLAttrName));
139 CheckExtendedAttributeValue(kReferrerURLAttrName, referrer_url().spec());
140 }
141
142 TEST_F(FileMetadataLinuxTest, InvalidReferrerURLTest) {
143 if (!is_xattr_supported()) return;
144 GURL invalid_url;
145 vector<string> attr_names;
146 AddOriginMetadataToFile(test_file(), source_url(), invalid_url);
147 GetExtendedAttributeNames(&attr_names);
148 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
149 kReferrerURLAttrName));
150 CheckExtendedAttributeValue(kSourceURLAttrName, source_url().spec());
151 }
152
153 TEST_F(FileMetadataLinuxTest, InvalidURLsTest) {
154 if (!is_xattr_supported()) return;
155 GURL invalid_url;
156 vector<string> attr_names;
157 AddOriginMetadataToFile(test_file(), invalid_url, invalid_url);
158 GetExtendedAttributeNames(&attr_names);
159 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
160 kSourceURLAttrName));
161 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
162 kReferrerURLAttrName));
163 }
164
165 } // namespace
166 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698