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

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

Issue 2124373002: [PPAPI] Quarantine files that are writeable by a Pepper plugin. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@consolidate-file-metadata
Patch Set: Address comments. Created 4 years 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/quarantine.h"
20 #include "content/browser/download/quarantine_constants_linux.h"
21 #include "testing/gtest/include/gtest/gtest.h"
22 #include "url/gurl.h"
23
24 namespace content {
25 namespace {
26
27 using std::istringstream;
28 using std::string;
29 using std::vector;
30
31 class QuarantineLinuxTest : public testing::Test {
32 public:
33 QuarantineLinuxTest()
34 : source_url_("http://www.source.com"),
35 referrer_url_("http://www.referrer.com"),
36 is_xattr_supported_(false) {}
37
38 const base::FilePath& test_file() const { return test_file_; }
39
40 const GURL& source_url() const { return source_url_; }
41
42 const GURL& referrer_url() const { return referrer_url_; }
43
44 bool is_xattr_supported() const { return is_xattr_supported_; }
45
46 protected:
47 void SetUp() override {
48 ASSERT_TRUE(temp_dir_.CreateUniqueTempDir());
49 ASSERT_TRUE(
50 base::CreateTemporaryFileInDir(temp_dir_.GetPath(), &test_file_));
51 int result =
52 setxattr(test_file_.value().c_str(), "user.test", "test", 4, 0);
53 is_xattr_supported_ = (!result) || (errno != ENOTSUP);
54 if (!is_xattr_supported_) {
55 DVLOG(0) << "Test will be skipped because extended attributes are not "
56 << "supported on this OS/file system.";
57 }
58 }
59
60 void CheckExtendedAttributeValue(const string attr_name,
61 const string expected_value) const {
62 ssize_t len =
63 getxattr(test_file().value().c_str(), attr_name.c_str(), NULL, 0);
64 if (len <= static_cast<ssize_t>(0)) {
65 FAIL() << "Attribute '" << attr_name << "' does not exist";
66 }
67 char* buffer = new char[len];
68 len = getxattr(test_file().value().c_str(), attr_name.c_str(), buffer, len);
69 EXPECT_EQ(expected_value.size(), static_cast<size_t>(len));
70 string real_value(buffer, len);
71 delete[] buffer;
72 EXPECT_EQ(expected_value, real_value);
73 }
74
75 void GetExtendedAttributeNames(vector<string>* attr_names) const {
76 ssize_t len = listxattr(test_file().value().c_str(), NULL, 0);
77 if (len <= static_cast<ssize_t>(0))
78 return;
79 char* buffer = new char[len];
80 len = listxattr(test_file().value().c_str(), buffer, len);
81 *attr_names =
82 base::SplitString(string(buffer, len), std::string(1, '\0'),
83 base::TRIM_WHITESPACE, base::SPLIT_WANT_ALL);
84 delete[] buffer;
85 }
86
87 void VerifyAttributesAreSetCorrectly() const {
88 vector<string> attr_names;
89 GetExtendedAttributeNames(&attr_names);
90
91 // Check if the attributes are set on the file
92 vector<string>::const_iterator pos =
93 find(attr_names.begin(), attr_names.end(), kSourceURLExtendedAttrName);
94 EXPECT_NE(pos, attr_names.end());
95 pos = find(attr_names.begin(), attr_names.end(),
96 kReferrerURLExtendedAttrName);
97 EXPECT_NE(pos, attr_names.end());
98
99 // Check if the attribute values are set correctly
100 CheckExtendedAttributeValue(kSourceURLExtendedAttrName,
101 source_url().spec());
102 CheckExtendedAttributeValue(kReferrerURLExtendedAttrName,
103 referrer_url().spec());
104 }
105
106 private:
107 base::ScopedTempDir temp_dir_;
108 base::FilePath test_file_;
109 GURL source_url_;
110 GURL referrer_url_;
111 bool is_xattr_supported_;
112 };
113
114 TEST_F(QuarantineLinuxTest, CheckMetadataSetCorrectly) {
115 if (!is_xattr_supported())
116 return;
117 EXPECT_EQ(
118 QuarantineFileResult::OK,
119 QuarantineFile(test_file(), source_url(), referrer_url(), std::string()));
120 VerifyAttributesAreSetCorrectly();
121 }
122
123 TEST_F(QuarantineLinuxTest, SetMetadataMultipleTimes) {
124 if (!is_xattr_supported())
125 return;
126 GURL dummy_url("http://www.dummy.com");
127 EXPECT_EQ(QuarantineFileResult::OK,
128 QuarantineFile(test_file(), dummy_url, dummy_url, std::string()));
129 EXPECT_EQ(
130 QuarantineFileResult::OK,
131 QuarantineFile(test_file(), source_url(), referrer_url(), std::string()));
132 VerifyAttributesAreSetCorrectly();
133 }
134
135 TEST_F(QuarantineLinuxTest, InvalidSourceURLTest) {
136 if (!is_xattr_supported())
137 return;
138 GURL invalid_url;
139 vector<string> attr_names;
140 EXPECT_EQ(
141 QuarantineFileResult::ANNOTATION_FAILED,
142 QuarantineFile(test_file(), invalid_url, referrer_url(), std::string()));
143 GetExtendedAttributeNames(&attr_names);
144 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
145 kSourceURLExtendedAttrName));
146 CheckExtendedAttributeValue(kReferrerURLExtendedAttrName,
147 referrer_url().spec());
148 }
149
150 TEST_F(QuarantineLinuxTest, InvalidReferrerURLTest) {
151 if (!is_xattr_supported())
152 return;
153 GURL invalid_url;
154 vector<string> attr_names;
155 EXPECT_EQ(
156 QuarantineFileResult::OK,
157 QuarantineFile(test_file(), source_url(), invalid_url, std::string()));
158 GetExtendedAttributeNames(&attr_names);
159 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
160 kReferrerURLExtendedAttrName));
161 CheckExtendedAttributeValue(kSourceURLExtendedAttrName, source_url().spec());
162 }
163
164 TEST_F(QuarantineLinuxTest, InvalidURLsTest) {
165 if (!is_xattr_supported())
166 return;
167 GURL invalid_url;
168 vector<string> attr_names;
169 EXPECT_EQ(
170 QuarantineFileResult::ANNOTATION_FAILED,
171 QuarantineFile(test_file(), invalid_url, invalid_url, std::string()));
172 GetExtendedAttributeNames(&attr_names);
173 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
174 kSourceURLExtendedAttrName));
175 EXPECT_EQ(attr_names.end(), find(attr_names.begin(), attr_names.end(),
176 kReferrerURLExtendedAttrName));
177 }
178
179 } // namespace
180 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/download/quarantine_linux.cc ('k') | content/browser/download/quarantine_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698